Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -34,26 +34,38 @@ def _build_vehicle_recognition_markdown(vision_results: dict[str, Any]) -> str:
|
|
| 34 |
confidence = float(vision_results.get("confidence", 0.0) or 0.0)
|
| 35 |
method = vision_results.get("method", "fallback")
|
| 36 |
|
| 37 |
-
|
| 38 |
-
if confidence < 0.5 and predicted_class != "Unknown":
|
| 39 |
-
confidence_warning = "\nβ οΈ **Low confidence**: please verify the result manually."
|
| 40 |
-
|
| 41 |
-
method_note = ""
|
| 42 |
if method == "local_transfer_model":
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
elif method == "manual_input":
|
| 47 |
-
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
### Vehicle recognition
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
|
|
|
|
|
|
|
| 57 |
""".strip()
|
| 58 |
|
| 59 |
|
|
@@ -63,40 +75,44 @@ def _build_price_estimate_markdown(price_prediction: dict[str, Any]) -> str:
|
|
| 63 |
upper_bound = format_currency_chf(price_prediction.get("upper_bound"))
|
| 64 |
|
| 65 |
return f"""
|
| 66 |
-
### Price estimate
|
| 67 |
|
| 68 |
- Estimated market price: **{estimated_price}**
|
| 69 |
-
-
|
| 70 |
|
| 71 |
-
|
| 72 |
""".strip()
|
| 73 |
|
| 74 |
|
| 75 |
-
def
|
| 76 |
-
return f"""
|
| 77 |
-
### Budget assessment
|
| 78 |
-
|
| 79 |
-
- Assessment: **{recommendation.get('price_budget_assessment', 'n/a')}**
|
| 80 |
-
- Reason: {recommendation.get('price_budget_reason', 'n/a')}
|
| 81 |
-
""".strip()
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
def _build_financing_markdown(recommendation: dict[str, Any]) -> str:
|
| 85 |
rough_months_needed = recommendation.get("rough_months_needed")
|
| 86 |
financing_gap = recommendation.get("financing_gap")
|
| 87 |
|
| 88 |
months_text = "n/a" if rough_months_needed in {None, "", 0} else f"{float(rough_months_needed):.1f} months"
|
| 89 |
gap_text = "n/a" if financing_gap in {None, ""} else format_currency_chf(financing_gap)
|
| 90 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
return f"""
|
| 92 |
-
###
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
- Rough months needed: **{months_text}**
|
| 97 |
-
- Note: {recommendation.get('financing_reason', 'n/a')}
|
| 98 |
|
| 99 |
-
|
|
|
|
| 100 |
""".strip()
|
| 101 |
|
| 102 |
|
|
@@ -147,12 +163,9 @@ def _run_advisor(
|
|
| 147 |
# Allow either image OR manual make_model input, but require at least one
|
| 148 |
if image is None and not (make_model and make_model.strip()):
|
| 149 |
return (
|
| 150 |
-
"Please upload a car image OR enter a known make/model.",
|
| 151 |
-
"
|
| 152 |
-
"
|
| 153 |
-
"No financing orientation available yet.",
|
| 154 |
-
"Please provide car details to start the analysis.",
|
| 155 |
-
"No disclaimer available yet.",
|
| 156 |
{},
|
| 157 |
)
|
| 158 |
|
|
@@ -197,16 +210,13 @@ def _run_advisor(
|
|
| 197 |
return (
|
| 198 |
_build_vehicle_recognition_markdown(vision_results),
|
| 199 |
_build_price_estimate_markdown(price_prediction),
|
| 200 |
-
|
| 201 |
-
_build_financing_markdown(recommendation),
|
| 202 |
-
recommendation.get("full_explanation", "n/a"),
|
| 203 |
-
recommendation.get("disclaimer", "n/a"),
|
| 204 |
_build_debug_payload(vision_results, price_prediction, recommendation),
|
| 205 |
)
|
| 206 |
|
| 207 |
|
| 208 |
def _clear_outputs():
|
| 209 |
-
return "", "", "",
|
| 210 |
|
| 211 |
|
| 212 |
def build_interface() -> gr.Blocks:
|
|
@@ -244,10 +254,7 @@ Image upload -> analyze_car_image() -> predicted_class -> predict_price() -> est
|
|
| 244 |
with gr.Column(scale=1):
|
| 245 |
vehicle_recognition = gr.Markdown(label="Vehicle recognition")
|
| 246 |
price_estimate = gr.Markdown(label="Price estimate")
|
| 247 |
-
|
| 248 |
-
financing_orientation = gr.Markdown(label="Simple financing orientation")
|
| 249 |
-
ai_description = gr.Markdown(label="AI-generated description")
|
| 250 |
-
disclaimer = gr.Markdown(label="Disclaimer")
|
| 251 |
|
| 252 |
with gr.Accordion("Debug details", open=False):
|
| 253 |
debug_output = gr.JSON(label="Raw pipeline output")
|
|
@@ -258,10 +265,7 @@ Image upload -> analyze_car_image() -> predicted_class -> predict_price() -> est
|
|
| 258 |
outputs=[
|
| 259 |
vehicle_recognition,
|
| 260 |
price_estimate,
|
| 261 |
-
|
| 262 |
-
financing_orientation,
|
| 263 |
-
ai_description,
|
| 264 |
-
disclaimer,
|
| 265 |
debug_output,
|
| 266 |
],
|
| 267 |
)
|
|
@@ -272,10 +276,7 @@ Image upload -> analyze_car_image() -> predicted_class -> predict_price() -> est
|
|
| 272 |
outputs=[
|
| 273 |
vehicle_recognition,
|
| 274 |
price_estimate,
|
| 275 |
-
|
| 276 |
-
financing_orientation,
|
| 277 |
-
ai_description,
|
| 278 |
-
disclaimer,
|
| 279 |
debug_output,
|
| 280 |
],
|
| 281 |
)
|
|
|
|
| 34 |
confidence = float(vision_results.get("confidence", 0.0) or 0.0)
|
| 35 |
method = vision_results.get("method", "fallback")
|
| 36 |
|
| 37 |
+
# Case A: Image analyzed by vision model
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
if method == "local_transfer_model":
|
| 39 |
+
confidence_warning = ""
|
| 40 |
+
if confidence < 0.5:
|
| 41 |
+
confidence_warning = "\n\nThe image model is uncertain. Please verify the result manually."
|
| 42 |
+
|
| 43 |
+
return f"""
|
| 44 |
+
### 1. Vehicle recognition β Computer Vision
|
| 45 |
+
|
| 46 |
+
Detected brand/model group: **{predicted_class}**
|
| 47 |
+
|
| 48 |
+
The image analysis provides a coarse vehicle brand/model prediction based on deep learning. It is not exact technical vehicle identification.{confidence_warning}
|
| 49 |
+
""".strip()
|
| 50 |
+
|
| 51 |
+
# Case B: Manual input without image
|
| 52 |
elif method == "manual_input":
|
| 53 |
+
return f"""
|
| 54 |
+
### 1. Vehicle recognition β Manual Input
|
| 55 |
|
| 56 |
+
Vehicle used for the estimate: **{predicted_class}**
|
|
|
|
| 57 |
|
| 58 |
+
The price estimate is based on your manual vehicle input and structured used-car listing data.
|
| 59 |
+
""".strip()
|
| 60 |
+
|
| 61 |
+
# Case C: Unknown or fallback
|
| 62 |
+
else:
|
| 63 |
+
return f"""
|
| 64 |
+
### 1. Vehicle recognition
|
| 65 |
|
| 66 |
+
Vehicle: **Unknown**
|
| 67 |
+
|
| 68 |
+
Please upload a car image or enter a known make/model to get a price estimate.
|
| 69 |
""".strip()
|
| 70 |
|
| 71 |
|
|
|
|
| 75 |
upper_bound = format_currency_chf(price_prediction.get("upper_bound"))
|
| 76 |
|
| 77 |
return f"""
|
| 78 |
+
### 2. Price estimate β ML Numeric Data
|
| 79 |
|
| 80 |
- Estimated market price: **{estimated_price}**
|
| 81 |
+
- Expected price range: **{lower_bound} β {upper_bound}**
|
| 82 |
|
| 83 |
+
This estimate is based on structured used-car listing data.
|
| 84 |
""".strip()
|
| 85 |
|
| 86 |
|
| 87 |
+
def _build_purchase_assessment_markdown(recommendation: dict[str, Any]) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
rough_months_needed = recommendation.get("rough_months_needed")
|
| 89 |
financing_gap = recommendation.get("financing_gap")
|
| 90 |
|
| 91 |
months_text = "n/a" if rough_months_needed in {None, "", 0} else f"{float(rough_months_needed):.1f} months"
|
| 92 |
gap_text = "n/a" if financing_gap in {None, ""} else format_currency_chf(financing_gap)
|
| 93 |
|
| 94 |
+
budget_assessment = recommendation.get('price_budget_assessment', 'n/a')
|
| 95 |
+
budget_reason = recommendation.get('price_budget_reason', '')
|
| 96 |
+
financing_orientation = recommendation.get('financing_orientation', 'n/a')
|
| 97 |
+
financing_reason = recommendation.get('financing_reason', '')
|
| 98 |
+
explanation = recommendation.get('full_explanation', 'n/a')
|
| 99 |
+
|
| 100 |
return f"""
|
| 101 |
+
### 3. Purchase assessment β NLP Explanation
|
| 102 |
+
|
| 103 |
+
**Budget assessment:** {budget_assessment}
|
| 104 |
+
{budget_reason}
|
| 105 |
+
|
| 106 |
+
**Simple financing orientation:** {financing_orientation}
|
| 107 |
+
- Financing gap: {gap_text}
|
| 108 |
+
- Rough months needed: {months_text}
|
| 109 |
+
{financing_reason}
|
| 110 |
|
| 111 |
+
**Assessment:**
|
| 112 |
+
{explanation}
|
|
|
|
|
|
|
| 113 |
|
| 114 |
+
---
|
| 115 |
+
**Disclaimer:** This output is a non-binding orientation only. It does not replace a technical vehicle inspection, financial advice or an insurance quote.
|
| 116 |
""".strip()
|
| 117 |
|
| 118 |
|
|
|
|
| 163 |
# Allow either image OR manual make_model input, but require at least one
|
| 164 |
if image is None and not (make_model and make_model.strip()):
|
| 165 |
return (
|
| 166 |
+
"Please upload a car image OR enter a known make/model to start the analysis.",
|
| 167 |
+
"",
|
| 168 |
+
"",
|
|
|
|
|
|
|
|
|
|
| 169 |
{},
|
| 170 |
)
|
| 171 |
|
|
|
|
| 210 |
return (
|
| 211 |
_build_vehicle_recognition_markdown(vision_results),
|
| 212 |
_build_price_estimate_markdown(price_prediction),
|
| 213 |
+
_build_purchase_assessment_markdown(recommendation),
|
|
|
|
|
|
|
|
|
|
| 214 |
_build_debug_payload(vision_results, price_prediction, recommendation),
|
| 215 |
)
|
| 216 |
|
| 217 |
|
| 218 |
def _clear_outputs():
|
| 219 |
+
return "", "", "", {}
|
| 220 |
|
| 221 |
|
| 222 |
def build_interface() -> gr.Blocks:
|
|
|
|
| 254 |
with gr.Column(scale=1):
|
| 255 |
vehicle_recognition = gr.Markdown(label="Vehicle recognition")
|
| 256 |
price_estimate = gr.Markdown(label="Price estimate")
|
| 257 |
+
purchase_assessment = gr.Markdown(label="Purchase assessment")
|
|
|
|
|
|
|
|
|
|
| 258 |
|
| 259 |
with gr.Accordion("Debug details", open=False):
|
| 260 |
debug_output = gr.JSON(label="Raw pipeline output")
|
|
|
|
| 265 |
outputs=[
|
| 266 |
vehicle_recognition,
|
| 267 |
price_estimate,
|
| 268 |
+
purchase_assessment,
|
|
|
|
|
|
|
|
|
|
| 269 |
debug_output,
|
| 270 |
],
|
| 271 |
)
|
|
|
|
| 276 |
outputs=[
|
| 277 |
vehicle_recognition,
|
| 278 |
price_estimate,
|
| 279 |
+
purchase_assessment,
|
|
|
|
|
|
|
|
|
|
| 280 |
debug_output,
|
| 281 |
],
|
| 282 |
)
|