| | import gradio as gr |
| | import os |
| | import io |
| | from PIL import Image |
| | from google import genai |
| | from google.genai import types |
| |
|
| |
|
| | |
| | |
| | |
| | client = genai.Client( |
| | api_key=os.environ.get("GEMINI_API_KEY") |
| | ) |
| |
|
| |
|
| | |
| | |
| | |
| | def analyze_image(image, prompt): |
| |
|
| | try: |
| | |
| | if image is None: |
| | return "β No image uploaded" |
| |
|
| | if not prompt or prompt.strip() == "": |
| | return "β Prompt is empty" |
| |
|
| | |
| | buffer = io.BytesIO() |
| | image.save(buffer, format="PNG") |
| | image_bytes = buffer.getvalue() |
| |
|
| | image_part = types.Part.from_bytes( |
| | data=image_bytes, |
| | mime_type="image/png" |
| | ) |
| |
|
| | |
| | response = client.models.generate_content( |
| | model="gemini-1.5-flash", |
| | contents=[ |
| | prompt, |
| | image_part |
| | ] |
| | ) |
| |
|
| | return response.text |
| |
|
| | except Exception as e: |
| | |
| | return f"β Gemini Error:\n{str(e)}" |
| |
|
| |
|
| | |
| | |
| | |
| | interface = gr.Interface( |
| | fn=analyze_image, |
| | inputs=[ |
| | gr.Image(type="pil", label="Upload Image"), |
| | gr.Textbox(label="Prompt", value="Describe the image") |
| | ], |
| | outputs=gr.Textbox(label="Response"), |
| | title="Gemini Multimodal Test App", |
| | ) |
| |
|
| |
|
| | |
| | |
| | |
| | interface.launch(ssr_mode=False) |
| |
|
| |
|