hanguyen1907 commited on
Commit
2183be9
Β·
verified Β·
1 Parent(s): 5624bf4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -77
app.py CHANGED
@@ -1,6 +1,6 @@
1
  # -*- coding: utf-8 -*-
2
  """
3
- Road Buddy - Vietnamese 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,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 = """BαΊ‘n lΓ  "Road Buddy" - trợ lΓ½ giao thΓ΄ng thΓ΄ng minh chuyΓͺn về:
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 Vietnamese road traffic laws
36
- 4. Guiding users through road situations
37
 
38
- Provide detailed, easy-to-understand answers and safe advice.
39
- If you see traffic images/videos, analyze carefully and provide observations."""
 
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 = "Hãy phÒn tích tình huống giao thông trong video này và đưa ra nhận xét."
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 = "Hãy phÒn tích hình ảnh giao thông này và đưa ra nhận xét."
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
- "TΓ¬nh huα»‘ng nΓ y tΓ΄i nΓͺn xα»­ lΓ½ nhΖ° thαΊΏ nΓ o?",
385
- "Ai cΓ³ quyền Ζ°u tiΓͺn trong tΓ¬nh huα»‘ng nΓ y?",
386
- "ĐÒy có phải là vi phẑm luật giao thông không?",
387
- "Tα»‘c Δ‘α»™ tα»‘i Δ‘a cho phΓ©p trΓͺn đường nΓ y lΓ  bao nhiΓͺu?",
388
- "LuαΊ­t giao thΓ΄ng quy Δ‘α»‹nh gΓ¬ về việc vượt xe?",
389
- "Khi nΓ o được phΓ©p quay Δ‘αΊ§u xe?",
 
390
  ]
391
 
392
 
393
  def create_demo():
394
  with gr.Blocks(
395
- title="Road Buddy - Vietnamese Traffic Assistant",
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 - Trợ LΓ½ Giao ThΓ΄ng ThΓ΄ng Minh
405
 
406
- **ChΓ o mα»«ng bαΊ‘n!** Road Buddy cΓ³ thể giΓΊp bαΊ‘n:
407
- - 🚦 **PhΓ’n tΓ­ch tΓ¬nh huα»‘ng giao thΓ΄ng** tα»« video/hΓ¬nh αΊ£nh
408
- - πŸͺ§ **NhαΊ­n diện & giαΊ£i thΓ­ch biển bΓ‘o** giao thΓ΄ng
409
- - πŸ“œ **TΖ° vαΊ₯n luαΊ­t giao thΓ΄ng** đường bα»™ Việt Nam
410
- - πŸ’‘ **HΖ°α»›ng dαΊ«n xα»­ lΓ½** cΓ‘c tΓ¬nh huα»‘ng trΓͺn đường
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("### Hỏi về luαΊ­t giao thΓ΄ng, quy tαΊ―c đường bα»™...")
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="Tin nhαΊ―n cα»§a bαΊ‘n",
432
- placeholder="Hỏi về luαΊ­t giao thΓ΄ng, cΓ‘ch xα»­ lΓ½ tΓ¬nh huα»‘ng...",
433
  lines=2,
434
  scale=4,
435
  )
436
- send_btn = gr.Button("Gα»­i πŸ“€", variant="primary", scale=1)
437
 
438
- gr.Markdown("**CÒu hỏi mẫu:**")
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 tokens")
452
  temp_chat = gr.Slider(0.0, 1.0, value=0.7, step=0.1, label="Temperature")
453
 
454
- clear_btn = gr.Button("πŸ—‘οΈ XΓ³a cuα»™c trΓ² chuyện")
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("πŸ“Ή PhΓ’n TΓ­ch Video"):
471
- gr.Markdown("### Upload video giao thΓ΄ng để phΓ’n tΓ­ch")
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="Sα»‘ frame trΓ­ch xuαΊ₯t"
481
  )
482
  frame_gallery = gr.Gallery(
483
- label="Frames trΓ­ch xuαΊ₯t",
484
  columns=4,
485
  height=150
486
  )
487
 
488
  with gr.Column(scale=1):
489
  question_v = gr.Textbox(
490
- label="CÒu hỏi về video",
491
- placeholder="VD: PhÒn tích tình huống này...",
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 tokens")
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("πŸ” PhΓ’n TΓ­ch Video", variant="primary")
502
- clear_btn_v = gr.Button("πŸ—‘οΈ XΓ³a")
503
 
504
  chat_state_v = gr.State([])
505
 
@@ -514,36 +506,36 @@ def create_demo():
514
  )
515
 
516
  # =================== IMAGE TAB ===================
517
- with gr.TabItem("πŸ–ΌοΈ PhΓ’n TΓ­ch HΓ¬nh αΊ’nh"):
518
- gr.Markdown("### Upload hΓ¬nh αΊ£nh giao thΓ΄ng (biển bΓ‘o, tΓ¬nh huα»‘ng đường...)")
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 hình ảnh:**")
525
  with gr.Row():
526
- img1 = gr.Image(label="αΊ’nh 1", type="pil")
527
- img2 = gr.Image(label="αΊ’nh 2", type="pil")
528
  with gr.Row():
529
- img3 = gr.Image(label="αΊ’nh 3", type="pil")
530
- img4 = gr.Image(label="αΊ’nh 4", type="pil")
531
 
532
  with gr.Column(scale=1):
533
  question_i = gr.Textbox(
534
- label="CÒu hỏi về hình ảnh",
535
- placeholder="VD: Biển bΓ‘o nΓ y cΓ³ Γ½ nghΔ©a gΓ¬?",
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 tokens")
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("πŸ” PhΓ’n TΓ­ch HΓ¬nh αΊ’nh", variant="primary")
546
- clear_btn_i = gr.Button("πŸ—‘οΈ XΓ³a")
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("πŸ“š Tra Cα»©u Nhanh"):
562
  gr.Markdown("""
