Spaces:
Running
Running
fix: generate image and change music in parallel
Browse files- src/images/image_generator.py +4 -6
- src/main.py +10 -6
src/images/image_generator.py
CHANGED
|
@@ -24,6 +24,8 @@ async def generate_image(prompt: str) -> tuple[str, str] | None:
|
|
| 24 |
# Ensure the generated/images directory exists
|
| 25 |
output_dir = "generated/images"
|
| 26 |
os.makedirs(output_dir, exist_ok=True)
|
|
|
|
|
|
|
| 27 |
|
| 28 |
try:
|
| 29 |
response = await client.models.generate_content(
|
|
@@ -37,9 +39,7 @@ async def generate_image(prompt: str) -> tuple[str, str] | None:
|
|
| 37 |
# Process the response parts
|
| 38 |
image_saved = False
|
| 39 |
for part in response.candidates[0].content.parts:
|
| 40 |
-
if part.
|
| 41 |
-
logger.info(f"Generated text: {part.text}")
|
| 42 |
-
elif part.inline_data is not None:
|
| 43 |
# Create a filename with timestamp
|
| 44 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 45 |
filename = f"gemini_{timestamp}.png"
|
|
@@ -102,9 +102,7 @@ async def modify_image(image_path: str, modification_prompt: str) -> str | None:
|
|
| 102 |
# Process the response parts
|
| 103 |
image_saved = False
|
| 104 |
for part in response.candidates[0].content.parts:
|
| 105 |
-
if part.
|
| 106 |
-
logger.info(f"Generated text: {part.text}")
|
| 107 |
-
elif part.inline_data is not None:
|
| 108 |
# Create a filename with timestamp
|
| 109 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 110 |
filename = f"gemini_modified_{timestamp}.png"
|
|
|
|
| 24 |
# Ensure the generated/images directory exists
|
| 25 |
output_dir = "generated/images"
|
| 26 |
os.makedirs(output_dir, exist_ok=True)
|
| 27 |
+
|
| 28 |
+
logger.info(f"Generating image with prompt: {prompt}")
|
| 29 |
|
| 30 |
try:
|
| 31 |
response = await client.models.generate_content(
|
|
|
|
| 39 |
# Process the response parts
|
| 40 |
image_saved = False
|
| 41 |
for part in response.candidates[0].content.parts:
|
| 42 |
+
if part.inline_data is not None:
|
|
|
|
|
|
|
| 43 |
# Create a filename with timestamp
|
| 44 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 45 |
filename = f"gemini_{timestamp}.png"
|
|
|
|
| 102 |
# Process the response parts
|
| 103 |
image_saved = False
|
| 104 |
for part in response.candidates[0].content.parts:
|
| 105 |
+
if part.inline_data is not None:
|
|
|
|
|
|
|
| 106 |
# Create a filename with timestamp
|
| 107 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 108 |
filename = f"gemini_modified_{timestamp}.png"
|
src/main.py
CHANGED
|
@@ -18,6 +18,7 @@ from game_constructor import (
|
|
| 18 |
load_character_suggestion,
|
| 19 |
start_game_with_settings,
|
| 20 |
)
|
|
|
|
| 21 |
|
| 22 |
logger = logging.getLogger(__name__)
|
| 23 |
|
|
@@ -54,15 +55,18 @@ async def update_scene(user_hash: str, choice):
|
|
| 54 |
story[new_scene]["choices"] = [
|
| 55 |
option.option_description for option in response.player_options
|
| 56 |
]
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
if img_path:
|
| 61 |
story[new_scene]["image"] = img_path
|
| 62 |
|
| 63 |
-
if response.change_music.change_music:
|
| 64 |
-
await change_music_tone(user_hash, response.change_music.music_description)
|
| 65 |
-
|
| 66 |
scene = story[state["scene"]]
|
| 67 |
return (
|
| 68 |
scene["text"],
|
|
|
|
| 18 |
load_character_suggestion,
|
| 19 |
start_game_with_settings,
|
| 20 |
)
|
| 21 |
+
import asyncio
|
| 22 |
|
| 23 |
logger = logging.getLogger(__name__)
|
| 24 |
|
|
|
|
| 55 |
story[new_scene]["choices"] = [
|
| 56 |
option.option_description for option in response.player_options
|
| 57 |
]
|
| 58 |
+
|
| 59 |
+
# run both tasks in parallel
|
| 60 |
+
img_res, _ = await asyncio.gather(
|
| 61 |
+
generate_image(response.change_scene.scene_description) if response.change_scene.change_scene else asyncio.sleep(0),
|
| 62 |
+
change_music_tone(user_hash, response.change_music.music_description) if response.change_music.change_music else asyncio.sleep(0)
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
if img_res and response.change_scene.change_scene:
|
| 66 |
+
img_path, _ = img_res
|
| 67 |
if img_path:
|
| 68 |
story[new_scene]["image"] = img_path
|
| 69 |
|
|
|
|
|
|
|
|
|
|
| 70 |
scene = story[state["scene"]]
|
| 71 |
return (
|
| 72 |
scene["text"],
|