ochsncon commited on
Commit
12aaa49
Β·
verified Β·
1 Parent(s): f55f64a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -57
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
- confidence_warning = ""
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
- method_note = "\nThe classifier can only predict one of the trained vehicle brands."
44
- elif method == "fallback":
45
- method_note = "\nNo trained model found. Please train the model first."
 
 
 
 
 
 
 
 
 
 
46
  elif method == "manual_input":
47
- method_note = "\nCar brand/model entered manually without image analysis."
 
48
 
49
- return f"""
50
- ### Vehicle recognition
51
 
52
- - Predicted vehicle brand: **{predicted_class}**
53
- - Confidence: **{confidence:.2f}**
54
- - Method: **{method}**
 
 
 
 
55
 
56
- The image classifier provides a coarse vehicle brand/model prediction, not exact technical vehicle identification.{confidence_warning}{method_note}
 
 
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
- - Price range: **{lower_bound} - {upper_bound}**
70
 
71
- The estimate is based on structured used-car listing data.
72
  """.strip()
73
 
74
 
75
- def _build_budget_assessment_markdown(recommendation: dict[str, Any]) -> str:
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
- ### Simple financing orientation
 
 
 
 
 
 
 
 
93
 
94
- - Orientation: **{recommendation.get('financing_orientation', 'n/a')}**
95
- - Financing gap: **{gap_text}**
96
- - Rough months needed: **{months_text}**
97
- - Note: {recommendation.get('financing_reason', 'n/a')}
98
 
99
- No interest rates, leasing offers, or true financing products are simulated here.
 
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
- "No price estimate available yet.",
152
- "No budget assessment available yet.",
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
- _build_budget_assessment_markdown(recommendation),
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
- budget_assessment = gr.Markdown(label="Budget assessment")
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
- budget_assessment,
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
- budget_assessment,
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
  )