Jeff28 commited on
Commit
822c8b6
Β·
verified Β·
1 Parent(s): 23b13a4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +127 -32
app.py CHANGED
@@ -1,63 +1,158 @@
1
  import gradio as gr
2
 
3
- # Dummy model functions for each version
4
- def model_v1(image):
5
- return "Model V1: Detected disease - [Example Disease]"
 
6
 
7
- def model_v2(image):
8
- return "Model V2: Detected disease after validation - [Example Disease]"
 
9
 
10
- def model_v3(image):
11
- return "Model V3: Detected disease with advanced analysis - [Example Disease]"
 
 
12
 
13
- # Main function to select the proper model based on the version choice
14
- def process_image(image, version):
 
 
 
 
 
 
 
15
  if image is None:
16
  return "No image provided."
17
- model_map = {
18
- "Version 1": model_v1,
19
- "Version 2": model_v2,
20
- "Version 3": model_v3
21
- }
22
- return model_map.get(version, lambda x: "Invalid version selected.")(image)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
- # Helper function to select between uploaded image and camera input
25
  def combine_images(uploaded, camera):
26
  return camera if camera is not None else uploaded
27
 
28
- # Define the Gradio Blocks interface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  with gr.Blocks() as demo:
 
 
 
30
  gr.Markdown("# 🌿 FarmVi8ion – AI-powered Crop Monitoring")
31
- gr.Markdown("### Detect tomato leaf diseases using AI-powered models.")
32
-
33
  with gr.Row():
 
34
  with gr.Column(scale=1):
35
- version = gr.Radio(
36
- ["Version 1", "Version 2", "Version 3"],
37
- label="Select Model Version",
38
- value="Version 1"
 
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 Result")
58
-
 
 
 
 
59
  submit.click(
60
- fn=lambda uploaded, camera, ver: process_image(combine_images(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
  )