Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
# -*- coding: utf-8 -*-
|
| 2 |
"""
|
| 3 |
-
Road Buddy -
|
| 4 |
Powered by Vision-Language Model with LoRA adapter
|
| 5 |
Analyzes road situations, explains traffic signs, and provides transportation law advice
|
| 6 |
"""
|
|
@@ -20,23 +20,15 @@ from peft import PeftModel
|
|
| 20 |
BASE_MODEL = "unsloth/Qwen3-VL-4B-Instruct"
|
| 21 |
ADAPTER_NAME = "hanguyen1907/Qwen3-4B-SFT"
|
| 22 |
|
| 23 |
-
SYSTEM_PROMPT = """
|
| 24 |
-
1. PhΓ’n tΓch tΓ¬nh huα»ng giao thΓ΄ng trΓͺn ΔΖ°α»ng
|
| 25 |
-
2. NhαΊn diα»n vΓ giαΊ£i thΓch biα»n bΓ‘o giao thΓ΄ng
|
| 26 |
-
3. TΖ° vαΊ₯n luαΊt giao thΓ΄ng ΔΖ°α»ng bα» Viα»t Nam
|
| 27 |
-
4. HΖ°α»ng dαΊ«n xα» lΓ½ cΓ‘c tΓ¬nh huα»ng trΓͺn ΔΖ°α»ng
|
| 28 |
-
|
| 29 |
-
HΓ£y trαΊ£ lα»i chi tiαΊΏt, dα»
hiα»u vΓ ΔΖ°a ra lα»i khuyΓͺn an toΓ n cho ngΖ°α»i dΓΉng.
|
| 30 |
-
NαΊΏu nhΓ¬n thαΊ₯y hΓ¬nh αΊ£nh/video vα» giao thΓ΄ng, hΓ£y phΓ’n tΓch kα»Ή vΓ ΔΖ°a ra nhαΊn xΓ©t.
|
| 31 |
-
|
| 32 |
-
You are "Road Buddy" - an intelligent traffic assistant specializing in:
|
| 33 |
1. Analyzing traffic situations on the road
|
| 34 |
2. Recognizing and explaining traffic signs
|
| 35 |
-
3. Advising on
|
| 36 |
-
4. Guiding users through road situations
|
| 37 |
|
| 38 |
-
Provide detailed, easy-to-understand answers and safe advice.
|
| 39 |
-
If you see traffic images
|
|
|
|
| 40 |
|
| 41 |
# ====================== Global Model ======================
|
| 42 |
MODEL = None
|
|
@@ -288,7 +280,7 @@ def process_video_chat(
|
|
| 288 |
|
| 289 |
# Default message if empty
|
| 290 |
if not message.strip():
|
| 291 |
-
message = "
|
| 292 |
|
| 293 |
# Convert history
|
| 294 |
history_messages = []
|
|
@@ -342,7 +334,7 @@ def process_image_chat(
|
|
| 342 |
|
| 343 |
# Default message if empty
|
| 344 |
if not message.strip():
|
| 345 |
-
message = "
|
| 346 |
|
| 347 |
# Convert history
|
| 348 |
history_messages = []
|
|
@@ -379,20 +371,20 @@ def clear_chat():
|
|
| 379 |
# ====================== Gradio Interface ======================
|
| 380 |
|
| 381 |
EXAMPLE_QUESTIONS = [
|
| 382 |
-
"Biα»n bΓ‘o nΓ y cΓ³ Γ½ nghΔ©a gΓ¬?",
|
| 383 |
"What does this traffic sign mean?",
|
| 384 |
-
"
|
| 385 |
-
"
|
| 386 |
-
"
|
| 387 |
-
"
|
| 388 |
-
"
|
| 389 |
-
"
|
|
|
|
| 390 |
]
|
| 391 |
|
| 392 |
|
| 393 |
def create_demo():
|
| 394 |
with gr.Blocks(
|
| 395 |
-
title="Road Buddy -
|
| 396 |
theme=gr.themes.Soft(),
|
| 397 |
css="""
|
| 398 |
.chat-container { min-height: 400px; }
|
|
@@ -401,13 +393,13 @@ def create_demo():
|
|
| 401 |
) as demo:
|
| 402 |
|
| 403 |
gr.Markdown("""
|
| 404 |
-
# π Road Buddy -
|
| 405 |
|
| 406 |
-
**
|
| 407 |
-
- π¦ **
|
| 408 |
-
- πͺ§ **
|
| 409 |
-
- π **
|
| 410 |
-
- π‘ **
|
| 411 |
|
| 412 |
---
|
| 413 |
""")
|
|
@@ -418,7 +410,7 @@ def create_demo():
|
|
| 418 |
with gr.Tabs():
|
| 419 |
# =================== TEXT CHAT TAB ===================
|
| 420 |
with gr.TabItem("π¬ Chat"):
|
| 421 |
-
gr.Markdown("###
|
| 422 |
|
| 423 |
chatbot = gr.Chatbot(
|
| 424 |
label="Road Buddy Chat",
|
|
@@ -428,14 +420,14 @@ def create_demo():
|
|
| 428 |
|
| 429 |
with gr.Row():
|
| 430 |
msg_input = gr.Textbox(
|
| 431 |
-
label="
|
| 432 |
-
placeholder="
|
| 433 |
lines=2,
|
| 434 |
scale=4,
|
| 435 |
)
|
| 436 |
-
send_btn = gr.Button("
|
| 437 |
|
| 438 |
-
gr.Markdown("**
|
| 439 |
with gr.Row():
|
| 440 |
for q in EXAMPLE_QUESTIONS[:4]:
|
| 441 |
gr.Button(q, size="sm").click(
|
|
@@ -448,10 +440,10 @@ def create_demo():
|
|
| 448 |
)
|
| 449 |
|
| 450 |
with gr.Accordion("βοΈ Settings", open=False):
|
| 451 |
-
max_tokens_chat = gr.Slider(128, 1024, value=512, step=64, label="Max
|
| 452 |
temp_chat = gr.Slider(0.0, 1.0, value=0.7, step=0.1, label="Temperature")
|
| 453 |
|
| 454 |
-
clear_btn = gr.Button("ποΈ
|
| 455 |
|
| 456 |
# Chat handlers
|
| 457 |
send_btn.click(
|
|
@@ -467,8 +459,8 @@ def create_demo():
|
|
| 467 |
clear_btn.click(clear_chat, outputs=[chatbot, chat_state])
|
| 468 |
|
| 469 |
# =================== VIDEO TAB ===================
|
| 470 |
-
with gr.TabItem("πΉ
|
| 471 |
-
gr.Markdown("### Upload
|
| 472 |
|
| 473 |
chatbot_v = gr.Chatbot(label="Road Buddy", height=350)
|
| 474 |
|
|
@@ -477,29 +469,29 @@ def create_demo():
|
|
| 477 |
video_input = gr.Video(label="Upload Video")
|
| 478 |
num_frames = gr.Slider(
|
| 479 |
minimum=1, maximum=8, value=4, step=1,
|
| 480 |
-
label="
|
| 481 |
)
|
| 482 |
frame_gallery = gr.Gallery(
|
| 483 |
-
label="
|
| 484 |
columns=4,
|
| 485 |
height=150
|
| 486 |
)
|
| 487 |
|
| 488 |
with gr.Column(scale=1):
|
| 489 |
question_v = gr.Textbox(
|
| 490 |
-
label="
|
| 491 |
-
placeholder="
|
| 492 |
lines=3,
|
| 493 |
)
|
| 494 |
with gr.Accordion("βοΈ Settings", open=False):
|
| 495 |
-
max_tokens_v = gr.Slider(128, 1024, value=512, step=64, label="Max
|
| 496 |
temp_v = gr.Slider(0.0, 1.0, value=0.7, step=0.1, label="Temperature")
|
| 497 |
|
| 498 |
status_v = gr.Markdown("")
|
| 499 |
|
| 500 |
with gr.Row():
|
| 501 |
-
analyze_btn_v = gr.Button("π
|
| 502 |
-
clear_btn_v = gr.Button("ποΈ
|
| 503 |
|
| 504 |
chat_state_v = gr.State([])
|
| 505 |
|
|
@@ -514,36 +506,36 @@ def create_demo():
|
|
| 514 |
)
|
| 515 |
|
| 516 |
# =================== IMAGE TAB ===================
|
| 517 |
-
with gr.TabItem("πΌοΈ
|
| 518 |
-
gr.Markdown("### Upload
|
| 519 |
|
| 520 |
chatbot_i = gr.Chatbot(label="Road Buddy", height=350)
|
| 521 |
|
| 522 |
with gr.Row():
|
| 523 |
with gr.Column(scale=1):
|
| 524 |
-
gr.Markdown("**Upload 1-4
|
| 525 |
with gr.Row():
|
| 526 |
-
img1 = gr.Image(label="
|
| 527 |
-
img2 = gr.Image(label="
|
| 528 |
with gr.Row():
|
| 529 |
-
img3 = gr.Image(label="
|
| 530 |
-
img4 = gr.Image(label="
|
| 531 |
|
| 532 |
with gr.Column(scale=1):
|
| 533 |
question_i = gr.Textbox(
|
| 534 |
-
label="
|
| 535 |
-
placeholder="
|
| 536 |
lines=3,
|
| 537 |
)
|
| 538 |
with gr.Accordion("βοΈ Settings", open=False):
|
| 539 |
-
max_tokens_i = gr.Slider(128, 1024, value=512, step=64, label="Max
|
| 540 |
temp_i = gr.Slider(0.0, 1.0, value=0.7, step=0.1, label="Temperature")
|
| 541 |
|
| 542 |
status_i = gr.Markdown("")
|
| 543 |
|
| 544 |
with gr.Row():
|
| 545 |
-
analyze_btn_i = gr.Button("π
|
| 546 |
-
clear_btn_i = gr.Button("ποΈ
|
| 547 |
|
| 548 |
chat_state_i = gr.State([])
|
| 549 |
|
|
@@ -558,38 +550,53 @@ def create_demo():
|
|
| 558 |
)
|
| 559 |
|
| 560 |
# =================== QUICK REFERENCE TAB ===================
|
| 561 |
-
with gr.TabItem("π
|
| 562 |
gr.Markdown("""
|
| 563 |
-
###
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 564 |
|
| 565 |
-
####
|
| 566 |
-
-
|
| 567 |
-
-
|
| 568 |
-
-
|
|
|
|
|
|
|
| 569 |
|
| 570 |
-
####
|
| 571 |
-
-
|
| 572 |
-
-
|
|
|
|
|
|
|
|
|
|
| 573 |
|
| 574 |
-
####
|
| 575 |
-
-
|
| 576 |
-
-
|
|
|
|
|
|
|
| 577 |
|
| 578 |
-
#### π±
|
| 579 |
-
-
|
| 580 |
-
-
|
|
|
|
| 581 |
|
| 582 |
---
|
| 583 |
-
*
|
| 584 |
""")
|
| 585 |
|
| 586 |
gr.Markdown(f"""
|
| 587 |
---
|
| 588 |
-
### βΉοΈ
|
| 589 |
- **Base Model:** [{BASE_MODEL}](https://huggingface.co/{BASE_MODEL})
|
| 590 |
- **LoRA Adapter:** [{ADAPTER_NAME}](https://huggingface.co/{ADAPTER_NAME})
|
| 591 |
|
| 592 |
-
*Road Buddy
|
| 593 |
""")
|
| 594 |
|
| 595 |
return demo
|
|
|
|
| 1 |
# -*- coding: utf-8 -*-
|
| 2 |
"""
|
| 3 |
+
Road Buddy - Traffic Assistant
|
| 4 |
Powered by Vision-Language Model with LoRA adapter
|
| 5 |
Analyzes road situations, explains traffic signs, and provides transportation law advice
|
| 6 |
"""
|
|
|
|
| 20 |
BASE_MODEL = "unsloth/Qwen3-VL-4B-Instruct"
|
| 21 |
ADAPTER_NAME = "hanguyen1907/Qwen3-4B-SFT"
|
| 22 |
|
| 23 |
+
SYSTEM_PROMPT = """You are "Road Buddy" - an intelligent traffic assistant specializing in:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
1. Analyzing traffic situations on the road
|
| 25 |
2. Recognizing and explaining traffic signs
|
| 26 |
+
3. Advising on road traffic laws and regulations
|
| 27 |
+
4. Guiding users through various road situations
|
| 28 |
|
| 29 |
+
Provide detailed, easy-to-understand answers and safe driving advice.
|
| 30 |
+
If you see traffic images or videos, analyze them carefully and provide observations.
|
| 31 |
+
Always prioritize safety in your recommendations."""
|
| 32 |
|
| 33 |
# ====================== Global Model ======================
|
| 34 |
MODEL = None
|
|
|
|
| 280 |
|
| 281 |
# Default message if empty
|
| 282 |
if not message.strip():
|
| 283 |
+
message = "Analyze the traffic situation in this video and provide your observations."
|
| 284 |
|
| 285 |
# Convert history
|
| 286 |
history_messages = []
|
|
|
|
| 334 |
|
| 335 |
# Default message if empty
|
| 336 |
if not message.strip():
|
| 337 |
+
message = "Analyze this traffic image and provide your observations."
|
| 338 |
|
| 339 |
# Convert history
|
| 340 |
history_messages = []
|
|
|
|
| 371 |
# ====================== Gradio Interface ======================
|
| 372 |
|
| 373 |
EXAMPLE_QUESTIONS = [
|
|
|
|
| 374 |
"What does this traffic sign mean?",
|
| 375 |
+
"How should I handle this situation?",
|
| 376 |
+
"Who has the right of way here?",
|
| 377 |
+
"Is this a traffic violation?",
|
| 378 |
+
"What's the speed limit on this road?",
|
| 379 |
+
"What are the rules for overtaking?",
|
| 380 |
+
"When is it legal to make a U-turn?",
|
| 381 |
+
"What should I do at this intersection?",
|
| 382 |
]
|
| 383 |
|
| 384 |
|
| 385 |
def create_demo():
|
| 386 |
with gr.Blocks(
|
| 387 |
+
title="Road Buddy - Traffic Assistant",
|
| 388 |
theme=gr.themes.Soft(),
|
| 389 |
css="""
|
| 390 |
.chat-container { min-height: 400px; }
|
|
|
|
| 393 |
) as demo:
|
| 394 |
|
| 395 |
gr.Markdown("""
|
| 396 |
+
# π Road Buddy - Your Intelligent Traffic Assistant
|
| 397 |
|
| 398 |
+
**Welcome!** Road Buddy can help you with:
|
| 399 |
+
- π¦ **Analyzing traffic situations** from videos and images
|
| 400 |
+
- πͺ§ **Recognizing & explaining traffic signs**
|
| 401 |
+
- π **Advising on traffic laws** and regulations
|
| 402 |
+
- π‘ **Guiding you through** various road situations
|
| 403 |
|
| 404 |
---
|
| 405 |
""")
|
|
|
|
| 410 |
with gr.Tabs():
|
| 411 |
# =================== TEXT CHAT TAB ===================
|
| 412 |
with gr.TabItem("π¬ Chat"):
|
| 413 |
+
gr.Markdown("### Ask about traffic laws, road rules, driving tips...")
|
| 414 |
|
| 415 |
chatbot = gr.Chatbot(
|
| 416 |
label="Road Buddy Chat",
|
|
|
|
| 420 |
|
| 421 |
with gr.Row():
|
| 422 |
msg_input = gr.Textbox(
|
| 423 |
+
label="Your Message",
|
| 424 |
+
placeholder="Ask about traffic laws, how to handle situations...",
|
| 425 |
lines=2,
|
| 426 |
scale=4,
|
| 427 |
)
|
| 428 |
+
send_btn = gr.Button("Send π€", variant="primary", scale=1)
|
| 429 |
|
| 430 |
+
gr.Markdown("**Example Questions:**")
|
| 431 |
with gr.Row():
|
| 432 |
for q in EXAMPLE_QUESTIONS[:4]:
|
| 433 |
gr.Button(q, size="sm").click(
|
|
|
|
| 440 |
)
|
| 441 |
|
| 442 |
with gr.Accordion("βοΈ Settings", open=False):
|
| 443 |
+
max_tokens_chat = gr.Slider(128, 1024, value=512, step=64, label="Max Tokens")
|
| 444 |
temp_chat = gr.Slider(0.0, 1.0, value=0.7, step=0.1, label="Temperature")
|
| 445 |
|
| 446 |
+
clear_btn = gr.Button("ποΈ Clear Conversation")
|
| 447 |
|
| 448 |
# Chat handlers
|
| 449 |
send_btn.click(
|
|
|
|
| 459 |
clear_btn.click(clear_chat, outputs=[chatbot, chat_state])
|
| 460 |
|
| 461 |
# =================== VIDEO TAB ===================
|
| 462 |
+
with gr.TabItem("πΉ Video Analysis"):
|
| 463 |
+
gr.Markdown("### Upload a traffic video for analysis")
|
| 464 |
|
| 465 |
chatbot_v = gr.Chatbot(label="Road Buddy", height=350)
|
| 466 |
|
|
|
|
| 469 |
video_input = gr.Video(label="Upload Video")
|
| 470 |
num_frames = gr.Slider(
|
| 471 |
minimum=1, maximum=8, value=4, step=1,
|
| 472 |
+
label="Number of Frames to Extract"
|
| 473 |
)
|
| 474 |
frame_gallery = gr.Gallery(
|
| 475 |
+
label="Extracted Frames",
|
| 476 |
columns=4,
|
| 477 |
height=150
|
| 478 |
)
|
| 479 |
|
| 480 |
with gr.Column(scale=1):
|
| 481 |
question_v = gr.Textbox(
|
| 482 |
+
label="Question about the video",
|
| 483 |
+
placeholder="E.g., Analyze this traffic situation...",
|
| 484 |
lines=3,
|
| 485 |
)
|
| 486 |
with gr.Accordion("βοΈ Settings", open=False):
|
| 487 |
+
max_tokens_v = gr.Slider(128, 1024, value=512, step=64, label="Max Tokens")
|
| 488 |
temp_v = gr.Slider(0.0, 1.0, value=0.7, step=0.1, label="Temperature")
|
| 489 |
|
| 490 |
status_v = gr.Markdown("")
|
| 491 |
|
| 492 |
with gr.Row():
|
| 493 |
+
analyze_btn_v = gr.Button("π Analyze Video", variant="primary")
|
| 494 |
+
clear_btn_v = gr.Button("ποΈ Clear")
|
| 495 |
|
| 496 |
chat_state_v = gr.State([])
|
| 497 |
|
|
|
|
| 506 |
)
|
| 507 |
|
| 508 |
# =================== IMAGE TAB ===================
|
| 509 |
+
with gr.TabItem("πΌοΈ Image Analysis"):
|
| 510 |
+
gr.Markdown("### Upload traffic images (signs, road situations, etc.)")
|
| 511 |
|
| 512 |
chatbot_i = gr.Chatbot(label="Road Buddy", height=350)
|
| 513 |
|
| 514 |
with gr.Row():
|
| 515 |
with gr.Column(scale=1):
|
| 516 |
+
gr.Markdown("**Upload 1-4 images:**")
|
| 517 |
with gr.Row():
|
| 518 |
+
img1 = gr.Image(label="Image 1", type="pil")
|
| 519 |
+
img2 = gr.Image(label="Image 2", type="pil")
|
| 520 |
with gr.Row():
|
| 521 |
+
img3 = gr.Image(label="Image 3", type="pil")
|
| 522 |
+
img4 = gr.Image(label="Image 4", type="pil")
|
| 523 |
|
| 524 |
with gr.Column(scale=1):
|
| 525 |
question_i = gr.Textbox(
|
| 526 |
+
label="Question about the image(s)",
|
| 527 |
+
placeholder="E.g., What does this sign mean?",
|
| 528 |
lines=3,
|
| 529 |
)
|
| 530 |
with gr.Accordion("βοΈ Settings", open=False):
|
| 531 |
+
max_tokens_i = gr.Slider(128, 1024, value=512, step=64, label="Max Tokens")
|
| 532 |
temp_i = gr.Slider(0.0, 1.0, value=0.7, step=0.1, label="Temperature")
|
| 533 |
|
| 534 |
status_i = gr.Markdown("")
|
| 535 |
|
| 536 |
with gr.Row():
|
| 537 |
+
analyze_btn_i = gr.Button("π Analyze Image", variant="primary")
|
| 538 |
+
clear_btn_i = gr.Button("ποΈ Clear")
|
| 539 |
|
| 540 |
chat_state_i = gr.State([])
|
| 541 |
|
|
|
|
| 550 |
)
|
| 551 |
|
| 552 |
# =================== QUICK REFERENCE TAB ===================
|
| 553 |
+
with gr.TabItem("π Quick Reference"):
|
| 554 |
gr.Markdown("""
|
| 555 |
+
### Common Traffic Rules & Guidelines
|
| 556 |
+
|
| 557 |
+
#### π¦ Traffic Light Basics:
|
| 558 |
+
- **Red**: Stop completely behind the stop line
|
| 559 |
+
- **Yellow**: Prepare to stop (don't accelerate to beat it)
|
| 560 |
+
- **Green**: Proceed if the intersection is clear
|
| 561 |
+
- **Flashing Red**: Treat as a stop sign
|
| 562 |
+
- **Flashing Yellow**: Proceed with caution
|
| 563 |
|
| 564 |
+
#### πͺ§ Common Sign Types:
|
| 565 |
+
- **Red Circle**: Prohibition (No entry, No parking, etc.)
|
| 566 |
+
- **Red Triangle**: Warning (Danger ahead)
|
| 567 |
+
- **Blue Circle**: Mandatory (Must follow)
|
| 568 |
+
- **Blue Rectangle**: Information
|
| 569 |
+
- **Green**: Direction and highway signs
|
| 570 |
|
| 571 |
+
#### π Right of Way Rules:
|
| 572 |
+
- Yield to vehicles already in the intersection
|
| 573 |
+
- At 4-way stops: First to arrive, first to go
|
| 574 |
+
- When arriving simultaneously: Yield to the right
|
| 575 |
+
- Always yield to emergency vehicles
|
| 576 |
+
- Pedestrians have right of way at crosswalks
|
| 577 |
|
| 578 |
+
#### π£οΈ Lane Discipline:
|
| 579 |
+
- Keep right except when passing
|
| 580 |
+
- Use turn signals before changing lanes
|
| 581 |
+
- Check mirrors and blind spots
|
| 582 |
+
- Don't weave between lanes
|
| 583 |
|
| 584 |
+
#### π± Distracted Driving:
|
| 585 |
+
- No handheld phone use while driving
|
| 586 |
+
- Pull over safely if you need to use your phone
|
| 587 |
+
- Use hands-free devices if necessary
|
| 588 |
|
| 589 |
---
|
| 590 |
+
*Note: Traffic laws vary by location. Always follow local regulations. Ask Road Buddy for specific advice!*
|
| 591 |
""")
|
| 592 |
|
| 593 |
gr.Markdown(f"""
|
| 594 |
---
|
| 595 |
+
### βΉοΈ About
|
| 596 |
- **Base Model:** [{BASE_MODEL}](https://huggingface.co/{BASE_MODEL})
|
| 597 |
- **LoRA Adapter:** [{ADAPTER_NAME}](https://huggingface.co/{ADAPTER_NAME})
|
| 598 |
|
| 599 |
+
*Road Buddy provides general information. Please verify with official sources when needed.*
|
| 600 |
""")
|
| 601 |
|
| 602 |
return demo
|