Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,54 @@
|
|
| 1 |
import os
|
| 2 |
-
import gradio as gr
|
| 3 |
import requests
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 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 |
-
|
|
|
|
| 13 |
|
| 14 |
headers = {"Content-Type": "application/json"}
|
| 15 |
data = {"prompt": {"text": prompt}}
|
| 16 |
|
|
|
|
| 17 |
res = requests.post(url, headers=headers, json=data)
|
| 18 |
|
| 19 |
-
#
|
| 20 |
if res.status_code != 200:
|
| 21 |
-
return f"❌ Error {res.status_code}
|
| 22 |
|
| 23 |
try:
|
| 24 |
result = res.json()
|
| 25 |
-
except Exception
|
| 26 |
-
return f"❌
|
| 27 |
-
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
-
image_base64 = result["
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
fn=image_generation,
|
| 38 |
-
inputs=gr.Textbox(label="Enter your prompt"),
|
| 39 |
-
outputs="
|
| 40 |
title="Google AI Studio Image Generator",
|
| 41 |
-
description="Generate images using Google AI Studio API."
|
| 42 |
)
|
| 43 |
|
| 44 |
if __name__ == "__main__":
|
| 45 |
-
|
|
|
|
| 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()
|