viswanani commited on
Commit
7855e61
Β·
verified Β·
1 Parent(s): b3b631f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +350 -0
app.py ADDED
@@ -0,0 +1,350 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ import os
3
+ import io
4
+ import uuid
5
+ from datetime import datetime
6
+ from typing import Tuple, Optional
7
+
8
+ import gradio as gr
9
+ import pandas as pd
10
+ from PIL import Image
11
+ from reportlab.lib.pagesizes import A4
12
+ from reportlab.pdfgen import canvas
13
+ from transformers import pipeline
14
+
15
+ # -------------------------
16
+ # Configuration / Filenames
17
+ # -------------------------
18
+ CSV_FILE = "job_cards.csv"
19
+ EXCEL_FILE = "job_cards.xlsx"
20
+ PDF_FOLDER = "pdf_reports"
21
+ os.makedirs(PDF_FOLDER, exist_ok=True)
22
+
23
+ # -------------------------
24
+ # Load AI models (pipelines)
25
+ # -------------------------
26
+ # NOTE: these models are reasonably capable but can be large.
27
+ # On Hugging Face Spaces choose a GPU runtime for best performance.
28
+
29
+ print("Loading models... this may take a while on first run.")
30
+
31
+ # 1) Text classification (zero-shot)
32
+ text_classifier = pipeline(
33
+ task="zero-shot-classification",
34
+ model="facebook/bart-large-mnli"
35
+ )
36
+
37
+ # 2) Image object detection (DETR)
38
+ image_detector = pipeline(
39
+ task="object-detection",
40
+ model="facebook/detr-resnet-50"
41
+ )
42
+
43
+ # 3) Speech-to-text (Whisper)
44
+ asr = pipeline(
45
+ task="automatic-speech-recognition",
46
+ model="openai/whisper-large-v2",
47
+ chunk_length_s=30
48
+ )
49
+
50
+ print("Models loaded.")
51
+
52
+ # -------------------------
53
+ # Knowledge bases (starter)
54
+ # -------------------------
55
+ CATEGORIES = [
56
+ "Engine issue", "Brake issue", "AC problem", "Steering issue",
57
+ "Electrical issue", "Regular service", "Body damage", "Tire issue"
58
+ ]
59
+
60
+ REPAIR_JOBS = {
61
+ "Engine issue": ["Engine diagnosis", "Oil change", "Spark plug check"],
62
+ "Brake issue": ["Brake pad replacement", "Brake fluid top-up", "Disc inspection"],
63
+ "AC problem": ["AC gas refill", "Compressor check", "Cabin filter replacement"],
64
+ "Steering issue": ["Power steering fluid check", "Alignment test"],
65
+ "Electrical issue": ["Battery test", "Alternator check", "Fuse replacement"],
66
+ "Regular service": ["Oil change", "Filter replacement", "General inspection"],
67
+ "Body damage": ["Dent repair", "Paint touch-up", "Bumper replacement"],
68
+ "Tire issue": ["Tire rotation", "Tire replacement", "Wheel balancing"]
69
+ }
70
+
71
+ COST_RANGES = {
72
+ "Engine issue": "β‚Ή3000–₹12,000",
73
+ "Brake issue": "β‚Ή1500–₹7000",
74
+ "AC problem": "β‚Ή2500–₹10,000",
75
+ "Steering issue": "β‚Ή1500–₹8000",
76
+ "Electrical issue": "β‚Ή1000–₹5000",
77
+ "Regular service": "β‚Ή1500–₹6000",
78
+ "Body damage": "β‚Ή2000–₹30,000",
79
+ "Tire issue": "β‚Ή800–₹8,000"
80
+ }
81
+
82
+ # Simple parts mapping (starter)
83
+ PARTS_DB = {
84
+ "Brake pad replacement": ["Brake pads (set)", "Brake cleaner", "Brake fluid"],
85
+ "AC gas refill": ["R134a refrigerant", "O-rings", "Compressor oil"],
86
+ "Oil change": ["Engine oil (4L)", "Oil filter"],
87
+ "Battery test": ["12V battery", "Battery terminal cleaner"]
88
+ }
89
+
90
+ # -------------------------
91
+ # Utility functions
92
+ # -------------------------
93
+ def ensure_csv_exists():
94
+ if not os.path.exists(CSV_FILE):
95
+ df = pd.DataFrame(columns=[
96
+ "ID", "Date", "Customer", "Vehicle", "Complaint",
97
+ "Category", "Suggested_Jobs", "Estimated_Cost",
98
+ "Detected_Damages", "Parts_Recommendations"
99
+ ])
100
+ df.to_csv(CSV_FILE, index=False)
101
+
102
+ def classify_complaint(complaint: str) -> Tuple[str, str, str]:
103
+ """Return human text, top category, comma-joined suggested jobs, and cost"""
104
+ if not complaint or not complaint.strip():
105
+ return ("❌ No complaint text provided.", "", "", "")
106
+ res = text_classifier(complaint, candidate_labels=CATEGORIES)
107
+ top_category = res["labels"][0]
108
+ suggested_jobs = REPAIR_JOBS.get(top_category, ["Workshop inspection required"])
109
+ cost = COST_RANGES.get(top_category, "Varies")
110
+ jobs_text = ", ".join(suggested_jobs)
111
+ human_text = f"πŸ” Category: {top_category}\nπŸ“‹ Jobs: {jobs_text}\nπŸ’° Estimated cost: {cost}"
112
+ return human_text, top_category, jobs_text, cost
113
+
114
+ def detect_damage_from_image(image: Image.Image) -> Tuple[str, str]:
115
+ """Return human text & comma-joined detections"""
116
+ if image is None:
117
+ return ("❌ No image uploaded.", "")
118
+ pil = image.convert("RGB")
119
+ results = image_detector(pil, threshold=0.3)
120
+ # results is a list of dicts with label and score
121
+ if not results:
122
+ return ("βœ… No obvious damage detected.", "")
123
+ detections = [f"{r['label']} ({r['score']:.2f})" for r in results]
124
+ return ("πŸš— Detected: " + ", ".join(detections), ", ".join(detections))
125
+
126
+ def speech_to_text(audio) -> str:
127
+ """Accept a wav/ogg file-like and return cleaned text"""
128
+ if audio is None:
129
+ return ""
130
+ # the pipeline accepts file path or array; Gradio supplies a filepath
131
+ try:
132
+ result = asr(audio)
133
+ text = result.get("text", "")
134
+ # simple cleanup
135
+ return text.strip()
136
+ except Exception as e:
137
+ return f"⚠️ ASR error: {e}"
138
+
139
+ def recommend_parts(jobs_list_str: str, damages_str: str) -> str:
140
+ """Return a simple list of recommended parts. Placeholder for vector/DB search."""
141
+ parts = set()
142
+ if jobs_list_str:
143
+ # heuristic: check if any known job matches
144
+ for job_key in PARTS_DB:
145
+ if job_key.lower() in jobs_list_str.lower():
146
+ for p in PARTS_DB[job_key]:
147
+ parts.add(p)
148
+ # also check damages keywords
149
+ if damages_str:
150
+ if "tire" in damages_str.lower():
151
+ parts.add("Tire(s)")
152
+ if "dent" in damages_str.lower():
153
+ parts.add("Body filler / Paint")
154
+ if not parts:
155
+ return "No specific parts suggested. Inspect to confirm."
156
+ return ", ".join(sorted(parts))
157
+
158
+ # -------------------------
159
+ # Persistence / Job Card CRUD
160
+ # -------------------------
161
+ ensure_csv_exists()
162
+
163
+ def save_job_card(
164
+ customer: str, vehicle: str, complaint: str,
165
+ category: str, suggested_jobs: str, estimated_cost: str,
166
+ detected_damages: str, parts: str
167
+ ) -> str:
168
+ if not (customer and vehicle and complaint):
169
+ return "⚠️ Provide Customer, Vehicle number, and Complaint before saving."
170
+ df = pd.read_csv(CSV_FILE)
171
+ new_id = str(uuid.uuid4())[:8]
172
+ entry = {
173
+ "ID": new_id,
174
+ "Date": datetime.now().strftime("%Y-%m-%d %H:%M"),
175
+ "Customer": customer,
176
+ "Vehicle": vehicle,
177
+ "Complaint": complaint,
178
+ "Category": category,
179
+ "Suggested_Jobs": suggested_jobs,
180
+ "Estimated_Cost": estimated_cost,
181
+ "Detected_Damages": detected_damages,
182
+ "Parts_Recommendations": parts
183
+ }
184
+ df = pd.concat([df, pd.DataFrame([entry])], ignore_index=True)
185
+ df.to_csv(CSV_FILE, index=False)
186
+ return f"βœ… Saved job card (ID: {new_id})"
187
+
188
+ def view_all_cards() -> pd.DataFrame:
189
+ df = pd.read_csv(CSV_FILE)
190
+ return df
191
+
192
+ def search_cards(customer_query: str, vehicle_query: str) -> pd.DataFrame:
193
+ df = pd.read_csv(CSV_FILE)
194
+ if customer_query:
195
+ df = df[df["Customer"].str.contains(customer_query, case=False, na=False)]
196
+ if vehicle_query:
197
+ df = df[df["Vehicle"].str.contains(vehicle_query, case=False, na=False)]
198
+ if df.empty:
199
+ return pd.DataFrame([{"Message": "No matching records found"}])
200
+ return df
201
+
202
+ def export_excel() -> Optional[str]:
203
+ if not os.path.exists(CSV_FILE):
204
+ return None
205
+ df = pd.read_csv(CSV_FILE)
206
+ df.to_excel(EXCEL_FILE, index=False, engine="openpyxl")
207
+ return EXCEL_FILE
208
+
209
+ def generate_jobcard_pdf(job_id: str) -> Optional[str]:
210
+ df = pd.read_csv(CSV_FILE)
211
+ row = df[df["ID"] == job_id]
212
+ if row.empty:
213
+ return None
214
+ row = row.iloc[0]
215
+ filename = os.path.join(PDF_FOLDER, f"jobcard_{job_id}.pdf")
216
+ buffer = io.BytesIO()
217
+ c = canvas.Canvas(buffer, pagesize=A4)
218
+ w, h = A4
219
+ margin = 50
220
+ y = h - margin
221
+
222
+ c.setFont("Helvetica-Bold", 16)
223
+ c.drawString(margin, y, f"Job Card β€” ID: {job_id}")
224
+ y -= 30
225
+ c.setFont("Helvetica", 11)
226
+ lines = [
227
+ f"Date: {row['Date']}",
228
+ f"Customer: {row['Customer']}",
229
+ f"Vehicle: {row['Vehicle']}",
230
+ f"Complaint: {row['Complaint']}",
231
+ f"Category: {row['Category']}",
232
+ f"Suggested Jobs: {row['Suggested_Jobs']}",
233
+ f"Estimated Cost: {row['Estimated_Cost']}",
234
+ f"Detected Damages: {row['Detected_Damages']}",
235
+ f"Parts Recommendations: {row['Parts_Recommendations']}"
236
+ ]
237
+ for line in lines:
238
+ c.drawString(margin, y, line)
239
+ y -= 18
240
+ if y < 80:
241
+ c.showPage()
242
+ y = h - margin
243
+ c.setFont("Helvetica", 11)
244
+ c.save()
245
+
246
+ with open(filename, "wb") as f:
247
+ f.write(buffer.getvalue())
248
+ return filename
249
+
250
+ # -------------------------
251
+ # Gradio UI
252
+ # -------------------------
253
+ with gr.Blocks() as demo:
254
+ gr.Markdown("# 🚘 Full AI Service Advisor β€” Multimodal (All features)")
255
+
256
+ with gr.Tab("Create Job Card"):
257
+ with gr.Row():
258
+ with gr.Column(scale=2):
259
+ customer = gr.Textbox(label="Customer Name")
260
+ vehicle = gr.Textbox(label="Vehicle Number")
261
+ complaint = gr.Textbox(label="Complaint (free text)", lines=3, placeholder="e.g. My steering is stiff when turning left...")
262
+ analyze_btn = gr.Button("Analyze Complaint (NLP)")
263
+ nlp_out = gr.Textbox(label="Complaint Analysis (NLP output)", lines=4)
264
+ category_hidden = gr.Textbox(visible=False)
265
+ jobs_hidden = gr.Textbox(visible=False)
266
+ cost_hidden = gr.Textbox(visible=False)
267
+
268
+ gr.Markdown("### Voice input")
269
+ audio_input = gr.Audio(label="Record Complaint (or upload audio)", source="microphone", type="filepath")
270
+ transcribe_btn = gr.Button("Transcribe Audio β†’ Text")
271
+ transcription_out = gr.Textbox(label="Transcribed Text", lines=3)
272
+
273
+ with gr.Column(scale=2):
274
+ image_input = gr.Image(type="pil", label="Upload car image (damage/photo)")
275
+ detect_btn = gr.Button("Detect Damage (Image)")
276
+ detect_out = gr.Textbox(label="Damage Detection (image)", lines=3)
277
+ det_hidden = gr.Textbox(visible=False)
278
+
279
+ parts_out = gr.Textbox(label="Parts Recommendation", lines=2)
280
+ save_btn = gr.Button("πŸ’Ύ Save Job Card")
281
+ save_status = gr.Textbox(label="Save status")
282
+ gen_pdf_btn = gr.Button("πŸ“„ Generate PDF for Last Saved")
283
+ last_pdf_link = gr.File(label="Download PDF")
284
+
285
+ with gr.Tab("View / Search / Export"):
286
+ view_btn = gr.Button("πŸ“‹ View All Job Cards")
287
+ df_view = gr.Dataframe()
288
+
289
+ gr.Markdown("### Search job cards")
290
+ cust_search = gr.Textbox(label="Search by Customer name (partial)")
291
+ veh_search = gr.Textbox(label="Search by Vehicle number (partial)")
292
+ search_btn = gr.Button("Search")
293
+ search_out = gr.Dataframe()
294
+
295
+ export_btn = gr.Button("Export all as Excel")
296
+ excel_file = gr.File(label="Download Excel")
297
+
298
+ # -----------------------
299
+ # Callbacks / wiring
300
+ # -----------------------
301
+ def on_analyze(complaint_text):
302
+ human, top_cat, jobs_text, cost_text = classify_complaint(complaint_text)
303
+ return human, top_cat, jobs_text, cost_text
304
+
305
+ analyze_btn.click(on_analyze, inputs=complaint, outputs=[nlp_out, category_hidden, jobs_hidden, cost_hidden])
306
+
307
+ def on_transcribe(audio_path):
308
+ txt = speech_to_text(audio_path)
309
+ return txt
310
+
311
+ transcribe_btn.click(on_transcribe, inputs=audio_input, outputs=transcription_out)
312
+
313
+ def on_detect(image_pil):
314
+ human, detections = detect_damage_from_image(image_pil)
315
+ return human, detections
316
+
317
+ detect_btn.click(on_detect, inputs=image_input, outputs=[detect_out, det_hidden])
318
+
319
+ def on_recommend_parts(jobs_text, det_text):
320
+ parts = recommend_parts(jobs_text, det_text)
321
+ return parts
322
+
323
+ # when either jobs_hidden or det_hidden change, update parts_out (we'll wire after)
324
+ jobs_hidden.change(on_recommend_parts, inputs=[jobs_hidden, det_hidden], outputs=parts_out)
325
+ det_hidden.change(on_recommend_parts, inputs=[jobs_hidden, det_hidden], outputs=parts_out)
326
+
327
+ def on_save(customer_name, vehicle_no, complaint_text, category_text, jobs_text, cost_text, det_text, parts_text):
328
+ return save_job_card(customer_name, vehicle_no, complaint_text, category_text, jobs_text, cost_text, det_text, parts_text)
329
+
330
+ save_btn.click(on_save, inputs=[customer, vehicle, complaint, category_hidden, jobs_hidden, cost_hidden, det_hidden, parts_out], outputs=[save_status])
331
+
332
+ # Generate PDF for the last saved job (we look up last row)
333
+ def on_generate_pdf():
334
+ df = pd.read_csv(CSV_FILE)
335
+ if df.empty:
336
+ return None
337
+ last_id = df.iloc[-1]["ID"]
338
+ path = generate_jobcard_pdf(last_id)
339
+ if path:
340
+ return path
341
+ return None
342
+
343
+ gen_pdf_btn.click(on_generate_pdf, outputs=last_pdf_link)
344
+
345
+ # View / search / export
346
+ view_btn.click(lambda: view_all_cards(), outputs=df_view)
347
+ search_btn.click(lambda a,b: search_cards(a,b), inputs=[cust_search, veh_search], outputs=search_out)
348
+ export_btn.click(lambda: export_excel(), outputs=excel_file)
349
+
350
+ demo.launch()