Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,63 +1,158 @@
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
# Dummy
|
4 |
-
|
5 |
-
|
|
|
6 |
|
7 |
-
|
8 |
-
|
|
|
9 |
|
10 |
-
|
11 |
-
|
|
|
|
|
12 |
|
13 |
-
#
|
14 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
if image is None:
|
16 |
return "No image provided."
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
# Helper
|
25 |
def combine_images(uploaded, camera):
|
26 |
return camera if camera is not None else uploaded
|
27 |
|
28 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
with gr.Blocks() as demo:
|
|
|
|
|
|
|
30 |
gr.Markdown("# πΏ FarmVi8ion β AI-powered Crop Monitoring")
|
31 |
-
gr.Markdown("
|
32 |
-
|
33 |
with gr.Row():
|
|
|
34 |
with gr.Column(scale=1):
|
35 |
-
version = gr.
|
36 |
-
["
|
37 |
-
label="Select
|
38 |
-
value="
|
|
|
39 |
)
|
40 |
theme_choice = gr.Radio(
|
41 |
-
["Light", "Dark"],
|
42 |
label="Select Theme",
|
43 |
value="Light"
|
44 |
)
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
with gr.Column(scale=2):
|
47 |
image_input = gr.Image(
|
48 |
-
label="π Upload Tomato Leaf Image",
|
49 |
-
type="pil"
|
|
|
50 |
)
|
51 |
camera_input = gr.Image(
|
52 |
-
label="πΈ Use Camera",
|
53 |
type="pil"
|
54 |
)
|
55 |
submit = gr.Button("π Analyze")
|
56 |
-
|
57 |
-
output = gr.Textbox(label="π Diagnosis
|
58 |
-
|
|
|
|
|
|
|
|
|
59 |
submit.click(
|
60 |
-
fn=lambda uploaded, camera, ver:
|
61 |
inputs=[image_input, camera_input, version],
|
62 |
outputs=output
|
63 |
)
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
+
# ===== Dummy Model & Utility Functions =====
|
4 |
+
# Model A (for versions 1.x)
|
5 |
+
def model_A(image):
|
6 |
+
return "Model A predicts: [Example Disease A]"
|
7 |
|
8 |
+
# Model B (for versions 2.x)
|
9 |
+
def model_B(image):
|
10 |
+
return "Model B predicts: [Example Disease B]"
|
11 |
|
12 |
+
# Dummy classifier: Checks if image is a tomato leaf.
|
13 |
+
def classifier(image):
|
14 |
+
# Replace with actual classification logic.
|
15 |
+
return "Tomato Leaf"
|
16 |
|
17 |
+
# Dummy AI assistants for each model type.
|
18 |
+
def ai_assistant_v1(image, prediction):
|
19 |
+
return "Advice from AI Assistant v1: Ensure proper irrigation and pest monitoring."
|
20 |
+
|
21 |
+
def ai_assistant_v2(image, prediction):
|
22 |
+
return "Advice from AI Assistant v2: Consider organic pesticides and crop rotation."
|
23 |
+
|
24 |
+
# ===== Process Function for Versioned Logic =====
|
25 |
+
def process_version(image, version):
|
26 |
if image is None:
|
27 |
return "No image provided."
|
28 |
+
|
29 |
+
# --- Version 1.x (Model A) ---
|
30 |
+
if version == "1.1":
|
31 |
+
# Model A only + link to notebook
|
32 |
+
result = model_A(image)
|
33 |
+
return f"Model A Prediction: {result}\n\n[View Model A Training Notebook](https://huggingface.co/your-model-a-notebook)"
|
34 |
+
|
35 |
+
elif version == "1.2":
|
36 |
+
# Model A + AI Assistant
|
37 |
+
result = model_A(image)
|
38 |
+
advice = ai_assistant_v1(image, result)
|
39 |
+
return f"Model A Prediction: {result}\nAdvice: {advice}"
|
40 |
+
|
41 |
+
elif version == "1.3":
|
42 |
+
# Classifier + Model A + AI Assistant
|
43 |
+
cls_result = classifier(image)
|
44 |
+
if cls_result != "Tomato Leaf":
|
45 |
+
return "Classifier: The image is not a tomato leaf. Please try again."
|
46 |
+
result = model_A(image)
|
47 |
+
advice = ai_assistant_v1(image, result)
|
48 |
+
return (f"Classifier: {cls_result}\nModel A Prediction: {result}\nAdvice: {advice}\n\n"
|
49 |
+
f"[View Model A & Classifier Training Notebook](https://huggingface.co/your-model-a-classifier-notebook)")
|
50 |
+
|
51 |
+
# --- Version 2.x (Model B) ---
|
52 |
+
elif version == "2.1":
|
53 |
+
# Model B only + link to notebook
|
54 |
+
result = model_B(image)
|
55 |
+
return f"Model B Prediction: {result}\n\n[View Model B Training Notebook](https://huggingface.co/your-model-b-notebook)"
|
56 |
+
|
57 |
+
elif version == "2.2":
|
58 |
+
# Model B + AI Assistant
|
59 |
+
result = model_B(image)
|
60 |
+
advice = ai_assistant_v2(image, result)
|
61 |
+
return f"Model B Prediction: {result}\nAdvice: {advice}"
|
62 |
+
|
63 |
+
elif version == "2.3":
|
64 |
+
# Classifier + Model B + AI Assistant
|
65 |
+
cls_result = classifier(image)
|
66 |
+
if cls_result != "Tomato Leaf":
|
67 |
+
return "Classifier: The image is not a tomato leaf. Please try again."
|
68 |
+
result = model_B(image)
|
69 |
+
advice = ai_assistant_v2(image, result)
|
70 |
+
return (f"Classifier: {cls_result}\nModel B Prediction: {result}\nAdvice: {advice}\n\n"
|
71 |
+
f"[View Model B & Classifier Training Notebook](https://huggingface.co/your-model-b-classifier-notebook)")
|
72 |
+
else:
|
73 |
+
return "Invalid version selected."
|
74 |
|
75 |
+
# ===== Helper to Select Between Uploaded & Camera Image =====
|
76 |
def combine_images(uploaded, camera):
|
77 |
return camera if camera is not None else uploaded
|
78 |
|
79 |
+
# ===== CSS for Theme Switching =====
|
80 |
+
light_css = """
|
81 |
+
<style>
|
82 |
+
body { background-color: white; color: black; }
|
83 |
+
.gr-button { background-color: #4CAF50; color: white; }
|
84 |
+
</style>
|
85 |
+
"""
|
86 |
+
|
87 |
+
dark_css = """
|
88 |
+
<style>
|
89 |
+
body { background-color: #333; color: white; }
|
90 |
+
.gr-button { background-color: #555; color: white; }
|
91 |
+
</style>
|
92 |
+
"""
|
93 |
+
|
94 |
+
def update_css(theme):
|
95 |
+
if theme == "Dark":
|
96 |
+
return dark_css
|
97 |
+
else:
|
98 |
+
return light_css
|
99 |
+
|
100 |
+
# ===== Gradio Interface =====
|
101 |
with gr.Blocks() as demo:
|
102 |
+
# Hidden element for injecting CSS (initially Light theme)
|
103 |
+
css_injector = gr.HTML(update_css("Light"))
|
104 |
+
|
105 |
gr.Markdown("# πΏ FarmVi8ion β AI-powered Crop Monitoring")
|
106 |
+
gr.Markdown("Detect tomato leaf diseases and get AI-driven advice for your crops.")
|
107 |
+
|
108 |
with gr.Row():
|
109 |
+
# ----- Left Column (β30%) -----
|
110 |
with gr.Column(scale=1):
|
111 |
+
version = gr.Dropdown(
|
112 |
+
choices=["1.1", "1.2", "1.3", "2.1", "2.2", "2.3"],
|
113 |
+
label="Select Version",
|
114 |
+
value="1.1",
|
115 |
+
info="Versions 1.x use Model A; Versions 2.x use Model B."
|
116 |
)
|
117 |
theme_choice = gr.Radio(
|
118 |
+
choices=["Light", "Dark"],
|
119 |
label="Select Theme",
|
120 |
value="Light"
|
121 |
)
|
122 |
+
gr.Markdown("### Notebook Links")
|
123 |
+
gr.Markdown(
|
124 |
+
"""
|
125 |
+
**For Model A:**
|
126 |
+
- Model A Only: [Training Notebook](https://huggingface.co/your-model-a-notebook)
|
127 |
+
- Model A & Classifier: [Training Notebook](https://huggingface.co/your-model-a-classifier-notebook)
|
128 |
+
|
129 |
+
**For Model B:**
|
130 |
+
- Model B Only: [Training Notebook](https://huggingface.co/your-model-b-notebook)
|
131 |
+
- Model B & Classifier: [Training Notebook](https://huggingface.co/your-model-b-classifier-notebook)
|
132 |
+
"""
|
133 |
+
)
|
134 |
+
|
135 |
+
# ----- Right Column (β70%) -----
|
136 |
with gr.Column(scale=2):
|
137 |
image_input = gr.Image(
|
138 |
+
label="π Upload Tomato Leaf Image",
|
139 |
+
type="pil",
|
140 |
+
tool="editor" # enables basic image editing (crop, zoom, rotate)
|
141 |
)
|
142 |
camera_input = gr.Image(
|
143 |
+
label="πΈ Use Camera (Live Preview)",
|
144 |
type="pil"
|
145 |
)
|
146 |
submit = gr.Button("π Analyze")
|
147 |
+
|
148 |
+
output = gr.Textbox(label="π Diagnosis & Advice", lines=8)
|
149 |
+
|
150 |
+
# Update CSS dynamically based on theme choice
|
151 |
+
theme_choice.change(fn=update_css, inputs=theme_choice, outputs=css_injector)
|
152 |
+
|
153 |
+
# Process image on submit (combining uploaded & camera image)
|
154 |
submit.click(
|
155 |
+
fn=lambda uploaded, camera, ver: process_version(combine_images(uploaded, camera), ver),
|
156 |
inputs=[image_input, camera_input, version],
|
157 |
outputs=output
|
158 |
)
|