Update app.py
Browse files
app.py
CHANGED
@@ -14,7 +14,6 @@ client = Together(api_key=TOGETHER_API_KEY)
|
|
14 |
# --- Pixabay API Configuration ---
|
15 |
PIXABAY_API_KEY = os.environ.get('PIXABAY_API_KEY')
|
16 |
IMAGE_API_URL = 'https://pixabay.com/api/'
|
17 |
-
VIDEO_API_URL = 'https://pixabay.com/api/videos/'
|
18 |
PER_PAGE = 5
|
19 |
|
20 |
def image_to_url(image_path):
|
@@ -40,20 +39,17 @@ def image_to_url(image_path):
|
|
40 |
except Exception as e:
|
41 |
return f"An error occurred: {e}"
|
42 |
|
43 |
-
def
|
44 |
-
orientation: str = "all", video_type: str = "all"):
|
45 |
"""
|
46 |
-
Searches the Pixabay API for royalty-free stock images
|
47 |
|
48 |
Args:
|
49 |
-
query (str): The search term for finding stock
|
50 |
-
|
51 |
-
|
52 |
-
orientation (str): Filter results by image orientation (used only if media_type is "Image"). Accepted values: "all", "horizontal", "vertical". Defaults to "all".
|
53 |
-
video_type (str): Filter results by video type (used only if media_type is "Video"). Accepted values: "all", "film", "animation". Defaults to "all".
|
54 |
|
55 |
Returns:
|
56 |
-
str: URL of the found
|
57 |
"""
|
58 |
if not query:
|
59 |
return "Please enter a search query."
|
@@ -66,64 +62,38 @@ def search_pixabay(query: str = "", media_type: str = "Image", image_type: str =
|
|
66 |
'q': query,
|
67 |
'per_page': PER_PAGE,
|
68 |
'page': 1,
|
69 |
-
'safesearch': 'true'
|
|
|
|
|
70 |
}
|
71 |
|
72 |
-
if media_type == "Image":
|
73 |
-
api_url = IMAGE_API_URL
|
74 |
-
params['image_type'] = image_type
|
75 |
-
params['orientation'] = orientation
|
76 |
-
elif media_type == "Video":
|
77 |
-
api_url = VIDEO_API_URL
|
78 |
-
params['video_type'] = video_type
|
79 |
-
else:
|
80 |
-
return "Invalid media type selected."
|
81 |
-
|
82 |
try:
|
83 |
-
response = requests.get(
|
84 |
response.raise_for_status()
|
85 |
data = response.json()
|
86 |
|
87 |
if data.get('totalHits', 0) == 0:
|
88 |
-
return f"No results found for '{query}'."
|
89 |
|
90 |
hits = data.get('hits', [])
|
91 |
if not hits:
|
92 |
-
|
93 |
|
94 |
selected_hit = random.choice(hits)
|
|
|
95 |
|
96 |
-
if
|
97 |
-
image_url
|
98 |
-
|
99 |
-
|
100 |
-
else:
|
101 |
-
return "Could not retrieve large image URL."
|
102 |
-
|
103 |
-
elif media_type == "Video":
|
104 |
-
video_urls = selected_hit.get('videos', {})
|
105 |
-
large_video = video_urls.get('large', {})
|
106 |
-
video_url = large_video.get('url')
|
107 |
-
|
108 |
-
if video_url:
|
109 |
-
return video_url
|
110 |
-
else:
|
111 |
-
medium_video = video_urls.get('medium', {})
|
112 |
-
video_url = medium_video.get('url')
|
113 |
-
if video_url:
|
114 |
-
return video_url
|
115 |
-
else:
|
116 |
-
return "Could not retrieve video URL."
|
117 |
|
118 |
except requests.exceptions.RequestException as e:
|
119 |
return f"API request error: {e}"
|
120 |
except json.JSONDecodeError:
|
121 |
-
|
122 |
except Exception as e:
|
123 |
return f"An unexpected error occurred: {e}"
|
124 |
|
125 |
-
# In the Gradio interface section, replace the Pixabay search outputs with:
|
126 |
-
|
127 |
def together_text_to_image(prompt: str = "", width: int = 1024, height: int = 1024):
|
128 |
"""
|
129 |
Generates an image from a text prompt using the Together AI API and the FLUX.1.1-pro model.
|
@@ -209,53 +179,68 @@ def together_image_to_image(image_path: str = None, prompt: str = ""):
|
|
209 |
except Exception as e:
|
210 |
return f"Error transforming image: {e}"
|
211 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
212 |
# --- Gradio Blocks Interface Definition ---
|
213 |
with gr.Blocks(title="Media Generation and Search Explorer") as demo:
|
214 |
gr.Markdown("## Media Generation and Search Explorer")
|
215 |
-
gr.Markdown("Explore royalty-free
|
216 |
|
217 |
-
with gr.Tab("Pixabay Search"):
|
218 |
-
gr.Markdown("Search for royalty-free images
|
219 |
gr.Warning("This requires setting the PIXABAY_API_KEY environment variable.")
|
|
|
220 |
with gr.Row():
|
221 |
-
pixabay_query_input = gr.Textbox(label="Search Query", placeholder="e.g., yellow flowers"
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
elif media_type == "Video":
|
239 |
-
return (gr.update(visible=False), gr.update(visible=True))
|
240 |
-
else:
|
241 |
-
return (gr.update(visible=True), gr.update(visible=False))
|
242 |
-
|
243 |
-
pixabay_media_type_radio.change(
|
244 |
-
fn=update_pixabay_inputs_blocks,
|
245 |
-
inputs=pixabay_media_type_radio,
|
246 |
-
outputs=[pixabay_image_options_col, pixabay_video_options_col],
|
247 |
-
api_name=False,
|
248 |
-
show_api=False
|
249 |
-
)
|
250 |
-
|
251 |
pixabay_search_button.click(
|
252 |
-
fn=
|
253 |
inputs=[
|
254 |
pixabay_query_input,
|
255 |
-
pixabay_media_type_radio,
|
256 |
pixabay_image_type_input,
|
257 |
-
pixabay_orientation_input
|
258 |
-
pixabay_video_type_input
|
259 |
],
|
260 |
outputs=pixabay_output
|
261 |
)
|
@@ -294,54 +279,29 @@ with gr.Blocks(title="Media Generation and Search Explorer") as demo:
|
|
294 |
with gr.Tab("Together AI - Text to Audio"):
|
295 |
gr.Markdown("Generate audio from text using Together AI's text-to-speech models.")
|
296 |
gr.Warning("This requires setting the TOGETHER_API_KEY environment variable.")
|
297 |
-
|
298 |
with gr.Row():
|
299 |
tts_input_text = gr.Textbox(label="Enter text to convert to speech", lines=3)
|
300 |
tts_voice_selection = gr.Dropdown(
|
301 |
label="Select Voice",
|
302 |
-
choices=[
|
|
|
|
|
|
|
|
|
|
|
303 |
value="helpful woman"
|
304 |
)
|
305 |
tts_generate_button = gr.Button("Generate Audio")
|
306 |
-
|
307 |
tts_audio_output = gr.Textbox(label="Generated Audio (url)", interactive=False)
|
308 |
-
|
309 |
-
def text_to_speech(text: str = "", voice: str = ""):
|
310 |
-
"""
|
311 |
-
Converts text to speech using Together AI's audio API.
|
312 |
-
|
313 |
-
Args:
|
314 |
-
text (str): The text to convert to speech
|
315 |
-
voice (str): The voice to use for speech synthesis. All available voices are: calm lady, meditation lady, storyteller lady, wise lady, teacher lady, wise man, customer support man, tutorial man, helpful woman, customer support lady, asmr lady, pleasant man, professional woman, reading lady, reading man. Default is Helpful Woman.
|
316 |
-
|
317 |
-
Returns:
|
318 |
-
url (str): Give url to the generated audio file or error message
|
319 |
-
"""
|
320 |
-
if not client:
|
321 |
-
return None, "Together AI client not initialized. Please set the TOGETHER_API_KEY environment variable."
|
322 |
-
if not text:
|
323 |
-
return None, "Please enter text to convert to speech."
|
324 |
-
|
325 |
-
try:
|
326 |
-
speech_file_path = "speech.mp3"
|
327 |
-
response = client.audio.speech.create(
|
328 |
-
model="cartesia/sonic",
|
329 |
-
input=text,
|
330 |
-
voice=voice,
|
331 |
-
)
|
332 |
-
response.stream_to_file(speech_file_path)
|
333 |
-
url = image_to_url(speech_file_path)
|
334 |
-
return url
|
335 |
-
except Exception as e:
|
336 |
-
return None, f"Error generating speech: {e}"
|
337 |
-
|
338 |
tts_generate_button.click(
|
339 |
fn=text_to_speech,
|
340 |
inputs=[tts_input_text, tts_voice_selection],
|
341 |
outputs=tts_audio_output
|
342 |
)
|
343 |
|
344 |
-
|
345 |
# --- Launch the Gradio app ---
|
346 |
if __name__ == "__main__":
|
347 |
demo.launch(mcp_server=True)
|
|
|
14 |
# --- Pixabay API Configuration ---
|
15 |
PIXABAY_API_KEY = os.environ.get('PIXABAY_API_KEY')
|
16 |
IMAGE_API_URL = 'https://pixabay.com/api/'
|
|
|
17 |
PER_PAGE = 5
|
18 |
|
19 |
def image_to_url(image_path):
|
|
|
39 |
except Exception as e:
|
40 |
return f"An error occurred: {e}"
|
41 |
|
42 |
+
def search_pixabay_images(query: str = "", image_type: str = "all", orientation: str = "all"):
|
|
|
43 |
"""
|
44 |
+
Searches the Pixabay API for royalty-free stock images based on user's query and filters.
|
45 |
|
46 |
Args:
|
47 |
+
query (str): The search term for finding stock images.
|
48 |
+
image_type (str): Filter results by image type. Accepted values: "all", "photo", "illustration", "vector".
|
49 |
+
orientation (str): Filter results by image orientation. Accepted values: "all", "horizontal", "vertical".
|
|
|
|
|
50 |
|
51 |
Returns:
|
52 |
+
str: URL of the found image or status/error message
|
53 |
"""
|
54 |
if not query:
|
55 |
return "Please enter a search query."
|
|
|
62 |
'q': query,
|
63 |
'per_page': PER_PAGE,
|
64 |
'page': 1,
|
65 |
+
'safesearch': 'true',
|
66 |
+
'image_type': image_type,
|
67 |
+
'orientation': orientation
|
68 |
}
|
69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
try:
|
71 |
+
response = requests.get(IMAGE_API_URL, params=params)
|
72 |
response.raise_for_status()
|
73 |
data = response.json()
|
74 |
|
75 |
if data.get('totalHits', 0) == 0:
|
76 |
+
return f"No image results found for '{query}'."
|
77 |
|
78 |
hits = data.get('hits', [])
|
79 |
if not hits:
|
80 |
+
return f"No image results found for '{query}'."
|
81 |
|
82 |
selected_hit = random.choice(hits)
|
83 |
+
image_url = selected_hit.get('largeImageURL')
|
84 |
|
85 |
+
if image_url:
|
86 |
+
return image_url
|
87 |
+
else:
|
88 |
+
return "Could not retrieve large image URL."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
|
90 |
except requests.exceptions.RequestException as e:
|
91 |
return f"API request error: {e}"
|
92 |
except json.JSONDecodeError:
|
93 |
+
return "Error decoding API response."
|
94 |
except Exception as e:
|
95 |
return f"An unexpected error occurred: {e}"
|
96 |
|
|
|
|
|
97 |
def together_text_to_image(prompt: str = "", width: int = 1024, height: int = 1024):
|
98 |
"""
|
99 |
Generates an image from a text prompt using the Together AI API and the FLUX.1.1-pro model.
|
|
|
179 |
except Exception as e:
|
180 |
return f"Error transforming image: {e}"
|
181 |
|
182 |
+
def text_to_speech(text: str = "", voice: str = ""):
|
183 |
+
"""
|
184 |
+
Converts text to speech using Together AI's audio API.
|
185 |
+
|
186 |
+
Args:
|
187 |
+
text (str): The text to convert to speech
|
188 |
+
voice (str): The voice to use for speech synthesis.
|
189 |
+
|
190 |
+
Returns:
|
191 |
+
url (str): Give url to the generated audio file or error message
|
192 |
+
"""
|
193 |
+
if not client:
|
194 |
+
return "Together AI client not initialized. Please set the TOGETHER_API_KEY environment variable."
|
195 |
+
if not text:
|
196 |
+
return "Please enter text to convert to speech."
|
197 |
+
|
198 |
+
try:
|
199 |
+
speech_file_path = "speech.mp3"
|
200 |
+
response = client.audio.speech.create(
|
201 |
+
model="cartesia/sonic",
|
202 |
+
input=text,
|
203 |
+
voice=voice,
|
204 |
+
)
|
205 |
+
response.stream_to_file(speech_file_path)
|
206 |
+
url = image_to_url(speech_file_path)
|
207 |
+
return url
|
208 |
+
except Exception as e:
|
209 |
+
return f"Error generating speech: {e}"
|
210 |
+
|
211 |
# --- Gradio Blocks Interface Definition ---
|
212 |
with gr.Blocks(title="Media Generation and Search Explorer") as demo:
|
213 |
gr.Markdown("## Media Generation and Search Explorer")
|
214 |
+
gr.Markdown("Explore royalty-free images from Pixabay and generate/transform images using Together AI.")
|
215 |
|
216 |
+
with gr.Tab("Pixabay Image Search"):
|
217 |
+
gr.Markdown("Search for royalty-free images on Pixabay.")
|
218 |
gr.Warning("This requires setting the PIXABAY_API_KEY environment variable.")
|
219 |
+
|
220 |
with gr.Row():
|
221 |
+
pixabay_query_input = gr.Textbox(label="Search Query", placeholder="e.g., yellow flowers")
|
222 |
+
pixabay_search_button = gr.Button("Search Images")
|
223 |
+
|
224 |
+
with gr.Row():
|
225 |
+
pixabay_image_type_input = gr.Radio(
|
226 |
+
["all", "photo", "illustration", "vector"],
|
227 |
+
label="Image Type",
|
228 |
+
value="all"
|
229 |
+
)
|
230 |
+
pixabay_orientation_input = gr.Radio(
|
231 |
+
["all", "horizontal", "vertical"],
|
232 |
+
label="Orientation",
|
233 |
+
value="all"
|
234 |
+
)
|
235 |
+
|
236 |
+
pixabay_output = gr.Textbox(label="Result Image URL", interactive=False)
|
237 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
238 |
pixabay_search_button.click(
|
239 |
+
fn=search_pixabay_images,
|
240 |
inputs=[
|
241 |
pixabay_query_input,
|
|
|
242 |
pixabay_image_type_input,
|
243 |
+
pixabay_orientation_input
|
|
|
244 |
],
|
245 |
outputs=pixabay_output
|
246 |
)
|
|
|
279 |
with gr.Tab("Together AI - Text to Audio"):
|
280 |
gr.Markdown("Generate audio from text using Together AI's text-to-speech models.")
|
281 |
gr.Warning("This requires setting the TOGETHER_API_KEY environment variable.")
|
282 |
+
|
283 |
with gr.Row():
|
284 |
tts_input_text = gr.Textbox(label="Enter text to convert to speech", lines=3)
|
285 |
tts_voice_selection = gr.Dropdown(
|
286 |
label="Select Voice",
|
287 |
+
choices=[
|
288 |
+
'calm lady', 'meditation lady', 'storyteller lady', 'wise lady', 'teacher lady',
|
289 |
+
'wise man', 'customer support man', 'tutorial man', 'helpful woman',
|
290 |
+
'customer support lady', 'asmr lady', 'pleasant man', 'professional woman',
|
291 |
+
'reading lady', 'reading man'
|
292 |
+
],
|
293 |
value="helpful woman"
|
294 |
)
|
295 |
tts_generate_button = gr.Button("Generate Audio")
|
296 |
+
|
297 |
tts_audio_output = gr.Textbox(label="Generated Audio (url)", interactive=False)
|
298 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
299 |
tts_generate_button.click(
|
300 |
fn=text_to_speech,
|
301 |
inputs=[tts_input_text, tts_voice_selection],
|
302 |
outputs=tts_audio_output
|
303 |
)
|
304 |
|
|
|
305 |
# --- Launch the Gradio app ---
|
306 |
if __name__ == "__main__":
|
307 |
demo.launch(mcp_server=True)
|