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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -21
app.py CHANGED
@@ -1,45 +1,54 @@
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()
 
1
  import os
 
2
  import requests
3
+ import gradio as gr
4
 
5
+ # Get the API key from Hugging Face Secrets
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
+ # Google Imagen 3.0 endpoint for image generation
13
+ url = f"https://generativelanguage.googleapis.com/v1beta/models/imagen-3.0-generate:generate?key={API_KEY}"
14
 
15
  headers = {"Content-Type": "application/json"}
16
  data = {"prompt": {"text": prompt}}
17
 
18
+ # Send POST request
19
  res = requests.post(url, headers=headers, json=data)
20
 
21
+ # Check for HTTP errors
22
  if res.status_code != 200:
23
+ return f"❌ Error {res.status_code}:\n{res.text}"
24
 
25
  try:
26
  result = res.json()
27
+ except Exception:
28
+ return f"❌ Invalid JSON response:\n{res.text}"
29
+
30
+ # If API returned base64 image data
31
+ if "images" in result and len(result["images"]) > 0:
32
+ image_base64 = result["images"][0].get("image_base64", None)
33
+ if image_base64:
34
+ import base64
35
+ from io import BytesIO
36
+ from PIL import Image
37
+ image_bytes = base64.b64decode(image_base64)
38
+ image = Image.open(BytesIO(image_bytes))
39
+ return image
40
+
41
+ # Otherwise show the raw response for debugging
42
+ return f"⚠️ Unexpected response:\n{result}"
43
+
44
+ # Gradio interface
45
+ iface = gr.Interface(
46
  fn=image_generation,
47
+ inputs=gr.Textbox(label="Enter your prompt", placeholder="e.g. A robot painting a sunset over the ocean"),
48
+ outputs=gr.Image(label="Generated Image"),
49
  title="Google AI Studio Image Generator",
50
+ description="Generate images using Google AI Studio (Imagen 3.0 API). Educational use only."
51
  )
52
 
53
  if __name__ == "__main__":
54
+ iface.launch()