Spaces:
Build error
Build error
| import gradio as gr | |
| from huggingface_hub import InferenceClient | |
| import base64 | |
| import io | |
| # Initialize the Hugging Face Inference Client | |
| client = InferenceClient() | |
| # Function to analyze plant images | |
| def analyze_plant_image(image): | |
| buffered = io.BytesIO() | |
| image.save(buffered, format="JPEG") | |
| image_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8") | |
| image_url = f"data:image/jpeg;base64,{image_base64}" | |
| # Create the message structure | |
| messages = [ | |
| { | |
| "role": "user", | |
| "content": [ | |
| { | |
| "type": "text", | |
| "text": "Analyze the plant in this image in detail, including the following aspects: 1) Identify the plant species with a scientific name if possible. 2) Assess the health status of the plant, indicating any visible signs of disease, nutrient deficiency, or pests. 3) Determine the growth stage of the plant (e.g., seedling, vegetative, flowering, or fruiting stage). 4) Provide any additional information that could help in understanding the overall condition of the plant." | |
| }, | |
| { | |
| "type": "image_url", | |
| "image_url": { | |
| "url": image_url | |
| } | |
| } | |
| ] | |
| } | |
| ] | |
| # Create the completion request | |
| stream = client.chat.completions.create( | |
| model="Qwen/Qwen2-VL-7B-Instruct", | |
| messages=messages, | |
| max_tokens=1024, | |
| stream=True | |
| ) | |
| # Stream content as it is generated | |
| output_text = "" | |
| for chunk in stream: | |
| output_text += chunk.choices[0].delta.content | |
| yield output_text | |
| # Create Gradio interface | |
| with gr.Blocks() as app: | |
| gr.Markdown("## Automated Botanical Analyzer") | |
| gr.Markdown("Upload an image of a plant to identify its species, detect any diseases, and monitor growth stages.") | |
| with gr.Row(): | |
| # First column for input components | |
| with gr.Column(): | |
| image_input = gr.Image(type="pil", label="Upload Plant Image", image_mode="RGB") | |
| analyze_button = gr.Button("Analyze Plant Image") | |
| # Second column for output | |
| with gr.Column(): | |
| output_markdown = gr.Markdown() # This acts as the label for the output | |
| # Link button to function with inputs and outputs | |
| analyze_button.click(fn=analyze_plant_image, inputs=image_input, outputs=output_markdown) | |
| # Run the Gradio app | |
| app.launch() | |