Spaces:
Sleeping
Sleeping
RobertoBarrosoLuque
commited on
Commit
Β·
dd2cb69
1
Parent(s):
554c731
Add key
Browse files- src/app.py +14 -54
src/app.py
CHANGED
|
@@ -16,7 +16,7 @@ from modules.claim_processing import generate_claim_report_pdf
|
|
| 16 |
load_dotenv()
|
| 17 |
|
| 18 |
_FILE_PATH = Path(__file__).parents[1]
|
| 19 |
-
|
| 20 |
|
| 21 |
class ClaimsAssistantApp:
|
| 22 |
def __init__(self):
|
|
@@ -162,16 +162,6 @@ class ClaimsAssistantApp:
|
|
| 162 |
|
| 163 |
gr.Markdown("## βοΈ Configuration")
|
| 164 |
|
| 165 |
-
val = os.getenv("FIREWORKS_API_KEY", "")
|
| 166 |
-
|
| 167 |
-
api_key = gr.Textbox(
|
| 168 |
-
label="Fireworks AI API Key",
|
| 169 |
-
type="password",
|
| 170 |
-
placeholder="Enter your Fireworks AI API key",
|
| 171 |
-
value=val,
|
| 172 |
-
info="Required for AI processing",
|
| 173 |
-
)
|
| 174 |
-
|
| 175 |
gr.Markdown("## π Instructions")
|
| 176 |
gr.Markdown(
|
| 177 |
"""
|
|
@@ -319,29 +309,22 @@ class ClaimsAssistantApp:
|
|
| 319 |
)
|
| 320 |
|
| 321 |
# Event Handlers
|
| 322 |
-
def handle_damage_analysis(image
|
| 323 |
if image is None:
|
| 324 |
return (
|
| 325 |
"β Please upload an image first",
|
| 326 |
gr.update(visible=False),
|
| 327 |
)
|
| 328 |
|
| 329 |
-
if not api_key.strip():
|
| 330 |
-
return (
|
| 331 |
-
"β Please enter your Fireworks AI API key first",
|
| 332 |
-
gr.update(visible=False),
|
| 333 |
-
)
|
| 334 |
-
|
| 335 |
try:
|
| 336 |
# Update status to show processing
|
| 337 |
yield (
|
| 338 |
"π Analyzing damage... Please wait",
|
| 339 |
gr.update(visible=False),
|
| 340 |
)
|
| 341 |
-
|
| 342 |
image_dict = pil_to_base64_dict(image)
|
| 343 |
self.damage_analysis = analyze_damage_image(
|
| 344 |
-
image=image_dict, api_key=
|
| 345 |
)
|
| 346 |
|
| 347 |
yield (
|
|
@@ -362,11 +345,8 @@ class ClaimsAssistantApp:
|
|
| 362 |
with self.transcription_lock:
|
| 363 |
self.live_transcription = text
|
| 364 |
|
| 365 |
-
def initialize_transcription_service(
|
| 366 |
"""Initialize transcription service when audio starts"""
|
| 367 |
-
if not api_key.strip():
|
| 368 |
-
return False
|
| 369 |
-
|
| 370 |
# Always disconnect and cleanup existing service
|
| 371 |
if self.transcription_service:
|
| 372 |
try:
|
|
@@ -380,13 +360,13 @@ class ClaimsAssistantApp:
|
|
| 380 |
self.live_transcription = ""
|
| 381 |
|
| 382 |
# Create fresh service
|
| 383 |
-
self.transcription_service = FireworksTranscription(
|
| 384 |
self.transcription_service.set_callback(live_transcription_callback)
|
| 385 |
|
| 386 |
self.is_recording = True
|
| 387 |
return self.transcription_service._connect()
|
| 388 |
|
| 389 |
-
def process_audio_stream(audio_tuple
|
| 390 |
"""Process incoming audio stream for live transcription"""
|
| 391 |
if not audio_tuple:
|
| 392 |
# Audio stopped - cleanup service but KEEP the transcription text
|
|
@@ -404,8 +384,8 @@ class ClaimsAssistantApp:
|
|
| 404 |
|
| 405 |
# Starting new recording - always initialize fresh
|
| 406 |
if not self.is_recording:
|
| 407 |
-
if not initialize_transcription_service(
|
| 408 |
-
return "β Failed to initialize transcription service.
|
| 409 |
|
| 410 |
try:
|
| 411 |
sample_rate, audio_data = audio_tuple
|
|
@@ -458,7 +438,7 @@ class ClaimsAssistantApp:
|
|
| 458 |
with self.transcription_lock:
|
| 459 |
return self.live_transcription
|
| 460 |
|
| 461 |
-
def handle_incident_processing(
|
| 462 |
"""Process the recorded transcription into structured incident data with function calling"""
|
| 463 |
if not self.live_transcription.strip():
|
| 464 |
return (
|
|
@@ -473,13 +453,6 @@ class ClaimsAssistantApp:
|
|
| 473 |
self.transcription_service = None
|
| 474 |
self.is_recording = False
|
| 475 |
|
| 476 |
-
if not api_key.strip():
|
| 477 |
-
return (
|
| 478 |
-
"β Please enter your Fireworks AI API key first",
|
| 479 |
-
gr.update(visible=False),
|
| 480 |
-
gr.update(visible=False),
|
| 481 |
-
)
|
| 482 |
-
|
| 483 |
try:
|
| 484 |
# Update status
|
| 485 |
yield (
|
|
@@ -490,7 +463,7 @@ class ClaimsAssistantApp:
|
|
| 490 |
|
| 491 |
# Use enhanced Fireworks processing with function calling
|
| 492 |
incident_analysis = process_transcript_description(
|
| 493 |
-
transcript=self.live_transcription, api_key=
|
| 494 |
)
|
| 495 |
|
| 496 |
# Convert Pydantic model to dict for JSON display
|
|
@@ -524,7 +497,7 @@ class ClaimsAssistantApp:
|
|
| 524 |
)
|
| 525 |
return None
|
| 526 |
|
| 527 |
-
def handle_report_generation(
|
| 528 |
"""Generate comprehensive claim report as PDF using AI"""
|
| 529 |
if not self.damage_analysis or not self.incident_data:
|
| 530 |
return (
|
|
@@ -535,15 +508,6 @@ class ClaimsAssistantApp:
|
|
| 535 |
gr.update(open=False),
|
| 536 |
)
|
| 537 |
|
| 538 |
-
if not api_key.strip():
|
| 539 |
-
return (
|
| 540 |
-
"β Please enter your Fireworks AI API key first",
|
| 541 |
-
"<p style='text-align: center; color: gray;'>PDF report will appear here after generation</p>",
|
| 542 |
-
gr.update(visible=False),
|
| 543 |
-
gr.update(visible=False),
|
| 544 |
-
gr.update(open=False),
|
| 545 |
-
)
|
| 546 |
-
|
| 547 |
try:
|
| 548 |
# Show processing status
|
| 549 |
yield (
|
|
@@ -638,13 +602,11 @@ class ClaimsAssistantApp:
|
|
| 638 |
# Wire up the events
|
| 639 |
analyze_btn.click(
|
| 640 |
fn=handle_damage_analysis,
|
| 641 |
-
inputs=[image_input
|
| 642 |
outputs=[damage_status, damage_results],
|
| 643 |
)
|
| 644 |
|
| 645 |
-
|
| 646 |
-
# Users can use the clear button if needed
|
| 647 |
-
|
| 648 |
# Add clear transcription button handler
|
| 649 |
def clear_transcription():
|
| 650 |
"""Manually clear the transcription text"""
|
|
@@ -668,7 +630,7 @@ class ClaimsAssistantApp:
|
|
| 668 |
# Handle streaming audio for live transcription
|
| 669 |
audio_input.stream(
|
| 670 |
fn=process_audio_stream,
|
| 671 |
-
inputs=[audio_input
|
| 672 |
outputs=[transcription_display],
|
| 673 |
show_progress="hidden",
|
| 674 |
)
|
|
@@ -676,13 +638,11 @@ class ClaimsAssistantApp:
|
|
| 676 |
# Updated to include function calls display
|
| 677 |
process_incident_btn.click(
|
| 678 |
fn=handle_incident_processing,
|
| 679 |
-
inputs=[api_key],
|
| 680 |
outputs=[incident_status, function_calls_display, incident_results],
|
| 681 |
)
|
| 682 |
|
| 683 |
generate_report_btn.click(
|
| 684 |
fn=handle_report_generation,
|
| 685 |
-
inputs=[api_key],
|
| 686 |
outputs=[
|
| 687 |
report_status,
|
| 688 |
pdf_viewer,
|
|
|
|
| 16 |
load_dotenv()
|
| 17 |
|
| 18 |
_FILE_PATH = Path(__file__).parents[1]
|
| 19 |
+
API_KEY = os.getenv("FIREWORKS_API_KEY")
|
| 20 |
|
| 21 |
class ClaimsAssistantApp:
|
| 22 |
def __init__(self):
|
|
|
|
| 162 |
|
| 163 |
gr.Markdown("## βοΈ Configuration")
|
| 164 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
gr.Markdown("## π Instructions")
|
| 166 |
gr.Markdown(
|
| 167 |
"""
|
|
|
|
| 309 |
)
|
| 310 |
|
| 311 |
# Event Handlers
|
| 312 |
+
def handle_damage_analysis(image):
|
| 313 |
if image is None:
|
| 314 |
return (
|
| 315 |
"β Please upload an image first",
|
| 316 |
gr.update(visible=False),
|
| 317 |
)
|
| 318 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 319 |
try:
|
| 320 |
# Update status to show processing
|
| 321 |
yield (
|
| 322 |
"π Analyzing damage... Please wait",
|
| 323 |
gr.update(visible=False),
|
| 324 |
)
|
|
|
|
| 325 |
image_dict = pil_to_base64_dict(image)
|
| 326 |
self.damage_analysis = analyze_damage_image(
|
| 327 |
+
image=image_dict, api_key=API_KEY
|
| 328 |
)
|
| 329 |
|
| 330 |
yield (
|
|
|
|
| 345 |
with self.transcription_lock:
|
| 346 |
self.live_transcription = text
|
| 347 |
|
| 348 |
+
def initialize_transcription_service():
|
| 349 |
"""Initialize transcription service when audio starts"""
|
|
|
|
|
|
|
|
|
|
| 350 |
# Always disconnect and cleanup existing service
|
| 351 |
if self.transcription_service:
|
| 352 |
try:
|
|
|
|
| 360 |
self.live_transcription = ""
|
| 361 |
|
| 362 |
# Create fresh service
|
| 363 |
+
self.transcription_service = FireworksTranscription(API_KEY)
|
| 364 |
self.transcription_service.set_callback(live_transcription_callback)
|
| 365 |
|
| 366 |
self.is_recording = True
|
| 367 |
return self.transcription_service._connect()
|
| 368 |
|
| 369 |
+
def process_audio_stream(audio_tuple):
|
| 370 |
"""Process incoming audio stream for live transcription"""
|
| 371 |
if not audio_tuple:
|
| 372 |
# Audio stopped - cleanup service but KEEP the transcription text
|
|
|
|
| 384 |
|
| 385 |
# Starting new recording - always initialize fresh
|
| 386 |
if not self.is_recording:
|
| 387 |
+
if not initialize_transcription_service():
|
| 388 |
+
return "β Failed to initialize transcription service. Please try again later."
|
| 389 |
|
| 390 |
try:
|
| 391 |
sample_rate, audio_data = audio_tuple
|
|
|
|
| 438 |
with self.transcription_lock:
|
| 439 |
return self.live_transcription
|
| 440 |
|
| 441 |
+
def handle_incident_processing():
|
| 442 |
"""Process the recorded transcription into structured incident data with function calling"""
|
| 443 |
if not self.live_transcription.strip():
|
| 444 |
return (
|
|
|
|
| 453 |
self.transcription_service = None
|
| 454 |
self.is_recording = False
|
| 455 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 456 |
try:
|
| 457 |
# Update status
|
| 458 |
yield (
|
|
|
|
| 463 |
|
| 464 |
# Use enhanced Fireworks processing with function calling
|
| 465 |
incident_analysis = process_transcript_description(
|
| 466 |
+
transcript=self.live_transcription, api_key=API_KEY
|
| 467 |
)
|
| 468 |
|
| 469 |
# Convert Pydantic model to dict for JSON display
|
|
|
|
| 497 |
)
|
| 498 |
return None
|
| 499 |
|
| 500 |
+
def handle_report_generation():
|
| 501 |
"""Generate comprehensive claim report as PDF using AI"""
|
| 502 |
if not self.damage_analysis or not self.incident_data:
|
| 503 |
return (
|
|
|
|
| 508 |
gr.update(open=False),
|
| 509 |
)
|
| 510 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 511 |
try:
|
| 512 |
# Show processing status
|
| 513 |
yield (
|
|
|
|
| 602 |
# Wire up the events
|
| 603 |
analyze_btn.click(
|
| 604 |
fn=handle_damage_analysis,
|
| 605 |
+
inputs=[image_input],
|
| 606 |
outputs=[damage_status, damage_results],
|
| 607 |
)
|
| 608 |
|
| 609 |
+
|
|
|
|
|
|
|
| 610 |
# Add clear transcription button handler
|
| 611 |
def clear_transcription():
|
| 612 |
"""Manually clear the transcription text"""
|
|
|
|
| 630 |
# Handle streaming audio for live transcription
|
| 631 |
audio_input.stream(
|
| 632 |
fn=process_audio_stream,
|
| 633 |
+
inputs=[audio_input],
|
| 634 |
outputs=[transcription_display],
|
| 635 |
show_progress="hidden",
|
| 636 |
)
|
|
|
|
| 638 |
# Updated to include function calls display
|
| 639 |
process_incident_btn.click(
|
| 640 |
fn=handle_incident_processing,
|
|
|
|
| 641 |
outputs=[incident_status, function_calls_display, incident_results],
|
| 642 |
)
|
| 643 |
|
| 644 |
generate_report_btn.click(
|
| 645 |
fn=handle_report_generation,
|
|
|
|
| 646 |
outputs=[
|
| 647 |
report_status,
|
| 648 |
pdf_viewer,
|