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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -22
app.py CHANGED
@@ -1,53 +1,52 @@
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__":
 
1
  import os
2
  import requests
3
  import gradio as gr
4
+ import base64
5
+ from io import BytesIO
6
+ from PIL import Image
7
 
 
8
  API_KEY = os.getenv("GOOGLE_API_KEY")
9
 
10
  def image_generation(prompt):
11
  if not API_KEY:
12
+ return "❌ API key not found. Please add GOOGLE_API_KEY in Hugging Face secrets."
13
 
 
14
  url = f"https://generativelanguage.googleapis.com/v1beta/models/imagen-3.0-generate:generate?key={API_KEY}"
 
15
  headers = {"Content-Type": "application/json"}
16
  data = {"prompt": {"text": prompt}}
17
 
18
+ try:
19
+ res = requests.post(url, headers=headers, json=data)
20
+ except Exception as e:
21
+ return f"❌ Request failed: {str(e)}"
22
+
23
+ # Always show HTTP status and text
24
+ debug_info = f"HTTP {res.status_code}\n\nResponse:\n{res.text}\n\n"
25
 
 
26
  if res.status_code != 200:
27
+ return debug_info
28
 
29
  try:
30
  result = res.json()
31
  except Exception:
32
+ return f"❌ Invalid JSON\n\n{debug_info}"
33
 
34
+ # Extract image if present
35
  if "images" in result and len(result["images"]) > 0:
36
+ image_base64 = result["images"][0].get("image_base64")
37
  if image_base64:
38
+ image = Image.open(BytesIO(base64.b64decode(image_base64)))
 
 
 
 
39
  return image
40
 
41
+ # No image — show what was actually returned
42
+ return f"⚠️ No image found in response.\n\nFull response:\n{result}"
43
 
 
44
  iface = gr.Interface(
45
  fn=image_generation,
46
+ inputs=gr.Textbox(label="Enter your prompt", placeholder="e.g. A cat astronaut floating in space"),
47
  outputs=gr.Image(label="Generated Image"),
48
+ title="Google AI Studio Image Generator (Debug Mode)",
49
+ description="Uses Imagen 3.0 API shows debug info if response is empty."
50
  )
51
 
52
  if __name__ == "__main__":