Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from openai import OpenAI
|
3 |
+
import base64
|
4 |
+
from io import BytesIO
|
5 |
+
|
6 |
+
def analyze_plant_image(image, api_key):
|
7 |
+
try:
|
8 |
+
client = OpenAI(
|
9 |
+
base_url="https://openrouter.ai/api/v1",
|
10 |
+
api_key=api_key,
|
11 |
+
)
|
12 |
+
|
13 |
+
# Convert PIL Image to base64
|
14 |
+
buffered = BytesIO()
|
15 |
+
image.save(buffered, format="JPEG")
|
16 |
+
encoded_image = base64.b64encode(buffered.getvalue()).decode('utf-8')
|
17 |
+
|
18 |
+
image_url = f"data:image/jpeg;base64,{encoded_image}"
|
19 |
+
|
20 |
+
completion = client.chat.completions.create(
|
21 |
+
model="opengvlab/internvl3-14b:free",
|
22 |
+
messages=[
|
23 |
+
{
|
24 |
+
"role": "user",
|
25 |
+
"content": [
|
26 |
+
{
|
27 |
+
"type": "text",
|
28 |
+
"text": "Identify this plant, provide care instructions, and diagnose any health issues visible in the image."
|
29 |
+
},
|
30 |
+
{
|
31 |
+
"type": "image_url",
|
32 |
+
"image_url": {
|
33 |
+
"url": image_url
|
34 |
+
}
|
35 |
+
}
|
36 |
+
]
|
37 |
+
}
|
38 |
+
]
|
39 |
+
)
|
40 |
+
|
41 |
+
return completion.choices[0].message.content
|
42 |
+
|
43 |
+
except Exception as e:
|
44 |
+
return f"An error occurred: {str(e)}"
|
45 |
+
|
46 |
+
# Create the Gradio interface
|
47 |
+
interface = gr.Interface(
|
48 |
+
fn=analyze_plant_image,
|
49 |
+
inputs=[
|
50 |
+
gr.Image(label="Upload Plant Image"),
|
51 |
+
gr.Textbox(type="password", label="OpenRouter API Key")
|
52 |
+
],
|
53 |
+
outputs=gr.Textbox(label="Analysis Result"),
|
54 |
+
title="PlantPal: Your AI-Powered Plant Care Assistant",
|
55 |
+
description="""
|
56 |
+
Upload an image of your plant and enter your OpenRouter API key to get started.
|
57 |
+
If you don't have an API key, you can get one from [OpenRouter](https://openrouter.ai/).
|
58 |
+
"""
|
59 |
+
)
|
60 |
+
|
61 |
+
# Launch the app
|
62 |
+
interface.launch()
|