Siddharth Ravikumar commited on
Commit
c1b133d
·
1 Parent(s): 656a6ab

feat: implement proper markdown formatting for scene analysis

Browse files
backend/app/core/inference.py CHANGED
@@ -22,40 +22,39 @@ logger = get_logger("inference")
22
 
23
  SCENE_ANALYSIS_PROMPT = """You are an expert AI Accident Investigator. Analyze this accident scene photograph and extract specific details EXACTLY matching the formatting below.
24
 
25
- DO NOT output a generic "Accident Report" with Date/Time/Location. Do not use markdown headers like "## Accident Report".
26
- You MUST output EXACTLY these four bracketed sections and nothing else. Output the brackets exactly as shown.
27
-
28
- [AI Observation]
29
- Vehicles Involved: <Number>
30
- Vehicle 1 Make/Model: <Year Make Model (Color BodyType)>
31
- Vehicle 1 License Plate: <Plate or "Not visible">
32
- Vehicle 1 Geographic Location: <Place name and Country derived from license plate format/text. E.g. "Maharashtra, India" or "Texas, USA" if possible, else "Unknown">
33
- Vehicle 1 Owner: <Owner Name or "Not visible">
34
- ... (Repeat exactly the same 4 fields for Vehicle 2, Vehicle 3, etc. ONLY if they are clearly visible in the image. DO NOT invent vehicles that do not exist.)
35
- Non-vehicle Parties: <Pedestrians, cyclists, objects if any>
36
-
37
- [Accident Severity]
38
- Accident Category: <mild / medium / critical based on structural damage>
39
- Survival/Injury Estimation: <minor injury / critical injury / life threatening injury based on passenger compartment intrusion and airbag deployment>
40
-
41
- [Condition Assessment]
42
- Time of Incident: <Day / Dusk / Night based on lighting>
43
- Weather: <Clear / Cloudy / Rain / Fog / Snow>
44
- Road Type: <1-way / 2-way / Intersection / Parking Lot / Highway>
45
- Road Surface: <Dry / Wet / Icy / Debris-covered>
46
- Vehicle 1 Position: <Direction facing, location on road>
47
- Vehicle 1 Tyre Direction: <Straight / Turned Left / Turned Right>
48
- Vehicle 1 Tyre Condition: <Condition details or "Not visible">
49
  ... (Repeat exactly the same 3 fields for Vehicle 2, Vehicle 3, etc. ONLY if they are present)
50
- Area of Impact: <Front / Rear / Side / Corner etc. for the primary collision>
51
- Visible Debris/Skid Marks: <Description of glass, plastic, fluids, or tire marks on road>
52
- Airbag/Safety: <Deployed / Not visible>
53
 
54
- [Damage Analysis & Insurance Assessment]
55
- - <Specific Part of Vehicle 1>: <Damage description> (Intensity: <Low/Medium/High>). Clause <Guess> -> <SUBROGATION CLAIM / FULLY COVERED / SUBJECT TO DEDUCTIBLE>
56
  ... (List damage for other visible vehicles only)
57
 
58
- [Summary]
59
  <2-3 sentences summarizing the collision, layout, and visible damages>"""
60
 
61
 
 
22
 
