Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
|
|
|
| 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
|
| 28 |
|
| 29 |
try:
|
| 30 |
result = res.json()
|
| 31 |
except Exception:
|
| 32 |
-
return f"β Invalid JSON
|
| 33 |
|
| 34 |
# Extract image if present
|
| 35 |
-
if "
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
iface = gr.Interface(
|
| 45 |
fn=image_generation,
|
| 46 |
-
inputs=gr.Textbox(label="Enter your prompt", placeholder="e.g. A
|
| 47 |
outputs=gr.Image(label="Generated Image"),
|
| 48 |
-
title="Google
|
| 49 |
-
description="Uses
|
| 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__":
|