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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -88
app.py CHANGED
@@ -1,108 +1,45 @@
 
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()
 
1
+ import os
2
  import gradio as gr
3
  import requests
 
 
 
 
4
 
5
+ # Load API key from secret
6
  API_KEY = os.getenv("GOOGLE_API_KEY")
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  def image_generation(prompt):
9
+ if not API_KEY:
10
+ return " API key not found. Please add GOOGLE_API_KEY in your Hugging Face secrets."
 
 
 
 
 
 
 
 
 
11
 
12
+ url = "https://generativelanguage.googleapis.com/v1beta/models/imagegeneration:generate?key=" + API_KEY
13
 
14
+ headers = {"Content-Type": "application/json"}
 
 
15
  data = {"prompt": {"text": prompt}}
16
+
17
  res = requests.post(url, headers=headers, json=data)
 
 
 
 
 
 
 
 
 
18
 
19
+ # Debugging info
20
+ if res.status_code != 200:
21
+ return f"❌ Error {res.status_code}: {res.text}"
22
 
 
 
 
 
 
 
23
  try:
24
+ result = res.json()
25
+ except Exception as e:
26
+ return f"❌ Failed to parse JSON response: {str(e)}\n\nRaw response:\n{res.text}"
 
 
 
 
27
 
28
+ # Extract image
29
+ try:
30
+ image_base64 = result["candidates"][0]["image"]["base64"]
31
+ return gr.Image(value=image_base64)
32
+ except Exception as e:
33
+ return f"⚠️ Could not extract image: {str(e)}\n\nFull JSON:\n{result}"
34
 
35
+ # Gradio UI
36
+ demo = gr.Interface(
37
  fn=image_generation,
38
+ inputs=gr.Textbox(label="Enter your prompt"),
39
+ outputs="image",
40
+ title="Google AI Studio Image Generator",
41
+ description="Generate images using Google AI Studio API."
 
 
 
 
 
 
42
  )
43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  if __name__ == "__main__":
45
  demo.launch()