23
  SCENE_ANALYSIS_PROMPT = """You are an expert AI Accident Investigator. Analyze this accident scene photograph and extract specific details EXACTLY matching the formatting below.
24
 
25
+ You MUST use Markdown formatting with headers (###) and bullet points (- **Key**: Value) as shown below.
26
+
27
+ ### [AI Observation]
28
+ - **Vehicles Involved**: <Number>
29
+ - **Vehicle 1 Make/Model**: <Year Make Model (Color BodyType)>
30
+ - **Vehicle 1 License Plate**: <Plate or "Not visible">
31
+ - **Vehicle 1 Geographic Location**: <Place name and Country derived from license plate format/text. E.g. "Dubai, UAE" or "Texas, USA" if possible, else "Unknown">
32
+ - **Vehicle 1 Owner**: <Owner Name or "Not visible">
33
+ ... (Repeat exactly the same fields for Vehicle 2, Vehicle 3, etc. ONLY if they are clearly visible in the image. DO NOT invent vehicles that do not exist.)
34
+ - **Non-vehicle Parties**: <Pedestrians, cyclists, objects if any>
35
+
36
+ ### [Accident Severity]
37
+ - **Accident Category**: <mild / medium / critical based on structural damage>
38
+ - **Survival/Injury Estimation**: <minor injury / critical injury / life threatening injury based on passenger compartment intrusion and airbag deployment>
39
+
40
+ ### [Condition Assessment]
41
+ - **Time of Incident**: <Day / Dusk / Night based on lighting>
42
+ - **Weather**: <Clear / Cloudy / Rain / Fog / Snow>
43
+ - **Road Type**: <1-way / 2-way / Intersection / Parking Lot / Highway>
44
+ - **Road Surface**: <Dry / Wet / Icy / Debris-covered>
45
+ - **Vehicle 1 Position**: <Direction facing, location on road>
46
+ - **Vehicle 1 Tyre Direction**: <Straight / Turned Left / Turned Right>
47
+ - **Vehicle 1 Tyre Condition**: <Condition details or "Not visible">
 
48
  ... (Repeat exactly the same 3 fields for Vehicle 2, Vehicle 3, etc. ONLY if they are present)
49
+ - **Area of Impact**: <Front / Rear / Side / Corner etc. for the primary collision>
50
+ - **Visible Debris/Skid Marks**: <Description of glass, plastic, fluids, tire marks on road>
51
+ - **Airbag/Safety**: <Deployed / Not visible>
52
 
53
+ ### [Damage Analysis & Insurance Assessment]
54
+ - **<Specific Part of Vehicle 1>**: <Damage description> (Intensity: <Low/Medium/High>). Clause <Guess> -> <SUBROGATION CLAIM / FULLY COVERED / SUBJECT TO DEDUCTIBLE>
55
  ... (List damage for other visible vehicles only)
56
 
57
+ ### [Summary]
58
  <2-3 sentences summarizing the collision, layout, and visible damages>"""
59
 
60
 
