Spaces:
Running on Zero
Running on Zero
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 +29 -30
- backend/app/core/reference_data.py +45 -2
- frontend/css/styles.css +46 -4
- frontend/index.html +2 -0
- frontend/js/alt_app.js +5 -3
- frontend/js/app.js +5 -3
- test_chat.py +21 -0
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 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
Vehicle 1
|
| 31 |
-
Vehicle 1
|
| 32 |
-
Vehicle 1
|
| 33 |
-
Vehicle
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
Road
|
| 45 |
-
|
| 46 |
-
Vehicle 1
|
| 47 |
-
Vehicle 1 Tyre
|
| 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,
|
| 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]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
.
|
| 848 |
-
|
| 849 |
-
|
| 850 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
<
|
|
|
|
|
|
|
| 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 |
-
<
|
|
|
|
|
|
|
| 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"))
|