PlantPal / app.py
shukdevdatta123's picture
Create app.py
eb8980a verified
raw
history blame
1.97 kB
import gradio as gr
from openai import OpenAI
import base64
from io import BytesIO
def analyze_plant_image(image, api_key):
try:
client = OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key=api_key,
)
# Convert PIL Image to base64
buffered = BytesIO()
image.save(buffered, format="JPEG")
encoded_image = base64.b64encode(buffered.getvalue()).decode('utf-8')
image_url = f"data:image/jpeg;base64,{encoded_image}"
completion = client.chat.completions.create(
model="opengvlab/internvl3-14b:free",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": "Identify this plant, provide care instructions, and diagnose any health issues visible in the image."
},
{
"type": "image_url",
"image_url": {
"url": image_url
}
}
]
}
]
)
return completion.choices[0].message.content
except Exception as e:
return f"An error occurred: {str(e)}"
# Create the Gradio interface
interface = gr.Interface(
fn=analyze_plant_image,
inputs=[
gr.Image(label="Upload Plant Image"),
gr.Textbox(type="password", label="OpenRouter API Key")
],
outputs=gr.Textbox(label="Analysis Result"),
title="PlantPal: Your AI-Powered Plant Care Assistant",
description="""
Upload an image of your plant and enter your OpenRouter API key to get started.
If you don't have an API key, you can get one from [OpenRouter](https://openrouter.ai/).
"""
)
# Launch the app
interface.launch()