backend/app/core/reference_data.py CHANGED
@@ -24,7 +24,37 @@ REFERENCE_CASES = {
24
  "analyses": [
25
  {
26
  "filename": "01a252ba0708_car_8.jpg",
27
- "raw_analysis": "[AI Observation]\\nVehicles Involved: 1\\nVehicle 1 Make/Model: 2018 Toyota Highlander (Silver SUV)\\nVehicle 1 License Plate: CA 8XYZ123\\nVehicle 1 Geographic Location: California, USA\\nVehicle 1 Owner: Sarah Jenkins\\nNon-vehicle Parties: None\\n\\n[Accident Severity]\\nAccident Category: critical\\nSurvival/Injury Estimation: minor injury\\n\\n[Condition Assessment]\\nTime of Incident: Day\\nWeather: Clear\\nRoad Type: 2-way\\nRoad Surface: Dry\\nVehicle 1 Position: Facing right, near the curb\\nVehicle 1 Tyre Direction: Straight\\nVehicle 1 Tyre Condition: Front-Left (Good, 6mm tread), Front-Right (Good, 6mm tread), Rears (Worn, 3mm tread)\\nArea of Impact: Front\\nVisible Debris/Skid Marks: Glass shards and fluid leak near the front bumper\\nAirbag/Safety: Deployed\\n\\n[Damage Analysis & Insurance Assessment]\\n- Front Bumper: Severe crush damage (Intensity: High). \\n- Radiator & Core Support: Destroyed (Intensity: High).\\n- Right Fender: Crumpled (Intensity: High).\\n\\n[Summary]\\nSevere front-end deformation observed consistent with high-speed frontal impact. Airbags deployed. Vehicle is likely a total loss."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  }
29
  ],
30
  "violations": [
@@ -61,7 +91,18 @@ REFERENCE_CASES = {
61
  "analyses": [
62
  {
63
  "filename": "a358e234c528_accident_scene_2.jpg",
64
- "raw_analysis": "[AI Observation]\\nVehicles Involved: 2\\nVehicle 1 Make/Model: 2020 Honda Civic (Blue Sedan)\\nVehicle 1 License Plate: TX LMN-4567\\nVehicle 2 Make/Model: 2015 Ford F-150 (White Truck)\\nVehicle 2 License Plate: TX PQR-8910\\n\\n[Accident Severity]\\nAccident Category: medium\\n\\n[Summary]\\nT-bone intersection collision. Blue Sedan sustained heavy side impact; White Truck shows front-end damage. Airbags deployed on Blue Sedan."
 
 
 
 
 
 
 
 
 
 
 
65
  }
66
  ],
67
  "violations": [
@@ -82,3 +123,5 @@ REFERENCE_CASES = {
82
  ]
83
  }
84
  }
 
 
 
24
  "analyses": [
25
  {
26
  "filename": "01a252ba0708_car_8.jpg",
27
+ "raw_analysis": """### [AI Observation]
28
+ - **Vehicles Involved**: 1
29
+ - **Vehicle 1 Make/Model**: 2018 Toyota Highlander (Silver SUV)
30
+ - **Vehicle 1 License Plate**: CA 8XYZ123
31
+ - **Vehicle 1 Geographic Location**: California, USA
32
+ - **Vehicle 1 Owner**: Sarah Jenkins
33
+ - **Non-vehicle Parties**: None
34
+
35
+ ### [Accident Severity]
36
+ - **Accident Category**: critical
37
+ - **Survival/Injury Estimation**: minor injury
38
+
39
+ ### [Condition Assessment]
40
+ - **Time of Incident**: Day
41
+ - **Weather**: Clear
42
+ - **Road Type**: 2-way
43
+ - **Road Surface**: Dry
44
+ - **Vehicle 1 Position**: Facing right, near the curb
45
+ - **Vehicle 1 Tyre Direction**: Straight
46
+ - **Vehicle 1 Tyre Condition**: Front-Left (Good, 6mm tread), Front-Right (Good, 6mm tread), Rears (Worn, 3mm tread)
47
+ - **Area of Impact**: Front
48
+ - **Visible Debris/Skid Marks**: Glass shards and fluid leak near the front bumper
49
+ - **Airbag/Safety**: Deployed
50
+
51
+ ### [Damage Analysis & Insurance Assessment]
52
+ - **Front Bumper**: Severe crush damage (Intensity: High).
53
+ - **Radiator & Core Support**: Destroyed (Intensity: High).
54
+ - **Right Fender**: Crumpled (Intensity: High).
55
+
56
+ ### [Summary]
57
+ Severe front-end deformation observed consistent with high-speed frontal impact. Airbags deployed. Vehicle is likely a total loss."""
58
  }
59
  ],
60
  "violations": [
 
91
  "analyses": [
92
  {
93
  "filename": "a358e234c528_accident_scene_2.jpg",
94
+ "raw_analysis": """### [AI Observation]
95
+ - **Vehicles Involved**: 2
96
+ - **Vehicle 1 Make/Model**: 2020 Honda Civic (Blue Sedan)
97
+ - **Vehicle 1 License Plate**: TX LMN-4567
98
+ - **Vehicle 2 Make/Model**: 2015 Ford F-150 (White Truck)
99
+ - **Vehicle 2 License Plate**: TX PQR-8910
100
+
101
+ ### [Accident Severity]
102
+ - **Accident Category**: medium
103
+
104
+ ### [Summary]
105
+ T-bone intersection collision. Blue Sedan sustained heavy side impact; White Truck shows front-end damage. Airbags deployed on Blue Sedan."""
106
  }
107
  ],
108
  "violations": [
 
123
  ]
124
  }
125
  }
126
+
127
+
frontend/css/styles.css CHANGED
@@ -844,10 +844,52 @@ button {
844
  color: var(--text-secondary);
845
  }
846
 
847
- .analysis-content pre {
848
- white-space: pre-wrap;
849
- word-break: break-word;
850
- font-family: var(--font-body);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
851
  }
852
 
853
  .analysis-photo-section {
 
844
  color: var(--text-secondary);
845
  }
846
 
847
+ .markdown-body h1,
848
+ .markdown-body h2,
849
+ .markdown-body h3,
850
+ .markdown-body h4 {
851
+ margin-top: 1.5rem;
852
+ margin-bottom: 0.8rem;
853
+ color: var(--text-primary);
854
+ font-weight: 600;
855
+ }
856
+
857
+ .markdown-body h3 {
858
+ font-size: 1.1rem;
859
+ border-bottom: 1px solid var(--border);
860
+ padding-bottom: 0.3rem;
861
+ }
862
+
863
+ .markdown-body h4 {
864
+ font-size: 0.95rem;
865
+ color: var(--accent);
866
+ }
867
+
868
+ .markdown-body p {
869
+ margin-bottom: 1rem;
870
+ }
871
+
872
+ .markdown-body ul,
873
+ .markdown-body ol {
874
+ margin-bottom: 1rem;
875
+ padding-left: 1.2rem;
876
+ }
877
+
878
+ .markdown-body li {
879
+ margin-bottom: 0.4rem;
880
+ }
881
+
882
+ .markdown-body strong {
883
+ color: var(--text-primary);
884
+ font-weight: 600;
885
+ }
886
+
887
+ .markdown-body code {
888
+ background: var(--surface);
889
+ padding: 0.2rem 0.4rem;
890
+ border-radius: 4px;
891
+ font-family: monospace;
892
+ font-size: 0.8rem;
893
  }
894
 
895
  .analysis-photo-section {
frontend/index.html CHANGED
@@ -20,6 +20,8 @@
20
 
21
  <!-- Styles -->
22
  <link rel="stylesheet" href="/static/css/styles.css">
 
 
23
  </head>
24
 
25
  <body>
 
20
 
21
  <!-- Styles -->
22
  <link rel="stylesheet" href="/static/css/styles.css">
23
+ <!-- Scripts -->
24
+ <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
25
  </head>
26
 
27
  <body>
frontend/js/alt_app.js CHANGED
@@ -725,9 +725,11 @@ function renderCaseDetail(data) {
725
  const analysisContent = document.getElementById('analysis-content');
726
  if (data.analyses?.length) {
727
  analysisContent.innerHTML = data.analyses.map(a => `
728
- <div class="analysis-photo-section">
729
- <div class="analysis-photo-label">📷 ${escHtml(a.filename || 'Photo')}</div>
730
- <pre>${escHtml(a.raw_analysis || '')}</pre>
 
 
731
  </div>
732
  `).join('');
733
  } else {
 
725
  const analysisContent = document.getElementById('analysis-content');
726
  if (data.analyses?.length) {
727
  analysisContent.innerHTML = data.analyses.map(a => `
728
+ <div class="analysis-photo-section" style="margin-bottom: 2rem; padding-bottom: 1.5rem; border-bottom: 1px solid var(--border);">
729
+ <div class="analysis-photo-label" style="font-weight: 600; margin-bottom: 1rem;">📷 ${escHtml(a.filename || 'Photo')}</div>
730
+ <div class="markdown-body">
731
+ ${marked.parse(a.raw_analysis || '')}
732
+ </div>
733
  </div>
734
  `).join('');
735
  } else {
frontend/js/app.js CHANGED
@@ -552,9 +552,11 @@ function renderCaseDetail(data) {
552
  const analysisContent = document.getElementById('analysis-content');
553
  if (data.analyses?.length) {
554
  analysisContent.innerHTML = data.analyses.map(a => `
555
- <div class="analysis-photo-section">
556
- <div class="analysis-photo-label">📷 ${escHtml(a.filename || 'Photo')}</div>
557
- <pre>${escHtml(a.raw_analysis || '')}</pre>
 
 
558
  </div>
559
  `).join('');
560
  } else {
 
552
  const analysisContent = document.getElementById('analysis-content');
553
  if (data.analyses?.length) {
554
  analysisContent.innerHTML = data.analyses.map(a => `
555
+ <div class="analysis-photo-section" style="margin-bottom: 2rem; padding-bottom: 1.5rem; border-bottom: 1px solid var(--border);">
556
+ <div class="analysis-photo-label" style="font-weight: 600; margin-bottom: 1rem;">📷 ${escHtml(a.filename || 'Photo')}</div>
557
+ <div class="markdown-body">
558
+ ${marked.parse(a.raw_analysis || '')}
559
+ </div>
560
  </div>
561
  `).join('');
562
  } else {
test_chat.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import json
3
+
4
+ url = "http://127.0.0.1:8001/gradio_api/call/chat"
5
+ headers = {"Content-Type": "application/json"}
6
+ payload = {
7
+ "data": ["Hello", [], "System Context here"]
8
+ }
9
+
10
+ res = requests.post(url, headers=headers, json=payload)
11
+ print("POST Response:", res.status_code, res.text)
12
+
13
+ if res.status_code == 200:
14
+ event_id = res.json().get("event_id")
15
+ print("Event ID:", event_id)
16
+ stream_url = f"{url}/{event_id}"
17
+ res2 = requests.get(stream_url, headers={"Accept": "text/event-stream"})
18
+ print("STREAM Response:", res2.status_code)
19
+ for line in res2.iter_lines():
20
+ if line:
21
+ print(line.decode("utf-8"))