Update app.py
Browse files
app.py
CHANGED
|
@@ -10,67 +10,65 @@ from google.genai import types
|
|
| 10 |
# 1. Gemini Client
|
| 11 |
# ==============================
|
| 12 |
client = genai.Client(
|
| 13 |
-
api_key=os.environ
|
| 14 |
)
|
| 15 |
|
| 16 |
|
| 17 |
# ==============================
|
| 18 |
-
# 2.
|
| 19 |
# ==============================
|
| 20 |
def analyze_image(image, prompt):
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
|
| 41 |
-
response = client.models.generate_content(
|
| 42 |
-
model="gemini-1.5-pro",
|
| 43 |
-
contents=[
|
| 44 |
-
prompt,
|
| 45 |
-
image_part
|
| 46 |
-
]
|
| 47 |
-
)
|
| 48 |
|
| 49 |
-
|
|
|
|
|
|
|
| 50 |
|
| 51 |
|
| 52 |
# ==============================
|
| 53 |
-
# 3. Gradio
|
| 54 |
# ==============================
|
| 55 |
interface = gr.Interface(
|
| 56 |
fn=analyze_image,
|
| 57 |
inputs=[
|
| 58 |
gr.Image(type="pil", label="Upload Image"),
|
| 59 |
-
gr.Textbox(
|
| 60 |
-
label="Prompt",
|
| 61 |
-
placeholder="Describe the image"
|
| 62 |
-
)
|
| 63 |
],
|
| 64 |
-
outputs=gr.Textbox(label="
|
| 65 |
-
title="Multimodal
|
| 66 |
-
description="Upload an image and ask a simple question."
|
| 67 |
)
|
| 68 |
|
| 69 |
|
| 70 |
# ==============================
|
| 71 |
# 4. Launch
|
| 72 |
# ==============================
|
| 73 |
-
interface.launch()
|
| 74 |
-
|
| 75 |
-
|
| 76 |
|
|
|
|
| 10 |
# 1. Gemini Client
|
| 11 |
# ==============================
|
| 12 |
client = genai.Client(
|
| 13 |
+
api_key=os.environ.get("GEMINI_API_KEY")
|
| 14 |
)
|
| 15 |
|
| 16 |
|
| 17 |
# ==============================
|
| 18 |
+
# 2. Multimodal Function (SAFE)
|
| 19 |
# ==============================
|
| 20 |
def analyze_image(image, prompt):
|
| 21 |
|
| 22 |
+
try:
|
| 23 |
+
# ---- Checks ----
|
| 24 |
+
if image is None:
|
| 25 |
+
return "❌ No image uploaded"
|
| 26 |
|
| 27 |
+
if not prompt or prompt.strip() == "":
|
| 28 |
+
return "❌ Prompt is empty"
|
| 29 |
|
| 30 |
+
# Convert image to bytes
|
| 31 |
+
buffer = io.BytesIO()
|
| 32 |
+
image.save(buffer, format="PNG")
|
| 33 |
+
image_bytes = buffer.getvalue()
|
| 34 |
|
| 35 |
+
image_part = types.Part.from_bytes(
|
| 36 |
+
data=image_bytes,
|
| 37 |
+
mime_type="image/png"
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
# ✅ USE FLASH MODEL (IMPORTANT)
|
| 41 |
+
response = client.models.generate_content(
|
| 42 |
+
model="gemini-1.5-flash",
|
| 43 |
+
contents=[
|
| 44 |
+
prompt,
|
| 45 |
+
image_part
|
| 46 |
+
]
|
| 47 |
+
)
|
| 48 |
|
| 49 |
+
return response.text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
except Exception as e:
|
| 52 |
+
# 🔍 Show real Gemini error
|
| 53 |
+
return f"❌ Gemini Error:\n{str(e)}"
|
| 54 |
|
| 55 |
|
| 56 |
# ==============================
|
| 57 |
+
# 3. Gradio UI
|
| 58 |
# ==============================
|
| 59 |
interface = gr.Interface(
|
| 60 |
fn=analyze_image,
|
| 61 |
inputs=[
|
| 62 |
gr.Image(type="pil", label="Upload Image"),
|
| 63 |
+
gr.Textbox(label="Prompt", value="Describe the image")
|
|
|
|
|
|
|
|
|
|
| 64 |
],
|
| 65 |
+
outputs=gr.Textbox(label="Response"),
|
| 66 |
+
title="Gemini Multimodal Test App",
|
|
|
|
| 67 |
)
|
| 68 |
|
| 69 |
|
| 70 |
# ==============================
|
| 71 |
# 4. Launch
|
| 72 |
# ==============================
|
| 73 |
+
interface.launch(ssr_mode=False)
|
|
|
|
|
|
|
| 74 |
|