omaralaa2004 commited on
Commit
a0bcfb0
·
verified ·
1 Parent(s): 2f16637

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +108 -0
app.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import base64
4
+ from io import BytesIO
5
+ from PIL import Image
6
+ import os
7
+
8
+ # 🔐 Use environment variable for your key (set it in Hugging Face "Secrets")
9
+ API_KEY = os.getenv("GOOGLE_API_KEY")
10
+
11
+ # ---------- Helper Functions ---------- #
12
+
13
+ def text_generation(prompt):
14
+ url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent"
15
+ headers = {"Content-Type": "application/json", "x-goog-api-key": API_KEY}
16
+ data = {"contents": [{"parts": [{"text": prompt}]}]}
17
+ res = requests.post(url, headers=headers, json=data)
18
+ output = res.json()
19
+ try:
20
+ return output["candidates"][0]["content"]["parts"][0]["text"]
21
+ except Exception:
22
+ return str(output)
23
+
24
+
25
+ def image_generation(prompt):
26
+ url = "https://generativelanguage.googleapis.com/v1beta/models/imagen-3.0-generate:generateImage"
27
+ headers = {"Content-Type": "application/json", "x-goog-api-key": API_KEY}
28
+ data = {"prompt": {"text": prompt}}
29
+ res = requests.post(url, headers=headers, json=data)
30
+ result = res.json()
31
+ try:
32
+ img_b64 = result["images"][0]["data"]
33
+ image_bytes = base64.b64decode(img_b64)
34
+ return Image.open(BytesIO(image_bytes))
35
+ except Exception:
36
+ return str(result)
37
+
38
+
39
+ def video_generation(prompt):
40
+ url = "https://generativelanguage.googleapis.com/v1beta/models/veo-2.0-generate-001:generateVideo"
41
+ headers = {"Content-Type": "application/json", "x-goog-api-key": API_KEY}
42
+ data = {"prompt": {"text": prompt}}
43
+ res = requests.post(url, headers=headers, json=data)
44
+ result = res.json()
45
+ try:
46
+ video_url = result["videos"][0].get("url", None)
47
+ if video_url:
48
+ return video_url
49
+ else:
50
+ return "Video generated (but no URL returned): " + str(result)
51
+ except Exception:
52
+ return str(result)
53
+
54
+
55
+ def text_to_speech(text):
56
+ url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-tts:generateContent"
57
+ headers = {"Content-Type": "application/json", "x-goog-api-key": API_KEY}
58
+ data = {"contents": [{"parts": [{"text": text}]}]}
59
+ res = requests.post(url, headers=headers, json=data)
60
+ result = res.json()
61
+ try:
62
+ audio_b64 = result["candidates"][0]["content"]["parts"][0]["inline_data"]["data"]
63
+ audio_bytes = base64.b64decode(audio_b64)
64
+ return (audio_bytes, "audio/wav")
65
+ except Exception:
66
+ return str(result)
67
+
68
+ # ---------- Gradio Interfaces ---------- #
69
+
70
+ text_tab = gr.Interface(
71
+ fn=text_generation,
72
+ inputs=gr.Textbox(label="Enter your text prompt"),
73
+ outputs=gr.Textbox(label="Generated Text"),
74
+ title="🧠 Text Generation (Gemini 2.5 Flash)"
75
+ )
76
+
77
+ image_tab = gr.Interface(
78
+ fn=image_generation,
79
+ inputs=gr.Textbox(label="Enter image prompt"),
80
+ outputs=gr.Image(type="pil"),
81
+ title="🖼️ Image Generation (Imagen 3)"
82
+ )
83
+
84
+ video_tab = gr.Interface(
85
+ fn=video_generation,
86
+ inputs=gr.Textbox(label="Describe your video idea"),
87
+ outputs=gr.Textbox(label="Generated Video URL / Response"),
88
+ title="🎥 Video Generation (Veo 2)"
89
+ )
90
+
91
+ tts_tab = gr.Interface(
92
+ fn=text_to_speech,
93
+ inputs=gr.Textbox(label="Enter text for speech"),
94
+ outputs=gr.Audio(label="Generated Speech"),
95
+ title="🗣️ Text-to-Speech (Gemini TTS)"
96
+ )
97
+
98
+ # ---------- Combine All Tabs ---------- #
99
+
100
+ demo = gr.TabbedInterface(
101
+ [text_tab, image_tab, video_tab, tts_tab],
102
+ tab_names=["Text", "Image", "Video", "Speech"]
103
+ )
104
+
105
+ # ---------- Launch ---------- #
106
+
107
+ if __name__ == "__main__":
108
+ demo.launch()