Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import google.generativeai as genai
|
2 |
+
import gradio as gr
|
3 |
+
from PIL import Image
|
4 |
+
|
5 |
+
# Set up Gemini API key
|
6 |
+
genai.configure(api_key="AIzaSyDnx_qUjGTFG1pv1otPUhNt_bGGv14aMDI") # Replace with your API key
|
7 |
+
|
8 |
+
def predict_rat(image):
|
9 |
+
"""Predict if the uploaded image contains a rat using Google Gemini Pro Vision API."""
|
10 |
+
|
11 |
+
model = genai.GenerativeModel("gemini-1.5-flash")
|
12 |
+
|
13 |
+
# Open image using PIL
|
14 |
+
img = Image.open(image)
|
15 |
+
|
16 |
+
# Generate prediction
|
17 |
+
response = model.generate_content(
|
18 |
+
[img, "Is this an image of a rat? Answer with 'Yes' or 'No' and provide a brief explanation."]
|
19 |
+
)
|
20 |
+
|
21 |
+
# Extract prediction and explanation
|
22 |
+
result = response.text
|
23 |
+
return result
|
24 |
+
|
25 |
+
# Gradio UI
|
26 |
+
iface = gr.Interface(
|
27 |
+
fn=predict_rat,
|
28 |
+
inputs=gr.Image(type="filepath"),
|
29 |
+
outputs="text",
|
30 |
+
title="Rat Detection App",
|
31 |
+
description="Upload an image to check if it contains a rat."
|
32 |
+
)
|
33 |
+
|
34 |
+
# Run the app
|
35 |
+
if __name__ == "__main__":
|
36 |
+
iface.launch()
|