omaralaa2004 commited on
Commit
fe614be
Β·
verified Β·
1 Parent(s): 6c05b54

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -18
app.py CHANGED
@@ -9,9 +9,10 @@ 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
 
@@ -20,33 +21,33 @@ def image_generation(prompt):
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__":
 
9
 
10
  def image_generation(prompt):
11
  if not API_KEY:
12
+ return "❌ API key not found. Please add GOOGLE_API_KEY in your Hugging Face secrets."
13
 
14
+ # Use Gemini's free image generation model
15
+ url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-preview-image-generation:generate?key={API_KEY}"
16
  headers = {"Content-Type": "application/json"}
17
  data = {"prompt": {"text": prompt}}
18
 
 
21
  except Exception as e:
22
  return f"❌ Request failed: {str(e)}"
23
 
 
 
 
24
  if res.status_code != 200:
25
+ return f"❌ Error {res.status_code}:\n{res.text}"
26
 
27
  try:
28
  result = res.json()
29
  except Exception:
30
+ return f"❌ Invalid JSON response:\n{res.text}"
31
 
32
  # Extract image if present
33
+ if "candidates" in result:
34
+ for candidate in result["candidates"]:
35
+ if "content" in candidate:
36
+ for part in candidate["content"].get("parts", []):
37
+ if "inline_data" in part:
38
+ image_base64 = part["inline_data"].get("data")
39
+ if image_base64:
40
+ image = Image.open(BytesIO(base64.b64decode(image_base64)))
41
+ return image
42
+
43
+ return f"⚠️ No image data found.\n\nFull response:\n{result}"
44
 
45
  iface = gr.Interface(
46
  fn=image_generation,
47
+ inputs=gr.Textbox(label="Enter your prompt", placeholder="e.g. A futuristic city under sunset"),
48
  outputs=gr.Image(label="Generated Image"),
49
+ title="Google Gemini Image Generator",
50
+ description="Uses gemini-2.0-flash-preview-image-generation (free-tier). Educational purpose only."
51
  )
52
 
53
  if __name__ == "__main__":