Spaces:
Sleeping
Sleeping
File size: 1,972 Bytes
eb8980a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
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() |