Spaces:
Sleeping
Sleeping
Update synthscope.py
Browse files- synthscope.py +57 -9
synthscope.py
CHANGED
|
@@ -14,25 +14,51 @@ google_search_tool = Tool(
|
|
| 14 |
|
| 15 |
model_id = "gemini-2.0-flash"
|
| 16 |
|
| 17 |
-
def GoogleSearchImageGen(prompt, image_style):
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
style_prompts = {
|
| 20 |
"Comic": f"{prompt}. Convert the search result into a well-crafted text-to-image prompt that generates a comic book-style image.",
|
| 21 |
"Cartoon": f"{prompt}. Convert the search result into a well-crafted text-to-image prompt that generates a cartoon-style image.",
|
| 22 |
"3D": f"{prompt}. Convert the search result into a well-crafted text-to-image prompt that generates a Pixar-style 3D image.",
|
| 23 |
"Anime": f"{prompt}. Convert the search result into a well-crafted text-to-image prompt that generates an Anime-style image.",
|
| 24 |
"Ghibli": f"{prompt}. Convert the search result into a well-crafted text-to-image prompt that generates a Ghibli-style image.",
|
| 25 |
-
"Victorian": f"{prompt}. Convert the search result into a well-crafted text-to-image prompt that generates a Victorian-era image."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
}
|
| 27 |
-
|
| 28 |
# Check if the image_style is supported
|
| 29 |
if image_style in style_prompts:
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
# Initialize variables to avoid UnboundLocalError
|
| 33 |
search_result = ""
|
| 34 |
image = None
|
| 35 |
-
|
| 36 |
try:
|
| 37 |
# Get search result to be displayed to the user
|
| 38 |
response = client.models.generate_content(
|
|
@@ -49,6 +75,28 @@ def GoogleSearchImageGen(prompt, image_style):
|
|
| 49 |
if each.text:
|
| 50 |
search_result += each.text
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
# Generate image prompt from search result
|
| 53 |
output = client.models.generate_content(
|
| 54 |
model=model_id,
|
|
@@ -85,6 +133,6 @@ def GoogleSearchImageGen(prompt, image_style):
|
|
| 85 |
except Exception as e:
|
| 86 |
print(f"Error occurred: {e}")
|
| 87 |
# Return default values in case of error
|
| 88 |
-
return search_result or "No search result available", None
|
| 89 |
|
| 90 |
-
return search_result, image
|
|
|
|
| 14 |
|
| 15 |
model_id = "gemini-2.0-flash"
|
| 16 |
|
| 17 |
+
def GoogleSearchImageGen(prompt, image_style, voice):
|
| 18 |
+
def wave_file(filename, pcm, channels=1, rate=24000, sample_width=2):
|
| 19 |
+
with wave.open(filename, "wb") as wf:
|
| 20 |
+
wf.setnchannels(channels)
|
| 21 |
+
wf.setsampwidth(sample_width)
|
| 22 |
+
wf.setframerate(rate)
|
| 23 |
+
wf.writeframes(pcm)
|
| 24 |
+
|
| 25 |
+
# define style-specific prompts
|
| 26 |
style_prompts = {
|
| 27 |
"Comic": f"{prompt}. Convert the search result into a well-crafted text-to-image prompt that generates a comic book-style image.",
|
| 28 |
"Cartoon": f"{prompt}. Convert the search result into a well-crafted text-to-image prompt that generates a cartoon-style image.",
|
| 29 |
"3D": f"{prompt}. Convert the search result into a well-crafted text-to-image prompt that generates a Pixar-style 3D image.",
|
| 30 |
"Anime": f"{prompt}. Convert the search result into a well-crafted text-to-image prompt that generates an Anime-style image.",
|
| 31 |
"Ghibli": f"{prompt}. Convert the search result into a well-crafted text-to-image prompt that generates a Ghibli-style image.",
|
| 32 |
+
"Victorian": f"{prompt}. Convert the search result into a well-crafted text-to-image prompt that generates a Victorian-era image.",
|
| 33 |
+
"Movie": f"{prompt}. Convert the search result into a well-crafted text-to-image prompt that generates a Movie-style image.",
|
| 34 |
+
"Star Wars": f"{prompt}. Convert the search result into a well-crafted text-to-image prompt that generates a Star Wars-style image.",
|
| 35 |
+
"Marvel": f"{prompt}. Convert the search result into a well-crafted text-to-image prompt that generates a Marvel-style image."
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
# define audio output voice
|
| 39 |
+
select_voice = {
|
| 40 |
+
"Kore": "Kore",
|
| 41 |
+
"Zephyr": "Zephyr",
|
| 42 |
+
"Orus": "Orus",
|
| 43 |
+
"Fenrir": "Fenrir",
|
| 44 |
+
"Charon": "Charon",
|
| 45 |
+
"Umbriel": "Umbriel",
|
| 46 |
+
"Schedar": "Schedar",
|
| 47 |
+
"Iapetus": "Iapetus",
|
| 48 |
+
"Puck": "Puck",
|
| 49 |
+
"Gacrux": "Gacrux"
|
| 50 |
}
|
| 51 |
+
|
| 52 |
# Check if the image_style is supported
|
| 53 |
if image_style in style_prompts:
|
| 54 |
+
image_gen_prompt = style_prompts[image_style]
|
| 55 |
+
else:
|
| 56 |
+
return "Invalid image style", None, None
|
| 57 |
+
|
| 58 |
# Initialize variables to avoid UnboundLocalError
|
| 59 |
search_result = ""
|
| 60 |
image = None
|
| 61 |
+
|
| 62 |
try:
|
| 63 |
# Get search result to be displayed to the user
|
| 64 |
response = client.models.generate_content(
|
|
|
|
| 75 |
if each.text:
|
| 76 |
search_result += each.text
|
| 77 |
|
| 78 |
+
# Generate audio from search result
|
| 79 |
+
audio_resp = client.models.generate_content(
|
| 80 |
+
model="gemini-2.5-flash-preview-tts",
|
| 81 |
+
contents=search_result,
|
| 82 |
+
config=types.GenerateContentConfig(
|
| 83 |
+
response_modalities=["AUDIO"],
|
| 84 |
+
speech_config=types.SpeechConfig(
|
| 85 |
+
voice_config=types.VoiceConfig(
|
| 86 |
+
prebuilt_voice_config=types.PrebuiltVoiceConfig(
|
| 87 |
+
voice_name=select_voice[voice],
|
| 88 |
+
|
| 89 |
+
)
|
| 90 |
+
)
|
| 91 |
+
),
|
| 92 |
+
)
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
data = audio_resp.candidates[0].content.parts[0].inline_data.data
|
| 96 |
+
|
| 97 |
+
audio_output_file = "out.wav"
|
| 98 |
+
wave_file(audio_output_file, data)
|
| 99 |
+
|
| 100 |
# Generate image prompt from search result
|
| 101 |
output = client.models.generate_content(
|
| 102 |
model=model_id,
|
|
|
|
| 133 |
except Exception as e:
|
| 134 |
print(f"Error occurred: {e}")
|
| 135 |
# Return default values in case of error
|
| 136 |
+
return search_result or "No search result available", None, None
|
| 137 |
|
| 138 |
+
return search_result, image, audio_output_file
|