geshang commited on
Commit
b864951
·
verified ·
1 Parent(s): 24a032c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +287 -24
app.py CHANGED
@@ -315,37 +315,300 @@ def run_pipeline(image: PILImage.Image, prompt: str):
315
  return f"Error processing request: {str(e)}", None
316
 
317
 
318
- with gr.Blocks(title="Seg-R1") as demo:
319
- gr.Markdown("# Seg-R1: Visual Segmentation Assistant")
320
- gr.Markdown("Upload an image and ask questions about segmentation.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
321
 
322
- with gr.Row():
323
- with gr.Column():
324
- image_input = gr.Image(type="pil", label="Upload Image")
325
- text_input = gr.Textbox(lines=2, label="Question", placeholder="Ask about objects in the image...")
326
- submit_btn = gr.Button("Submit", variant="primary")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
327
 
328
- with gr.Column():
329
- text_output = gr.Textbox(label="Model Response", interactive=False)
330
- image_output = gr.Image(type="pil", label="Segmentation Result", interactive=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
331
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
332
  submit_btn.click(
333
  fn=run_pipeline,
334
  inputs=[image_input, text_input],
335
- outputs=[text_output, image_output]
 
336
  )
337
 
338
- # gr.Examples(
339
- # examples=[
340
- # ["examples/dog.jpg", "Segment the dog in the image"],
341
- # ["examples/street.jpg", "Find all cars in the image"],
342
- # ["examples/fruits.jpg", "Identify the apples"]
343
- # ],
344
- # inputs=[image_input, text_input],
345
- # outputs=[text_output, image_output],
346
- # fn=run_pipeline,
347
- # cache_examples=False
348
- # )
349
 
350
  if __name__ == "__main__":
351
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
 
 
 
 
 
315
  return f"Error processing request: {str(e)}", None
316
 
317
 
318
+ # ... 之前的代码保持不变 ...
319
+
320
+ # ------------------ 启动 Gradio ------------------
321
+ # 自定义CSS样式
322
+ custom_css = """
323
+ :root {
324
+ --primary: #6C63FF;
325
+ --secondary: #4A44A6;
326
+ --accent: #FF6584;
327
+ --dark: #2A2A3C;
328
+ --light: #F8F9FF;
329
+ --success: #36D399;
330
+ }
331
+
332
+ body {
333
+ background: linear-gradient(135deg, #2A2A3C 0%, #1A1A2E 100%);
334
+ color: var(--light);
335
+ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
336
+ }
337
+
338
+ .gradio-container {
339
+ max-width: 1200px !important;
340
+ margin: 0 auto;
341
+ background: rgba(30, 30, 46, 0.8) !important;
342
+ backdrop-filter: blur(10px);
343
+ border-radius: 20px;
344
+ border: 1px solid rgba(255, 255, 255, 0.1);
345
+ box-shadow: 0 15px 35px rgba(0, 0, 0, 0.5);
346
+ }
347
+
348
+ header {
349
+ text-align: center;
350
+ padding: 2rem 0;
351
+ background: linear-gradient(90deg, var(--primary) 0%, var(--secondary) 100%);
352
+ border-radius: 20px 20px 0 0 !important;
353
+ margin-bottom: 2rem;
354
+ }
355
+
356
+ h1 {
357
+ font-size: 2.5rem !important;
358
+ font-weight: 700;
359
+ margin-bottom: 0.5rem;
360
+ background: linear-gradient(90deg, #FFFFFF 0%, #E0E0FF 100%);
361
+ -webkit-background-clip: text;
362
+ -webkit-text-fill-color: transparent;
363
+ text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
364
+ }
365
+
366
+ .description {
367
+ font-size: 1.1rem;
368
+ max-width: 800px;
369
+ margin: 0 auto 1.5rem;
370
+ color: rgba(255, 255, 255, 0.85);
371
+ line-height: 1.6;
372
+ }
373
+
374
+ .input-panel, .output-panel {
375
+ background: rgba(40, 40, 60, 0.7) !important;
376
+ border-radius: 15px !important;
377
+ padding: 1.5rem !important;
378
+ border: 1px solid rgba(255, 255, 255, 0.1);
379
+ box-shadow: 0 8px 16px rgba(0, 0, 0, 0.3);
380
+ transition: all 0.3s ease;
381
+ }
382
+
383
+ .input-panel:hover, .output-panel:hover {
384
+ transform: translateY(-5px);
385
+ box-shadow: 0 12px 24px rgba(0, 0, 0, 0.4);
386
+ }
387
+
388
+ .input-panel h2, .output-panel h2 {
389
+ color: var(--primary) !important;
390
+ font-size: 1.4rem !important;
391
+ margin-bottom: 1.2rem !important;
392
+ display: flex;
393
+ align-items: center;
394
+ }
395
+
396
+ .input-panel h2:before, .output-panel h2:before {
397
+ content: "•";
398
+ color: var(--accent);
399
+ margin-right: 10px;
400
+ font-size: 1.8rem;
401
+ }
402
+
403
+ .image-preview {
404
+ border-radius: 12px !important;
405
+ overflow: hidden;
406
+ border: 2px solid rgba(255, 255, 255, 0.1);
407
+ }
408
+
409
+ textarea, input {
410
+ background: rgba(30, 30, 50, 0.8) !important;
411
+ color: white !important;
412
+ border: 1px solid rgba(255, 255, 255, 0.1) !important;
413
+ border-radius: 10px !important;
414
+ padding: 12px 15px !important;
415
+ }
416
+
417
+ textarea:focus, input:focus {
418
+ border-color: var(--primary) !important;
419
+ box-shadow: 0 0 0 2px rgba(108, 99, 255, 0.3) !important;
420
+ }
421
+
422
+ button {
423
+ background: linear-gradient(90deg, var(--primary) 0%, var(--secondary) 100%) !important;
424
+ color: white !important;
425
+ border: none !important;
426
+ border-radius: 50px !important;
427
+ padding: 12px 30px !important;
428
+ font-weight: 600 !important;
429
+ font-size: 1.1rem !important;
430
+ box-shadow: 0 5px 15px rgba(108, 99, 255, 0.4) !important;
431
+ transition: all 0.3s ease !important;
432
+ text-transform: uppercase;
433
+ letter-spacing: 1px;
434
+ }
435
+
436
+ button:hover {
437
+ transform: translateY(-3px) !important;
438
+ box-shadow: 0 8px 20px rgba(108, 99, 255, 0.6) !important;
439
+ }
440
+
441
+ button:active {
442
+ transform: translateY(1px) !important;
443
+ }
444
+
445
+ .examples {
446
+ background: rgba(40, 40, 60, 0.7) !important;
447
+ border-radius: 15px !important;
448
+ padding: 1.5rem !important;
449
+ margin-top: 1.5rem;
450
+ }
451
+
452
+ .examples h2 {
453
+ color: var(--accent) !important;
454
+ font-size: 1.4rem !important;
455
+ margin-bottom: 1.2rem !important;
456
+ }
457
+
458
+ .example-image {
459
+ border-radius: 12px !important;
460
+ overflow: hidden;
461
+ transition: all 0.3s ease;
462
+ border: 2px solid transparent;
463
+ }
464
+
465
+ .example-image:hover {
466
+ transform: scale(1.03);
467
+ border-color: var(--primary);
468
+ box-shadow: 0 10px 20px rgba(0, 0, 0, 0.4);
469
+ }
470
+
471
+ .output-text {
472
+ background: rgba(30, 30, 50, 0.8) !important;
473
+ color: white !important;
474
+ border-radius: 12px !important;
475
+ padding: 20px !important;
476
+ border: 1px solid rgba(255, 255, 255, 0.1);
477
+ min-height: 150px;
478
+ font-family: monospace;
479
+ white-space: pre-wrap;
480
+ overflow: auto;
481
+ max-height: 300px;
482
+ }
483
+
484
+ .footer {
485
+ text-align: center;
486
+ padding: 1.5rem 0;
487
+ margin-top: 2rem;
488
+ color: rgba(255, 255, 255, 0.6);
489
+ font-size: 0.9rem;
490
+ border-top: 1px solid rgba(255, 255, 255, 0.1);
491
+ }
492
+
493
+ @keyframes pulse {
494
+ 0% { box-shadow: 0 0 0 0 rgba(108, 99, 255, 0.7); }
495
+ 70% { box-shadow: 0 0 0 10px rgba(108, 99, 255, 0); }
496
+ 100% { box-shadow: 0 0 0 0 rgba(108, 99, 255, 0); }
497
+ }
498
+
499
+ .pulse {
500
+ animation: pulse 2s infinite;
501
+ }
502
+ """
503
+
504
+ # 创建界面
505
+ with gr.Blocks(
506
+ title="Seg-R1 | Advanced Visual Segmentation",
507
+ css=custom_css,
508
+ theme=gr.themes.Default(
509
+ primary_hue="purple",
510
+ secondary_hue="indigo",
511
+ neutral_hue="slate"
512
+ )
513
+ ) as demo:
514
+ # 标题区域
515
+ gr.Markdown("""
516
+ <div style='text-align: center; padding: 2rem 0;'>
517
+ <h1>Seg-R1: Advanced Visual Segmentation Assistant</h1>
518
+ <p class='description'>
519
+ Upload an image and describe what you want to segment. Our AI will analyze the image,
520
+ generate segmentation prompts, and highlight the selected objects with precision.
521
+ </p>
522
+ </div>
523
+ """)
524
 
525
+ # 主内容区
526
+ with gr.Row(equal_height=True):
527
+ # 左侧输入面板
528
+ with gr.Column(scale=5):
529
+ with gr.Group(elem_classes="input-panel"):
530
+ gr.Markdown("## 📷 Image Input")
531
+ image_input = gr.Image(
532
+ type="pil",
533
+ label="Upload Image",
534
+ elem_classes="image-preview",
535
+ height=400
536
+ )
537
+
538
+ gr.Markdown("## 💬 Question")
539
+ text_input = gr.Textbox(
540
+ lines=3,
541
+ label="Describe what you want to segment",
542
+ placeholder="Example: 'Segment the dog in the image' or 'Find all cars in the scene'",
543
+ interactive=True
544
+ )
545
+
546
+ with gr.Row():
547
+ clear_btn = gr.Button("Clear", variant="secondary")
548
+ submit_btn = gr.Button("Analyze & Segment", variant="primary", elem_classes="pulse")
549
 
550
+ # 右侧输出面板
551
+ with gr.Column(scale=5):
552
+ with gr.Group(elem_classes="output-panel"):
553
+ gr.Markdown("## 🧠 Model Response")
554
+ text_output = gr.Textbox(
555
+ label="Reasoning & Output",
556
+ interactive=False,
557
+ elem_classes="output-text"
558
+ )
559
+
560
+ gr.Markdown("## 🎯 Segmentation Result")
561
+ image_output = gr.Image(
562
+ type="pil",
563
+ label="Highlighted Objects",
564
+ elem_classes="image-preview",
565
+ height=400
566
+ )
567
 
568
+ # 示例区域
569
+ with gr.Group(elem_classes="examples"):
570
+ gr.Markdown("## 🚀 Try These Examples")
571
+
572
+ examples = gr.Examples(
573
+ examples=[
574
+ ["imgs/cards.jpg", "Identify and segment the Ace of Spades."],
575
+ ["imgs/painting.jpg", "Identify and segment the man an the house."],
576
+ ["imgs/dogs.jpg", "Identify and segment the tongue of the dog."],
577
+ ],
578
+ inputs=[image_input, text_input],
579
+ outputs=[text_output, image_output],
580
+ fn=run_pipeline,
581
+ cache_examples=False,
582
+ label="Click any example to load it",
583
+ examples_per_page=2
584
+ )
585
+
586
+ # 页脚
587
+ gr.Markdown("""
588
+ <div class='footer'>
589
+ <p>Seg-R1: Advanced Visual Segmentation Assistant | Built with ❤️ using Qwen-VL and SAM</p>
590
+ <p>Note: Processing may take 10-20 seconds for complex images</p>
591
+ </div>
592
+ """)
593
+
594
+ # 事件处理
595
  submit_btn.click(
596
  fn=run_pipeline,
597
  inputs=[image_input, text_input],
598
+ outputs=[text_output, image_output],
599
+ api_name="segment"
600
  )
601
 
602
+ clear_btn.click(
603
+ fn=lambda: [None, "", "", None],
604
+ inputs=[],
605
+ outputs=[image_input, text_input, text_output, image_output]
606
+ )
 
 
 
 
 
 
607
 
608
  if __name__ == "__main__":
609
+ demo.launch(
610
+ server_name="0.0.0.0",
611
+ server_port=7860,
612
+ share=False,
613
+ # favicon_path="favicon.ico" # 可选:添加自定义favicon
614
+ )