Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,15 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import datetime
|
| 4 |
-
import pytesseract
|
| 5 |
-
import cv2
|
| 6 |
import random
|
| 7 |
from fpdf import FPDF
|
|
|
|
|
|
|
| 8 |
import time
|
| 9 |
|
| 10 |
-
# Quantum
|
| 11 |
class QuantumSim:
|
| 12 |
def __init__(self):
|
| 13 |
self.states = {
|
|
@@ -19,31 +21,52 @@ class QuantumSim:
|
|
| 19 |
def evaluate_transaction(self):
|
| 20 |
return random.choice(list(self.states.items()))
|
| 21 |
|
| 22 |
-
#
|
| 23 |
class CVProcessor:
|
| 24 |
-
def extract_vehicle_number(self,
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
text = pytesseract.image_to_string(gray)
|
| 27 |
-
return text.strip() or "
|
| 28 |
|
| 29 |
-
def detect_damage_severity(self,
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
def detect_person_injury(self,
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
# Agent
|
| 37 |
class InsuranceAgent:
|
| 38 |
def __init__(self):
|
| 39 |
self.qsim = QuantumSim()
|
| 40 |
self.cv = CVProcessor()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
def
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
injury = self.cv.detect_person_injury(dummy_frame)
|
| 47 |
q_code, status = self.qsim.evaluate_transaction()
|
| 48 |
|
| 49 |
response = {
|
|
@@ -60,41 +83,47 @@ class InsuranceAgent:
|
|
| 60 |
}
|
| 61 |
|
| 62 |
self.generate_pdf_report(response)
|
| 63 |
-
|
|
|
|
| 64 |
|
| 65 |
def generate_pdf_report(self, data):
|
| 66 |
pdf = FPDF()
|
| 67 |
pdf.add_page()
|
| 68 |
pdf.set_font("Arial", size=12)
|
| 69 |
-
pdf.cell(200, 10, txt="Insurance
|
| 70 |
for key, value in data.items():
|
| 71 |
pdf.cell(200, 10, txt=f"{key}: {value}", ln=True)
|
| 72 |
-
filename = f"
|
| 73 |
pdf.output(filename)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
def start_no_ops_agent():
|
| 78 |
-
return agent.process_autonomous_claim()
|
| 79 |
-
|
| 80 |
-
# Gradio Interface (No Uploads Needed)
|
| 81 |
-
interface = gr.Interface(
|
| 82 |
-
fn=start_no_ops_agent,
|
| 83 |
-
inputs=[],
|
| 84 |
-
outputs=[
|
| 85 |
-
gr.Textbox(label="Time"),
|
| 86 |
-
gr.Textbox(label="Vehicle Number"),
|
| 87 |
-
gr.Textbox(label="Damage Level"),
|
| 88 |
-
gr.Textbox(label="Injury Level"),
|
| 89 |
-
gr.Textbox(label="Quantum Status"),
|
| 90 |
-
gr.Textbox(label="Code"),
|
| 91 |
-
gr.Textbox(label="Hospital Notified"),
|
| 92 |
-
gr.Textbox(label="Tow Vehicle Dispatched"),
|
| 93 |
-
gr.Textbox(label="IRDA Notified"),
|
| 94 |
-
gr.Textbox(label="Final Status"),
|
| 95 |
-
],
|
| 96 |
-
title="No-Ops AI Agent - Auto Insurance Workflow",
|
| 97 |
-
description="Fully autonomous agent that handles road accident insurance, hospital dispatch, fraud detection, and IRDA reporting with no human input."
|
| 98 |
-
)
|
| 99 |
-
|
| 100 |
-
interface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
import pytesseract
|
| 4 |
import numpy as np
|
| 5 |
import datetime
|
|
|
|
|
|
|
| 6 |
import random
|
| 7 |
from fpdf import FPDF
|
| 8 |
+
import os
|
| 9 |
+
import threading
|
| 10 |
import time
|
| 11 |
|
| 12 |
+
# Quantum Simulation Logic
|
| 13 |
class QuantumSim:
|
| 14 |
def __init__(self):
|
| 15 |
self.states = {
|
|
|
|
| 21 |
def evaluate_transaction(self):
|
| 22 |
return random.choice(list(self.states.items()))
|
| 23 |
|
| 24 |
+
# Computer Vision & OCR Agent
|
| 25 |
class CVProcessor:
|
| 26 |
+
def extract_vehicle_number(self, image_np):
|
| 27 |
+
if image_np is None:
|
| 28 |
+
return "No image provided"
|
| 29 |
+
gray = cv2.cvtColor(image_np, cv2.COLOR_BGR2GRAY)
|
| 30 |
text = pytesseract.image_to_string(gray)
|
| 31 |
+
return text.strip() or "Not detected"
|
| 32 |
|
| 33 |
+
def detect_damage_severity(self, image_np):
|
| 34 |
+
if image_np is None:
|
| 35 |
+
return "No image provided"
|
| 36 |
+
# Simulate damage level detection
|
| 37 |
+
damage_score = np.random.randint(1, 10)
|
| 38 |
+
if damage_score > 7:
|
| 39 |
+
return "Severe"
|
| 40 |
+
elif damage_score > 4:
|
| 41 |
+
return "Moderate"
|
| 42 |
+
else:
|
| 43 |
+
return "Minor"
|
| 44 |
|
| 45 |
+
def detect_person_injury(self, image_np):
|
| 46 |
+
if image_np is None:
|
| 47 |
+
return "No image provided"
|
| 48 |
+
injury_score = np.random.randint(0, 2)
|
| 49 |
+
return "Critical" if injury_score else "Stable"
|
| 50 |
|
| 51 |
+
# Insurance Automation Agent
|
| 52 |
class InsuranceAgent:
|
| 53 |
def __init__(self):
|
| 54 |
self.qsim = QuantumSim()
|
| 55 |
self.cv = CVProcessor()
|
| 56 |
+
self.reports = []
|
| 57 |
+
|
| 58 |
+
def simulate_live_camera_feed(self):
|
| 59 |
+
print("[Agent] Watching live feed... analyzing frame by frame.")
|
| 60 |
+
for i in range(3): # Simulate 3 frames for brevity
|
| 61 |
+
dummy_frame = np.ones((480, 640, 3), dtype=np.uint8) * np.random.randint(0, 255)
|
| 62 |
+
report = self.process_frame(dummy_frame)
|
| 63 |
+
self.reports.append(report)
|
| 64 |
+
time.sleep(2) # Simulate a delay between frames
|
| 65 |
|
| 66 |
+
def process_frame(self, frame):
|
| 67 |
+
vehicle_number = self.cv.extract_vehicle_number(frame)
|
| 68 |
+
damage = self.cv.detect_damage_severity(frame)
|
| 69 |
+
injury = self.cv.detect_person_injury(frame)
|
|
|
|
| 70 |
q_code, status = self.qsim.evaluate_transaction()
|
| 71 |
|
| 72 |
response = {
|
|
|
|
| 83 |
}
|
| 84 |
|
| 85 |
self.generate_pdf_report(response)
|
| 86 |
+
self.notify_services(response)
|
| 87 |
+
return response
|
| 88 |
|
| 89 |
def generate_pdf_report(self, data):
|
| 90 |
pdf = FPDF()
|
| 91 |
pdf.add_page()
|
| 92 |
pdf.set_font("Arial", size=12)
|
| 93 |
+
pdf.cell(200, 10, txt="Auto Insurance Claim Report", ln=True, align='C')
|
| 94 |
for key, value in data.items():
|
| 95 |
pdf.cell(200, 10, txt=f"{key}: {value}", ln=True)
|
| 96 |
+
filename = f"Claim_Report_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.pdf"
|
| 97 |
pdf.output(filename)
|
| 98 |
+
print(f"[Agent] Report generated: {filename}")
|
| 99 |
+
|
| 100 |
+
def notify_services(self, data):
|
| 101 |
+
print(f"[Hospital] Emergency Level: {data['Injury Level']} → Ambulance dispatched.")
|
| 102 |
+
print(f"[Tow Vehicle] Vehicle #{data['Vehicle Number']} scheduled for towing.")
|
| 103 |
+
if data["IRDA Notified"] == "Yes":
|
| 104 |
+
print("[IRDA] Fraud suspected. Penalty log initiated.")
|
| 105 |
+
|
| 106 |
+
# Gradio UI for No-Ops Simulation
|
| 107 |
+
if __name__ == "__main__":
|
| 108 |
+
agent = InsuranceAgent()
|
| 109 |
+
|
| 110 |
+
def start_agent_simulation():
|
| 111 |
+
agent.reports = [] # Clear previous reports
|
| 112 |
+
agent.simulate_live_camera_feed()
|
| 113 |
+
all_reports_text = ""
|
| 114 |
+
for i, report in enumerate(agent.reports):
|
| 115 |
+
all_reports_text += f"--- Report {i+1} ---\n"
|
| 116 |
+
for key, value in report.items():
|
| 117 |
+
all_reports_text += f"{key}: {value}\n"
|
| 118 |
+
all_reports_text += "\n"
|
| 119 |
+
return all_reports_text
|
| 120 |
+
|
| 121 |
+
with gr.Blocks() as iface:
|
| 122 |
+
gr.Markdown("# No-Ops AI Agent for Road Accident Management")
|
| 123 |
+
with gr.Column():
|
| 124 |
+
start_button = gr.Button("Start Agent Simulation")
|
| 125 |
+
report_output = gr.Textbox(label="Agent Activity and Reports")
|
| 126 |
+
|
| 127 |
+
start_button.click(start_agent_simulation, outputs=report_output)
|
| 128 |
|
| 129 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|