Update app.py
Browse files
app.py
CHANGED
@@ -53,16 +53,13 @@ def search_pixabay(query: str = "", media_type: str = "Image", image_type: str =
|
|
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 |
-
|
57 |
-
- str: URL of the found image (or None if not found or media_type is "Video")
|
58 |
-
- str: URL of the found video (or None if not found or media_type is "Image")
|
59 |
-
- str: Status message or error message
|
60 |
"""
|
61 |
if not query:
|
62 |
-
return
|
63 |
|
64 |
if not PIXABAY_API_KEY:
|
65 |
-
return
|
66 |
|
67 |
params = {
|
68 |
'key': PIXABAY_API_KEY,
|
@@ -80,7 +77,7 @@ def search_pixabay(query: str = "", media_type: str = "Image", image_type: str =
|
|
80 |
api_url = VIDEO_API_URL
|
81 |
params['video_type'] = video_type
|
82 |
else:
|
83 |
-
return
|
84 |
|
85 |
try:
|
86 |
response = requests.get(api_url, params=params)
|
@@ -88,20 +85,20 @@ def search_pixabay(query: str = "", media_type: str = "Image", image_type: str =
|
|
88 |
data = response.json()
|
89 |
|
90 |
if data.get('totalHits', 0) == 0:
|
91 |
-
return
|
92 |
|
93 |
hits = data.get('hits', [])
|
94 |
if not hits:
|
95 |
-
return
|
96 |
|
97 |
selected_hit = random.choice(hits)
|
98 |
|
99 |
if media_type == "Image":
|
100 |
image_url = selected_hit.get('largeImageURL')
|
101 |
if image_url:
|
102 |
-
return image_url
|
103 |
else:
|
104 |
-
return
|
105 |
|
106 |
elif media_type == "Video":
|
107 |
video_urls = selected_hit.get('videos', {})
|
@@ -109,21 +106,23 @@ def search_pixabay(query: str = "", media_type: str = "Image", image_type: str =
|
|
109 |
video_url = large_video.get('url')
|
110 |
|
111 |
if video_url:
|
112 |
-
return
|
113 |
else:
|
114 |
medium_video = video_urls.get('medium', {})
|
115 |
video_url = medium_video.get('url')
|
116 |
if video_url:
|
117 |
-
return
|
118 |
else:
|
119 |
-
return
|
120 |
|
121 |
except requests.exceptions.RequestException as e:
|
122 |
-
return
|
123 |
except json.JSONDecodeError:
|
124 |
-
return
|
125 |
except Exception as e:
|
126 |
-
return
|
|
|
|
|
127 |
|
128 |
def together_text_to_image(prompt: str = "", width: int = 1024, height: int = 1024):
|
129 |
"""
|
@@ -222,41 +221,33 @@ with gr.Blocks(title="Media Generation and Search Explorer") as demo:
|
|
222 |
pixabay_query_input = gr.Textbox(label="Search Query", placeholder="e.g., yellow flowers", scale=2)
|
223 |
pixabay_media_type_radio = gr.Radio(["Image", "Video"], label="Media Type", value="Image", scale=1)
|
224 |
pixabay_search_button = gr.Button("Search")
|
225 |
-
|
226 |
with gr.Column(visible=True) as pixabay_image_options_col:
|
227 |
pixabay_image_type_input = gr.Radio(["all", "photo", "illustration", "vector"], label="Image Type", value="all")
|
228 |
pixabay_orientation_input = gr.Radio(["all", "horizontal", "vertical"], label="Orientation", value="all")
|
229 |
-
|
230 |
with gr.Column(visible=False) as pixabay_video_options_col:
|
231 |
pixabay_video_type_input = gr.Radio(["all", "film", "animation"], label="Video Type", value="all")
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
with gr.Row():
|
236 |
-
pixabay_image_output = gr.Image(label="Result Image (URL)", interactive=False)
|
237 |
-
pixabay_video_output = gr.Video(label="Result Video (URL)", interactive=False)
|
238 |
-
|
239 |
def update_pixabay_inputs_blocks(media_type):
|
240 |
"""Updates the visibility of input columns based on selected media type."""
|
241 |
if media_type == "Image":
|
242 |
-
return (gr.update(visible=True), gr.update(visible=False)
|
243 |
-
gr.update(visible=True), gr.update(visible=False))
|
244 |
elif media_type == "Video":
|
245 |
-
return (gr.update(visible=False), gr.update(visible=True)
|
246 |
-
gr.update(visible=False), gr.update(visible=True))
|
247 |
else:
|
248 |
-
return (gr.update(visible=True), gr.update(visible=False)
|
249 |
-
|
250 |
-
|
251 |
pixabay_media_type_radio.change(
|
252 |
fn=update_pixabay_inputs_blocks,
|
253 |
inputs=pixabay_media_type_radio,
|
254 |
-
outputs=[pixabay_image_options_col, pixabay_video_options_col,
|
255 |
-
pixabay_image_output, pixabay_video_output],
|
256 |
api_name=False,
|
257 |
show_api=False
|
258 |
)
|
259 |
-
|
260 |
pixabay_search_button.click(
|
261 |
fn=search_pixabay,
|
262 |
inputs=[
|
@@ -266,7 +257,7 @@ with gr.Blocks(title="Media Generation and Search Explorer") as demo:
|
|
266 |
pixabay_orientation_input,
|
267 |
pixabay_video_type_input
|
268 |
],
|
269 |
-
outputs=
|
270 |
)
|
271 |
|
272 |
with gr.Tab("Together AI - Text to Image"):
|
@@ -277,7 +268,7 @@ with gr.Blocks(title="Media Generation and Search Explorer") as demo:
|
|
277 |
together_text_to_image_width = gr.Slider(label="Width", value=1024, minimum=512, maximum=1440)
|
278 |
together_text_to_image_height = gr.Slider(label="Height", value=1024, minimum=512, maximum=1440)
|
279 |
together_text_to_image_button = gr.Button("Generate Image", scale=1)
|
280 |
-
together_text_to_image_output = gr.
|
281 |
|
282 |
together_text_to_image_button.click(
|
283 |
fn=together_text_to_image,
|
@@ -292,7 +283,7 @@ with gr.Blocks(title="Media Generation and Search Explorer") as demo:
|
|
292 |
together_image_input = gr.Image(label="Upload or paste an image", type="filepath", scale=2)
|
293 |
together_image_to_image_prompt = gr.Textbox(label="Enter your transformation prompt", scale=2)
|
294 |
together_image_to_image_button = gr.Button("Transform Image", scale=1)
|
295 |
-
together_image_to_image_output = gr.
|
296 |
|
297 |
together_image_to_image_button.click(
|
298 |
fn=together_image_to_image,
|
@@ -313,7 +304,7 @@ with gr.Blocks(title="Media Generation and Search Explorer") as demo:
|
|
313 |
)
|
314 |
tts_generate_button = gr.Button("Generate Audio")
|
315 |
|
316 |
-
tts_audio_output = gr.
|
317 |
|
318 |
def text_to_speech(text: str = "", voice: str = ""):
|
319 |
"""
|
@@ -324,7 +315,7 @@ with gr.Blocks(title="Media Generation and Search Explorer") as demo:
|
|
324 |
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.
|
325 |
|
326 |
Returns:
|
327 |
-
str:
|
328 |
"""
|
329 |
if not client:
|
330 |
return None, "Together AI client not initialized. Please set the TOGETHER_API_KEY environment variable."
|
|
|
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 media or status/error message
|
|
|
|
|
|
|
57 |
"""
|
58 |
if not query:
|
59 |
+
return "Please enter a search query."
|
60 |
|
61 |
if not PIXABAY_API_KEY:
|
62 |
+
return "Pixabay API Key not found. Please set the PIXABAY_API_KEY environment variable."
|
63 |
|
64 |
params = {
|
65 |
'key': PIXABAY_API_KEY,
|
|
|
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(api_url, params=params)
|
|
|
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 |
+
return f"No results found for '{query}'."
|
93 |
|
94 |
selected_hit = random.choice(hits)
|
95 |
|
96 |
if media_type == "Image":
|
97 |
image_url = selected_hit.get('largeImageURL')
|
98 |
if image_url:
|
99 |
+
return image_url
|
100 |
else:
|
101 |
+
return "Could not retrieve large image URL."
|
102 |
|
103 |
elif media_type == "Video":
|
104 |
video_urls = selected_hit.get('videos', {})
|
|
|
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 |
+
return "Error decoding API response."
|
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 |
"""
|
|
|
221 |
pixabay_query_input = gr.Textbox(label="Search Query", placeholder="e.g., yellow flowers", scale=2)
|
222 |
pixabay_media_type_radio = gr.Radio(["Image", "Video"], label="Media Type", value="Image", scale=1)
|
223 |
pixabay_search_button = gr.Button("Search")
|
224 |
+
|
225 |
with gr.Column(visible=True) as pixabay_image_options_col:
|
226 |
pixabay_image_type_input = gr.Radio(["all", "photo", "illustration", "vector"], label="Image Type", value="all")
|
227 |
pixabay_orientation_input = gr.Radio(["all", "horizontal", "vertical"], label="Orientation", value="all")
|
228 |
+
|
229 |
with gr.Column(visible=False) as pixabay_video_options_col:
|
230 |
pixabay_video_type_input = gr.Radio(["all", "film", "animation"], label="Video Type", value="all")
|
231 |
+
|
232 |
+
pixabay_output = gr.Textbox(label="Result", interactive=False)
|
233 |
+
|
|
|
|
|
|
|
|
|
234 |
def update_pixabay_inputs_blocks(media_type):
|
235 |
"""Updates the visibility of input columns based on selected media type."""
|
236 |
if media_type == "Image":
|
237 |
+
return (gr.update(visible=True), gr.update(visible=False))
|
|
|
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=search_pixabay,
|
253 |
inputs=[
|
|
|
257 |
pixabay_orientation_input,
|
258 |
pixabay_video_type_input
|
259 |
],
|
260 |
+
outputs=pixabay_output
|
261 |
)
|
262 |
|
263 |
with gr.Tab("Together AI - Text to Image"):
|
|
|
268 |
together_text_to_image_width = gr.Slider(label="Width", value=1024, minimum=512, maximum=1440)
|
269 |
together_text_to_image_height = gr.Slider(label="Height", value=1024, minimum=512, maximum=1440)
|
270 |
together_text_to_image_button = gr.Button("Generate Image", scale=1)
|
271 |
+
together_text_to_image_output = gr.Textbox(label="Generated Image (URL)", type="filepath", interactive=False)
|
272 |
|
273 |
together_text_to_image_button.click(
|
274 |
fn=together_text_to_image,
|
|
|
283 |
together_image_input = gr.Image(label="Upload or paste an image", type="filepath", scale=2)
|
284 |
together_image_to_image_prompt = gr.Textbox(label="Enter your transformation prompt", scale=2)
|
285 |
together_image_to_image_button = gr.Button("Transform Image", scale=1)
|
286 |
+
together_image_to_image_output = gr.Textbox(label="Transformed Image (URL)", type="filepath", interactive=False)
|
287 |
|
288 |
together_image_to_image_button.click(
|
289 |
fn=together_image_to_image,
|
|
|
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 |
"""
|
|
|
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."
|