KingNish commited on
Commit
0cf5240
·
verified ·
1 Parent(s): 3f54e46

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +122 -0
app.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import random
4
+ import json
5
+ import os
6
+
7
+ PIXABAY_API_KEY = os.environ.get('PIXABAY_API_KEY')
8
+ IMAGE_API_URL = 'https://pixabay.com/api/'
9
+ VIDEO_API_URL = 'https://pixabay.com/api/videos/'
10
+ PER_PAGE = 5
11
+
12
+ def search_pixabay(query: str, media_type: str, image_type: str = 'all', orientation: str = 'all', video_type: str = 'all'):
13
+ if not query:
14
+ return None, None, "Please enter a search query."
15
+
16
+ params = {
17
+ 'key': PIXABAY_API_KEY,
18
+ 'q': query,
19
+ 'per_page': PER_PAGE,
20
+ 'page': 1,
21
+ 'safesearch': 'true'
22
+ }
23
+
24
+ if media_type == "Image":
25
+ api_url = IMAGE_API_URL
26
+ params['image_type'] = image_type
27
+ params['orientation'] = orientation
28
+ elif media_type == "Video":
29
+ api_url = VIDEO_API_URL
30
+ params['video_type'] = video_type
31
+ else:
32
+ return None, None, "Invalid media type selected."
33
+
34
+ try:
35
+ response = requests.get(api_url, params=params)
36
+ response.raise_for_status()
37
+ data = response.json()
38
+
39
+ if data.get('totalHits', 0) == 0:
40
+ return None, None, f"No results found for '{query}'."
41
+
42
+ hits = data.get('hits', [])
43
+ if not hits:
44
+ return None, None, f"No results found for '{query}'."
45
+
46
+ selected_hit = random.choice(hits)
47
+
48
+ if media_type == "Image":
49
+ image_url = selected_hit.get('largeImageURL')
50
+ if image_url:
51
+ return image_url, None, ""
52
+ else:
53
+ return None, None, "Could not retrieve large image URL."
54
+
55
+ elif media_type == "Video":
56
+ video_urls = selected_hit.get('videos', {})
57
+ large_video = video_urls.get('large', {})
58
+ video_url = large_video.get('url')
59
+
60
+ if video_url:
61
+ return None, video_url, ""
62
+ else:
63
+ medium_video = video_urls.get('medium', {})
64
+ video_url = medium_video.get('url')
65
+ if video_url:
66
+ return None, video_url, "Using medium quality video."
67
+ else:
68
+ return None, None, "Could not retrieve video URL."
69
+
70
+ except requests.exceptions.RequestException as e:
71
+ return None, None, f"API request error: {e}"
72
+ except json.JSONDecodeError:
73
+ return None, None, "Error decoding API response."
74
+ except Exception as e:
75
+ return None, None, f"An unexpected error occurred: {e}"
76
+
77
+ # Gradio input components
78
+ image_type_input = gr.Radio(["all", "photo", "illustration", "vector"], label="Image Type", value="all", visible=True)
79
+ orientation_input = gr.Radio(["all", "horizontal", "vertical"], label="Orientation", value="all", visible=True)
80
+ video_type_input = gr.Radio(["all", "film", "animation"], label="Video Type", value="all", visible=False)
81
+ media_type_radio = gr.Radio(["Image", "Video"], label="Media Type", value="Image")
82
+
83
+ # Function to toggle input visibility
84
+ def update_inputs(media_type):
85
+ if media_type == "Image":
86
+ return gr.update(visible=True), gr.update(visible=True), gr.update(visible=False)
87
+ elif media_type == "Video":
88
+ return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
89
+ else:
90
+ return gr.update(visible=True), gr.update(visible=True), gr.update(visible=False)
91
+
92
+ media_type_radio.change(
93
+ fn=update_inputs,
94
+ inputs=media_type_radio,
95
+ outputs=[image_type_input, orientation_input, video_type_input]
96
+ )
97
+
98
+ # Gradio output components
99
+ output_components = [
100
+ gr.Image(label="Result URL", type="filepath", interactive=False),
101
+ gr.Video(label="Result URL", interactive=False),
102
+ gr.Textbox(label="Status", interactive=False)
103
+ ]
104
+
105
+ # Define the Gradio interface
106
+ interface = gr.Interface(
107
+ fn=search_pixabay,
108
+ inputs=[
109
+ gr.Textbox(label="Search Query", placeholder="e.g., yellow flowers"),
110
+ media_type_radio,
111
+ image_type_input,
112
+ orientation_input,
113
+ video_type_input
114
+ ],
115
+ outputs=output_components,
116
+ title="Pixabay Random Media Explorer (URL Output)",
117
+ description="Get a random media result from Pixabay (URL output)."
118
+ )
119
+
120
+ # Launch the app
121
+ if __name__ == "__main__":
122
+ interface.launch()