ha7naa commited on
Commit
8fb5e72
·
verified ·
1 Parent(s): edd79f1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -36
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["GEMINI_API_KEY"]
14
  )
15
 
16
 
17
  # ==============================
18
- # 2. Safe Multimodal Function
19
  # ==============================
20
  def analyze_image(image, prompt):
21
 
22
- # ---- SAFETY CHECKS ----
23
- if image is None:
24
- return "❌ Please upload an image."
 
25
 
26
- if prompt is None or prompt.strip() == "":
27
- return "❌ Please enter a text prompt."
28
 
29
- # Convert image to bytes
30
- buffer = io.BytesIO()
31
- image.save(buffer, format="PNG")
32
- image_bytes = buffer.getvalue()
33
 
34
- # Create Gemini image part
35
- image_part = types.Part.from_bytes(
36
- data=image_bytes,
37
- mime_type="image/png"
38
- )
 
 
 
 
 
 
 
 
39
 
40
- # Generate response
41
- response = client.models.generate_content(
42
- model="gemini-1.5-pro",
43
- contents=[
44
- prompt,
45
- image_part
46
- ]
47
- )
48
 
49
- return response.text
 
 
50
 
51
 
52
  # ==============================
53
- # 3. Gradio Interface
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="Gemini Response"),
65
- title="Multimodal AI App (Gemini)",
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