Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,11 +15,14 @@ import torch
|
|
| 15 |
import numpy as np
|
| 16 |
from loguru import logger
|
| 17 |
from PIL import Image
|
| 18 |
-
from transformers import AutoProcessor, Gemma3ForConditionalGeneration, TextIteratorStreamer,
|
| 19 |
import time
|
| 20 |
import warnings
|
| 21 |
from typing import Dict, List, Optional, Union
|
| 22 |
import librosa
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
# CSV/TXT ๋ถ์
|
| 25 |
import pandas as pd
|
|
@@ -28,7 +31,7 @@ import PyPDF2
|
|
| 28 |
|
| 29 |
warnings.filterwarnings('ignore')
|
| 30 |
|
| 31 |
-
print("๐ฎ ๋ก๋ด ์๊ฐ ์์คํ
์ด๊ธฐํ (Gemma3-R1984-4B + Whisper)...")
|
| 32 |
|
| 33 |
##############################################################################
|
| 34 |
# ์์ ์ ์
|
|
@@ -44,7 +47,6 @@ SERPHOUSE_API_KEY = os.getenv("SERPHOUSE_API_KEY", "")
|
|
| 44 |
model = None
|
| 45 |
processor = None
|
| 46 |
whisper_model = None
|
| 47 |
-
whisper_processor = None
|
| 48 |
model_loaded = False
|
| 49 |
whisper_loaded = False
|
| 50 |
model_name = "Gemma3-R1984-4B"
|
|
@@ -63,7 +65,7 @@ def clear_cuda_cache():
|
|
| 63 |
##############################################################################
|
| 64 |
@spaces.GPU(duration=60)
|
| 65 |
def load_whisper():
|
| 66 |
-
global whisper_model,
|
| 67 |
|
| 68 |
if whisper_loaded:
|
| 69 |
logger.info("Whisper ๋ชจ๋ธ์ด ์ด๋ฏธ ๋ก๋๋์ด ์์ต๋๋ค.")
|
|
@@ -71,11 +73,13 @@ def load_whisper():
|
|
| 71 |
|
| 72 |
try:
|
| 73 |
logger.info("Whisper ๋ชจ๋ธ ๋ก๋ฉ ์์...")
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
| 79 |
)
|
| 80 |
whisper_loaded = True
|
| 81 |
logger.info("โ
Whisper ๋ชจ๋ธ ๋ก๋ฉ ์๋ฃ!")
|
|
@@ -87,42 +91,141 @@ def load_whisper():
|
|
| 87 |
##############################################################################
|
| 88 |
# ์ค๋์ค ์ฒ๋ฆฌ ํจ์
|
| 89 |
##############################################################################
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
@spaces.GPU(duration=30)
|
| 91 |
-
def
|
| 92 |
"""Whisper๋ฅผ ์ฌ์ฉํ ์ค๋์ค ์ ์ฌ"""
|
| 93 |
-
global whisper_model,
|
| 94 |
|
| 95 |
if not whisper_loaded:
|
| 96 |
if not load_whisper():
|
| 97 |
-
return
|
| 98 |
|
| 99 |
try:
|
| 100 |
-
if audio_data is None:
|
| 101 |
-
return None
|
| 102 |
-
|
| 103 |
-
# ์ค๋์ค ๋ฐ์ดํฐ ์ฒ๋ฆฌ
|
| 104 |
-
sample_rate, audio = audio_data
|
| 105 |
-
|
| 106 |
-
# 16kHz๋ก ๋ฆฌ์ํ๋ง
|
| 107 |
-
if sample_rate != 16000:
|
| 108 |
-
audio = librosa.resample(audio.astype(float), orig_sr=sample_rate, target_sr=16000)
|
| 109 |
-
|
| 110 |
-
# Whisper ์
๋ ฅ ์ฒ๋ฆฌ
|
| 111 |
-
inputs = whisper_processor(audio, sampling_rate=16000, return_tensors="pt")
|
| 112 |
-
inputs = {k: v.to(whisper_model.device) for k, v in inputs.items()}
|
| 113 |
-
|
| 114 |
# ์์ฑ ์ธ์
|
| 115 |
-
|
| 116 |
-
|
| 117 |
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
except Exception as e:
|
| 124 |
-
logger.error(f"์ค๋์ค
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
##############################################################################
|
| 128 |
# ํค์๋ ์ถ์ถ ํจ์
|
|
@@ -591,13 +694,19 @@ css = """
|
|
| 591 |
background: #e3f2fd;
|
| 592 |
color: #1565c0;
|
| 593 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 594 |
"""
|
| 595 |
|
| 596 |
with gr.Blocks(title="๐ค ๋ก๋ด ์๊ฐ ์์คํ
(Gemma3-4B)", css=css) as demo:
|
| 597 |
gr.HTML("""
|
| 598 |
<div class="robot-header">
|
| 599 |
<h1>๐ค ๋ก๋ด ์๊ฐ ์์คํ
</h1>
|
| 600 |
-
<h3>๐ฎ Gemma3-R1984-4B + ๐ท ์ค์๊ฐ ์น์บ + ๐ค ์์ฑ ์ธ์</h3>
|
| 601 |
<p>โก ๋ฉํฐ๋ชจ๋ฌ AI๋ก ๋ก๋ด ์์
๋ถ์!</p>
|
| 602 |
</div>
|
| 603 |
""")
|
|
@@ -636,6 +745,11 @@ with gr.Blocks(title="๐ค ๋ก๋ด ์๊ฐ ์์คํ
(Gemma3-4B)", css=css) as dem
|
|
| 636 |
'<div class="audio-status">๐ค ์์ฑ ์ธ์: ๋นํ์ฑํ</div>'
|
| 637 |
)
|
| 638 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 639 |
# ๋ง์ง๋ง ์ธ์๋ ํ
์คํธ
|
| 640 |
last_transcript = gr.Textbox(
|
| 641 |
label="์ธ์๋ ์์ฑ",
|
|
@@ -657,9 +771,9 @@ with gr.Blocks(title="๐ค ๋ก๋ด ์๊ฐ ์์คํ
(Gemma3-4B)", css=css) as dem
|
|
| 657 |
)
|
| 658 |
|
| 659 |
use_audio_toggle = gr.Checkbox(
|
| 660 |
-
label="๐ค ์์ฑ ์ธ์ ์ฌ์ฉ",
|
| 661 |
value=False,
|
| 662 |
-
info="
|
| 663 |
)
|
| 664 |
|
| 665 |
with gr.Row():
|
|
@@ -719,6 +833,19 @@ with gr.Blocks(title="๐ค ๋ก๋ด ์๊ฐ ์์คํ
(Gemma3-4B)", css=css) as dem
|
|
| 719 |
label="๋ง์ดํฌ ์
๋ ฅ"
|
| 720 |
)
|
| 721 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 722 |
# ๋ฌธ์ ๋ถ์ ํญ (์จ๊น)
|
| 723 |
with gr.Tab("๐ ๋ฌธ์ ๋ถ์", visible=False):
|
| 724 |
with gr.Row():
|
|
@@ -752,8 +879,6 @@ with gr.Blocks(title="๐ค ๋ก๋ด ์๊ฐ ์์คํ
(Gemma3-4B)", css=css) as dem
|
|
| 752 |
|
| 753 |
# ์ด๋ฒคํธ ํธ๋ค๋ฌ
|
| 754 |
webcam_state = gr.State(None)
|
| 755 |
-
audio_state = gr.State(None)
|
| 756 |
-
transcript_state = gr.State("")
|
| 757 |
|
| 758 |
def capture_webcam(frame):
|
| 759 |
"""์น์บ ํ๋ ์ ์บก์ฒ"""
|
|
@@ -763,15 +888,29 @@ with gr.Blocks(title="๐ค ๋ก๋ด ์๊ฐ ์์คํ
(Gemma3-4B)", css=css) as dem
|
|
| 763 |
|
| 764 |
def clear_capture():
|
| 765 |
"""์บก์ฒ ์ด๊ธฐํ"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 766 |
return None, gr.update(visible=False), '<div class="status-box" style="background:#d4edda; color:#155724;">๐ฎ ์์คํ
์ค๋น</div>', ""
|
| 767 |
|
| 768 |
-
def analyze_with_task(image, prompt, task_type, use_search, thinking, tokens
|
| 769 |
"""ํน์ ํ์คํฌ๋ก ์ด๋ฏธ์ง ๋ถ์"""
|
|
|
|
|
|
|
| 770 |
if image is None:
|
| 771 |
return "โ ๋จผ์ ์ด๋ฏธ์ง๋ฅผ ์บก์ฒํ์ธ์.", '<div class="status-box" style="background:#f8d7da; color:#721c24;">โ ์ด๋ฏธ์ง ์์</div>'
|
| 772 |
|
| 773 |
status = f'<div class="status-box" style="background:#cce5ff; color:#004085;">๐ {task_type} ๋ถ์ ์ค...</div>'
|
| 774 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 775 |
result = analyze_image_for_robot(
|
| 776 |
image=image,
|
| 777 |
prompt=prompt,
|
|
@@ -800,27 +939,31 @@ with gr.Blocks(title="๐ค ๋ก๋ด ์๊ฐ ์์คํ
(Gemma3-4B)", css=css) as dem
|
|
| 800 |
return formatted_result, complete_status
|
| 801 |
|
| 802 |
# ์๋ ์บก์ฒ ๋ฐ ๋ถ์ ํจ์
|
| 803 |
-
def auto_capture_and_analyze(webcam_frame,
|
| 804 |
-
"""์๋ ์บก์ฒ ๋ฐ ๋ถ์ (์ค๋์ค
|
|
|
|
|
|
|
| 805 |
if webcam_frame is None:
|
| 806 |
return (
|
| 807 |
None,
|
| 808 |
"์๋ ์บก์ฒ ๋๊ธฐ ์ค...",
|
| 809 |
'<div class="status-box" style="background:#fff3cd; color:#856404;">โณ ์น์บ ๋๊ธฐ ์ค</div>',
|
| 810 |
'<div class="auto-capture-status">๐ ์๋ ์บก์ฒ: ์น์บ ๋๊ธฐ ์ค</div>',
|
| 811 |
-
|
| 812 |
-
current_transcript
|
| 813 |
)
|
| 814 |
|
| 815 |
# ์บก์ฒ ์ํ
|
| 816 |
timestamp = time.strftime("%H:%M:%S")
|
| 817 |
|
| 818 |
-
#
|
| 819 |
-
|
| 820 |
-
|
| 821 |
-
|
| 822 |
-
|
| 823 |
-
|
|
|
|
|
|
|
|
|
|
| 824 |
|
| 825 |
# ์ด๋ฏธ์ง ๋ถ์ (์์
๊ณํ ๋ชจ๋๋ก)
|
| 826 |
result = analyze_image_for_robot(
|
|
@@ -830,7 +973,7 @@ with gr.Blocks(title="๐ค ๋ก๋ด ์๊ฐ ์์คํ
(Gemma3-4B)", css=css) as dem
|
|
| 830 |
use_web_search=use_search,
|
| 831 |
enable_thinking=thinking,
|
| 832 |
max_new_tokens=tokens,
|
| 833 |
-
audio_transcript=
|
| 834 |
)
|
| 835 |
|
| 836 |
formatted_result = f"""๐ ์๋ ๋ถ์ ์๋ฃ ({timestamp})
|
|
@@ -838,13 +981,15 @@ with gr.Blocks(title="๐ค ๋ก๋ด ์๊ฐ ์์คํ
(Gemma3-4B)", css=css) as dem
|
|
| 838 |
{result}
|
| 839 |
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ"""
|
| 840 |
|
|
|
|
|
|
|
|
|
|
| 841 |
return (
|
| 842 |
webcam_frame,
|
| 843 |
formatted_result,
|
| 844 |
'<div class="status-box" style="background:#d4edda; color:#155724;">โ
์๋ ๋ถ์ ์๋ฃ</div>',
|
| 845 |
f'<div class="auto-capture-status">๐ ์๋ ์บก์ฒ: ๋ง์ง๋ง ๋ถ์ {timestamp}</div>',
|
| 846 |
-
|
| 847 |
-
new_transcript if new_transcript else current_transcript
|
| 848 |
)
|
| 849 |
|
| 850 |
# ์น์บ ์คํธ๋ฆฌ๋ฐ
|
|
@@ -854,14 +999,17 @@ with gr.Blocks(title="๐ค ๋ก๋ด ์๊ฐ ์์คํ
(Gemma3-4B)", css=css) as dem
|
|
| 854 |
outputs=[webcam_state]
|
| 855 |
)
|
| 856 |
|
| 857 |
-
# ์ค๋์ค ์คํธ๋ฆฌ๋ฐ
|
| 858 |
-
def
|
| 859 |
-
|
|
|
|
|
|
|
| 860 |
|
|
|
|
| 861 |
audio_input.stream(
|
| 862 |
-
fn=
|
| 863 |
inputs=[audio_input],
|
| 864 |
-
outputs=
|
| 865 |
)
|
| 866 |
|
| 867 |
# ์๋ ์บก์ฒ ๋ฒํผ
|
|
@@ -874,19 +1022,19 @@ with gr.Blocks(title="๐ค ๋ก๋ด ์๊ฐ ์์คํ
(Gemma3-4B)", css=css) as dem
|
|
| 874 |
# ์ด๊ธฐํ ๋ฒํผ
|
| 875 |
clear_capture_btn.click(
|
| 876 |
fn=clear_capture,
|
| 877 |
-
outputs=[webcam_state, captured_image, status_display,
|
| 878 |
)
|
| 879 |
|
| 880 |
# ์์
๋ฒํผ๋ค
|
| 881 |
planning_btn.click(
|
| 882 |
-
fn=lambda img, p, s, t, tk
|
| 883 |
-
inputs=[captured_image, task_prompt, use_web_search, enable_thinking, max_tokens
|
| 884 |
outputs=[result_output, status_display]
|
| 885 |
)
|
| 886 |
|
| 887 |
grounding_btn.click(
|
| 888 |
-
fn=lambda img, p, s, t, tk
|
| 889 |
-
inputs=[captured_image, task_prompt, use_web_search, enable_thinking, max_tokens
|
| 890 |
outputs=[result_output, status_display]
|
| 891 |
)
|
| 892 |
|
|
@@ -924,14 +1072,29 @@ with gr.Blocks(title="๐ค ๋ก๋ด ์๊ฐ ์์คํ
(Gemma3-4B)", css=css) as dem
|
|
| 924 |
|
| 925 |
# ์ค๋์ค ํ ๊ธ ์ด๋ฒคํธ
|
| 926 |
def toggle_audio(enabled):
|
|
|
|
|
|
|
| 927 |
if enabled:
|
| 928 |
# Whisper ๋ชจ๋ธ ๋ก๋
|
| 929 |
load_whisper()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 930 |
return (
|
| 931 |
gr.update(visible=True), # audio_input ํ์
|
| 932 |
-
'<div class="audio-status">๐ค ์์ฑ ์ธ์:
|
| 933 |
)
|
| 934 |
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 935 |
return (
|
| 936 |
gr.update(visible=False), # audio_input ์จ๊น
|
| 937 |
'<div class="audio-status">๐ค ์์ฑ ์ธ์: ๋นํ์ฑํ</div>'
|
|
@@ -946,13 +1109,18 @@ with gr.Blocks(title="๐ค ๋ก๋ด ์๊ฐ ์์คํ
(Gemma3-4B)", css=css) as dem
|
|
| 946 |
# ํ์ด๋จธ ํฑ ์ด๋ฒคํธ
|
| 947 |
timer.tick(
|
| 948 |
fn=auto_capture_and_analyze,
|
| 949 |
-
inputs=[webcam_state,
|
| 950 |
-
outputs=[captured_image, result_output, status_display, auto_capture_status,
|
| 951 |
)
|
| 952 |
|
| 953 |
# ์ด๊ธฐ ๋ชจ๋ธ ๋ก๋
|
| 954 |
def initial_load():
|
| 955 |
load_model()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 956 |
return "์์คํ
์ค๋น ์๋ฃ! ๐"
|
| 957 |
|
| 958 |
demo.load(
|
|
@@ -961,8 +1129,8 @@ with gr.Blocks(title="๐ค ๋ก๋ด ์๊ฐ ์์คํ
(Gemma3-4B)", css=css) as dem
|
|
| 961 |
)
|
| 962 |
|
| 963 |
if __name__ == "__main__":
|
| 964 |
-
print("๐ ๋ก๋ด ์๊ฐ ์์คํ
์์ (Gemma3-R1984-4B + Whisper)...")
|
| 965 |
-
demo.launch(
|
| 966 |
server_name="0.0.0.0",
|
| 967 |
server_port=7860,
|
| 968 |
share=False,
|
|
|
|
| 15 |
import numpy as np
|
| 16 |
from loguru import logger
|
| 17 |
from PIL import Image
|
| 18 |
+
from transformers import AutoProcessor, Gemma3ForConditionalGeneration, TextIteratorStreamer, pipeline
|
| 19 |
import time
|
| 20 |
import warnings
|
| 21 |
from typing import Dict, List, Optional, Union
|
| 22 |
import librosa
|
| 23 |
+
import scipy.signal as sps
|
| 24 |
+
from threading import Thread, Lock
|
| 25 |
+
import queue
|
| 26 |
|
| 27 |
# CSV/TXT ๋ถ์
|
| 28 |
import pandas as pd
|
|
|
|
| 31 |
|
| 32 |
warnings.filterwarnings('ignore')
|
| 33 |
|
| 34 |
+
print("๐ฎ ๋ก๋ด ์๊ฐ ์์คํ
์ด๊ธฐํ (Gemma3-R1984-4B + Whisper + 10์ด ๊ต๋ ๋
น์)...")
|
| 35 |
|
| 36 |
##############################################################################
|
| 37 |
# ์์ ์ ์
|
|
|
|
| 47 |
model = None
|
| 48 |
processor = None
|
| 49 |
whisper_model = None
|
|
|
|
| 50 |
model_loaded = False
|
| 51 |
whisper_loaded = False
|
| 52 |
model_name = "Gemma3-R1984-4B"
|
|
|
|
| 65 |
##############################################################################
|
| 66 |
@spaces.GPU(duration=60)
|
| 67 |
def load_whisper():
|
| 68 |
+
global whisper_model, whisper_loaded
|
| 69 |
|
| 70 |
if whisper_loaded:
|
| 71 |
logger.info("Whisper ๋ชจ๋ธ์ด ์ด๋ฏธ ๋ก๋๋์ด ์์ต๋๋ค.")
|
|
|
|
| 73 |
|
| 74 |
try:
|
| 75 |
logger.info("Whisper ๋ชจ๋ธ ๋ก๋ฉ ์์...")
|
| 76 |
+
# ํ์ดํ๋ผ์ธ ๋ฐฉ์์ผ๋ก ๋ก๋
|
| 77 |
+
device = 0 if torch.cuda.is_available() else "cpu"
|
| 78 |
+
whisper_model = pipeline(
|
| 79 |
+
task="automatic-speech-recognition",
|
| 80 |
+
model="openai/whisper-base",
|
| 81 |
+
chunk_length_s=30,
|
| 82 |
+
device=device,
|
| 83 |
)
|
| 84 |
whisper_loaded = True
|
| 85 |
logger.info("โ
Whisper ๋ชจ๋ธ ๋ก๋ฉ ์๋ฃ!")
|
|
|
|
| 91 |
##############################################################################
|
| 92 |
# ์ค๋์ค ์ฒ๋ฆฌ ํจ์
|
| 93 |
##############################################################################
|
| 94 |
+
import scipy.signal as sps
|
| 95 |
+
from threading import Thread, Lock
|
| 96 |
+
import queue
|
| 97 |
+
|
| 98 |
+
# ์ค๋์ค ๋ฒํผ ๊ด๋ฆฌ
|
| 99 |
+
audio_buffer_lock = Lock()
|
| 100 |
+
audio_buffer_a = []
|
| 101 |
+
audio_buffer_b = []
|
| 102 |
+
current_buffer = 'a' # ํ์ฌ ๋
น์ ์ค์ธ ๋ฒํผ
|
| 103 |
+
processing_queue = queue.Queue() # ์ฒ๋ฆฌ ๋๊ธฐ ํ
|
| 104 |
+
last_transcription = "" # ๋ง์ง๋ง ์ ์ฌ ๊ฒฐ๊ณผ
|
| 105 |
+
|
| 106 |
+
def resample_audio(audio: np.ndarray, orig_sr: int, target_sr: int = 16000) -> np.ndarray:
|
| 107 |
+
"""์ค๋์ค ๋ฆฌ์ํ๋ง"""
|
| 108 |
+
if orig_sr == target_sr:
|
| 109 |
+
return audio.astype(np.float32)
|
| 110 |
+
|
| 111 |
+
# scipy๋ฅผ ์ฌ์ฉํ ๋ฆฌ์ํ๋ง
|
| 112 |
+
number_of_samples = round(len(audio) * float(target_sr) / orig_sr)
|
| 113 |
+
audio_resampled = sps.resample(audio, number_of_samples)
|
| 114 |
+
return audio_resampled.astype(np.float32)
|
| 115 |
+
|
| 116 |
@spaces.GPU(duration=30)
|
| 117 |
+
def transcribe_audio_whisper(audio_array: np.ndarray, sr: int = 16000):
|
| 118 |
"""Whisper๋ฅผ ์ฌ์ฉํ ์ค๋์ค ์ ์ฌ"""
|
| 119 |
+
global whisper_model, whisper_loaded
|
| 120 |
|
| 121 |
if not whisper_loaded:
|
| 122 |
if not load_whisper():
|
| 123 |
+
return None
|
| 124 |
|
| 125 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
# ์์ฑ ์ธ์
|
| 127 |
+
result = whisper_model({"array": audio_array, "sampling_rate": sr})
|
| 128 |
+
transcription = result["text"].strip()
|
| 129 |
|
| 130 |
+
return transcription if transcription else None
|
| 131 |
+
|
| 132 |
+
except Exception as e:
|
| 133 |
+
logger.error(f"Whisper ์ค๋์ค ์ ์ฌ ์ค๋ฅ: {e}")
|
| 134 |
+
return None
|
| 135 |
+
|
| 136 |
+
def accumulate_audio(audio_chunk):
|
| 137 |
+
"""์ค๋์ค ์ฒญํฌ๋ฅผ ๋ฒํผ์ ๋์ """
|
| 138 |
+
global current_buffer, audio_buffer_a, audio_buffer_b
|
| 139 |
+
|
| 140 |
+
if audio_chunk is None:
|
| 141 |
+
return
|
| 142 |
+
|
| 143 |
+
sr, audio = audio_chunk
|
| 144 |
+
|
| 145 |
+
# ์คํ
๋ ์ค๋ฅผ ๋ชจ๋
ธ๋ก ๋ณํ
|
| 146 |
+
if audio.ndim > 1:
|
| 147 |
+
audio = audio.mean(axis=1)
|
| 148 |
+
|
| 149 |
+
with audio_buffer_lock:
|
| 150 |
+
if current_buffer == 'a':
|
| 151 |
+
audio_buffer_a.append((audio, sr))
|
| 152 |
+
else:
|
| 153 |
+
audio_buffer_b.append((audio, sr))
|
| 154 |
+
|
| 155 |
+
def switch_buffers():
|
| 156 |
+
"""๋ฒํผ ์ ํ ๋ฐ ์ฒ๋ฆฌ ํ์ ์ถ๊ฐ"""
|
| 157 |
+
global current_buffer, audio_buffer_a, audio_buffer_b
|
| 158 |
+
|
| 159 |
+
with audio_buffer_lock:
|
| 160 |
+
if current_buffer == 'a':
|
| 161 |
+
# A ๋ฒํผ๋ฅผ ์ฒ๋ฆฌ ํ์ ์ถ๊ฐ
|
| 162 |
+
if audio_buffer_a:
|
| 163 |
+
processing_queue.put(('a', audio_buffer_a.copy()))
|
| 164 |
+
audio_buffer_a.clear()
|
| 165 |
+
current_buffer = 'b'
|
| 166 |
+
else:
|
| 167 |
+
# B ๋ฒํผ๋ฅผ ์ฒ๋ฆฌ ํ์ ์ถ๊ฐ
|
| 168 |
+
if audio_buffer_b:
|
| 169 |
+
processing_queue.put(('b', audio_buffer_b.copy()))
|
| 170 |
+
audio_buffer_b.clear()
|
| 171 |
+
current_buffer = 'a'
|
| 172 |
+
|
| 173 |
+
def process_audio_buffer(buffer_data):
|
| 174 |
+
"""๋ฒํผ์ ์ค๋์ค ๋ฐ์ดํฐ ์ฒ๋ฆฌ"""
|
| 175 |
+
buffer_name, audio_chunks = buffer_data
|
| 176 |
+
|
| 177 |
+
if not audio_chunks:
|
| 178 |
+
return None
|
| 179 |
+
|
| 180 |
+
try:
|
| 181 |
+
# ๋ชจ๋ ์ฒญํฌ๋ฅผ ํ๋๋ก ๊ฒฐํฉ
|
| 182 |
+
combined_audio = []
|
| 183 |
+
sample_rate = 16000
|
| 184 |
|
| 185 |
+
for audio, sr in audio_chunks:
|
| 186 |
+
# 16kHz๋ก ๋ฆฌ์ํ๋ง
|
| 187 |
+
if sr != 16000:
|
| 188 |
+
audio = resample_audio(audio, sr, 16000)
|
| 189 |
+
combined_audio.append(audio)
|
| 190 |
|
| 191 |
+
# ๊ฒฐํฉ
|
| 192 |
+
if combined_audio:
|
| 193 |
+
full_audio = np.concatenate(combined_audio)
|
| 194 |
+
|
| 195 |
+
# Whisper๋ก ์ ์ฌ
|
| 196 |
+
transcription = transcribe_audio_whisper(full_audio, 16000)
|
| 197 |
+
|
| 198 |
+
if transcription:
|
| 199 |
+
logger.info(f"๋ฒํผ {buffer_name} ์ ์ฌ ์๋ฃ: {transcription[:50]}...")
|
| 200 |
+
return transcription
|
| 201 |
+
|
| 202 |
except Exception as e:
|
| 203 |
+
logger.error(f"์ค๋์ค ๋ฒํผ ์ฒ๋ฆฌ ์ค๋ฅ: {e}")
|
| 204 |
+
|
| 205 |
+
return None
|
| 206 |
+
|
| 207 |
+
# ๋ฐฑ๊ทธ๋ผ์ด๋ ์ฒ๋ฆฌ ์ค๋ ๋
|
| 208 |
+
def audio_processing_worker():
|
| 209 |
+
"""๋ฐฑ๊ทธ๋ผ์ด๋์์ ์ค๋์ค ๋ฒํผ ์ฒ๋ฆฌ"""
|
| 210 |
+
global last_transcription
|
| 211 |
+
|
| 212 |
+
while True:
|
| 213 |
+
try:
|
| 214 |
+
# ์ฒ๋ฆฌํ ๋ฒํผ ๊ฐ์ ธ์ค๊ธฐ
|
| 215 |
+
buffer_data = processing_queue.get(timeout=1)
|
| 216 |
+
|
| 217 |
+
# ์ค๋์ค ์ฒ๋ฆฌ
|
| 218 |
+
result = process_audio_buffer(buffer_data)
|
| 219 |
+
|
| 220 |
+
if result:
|
| 221 |
+
# ๊ฒฐ๊ณผ๋ฅผ ์ ์ญ ๋ณ์์ ์ ์ฅ (๋์ค์ ์ฌ์ฉ)
|
| 222 |
+
with audio_buffer_lock:
|
| 223 |
+
last_transcription = result
|
| 224 |
+
|
| 225 |
+
except queue.Empty:
|
| 226 |
+
continue
|
| 227 |
+
except Exception as e:
|
| 228 |
+
logger.error(f"์ค๋์ค ์ฒ๋ฆฌ ์์ปค ์ค๋ฅ: {e}")
|
| 229 |
|
| 230 |
##############################################################################
|
| 231 |
# ํค์๋ ์ถ์ถ ํจ์
|
|
|
|
| 694 |
background: #e3f2fd;
|
| 695 |
color: #1565c0;
|
| 696 |
}
|
| 697 |
+
.buffer-info {
|
| 698 |
+
font-size: 0.9em;
|
| 699 |
+
color: #666;
|
| 700 |
+
text-align: center;
|
| 701 |
+
margin-top: 5px;
|
| 702 |
+
}
|
| 703 |
"""
|
| 704 |
|
| 705 |
with gr.Blocks(title="๐ค ๋ก๋ด ์๊ฐ ์์คํ
(Gemma3-4B)", css=css) as demo:
|
| 706 |
gr.HTML("""
|
| 707 |
<div class="robot-header">
|
| 708 |
<h1>๐ค ๋ก๋ด ์๊ฐ ์์คํ
</h1>
|
| 709 |
+
<h3>๐ฎ Gemma3-R1984-4B + ๐ท ์ค์๊ฐ ์น์บ + ๐ค 10์ด ๊ต๋ ์์ฑ ์ธ์</h3>
|
| 710 |
<p>โก ๋ฉํฐ๋ชจ๋ฌ AI๋ก ๋ก๋ด ์์
๋ถ์!</p>
|
| 711 |
</div>
|
| 712 |
""")
|
|
|
|
| 745 |
'<div class="audio-status">๐ค ์์ฑ ์ธ์: ๋นํ์ฑํ</div>'
|
| 746 |
)
|
| 747 |
|
| 748 |
+
# ๋ฒํผ ์ ๋ณด
|
| 749 |
+
gr.HTML(
|
| 750 |
+
'<div class="buffer-info">A/B ๋ฒํผ ๊ต๋ ๋
น์์ผ๋ก ๋๊น ์๋ ์ธ์</div>'
|
| 751 |
+
)
|
| 752 |
+
|
| 753 |
# ๋ง์ง๋ง ์ธ์๋ ํ
์คํธ
|
| 754 |
last_transcript = gr.Textbox(
|
| 755 |
label="์ธ์๋ ์์ฑ",
|
|
|
|
| 771 |
)
|
| 772 |
|
| 773 |
use_audio_toggle = gr.Checkbox(
|
| 774 |
+
label="๐ค ์์ฑ ์ธ์ ์ฌ์ฉ (10์ด ๊ต๋ ๋
น์)",
|
| 775 |
value=False,
|
| 776 |
+
info="10์ด๋ง๋ค ๊ต๋๋ก ๋
น์ํ์ฌ ๋๊น ์์ด ์ธ์"
|
| 777 |
)
|
| 778 |
|
| 779 |
with gr.Row():
|
|
|
|
| 833 |
label="๋ง์ดํฌ ์
๋ ฅ"
|
| 834 |
)
|
| 835 |
|
| 836 |
+
# ์ค๋์ค ์คํธ๋ฆฌ๋ฐ ์ฒ๋ฆฌ
|
| 837 |
+
def audio_stream_callback(audio_chunk):
|
| 838 |
+
"""์ค๋์ค ์คํธ๋ฆผ ์ฝ๋ฐฑ - ๋ฒํผ์ ๋์ """
|
| 839 |
+
accumulate_audio(audio_chunk)
|
| 840 |
+
return None # ์ํ ์
๋ฐ๏ฟฝ๏ฟฝ๏ฟฝํธ ์์
|
| 841 |
+
|
| 842 |
+
# ์ค๋์ค ์คํธ๋ฆฌ๋ฐ ์ฐ๊ฒฐ
|
| 843 |
+
audio_input.stream(
|
| 844 |
+
fn=audio_stream_callback,
|
| 845 |
+
inputs=[audio_input],
|
| 846 |
+
outputs=None
|
| 847 |
+
)
|
| 848 |
+
|
| 849 |
# ๋ฌธ์ ๋ถ์ ํญ (์จ๊น)
|
| 850 |
with gr.Tab("๐ ๋ฌธ์ ๋ถ์", visible=False):
|
| 851 |
with gr.Row():
|
|
|
|
| 879 |
|
| 880 |
# ์ด๋ฒคํธ ํธ๋ค๋ฌ
|
| 881 |
webcam_state = gr.State(None)
|
|
|
|
|
|
|
| 882 |
|
| 883 |
def capture_webcam(frame):
|
| 884 |
"""์น์บ ํ๋ ์ ์บก์ฒ"""
|
|
|
|
| 888 |
|
| 889 |
def clear_capture():
|
| 890 |
"""์บก์ฒ ์ด๊ธฐํ"""
|
| 891 |
+
global last_transcription, audio_buffer_a, audio_buffer_b
|
| 892 |
+
|
| 893 |
+
with audio_buffer_lock:
|
| 894 |
+
last_transcription = ""
|
| 895 |
+
audio_buffer_a.clear()
|
| 896 |
+
audio_buffer_b.clear()
|
| 897 |
+
|
| 898 |
return None, gr.update(visible=False), '<div class="status-box" style="background:#d4edda; color:#155724;">๐ฎ ์์คํ
์ค๋น</div>', ""
|
| 899 |
|
| 900 |
+
def analyze_with_task(image, prompt, task_type, use_search, thinking, tokens):
|
| 901 |
"""ํน์ ํ์คํฌ๋ก ์ด๋ฏธ์ง ๋ถ์"""
|
| 902 |
+
global last_transcription
|
| 903 |
+
|
| 904 |
if image is None:
|
| 905 |
return "โ ๋จผ์ ์ด๋ฏธ์ง๋ฅผ ์บก์ฒํ์ธ์.", '<div class="status-box" style="background:#f8d7da; color:#721c24;">โ ์ด๋ฏธ์ง ์์</div>'
|
| 906 |
|
| 907 |
status = f'<div class="status-box" style="background:#cce5ff; color:#004085;">๐ {task_type} ๋ถ์ ์ค...</div>'
|
| 908 |
|
| 909 |
+
# ํ์ฌ ์ ์ฌ ํ
์คํธ ๊ฐ์ ธ์ค๊ธฐ
|
| 910 |
+
transcript = ""
|
| 911 |
+
with audio_buffer_lock:
|
| 912 |
+
transcript = last_transcription
|
| 913 |
+
|
| 914 |
result = analyze_image_for_robot(
|
| 915 |
image=image,
|
| 916 |
prompt=prompt,
|
|
|
|
| 939 |
return formatted_result, complete_status
|
| 940 |
|
| 941 |
# ์๋ ์บก์ฒ ๋ฐ ๋ถ์ ํจ์
|
| 942 |
+
def auto_capture_and_analyze(webcam_frame, task_prompt, use_search, thinking, tokens, use_audio):
|
| 943 |
+
"""์๋ ์บก์ฒ ๋ฐ ๋ถ์ (10์ด๋ง๋ค ์ค๋์ค ๋ฒํผ ์ ํ)"""
|
| 944 |
+
global last_transcription
|
| 945 |
+
|
| 946 |
if webcam_frame is None:
|
| 947 |
return (
|
| 948 |
None,
|
| 949 |
"์๋ ์บก์ฒ ๋๊ธฐ ์ค...",
|
| 950 |
'<div class="status-box" style="background:#fff3cd; color:#856404;">โณ ์น์บ ๋๊ธฐ ์ค</div>',
|
| 951 |
'<div class="auto-capture-status">๐ ์๋ ์บก์ฒ: ์น์บ ๋๊ธฐ ์ค</div>',
|
| 952 |
+
""
|
|
|
|
| 953 |
)
|
| 954 |
|
| 955 |
# ์บก์ฒ ์ํ
|
| 956 |
timestamp = time.strftime("%H:%M:%S")
|
| 957 |
|
| 958 |
+
# ๋ฒํผ ์ ํ (10์ด๋ง๋ค)
|
| 959 |
+
if use_audio:
|
| 960 |
+
switch_buffers()
|
| 961 |
+
|
| 962 |
+
# ๋ง์ง๋ง ์ ์ฌ ๊ฒฐ๊ณผ ๊ฐ์ ธ์ค๊ธฐ
|
| 963 |
+
audio_transcript = ""
|
| 964 |
+
if use_audio:
|
| 965 |
+
with audio_buffer_lock:
|
| 966 |
+
audio_transcript = last_transcription
|
| 967 |
|
| 968 |
# ์ด๋ฏธ์ง ๋ถ์ (์์
๊ณํ ๋ชจ๋๋ก)
|
| 969 |
result = analyze_image_for_robot(
|
|
|
|
| 973 |
use_web_search=use_search,
|
| 974 |
enable_thinking=thinking,
|
| 975 |
max_new_tokens=tokens,
|
| 976 |
+
audio_transcript=audio_transcript if audio_transcript else None
|
| 977 |
)
|
| 978 |
|
| 979 |
formatted_result = f"""๐ ์๋ ๋ถ์ ์๋ฃ ({timestamp})
|
|
|
|
| 981 |
{result}
|
| 982 |
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ"""
|
| 983 |
|
| 984 |
+
# ๋ง์ง๋ง ์ธ์๋ ํ
์คํธ ์
๋ฐ์ดํธ
|
| 985 |
+
transcript_display = audio_transcript if audio_transcript else "์์ฑ ์ธ์ ๋๊ธฐ ์ค..."
|
| 986 |
+
|
| 987 |
return (
|
| 988 |
webcam_frame,
|
| 989 |
formatted_result,
|
| 990 |
'<div class="status-box" style="background:#d4edda; color:#155724;">โ
์๋ ๋ถ์ ์๋ฃ</div>',
|
| 991 |
f'<div class="auto-capture-status">๐ ์๋ ์บก์ฒ: ๋ง์ง๋ง ๋ถ์ {timestamp}</div>',
|
| 992 |
+
transcript_display
|
|
|
|
| 993 |
)
|
| 994 |
|
| 995 |
# ์น์บ ์คํธ๋ฆฌ๋ฐ
|
|
|
|
| 999 |
outputs=[webcam_state]
|
| 1000 |
)
|
| 1001 |
|
| 1002 |
+
# ์ค๋์ค ์คํธ๋ฆฌ๋ฐ ์ฒ๋ฆฌ
|
| 1003 |
+
def audio_stream_callback(audio_chunk):
|
| 1004 |
+
"""์ค๋์ค ์คํธ๋ฆผ ์ฝ๋ฐฑ - ๋ฒํผ์ ๋์ """
|
| 1005 |
+
accumulate_audio(audio_chunk)
|
| 1006 |
+
return None # ์ํ ์
๋ฐ์ดํธ ์์
|
| 1007 |
|
| 1008 |
+
# ์ค๋์ค ์คํธ๋ฆฌ๋ฐ ์ฐ๊ฒฐ
|
| 1009 |
audio_input.stream(
|
| 1010 |
+
fn=audio_stream_callback,
|
| 1011 |
inputs=[audio_input],
|
| 1012 |
+
outputs=None
|
| 1013 |
)
|
| 1014 |
|
| 1015 |
# ์๋ ์บก์ฒ ๋ฒํผ
|
|
|
|
| 1022 |
# ์ด๊ธฐํ ๋ฒํผ
|
| 1023 |
clear_capture_btn.click(
|
| 1024 |
fn=clear_capture,
|
| 1025 |
+
outputs=[webcam_state, captured_image, status_display, last_transcript]
|
| 1026 |
)
|
| 1027 |
|
| 1028 |
# ์์
๋ฒํผ๋ค
|
| 1029 |
planning_btn.click(
|
| 1030 |
+
fn=lambda img, p, s, t, tk: analyze_with_task(img, p, "planning", s, t, tk),
|
| 1031 |
+
inputs=[captured_image, task_prompt, use_web_search, enable_thinking, max_tokens],
|
| 1032 |
outputs=[result_output, status_display]
|
| 1033 |
)
|
| 1034 |
|
| 1035 |
grounding_btn.click(
|
| 1036 |
+
fn=lambda img, p, s, t, tk: analyze_with_task(img, p, "grounding", s, t, tk),
|
| 1037 |
+
inputs=[captured_image, task_prompt, use_web_search, enable_thinking, max_tokens],
|
| 1038 |
outputs=[result_output, status_display]
|
| 1039 |
)
|
| 1040 |
|
|
|
|
| 1072 |
|
| 1073 |
# ์ค๋์ค ํ ๊ธ ์ด๋ฒคํธ
|
| 1074 |
def toggle_audio(enabled):
|
| 1075 |
+
global audio_buffer_a, audio_buffer_b, current_buffer, last_transcription
|
| 1076 |
+
|
| 1077 |
if enabled:
|
| 1078 |
# Whisper ๋ชจ๋ธ ๋ก๋
|
| 1079 |
load_whisper()
|
| 1080 |
+
# ๋ฒํผ ์ด๊ธฐํ
|
| 1081 |
+
with audio_buffer_lock:
|
| 1082 |
+
audio_buffer_a.clear()
|
| 1083 |
+
audio_buffer_b.clear()
|
| 1084 |
+
current_buffer = 'a'
|
| 1085 |
+
last_transcription = ""
|
| 1086 |
+
|
| 1087 |
return (
|
| 1088 |
gr.update(visible=True), # audio_input ํ์
|
| 1089 |
+
'<div class="audio-status">๐ค ์์ฑ ์ธ์: ํ์ฑํ๋จ (10์ด ๊ต๋ ๋
น์)</div>'
|
| 1090 |
)
|
| 1091 |
else:
|
| 1092 |
+
# ๋ฒํผ ์ด๊ธฐํ
|
| 1093 |
+
with audio_buffer_lock:
|
| 1094 |
+
audio_buffer_a.clear()
|
| 1095 |
+
audio_buffer_b.clear()
|
| 1096 |
+
last_transcription = ""
|
| 1097 |
+
|
| 1098 |
return (
|
| 1099 |
gr.update(visible=False), # audio_input ์จ๊น
|
| 1100 |
'<div class="audio-status">๐ค ์์ฑ ์ธ์: ๋นํ์ฑํ</div>'
|
|
|
|
| 1109 |
# ํ์ด๋จธ ํฑ ์ด๋ฒคํธ
|
| 1110 |
timer.tick(
|
| 1111 |
fn=auto_capture_and_analyze,
|
| 1112 |
+
inputs=[webcam_state, task_prompt, use_web_search, enable_thinking, max_tokens, use_audio_toggle],
|
| 1113 |
+
outputs=[captured_image, result_output, status_display, auto_capture_status, last_transcript]
|
| 1114 |
)
|
| 1115 |
|
| 1116 |
# ์ด๊ธฐ ๋ชจ๋ธ ๋ก๋
|
| 1117 |
def initial_load():
|
| 1118 |
load_model()
|
| 1119 |
+
|
| 1120 |
+
# ์ค๋์ค ์์ปค ์ค๋ ๋ ์์
|
| 1121 |
+
audio_worker_thread = Thread(target=audio_processing_worker, daemon=True)
|
| 1122 |
+
audio_worker_thread.start()
|
| 1123 |
+
|
| 1124 |
return "์์คํ
์ค๋น ์๋ฃ! ๐"
|
| 1125 |
|
| 1126 |
demo.load(
|
|
|
|
| 1129 |
)
|
| 1130 |
|
| 1131 |
if __name__ == "__main__":
|
| 1132 |
+
print("๐ ๋ก๋ด ์๊ฐ ์์คํ
์์ (Gemma3-R1984-4B + Whisper 10์ด ๊ต๋ ๋
น์)...")
|
| 1133 |
+
demo.queue().launch(
|
| 1134 |
server_name="0.0.0.0",
|
| 1135 |
server_port=7860,
|
| 1136 |
share=False,
|