Spaces:
Running
Running
Update synthscope.py
Browse files- synthscope.py +59 -27
synthscope.py
CHANGED
|
@@ -15,7 +15,7 @@ google_search_tool = Tool(
|
|
| 15 |
|
| 16 |
model_id = "gemini-2.0-flash"
|
| 17 |
|
| 18 |
-
def GoogleSearchImageGen(prompt, image_style,
|
| 19 |
def wave_file(filename, pcm, channels=1, rate=24000, sample_width=2):
|
| 20 |
with wave.open(filename, "wb") as wf:
|
| 21 |
wf.setnchannels(channels)
|
|
@@ -23,18 +23,6 @@ def GoogleSearchImageGen(prompt, image_style, voice):
|
|
| 23 |
wf.setframerate(rate)
|
| 24 |
wf.writeframes(pcm)
|
| 25 |
|
| 26 |
-
# define style-specific prompts
|
| 27 |
-
style_prompts = {
|
| 28 |
-
"Comic": f"{prompt}. Convert the search result into a well-crafted text-to-image prompt that generates a comic book-style image.",
|
| 29 |
-
"Cartoon": f"{prompt}. Convert the search result into a well-crafted text-to-image prompt that generates a cartoon-style image.",
|
| 30 |
-
"3D": f"{prompt}. Convert the search result into a well-crafted text-to-image prompt that generates a Pixar-style 3D image.",
|
| 31 |
-
"Anime": f"{prompt}. Convert the search result into a well-crafted text-to-image prompt that generates an Anime-style image.",
|
| 32 |
-
"Ghibli": f"{prompt}. Convert the search result into a well-crafted text-to-image prompt that generates a Ghibli-style image.",
|
| 33 |
-
"Victorian": f"{prompt}. Convert the search result into a well-crafted text-to-image prompt that generates a Victorian-era image.",
|
| 34 |
-
"Movie": f"{prompt}. Convert the search result into a well-crafted text-to-image prompt that generates a Movie-style image.",
|
| 35 |
-
"Star Wars": f"{prompt}. Convert the search result into a well-crafted text-to-image prompt that generates a Star Wars-style image.",
|
| 36 |
-
"Marvel": f"{prompt}. Convert the search result into a well-crafted text-to-image prompt that generates a Marvel-style image."
|
| 37 |
-
}
|
| 38 |
|
| 39 |
# define audio output voice
|
| 40 |
select_voice = {
|
|
@@ -50,15 +38,6 @@ def GoogleSearchImageGen(prompt, image_style, voice):
|
|
| 50 |
"Gacrux": "Gacrux"
|
| 51 |
}
|
| 52 |
|
| 53 |
-
# Check if the image_style is supported
|
| 54 |
-
if image_style in style_prompts:
|
| 55 |
-
image_gen_prompt = style_prompts[image_style]
|
| 56 |
-
else:
|
| 57 |
-
return "Invalid image style", None, None
|
| 58 |
-
|
| 59 |
-
# Initialize variables to avoid UnboundLocalError
|
| 60 |
-
search_result = ""
|
| 61 |
-
image = None
|
| 62 |
|
| 63 |
try:
|
| 64 |
# Get search result to be displayed to the user
|
|
@@ -71,22 +50,75 @@ def GoogleSearchImageGen(prompt, image_style, voice):
|
|
| 71 |
)
|
| 72 |
)
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
# Extract search result
|
| 75 |
for each in response.candidates[0].content.parts:
|
| 76 |
if each.text:
|
| 77 |
search_result += each.text
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
# Generate audio from search result
|
| 80 |
audio_resp = client.models.generate_content(
|
| 81 |
model="gemini-2.5-flash-preview-tts",
|
| 82 |
-
contents=
|
| 83 |
config=types.GenerateContentConfig(
|
| 84 |
response_modalities=["AUDIO"],
|
| 85 |
speech_config=types.SpeechConfig(
|
| 86 |
voice_config=types.VoiceConfig(
|
| 87 |
prebuilt_voice_config=types.PrebuiltVoiceConfig(
|
| 88 |
-
voice_name=select_voice[
|
| 89 |
-
|
| 90 |
)
|
| 91 |
)
|
| 92 |
),
|
|
@@ -134,6 +166,6 @@ def GoogleSearchImageGen(prompt, image_style, voice):
|
|
| 134 |
except Exception as e:
|
| 135 |
print(f"Error occurred: {e}")
|
| 136 |
# Return default values in case of error
|
| 137 |
-
return
|
| 138 |
|
| 139 |
-
return
|
|
|
|
| 15 |
|
| 16 |
model_id = "gemini-2.0-flash"
|
| 17 |
|
| 18 |
+
def GoogleSearchImageGen(prompt, image_style, voices, language):
|
| 19 |
def wave_file(filename, pcm, channels=1, rate=24000, sample_width=2):
|
| 20 |
with wave.open(filename, "wb") as wf:
|
| 21 |
wf.setnchannels(channels)
|
|
|
|
| 23 |
wf.setframerate(rate)
|
| 24 |
wf.writeframes(pcm)
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
# define audio output voice
|
| 28 |
select_voice = {
|
|
|
|
| 38 |
"Gacrux": "Gacrux"
|
| 39 |
}
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
try:
|
| 43 |
# Get search result to be displayed to the user
|
|
|
|
| 50 |
)
|
| 51 |
)
|
| 52 |
|
| 53 |
+
# Initialize variables to avoid UnboundLocalError
|
| 54 |
+
search_result = ""
|
| 55 |
+
image = None
|
| 56 |
+
|
| 57 |
# Extract search result
|
| 58 |
for each in response.candidates[0].content.parts:
|
| 59 |
if each.text:
|
| 60 |
search_result += each.text
|
| 61 |
|
| 62 |
+
|
| 63 |
+
# define style-specific prompts
|
| 64 |
+
style_prompts = {
|
| 65 |
+
"Comic": f"Convert the {search_result} into a well-crafted text-to-image prompt that generates a comic book-style image.",
|
| 66 |
+
"Cartoon": f"Convert the {search_result} into a well-crafted text-to-image prompt that generates a cartoon-style image.",
|
| 67 |
+
"3D": f"Convert the {search_result} into a well-crafted text-to-image prompt that generates a Pixar-style 3D image.",
|
| 68 |
+
"Anime": f"Convert the {search_result} into a well-crafted text-to-image prompt that generates an Anime-style image.",
|
| 69 |
+
"Ghibli": f"Convert the {search_result} into a well-crafted text-to-image prompt that generates a Ghibli-style image.",
|
| 70 |
+
"Victorian": f"Convert the {search_result} into a well-crafted text-to-image prompt that generates a Victorian-era image.",
|
| 71 |
+
"Movie": f"Convert the {search_result} into a well-crafted text-to-image prompt that generates a Movie-style image.",
|
| 72 |
+
"Star Wars": f"Convert the {search_result} into a well-crafted text-to-image prompt that generates a Star Wars-style image.",
|
| 73 |
+
"Marvel": f"Convert the {search_result} into a well-crafted text-to-image prompt that generates a Marvel-style image.",
|
| 74 |
+
"Disney": f"Convert the {search_result} into a well-crafted text-to-image prompt that generates a Disney-style image.",
|
| 75 |
+
"Van Gogh": f"Convert the {search_result} into a well-crafted text-to-image prompt that generates a Van Gogh-style image.",
|
| 76 |
+
"Picasso": f"Convert the {search_result} into a well-crafted text-to-image prompt that generates a Picasso-style image"
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
# Check if the image_style is supported
|
| 80 |
+
if image_style in style_prompts:
|
| 81 |
+
image_gen_prompt = style_prompts[image_style]
|
| 82 |
+
else:
|
| 83 |
+
return "Invalid image style", None, None
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
# define translation options
|
| 87 |
+
translation_prompt = {
|
| 88 |
+
"English": f"Read out the {search_result}",
|
| 89 |
+
"Spanish": f"Translate {search_result} into Spanish. Return only the translated text.",
|
| 90 |
+
"French": f"Translate {search_result} into French. Return only the translated text.",
|
| 91 |
+
"German": f"Translate {search_result} into German. Return only the translated text.",
|
| 92 |
+
"Italian": f"Translate {search_result} into Italian. Return only the translated text.",
|
| 93 |
+
"Japanese": f"Translate {search_result} into Japanese. Return only the translated text.",
|
| 94 |
+
"Tamil": f"Translate {search_result} into Tamil. Return only the translated text.",
|
| 95 |
+
"Arabic": f"Translate {search_result} into Arabic. Return only the translated text.",
|
| 96 |
+
"Russian": f"Translate {search_result} into Russian. Return only the translated text.",
|
| 97 |
+
"Portuguese": f"Translate {search_result} Portuguese. Return only the translated text.",
|
| 98 |
+
"Dutch": f"Translate {search_result} into Dutch. Return only the translated text.",
|
| 99 |
+
"Thai": f"Translate {search_result} into Thai. Return only the translated text.",
|
| 100 |
+
"Turkish": f"Translate {search_result} into Turkish. Return only the translated text.",
|
| 101 |
+
"Romanian": f"Translate {search_result} into Romanian. Return only the translated text.",
|
| 102 |
+
"Polish": f"Translate {search_result} into Romanian. Return only the translated text."
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
# updated search result
|
| 106 |
+
trans_resp = client.models.generate_content(
|
| 107 |
+
model=model_id,
|
| 108 |
+
contents= translation_prompt[language]
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
|
| 112 |
# Generate audio from search result
|
| 113 |
audio_resp = client.models.generate_content(
|
| 114 |
model="gemini-2.5-flash-preview-tts",
|
| 115 |
+
contents=trans_resp.text,
|
| 116 |
config=types.GenerateContentConfig(
|
| 117 |
response_modalities=["AUDIO"],
|
| 118 |
speech_config=types.SpeechConfig(
|
| 119 |
voice_config=types.VoiceConfig(
|
| 120 |
prebuilt_voice_config=types.PrebuiltVoiceConfig(
|
| 121 |
+
voice_name=select_voice[voices],
|
|
|
|
| 122 |
)
|
| 123 |
)
|
| 124 |
),
|
|
|
|
| 166 |
except Exception as e:
|
| 167 |
print(f"Error occurred: {e}")
|
| 168 |
# Return default values in case of error
|
| 169 |
+
return trans_resp.text or "No search result available", None, None
|
| 170 |
|
| 171 |
+
return trans_resp.text, image, audio_output_file
|