Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ 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:
|
@@ -10,13 +11,20 @@ def analyze_plant_image(image, api_key):
|
|
10 |
api_key=api_key,
|
11 |
)
|
12 |
|
13 |
-
# Convert PIL Image
|
|
|
|
|
|
|
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=[
|
@@ -38,19 +46,21 @@ def analyze_plant_image(image, api_key):
|
|
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.
|
|
|
2 |
from openai import OpenAI
|
3 |
import base64
|
4 |
from io import BytesIO
|
5 |
+
from PIL import Image # Import PIL for image conversion
|
6 |
|
7 |
def analyze_plant_image(image, api_key):
|
8 |
try:
|
|
|
11 |
api_key=api_key,
|
12 |
)
|
13 |
|
14 |
+
# Convert NumPy array to PIL Image and ensure RGB mode for JPEG compatibility
|
15 |
+
image = Image.fromarray(image).convert("RGB")
|
16 |
+
|
17 |
+
# Save the image to a BytesIO buffer as JPEG
|
18 |
buffered = BytesIO()
|
19 |
image.save(buffered, format="JPEG")
|
20 |
+
|
21 |
+
# Encode the image to base64
|
22 |
encoded_image = base64.b64encode(buffered.getvalue()).decode('utf-8')
|
23 |
|
24 |
+
# Create the data URL for the image
|
25 |
image_url = f"data:image/jpeg;base64,{encoded_image}"
|
26 |
|
27 |
+
# Call the InternVL3 14B model with the image and prompt
|
28 |
completion = client.chat.completions.create(
|
29 |
model="opengvlab/internvl3-14b:free",
|
30 |
messages=[
|
|
|
46 |
]
|
47 |
)
|
48 |
|
49 |
+
# Return the model's response
|
50 |
return completion.choices[0].message.content
|
51 |
|
52 |
except Exception as e:
|
53 |
+
# Return error message if something goes wrong
|
54 |
return f"An error occurred: {str(e)}"
|
55 |
|
56 |
# Create the Gradio interface
|
57 |
interface = gr.Interface(
|
58 |
fn=analyze_plant_image,
|
59 |
inputs=[
|
60 |
+
gr.Image(label="Upload Plant Image"), # Expects a NumPy array
|
61 |
+
gr.Textbox(type="password", label="OpenRouter API Key") # Secure input for API key
|
62 |
],
|
63 |
+
outputs=gr.Textbox(label="Analysis Result"), # Displays the result or error
|
64 |
title="PlantPal: Your AI-Powered Plant Care Assistant",
|
65 |
description="""
|
66 |
Upload an image of your plant and enter your OpenRouter API key to get started.
|