yasserrmd commited on
Commit
9574999
·
verified ·
1 Parent(s): 67998e0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -0
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from huggingface_hub import InferenceClient
3
+ import os
4
+ import tempfile
5
+
6
+ # Initialize the Hugging Face Inference Client
7
+ client = InferenceClient()
8
+
9
+ # Function to analyze plant images
10
+ def analyze_plant_image(image):
11
+ # Save image to a temporary location
12
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_image:
13
+ image.save(temp_image.name)
14
+ image_url = f"file://{temp_image.name}"
15
+
16
+ # Create the message structure
17
+ messages = [
18
+ {
19
+ "role": "user",
20
+ "content": [
21
+ {
22
+ "type": "text",
23
+ "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."
24
+ },
25
+ {
26
+ "type": "image_url",
27
+ "image_url": {
28
+ "url": image_url
29
+ }
30
+ }
31
+ ]
32
+ }
33
+ ]
34
+
35
+ # Create a stream to receive generated content
36
+ stream = client.chat.completions.create(
37
+ model="Qwen/Qwen2-VL-7B-Instruct",
38
+ messages=messages,
39
+ max_tokens=500,
40
+ stream=True
41
+ )
42
+
43
+ # Stream content as it is generated
44
+ description = ""
45
+ for chunk in stream:
46
+ description += chunk.choices[0].delta.content
47
+ yield description # Yield incremental content to display immediately
48
+
49
+ # Create Gradio interface
50
+ with gr.Blocks() as app:
51
+ gr.Markdown("## Automated Botanical Analyzer")
52
+ gr.Markdown("Upload an image of a plant to identify its species, detect any diseases, and monitor growth stages.")
53
+
54
+ with gr.Row():
55
+ # First column for input components
56
+ with gr.Column():
57
+ image_input = gr.Image(type="pil", label="Upload Plant Image")
58
+ analyze_button = gr.Button("Analyze Plant Image")
59
+
60
+ # Second column for output
61
+ with gr.Column():
62
+ gr.Markdown("### Plant Analysis Result") # This acts as the label for the output
63
+ output_markdown = gr.Markdown()
64
+
65
+ # Link button to function with inputs and outputs
66
+ analyze_button.click(fn=analyze_plant_image, inputs=image_input, outputs=output_markdown)
67
+
68
+ # Run the Gradio app
69
+ app.launch()