563
- ### Mα»™t sα»‘ quy Δ‘α»‹nh giao thΓ΄ng cΖ‘ bαΊ£n
 
 
 
 
 
 
 
564
 
565
- #### πŸš— Tα»‘c Δ‘α»™ tα»‘i Δ‘a trong khu vα»±c Δ‘Γ΄ng dΓ’n cΖ°:
566
- - Xe mΓ‘y, xe gαΊ―n mΓ‘y: **40 km/h**
567
- - Γ” tΓ΄ con, xe tαΊ£i < 3.5 tαΊ₯n: **50 km/h**
568
- - Γ” tΓ΄ khΓ‘ch, xe tαΊ£i > 3.5 tαΊ₯n: **40 km/h**
 
 
569
 
570
- #### πŸ›£οΈ Tα»‘c Δ‘α»™ tα»‘i Δ‘a ngoΓ i khu vα»±c Δ‘Γ΄ng dΓ’n cΖ°:
571
- - Đường Δ‘Γ΄i cΓ³ dαΊ£i phΓ’n cΓ‘ch: **90 km/h** (Γ΄ tΓ΄ con), **80 km/h** (xe mΓ‘y)
572
- - Đường 2 chiều không có dải phÒn cÑch: **80 km/h** (ô tô), **70 km/h** (xe mÑy)
 
 
 
573
 
574
- #### 🍺 Nα»“ng Δ‘α»™ cα»“n:
575
- - **Xe mÑy**: 0 mg/lít khí thở hoặc 0 mg/100ml mÑu
576
- - **Γ” tΓ΄**: 0 mg/lΓ­t khΓ­ thở hoαΊ·c 0 mg/100ml mΓ‘u
 
 
577
 
578
- #### πŸ“± Sα»­ dα»₯ng Δ‘iện thoαΊ‘i:
579
- - **CαΊ₯m** sα»­ dα»₯ng Δ‘iện thoαΊ‘i khi Δ‘ang Δ‘iều khiển phΖ°Ζ‘ng tiện
580
- - CΓ³ thể sα»­ dα»₯ng thiαΊΏt bα»‹ rαΊ£nh tay (hands-free)
 
581
 
582
  ---
583
- *LΖ°u Γ½: ĐÒy chỉ lΓ  thΓ΄ng tin tham khαΊ£o. HΓ£y hỏi Road Buddy để được tΖ° vαΊ₯n chi tiαΊΏt!*
584
  """)
585
 
586
  gr.Markdown(f"""
587
  ---
588
- ### ℹ️ ThΓ΄ng tin
589
  - **Base Model:** [{BASE_MODEL}](https://huggingface.co/{BASE_MODEL})
590
  - **LoRA Adapter:** [{ADAPTER_NAME}](https://huggingface.co/{ADAPTER_NAME})
591
 
592
- *Road Buddy cung cαΊ₯p thΓ΄ng tin tham khαΊ£o. Vui lΓ²ng xΓ‘c minh vα»›i nguα»“n chΓ­nh thα»©c khi cαΊ§n thiαΊΏt.*
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