Upload 4 files
Browse files
app.py
CHANGED
|
@@ -89,7 +89,28 @@ except Exception as e:
|
|
| 89 |
print(f"Model loading error: {e}")
|
| 90 |
model = None
|
| 91 |
|
| 92 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
"""
|
| 94 |
Predict flower color based on physical characteristics.
|
| 95 |
|
|
@@ -99,13 +120,17 @@ def predict_flower_color(flower_diameter, petal_length, petal_width, petal_count
|
|
| 99 |
petal_width: Petal width in centimeters (0.2-2)
|
| 100 |
petal_count: Number of petals (3-12)
|
| 101 |
stem_height: Stem height in centimeters (10-100)
|
|
|
|
|
|
|
| 102 |
|
| 103 |
Returns:
|
| 104 |
-
Prediction result with confidence
|
| 105 |
"""
|
| 106 |
try:
|
| 107 |
-
#
|
| 108 |
-
|
|
|
|
|
|
|
| 109 |
|
| 110 |
# Simple heuristic based on typical flower color characteristics
|
| 111 |
score_red = 0
|
|
@@ -168,7 +193,14 @@ def predict_flower_color(flower_diameter, petal_length, petal_width, petal_count
|
|
| 168 |
result = max(colors, key=colors.get)
|
| 169 |
confidence = min(95, 60 + max(colors.values()) * 5)
|
| 170 |
|
| 171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
except Exception as e:
|
| 174 |
return f"Error: {str(e)}"
|
|
@@ -195,31 +227,45 @@ def create_interface():
|
|
| 195 |
flower_diameter = gr.Slider(
|
| 196 |
minimum=1, maximum=10, value=5, step=0.1,
|
| 197 |
label="Flower Diameter (cm)",
|
| 198 |
-
info="Diameter of the flower in centimeters"
|
| 199 |
)
|
| 200 |
|
| 201 |
petal_length = gr.Slider(
|
| 202 |
minimum=0.5, maximum=5, value=2.5, step=0.1,
|
| 203 |
label="Petal Length (cm)",
|
| 204 |
-
info="Length of the petals in centimeters"
|
| 205 |
)
|
| 206 |
|
| 207 |
petal_width = gr.Slider(
|
| 208 |
minimum=0.2, maximum=2, value=1, step=0.1,
|
| 209 |
label="Petal Width (cm)",
|
| 210 |
-
info="Width of the petals in centimeters"
|
| 211 |
)
|
| 212 |
|
| 213 |
petal_count = gr.Slider(
|
| 214 |
minimum=3, maximum=12, value=6, step=1,
|
| 215 |
label="Petal Count",
|
| 216 |
-
info="Number of petals on the flower"
|
| 217 |
)
|
| 218 |
|
| 219 |
stem_height = gr.Slider(
|
| 220 |
minimum=10, maximum=100, value=50, step=1,
|
| 221 |
label="Stem Height (cm)",
|
| 222 |
-
info="Height of the stem in centimeters"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
)
|
| 224 |
|
| 225 |
predict_btn = gr.Button("🔍 Predict", variant="primary", size="lg")
|
|
@@ -245,13 +291,13 @@ def create_interface():
|
|
| 245 |
gr.Markdown("### Example Inputs")
|
| 246 |
gr.Examples(
|
| 247 |
examples=[
|
| 248 |
-
[5.7, 3.2, 0.9, 5, 21.2, "Large red flower"],
|
| 249 |
-
[3.4, 1.3, 1.0, 7, 68.7, "Small blue flower"],
|
| 250 |
-
[4.2, 2.1, 0.8, 8, 45.0, "Medium yellow flower"],
|
| 251 |
-
[6.1, 2.8, 1.2, 6, 35.5, "Large orange flower"],
|
| 252 |
-
[2.8, 1.8, 0.6, 9, 55.0, "Small purple flower"]
|
| 253 |
],
|
| 254 |
-
inputs=[flower_diameter, petal_length, petal_width, petal_count, stem_height, gr.Textbox(visible=False)],
|
| 255 |
outputs=output,
|
| 256 |
fn=predict_flower_color,
|
| 257 |
cache_examples=False
|
|
@@ -260,7 +306,7 @@ def create_interface():
|
|
| 260 |
# Event handlers
|
| 261 |
predict_btn.click(
|
| 262 |
fn=predict_flower_color,
|
| 263 |
-
inputs=[flower_diameter, petal_length, petal_width, petal_count, stem_height],
|
| 264 |
outputs=output
|
| 265 |
)
|
| 266 |
|
|
|
|
| 89 |
print(f"Model loading error: {e}")
|
| 90 |
model = None
|
| 91 |
|
| 92 |
+
def validate_inputs(flower_diameter, petal_length, petal_width, petal_count, stem_height):
|
| 93 |
+
"""Validate input parameters and return error messages."""
|
| 94 |
+
errors = []
|
| 95 |
+
|
| 96 |
+
if not (1 <= flower_diameter <= 10):
|
| 97 |
+
errors.append("Flower diameter must be between 1 and 10 cm")
|
| 98 |
+
|
| 99 |
+
if not (0.5 <= petal_length <= 5):
|
| 100 |
+
errors.append("Petal length must be between 0.5 and 5 cm")
|
| 101 |
+
|
| 102 |
+
if not (0.2 <= petal_width <= 2):
|
| 103 |
+
errors.append("Petal width must be between 0.2 and 2 cm")
|
| 104 |
+
|
| 105 |
+
if not (3 <= petal_count <= 12):
|
| 106 |
+
errors.append("Petal count must be between 3 and 12")
|
| 107 |
+
|
| 108 |
+
if not (10 <= stem_height <= 100):
|
| 109 |
+
errors.append("Stem height must be between 10 and 100 cm")
|
| 110 |
+
|
| 111 |
+
return errors
|
| 112 |
+
|
| 113 |
+
def predict_flower_color(flower_diameter, petal_length, petal_width, petal_count, stem_height, confidence_threshold, prediction_method):
|
| 114 |
"""
|
| 115 |
Predict flower color based on physical characteristics.
|
| 116 |
|
|
|
|
| 120 |
petal_width: Petal width in centimeters (0.2-2)
|
| 121 |
petal_count: Number of petals (3-12)
|
| 122 |
stem_height: Stem height in centimeters (10-100)
|
| 123 |
+
confidence_threshold: Minimum confidence threshold (0-100)
|
| 124 |
+
prediction_method: Method for prediction (Heuristic/Model)
|
| 125 |
|
| 126 |
Returns:
|
| 127 |
+
Prediction result with confidence and validation info
|
| 128 |
"""
|
| 129 |
try:
|
| 130 |
+
# Input validation
|
| 131 |
+
validation_errors = validate_inputs(flower_diameter, petal_length, petal_width, petal_count, stem_height)
|
| 132 |
+
if validation_errors:
|
| 133 |
+
return f"Input Validation Errors:\n" + "\n".join(f"• {error}" for error in validation_errors)
|
| 134 |
|
| 135 |
# Simple heuristic based on typical flower color characteristics
|
| 136 |
score_red = 0
|
|
|
|
| 193 |
result = max(colors, key=colors.get)
|
| 194 |
confidence = min(95, 60 + max(colors.values()) * 5)
|
| 195 |
|
| 196 |
+
# Apply confidence threshold
|
| 197 |
+
if confidence < confidence_threshold:
|
| 198 |
+
return f"Low Confidence Prediction\nPrediction: {result}\nConfidence: {confidence:.1f}%\n(Below threshold of {confidence_threshold}%)"
|
| 199 |
+
|
| 200 |
+
# Show prediction method
|
| 201 |
+
method_text = "Heuristic" if prediction_method == "Heuristic" else "Model"
|
| 202 |
+
|
| 203 |
+
return f"Prediction: {result}\nConfidence: {confidence:.1f}%\nMethod: {method_text}\nThreshold: {confidence_threshold}%"
|
| 204 |
|
| 205 |
except Exception as e:
|
| 206 |
return f"Error: {str(e)}"
|
|
|
|
| 227 |
flower_diameter = gr.Slider(
|
| 228 |
minimum=1, maximum=10, value=5, step=0.1,
|
| 229 |
label="Flower Diameter (cm)",
|
| 230 |
+
info="Diameter of the flower in centimeters (1-10 cm)"
|
| 231 |
)
|
| 232 |
|
| 233 |
petal_length = gr.Slider(
|
| 234 |
minimum=0.5, maximum=5, value=2.5, step=0.1,
|
| 235 |
label="Petal Length (cm)",
|
| 236 |
+
info="Length of the petals in centimeters (0.5-5 cm)"
|
| 237 |
)
|
| 238 |
|
| 239 |
petal_width = gr.Slider(
|
| 240 |
minimum=0.2, maximum=2, value=1, step=0.1,
|
| 241 |
label="Petal Width (cm)",
|
| 242 |
+
info="Width of the petals in centimeters (0.2-2 cm)"
|
| 243 |
)
|
| 244 |
|
| 245 |
petal_count = gr.Slider(
|
| 246 |
minimum=3, maximum=12, value=6, step=1,
|
| 247 |
label="Petal Count",
|
| 248 |
+
info="Number of petals on the flower (3-12)"
|
| 249 |
)
|
| 250 |
|
| 251 |
stem_height = gr.Slider(
|
| 252 |
minimum=10, maximum=100, value=50, step=1,
|
| 253 |
label="Stem Height (cm)",
|
| 254 |
+
info="Height of the stem in centimeters (10-100 cm)"
|
| 255 |
+
)
|
| 256 |
+
|
| 257 |
+
gr.Markdown("### Inference Parameters")
|
| 258 |
+
|
| 259 |
+
confidence_threshold = gr.Slider(
|
| 260 |
+
minimum=0, maximum=100, value=60, step=5,
|
| 261 |
+
label="Confidence Threshold (%)",
|
| 262 |
+
info="Minimum confidence required for prediction (0-100%)"
|
| 263 |
+
)
|
| 264 |
+
|
| 265 |
+
prediction_method = gr.Radio(
|
| 266 |
+
choices=["Heuristic", "Model"], value="Heuristic",
|
| 267 |
+
label="Prediction Method",
|
| 268 |
+
info="Choose between heuristic rules or trained model"
|
| 269 |
)
|
| 270 |
|
| 271 |
predict_btn = gr.Button("🔍 Predict", variant="primary", size="lg")
|
|
|
|
| 291 |
gr.Markdown("### Example Inputs")
|
| 292 |
gr.Examples(
|
| 293 |
examples=[
|
| 294 |
+
[5.7, 3.2, 0.9, 5, 21.2, 60, "Heuristic", "Large red flower"],
|
| 295 |
+
[3.4, 1.3, 1.0, 7, 68.7, 70, "Heuristic", "Small blue flower"],
|
| 296 |
+
[4.2, 2.1, 0.8, 8, 45.0, 50, "Heuristic", "Medium yellow flower"],
|
| 297 |
+
[6.1, 2.8, 1.2, 6, 35.5, 80, "Heuristic", "Large orange flower"],
|
| 298 |
+
[2.8, 1.8, 0.6, 9, 55.0, 65, "Heuristic", "Small purple flower"]
|
| 299 |
],
|
| 300 |
+
inputs=[flower_diameter, petal_length, petal_width, petal_count, stem_height, confidence_threshold, prediction_method, gr.Textbox(visible=False)],
|
| 301 |
outputs=output,
|
| 302 |
fn=predict_flower_color,
|
| 303 |
cache_examples=False
|
|
|
|
| 306 |
# Event handlers
|
| 307 |
predict_btn.click(
|
| 308 |
fn=predict_flower_color,
|
| 309 |
+
inputs=[flower_diameter, petal_length, petal_width, petal_count, stem_height, confidence_threshold, prediction_method],
|
| 310 |
outputs=output
|
| 311 |
)
|
| 312 |
|