Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -23,21 +23,18 @@ if not GEMINI_API_KEY: print("⚠️ GEMINI_API_KEYが設定されていませ
|
|
| 23 |
if not GROQ_API_KEY: print("⚠️ GROQ_API_KEYが設定されていません")
|
| 24 |
|
| 25 |
genai.configure(api_key=GEMINI_API_KEY)
|
| 26 |
-
# Groqクライアント
|
| 27 |
groq_client = Groq(api_key=GROQ_API_KEY) if GROQ_API_KEY else None
|
| 28 |
|
| 29 |
-
# ログ設定
|
| 30 |
sys.stdout.reconfigure(line_buffering=True)
|
| 31 |
|
| 32 |
app = Flask(__name__)
|
| 33 |
app.config['SECRET_KEY'] = os.getenv("SECRET_KEY", "secret!")
|
| 34 |
socketio = SocketIO(app, cors_allowed_origins="*", max_http_buffer_size=10 * 1024 * 1024)
|
| 35 |
|
| 36 |
-
# Geminiモデル
|
| 37 |
model = genai.GenerativeModel("models/gemini-2.0-flash")
|
| 38 |
|
| 39 |
active_processors = {}
|
| 40 |
-
user_settings = {}
|
| 41 |
SILENCE_THRESHOLD = 500
|
| 42 |
|
| 43 |
class AudioProcessor:
|
|
@@ -45,6 +42,7 @@ class AudioProcessor:
|
|
| 45 |
self.sid = sid
|
| 46 |
self.running = True
|
| 47 |
self.settings = {'target_lang': 'ja', 'max_speakers': 2}
|
|
|
|
| 48 |
|
| 49 |
# FFmpegプロセス
|
| 50 |
self.ffmpeg_proc = subprocess.Popen(
|
|
@@ -71,6 +69,11 @@ class AudioProcessor:
|
|
| 71 |
def update_settings(self, data):
|
| 72 |
self.settings.update(data)
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
def stop(self):
|
| 75 |
self.running = False
|
| 76 |
if self.ffmpeg_proc:
|
|
@@ -80,52 +83,36 @@ class AudioProcessor:
|
|
| 80 |
except: pass
|
| 81 |
print(f"🛑 プロセッサ停止: {self.sid}")
|
| 82 |
|
| 83 |
-
# ---
|
| 84 |
-
def _call_groq_fast(self, wav_bytes, chunk_id):
|
| 85 |
if not groq_client: return
|
| 86 |
try:
|
| 87 |
-
#
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
# Groq APIへの送信 (Translation or Transcription)
|
| 91 |
-
# ※GroqのWhisperは英語への翻訳(translations)か、そのままの書き起こし(transcriptions)が得意。
|
| 92 |
-
# 日本語への直接翻訳はプロンプトで指示するか、別途翻訳が必要だが、
|
| 93 |
-
# ここでは「とりあえずの書き起こし」または「英語翻訳」として速さを優先する
|
| 94 |
-
|
| 95 |
-
# 今回は「transcriptions」を使用し、promptで翻訳を促すアプローチ
|
| 96 |
-
# (または、Groqの出力後に高速な軽量翻訳を噛ませる手もあるが、まずはWhisperの能力に頼る)
|
| 97 |
-
|
| 98 |
-
filename = "audio.wav"
|
| 99 |
-
# 日本語ターゲットの場合は、Whisperに「日本語で出力して」と頼むのは難しい場合があるが、
|
| 100 |
-
# prompt="Translate to Japanese" と入れると効くことがある
|
| 101 |
|
| 102 |
completion = groq_client.audio.transcriptions.create(
|
| 103 |
-
file=(
|
| 104 |
model="whisper-large-v3",
|
| 105 |
-
prompt=
|
| 106 |
response_format="json",
|
| 107 |
temperature=0.0
|
| 108 |
)
|
| 109 |
|
| 110 |
text = completion.text.strip()
|
| 111 |
if text:
|
|
|
|
| 112 |
socketio.emit('subtitle_preview', {
|
| 113 |
'chunk_id': chunk_id,
|
| 114 |
'text': text
|
| 115 |
}, room=self.sid)
|
| 116 |
-
|
| 117 |
|
| 118 |
except Exception as e:
|
| 119 |
print(f"⚠️ Groqエラー: {e}")
|
| 120 |
|
| 121 |
-
# ---
|
| 122 |
-
def _call_gemini_accurate(self, wav_bytes, chunk_id):
|
| 123 |
try:
|
| 124 |
-
settings = self.settings
|
| 125 |
-
global tgt_name
|
| 126 |
-
tgt_name = "Japanese" if settings.get('target_lang') == 'ja' else "English"
|
| 127 |
-
max_spk = settings.get('max_speakers', 2)
|
| 128 |
-
|
| 129 |
prompt = f"""
|
| 130 |
Task: Listen to the audio.
|
| 131 |
1. Identify distinct speakers (Max {max_spk}).
|
|
@@ -136,7 +123,8 @@ class AudioProcessor:
|
|
| 136 |
|
| 137 |
Rules:
|
| 138 |
- Output ONLY translation.
|
| 139 |
-
-
|
|
|
|
| 140 |
"""
|
| 141 |
|
| 142 |
response = model.generate_content([
|
|
@@ -146,7 +134,6 @@ class AudioProcessor:
|
|
| 146 |
text = response.text.strip()
|
| 147 |
|
| 148 |
if text:
|
| 149 |
-
# 整形して送信
|
| 150 |
parsed_lines = []
|
| 151 |
for line in text.split('\n'):
|
| 152 |
if ':' in line:
|
|
@@ -157,22 +144,29 @@ class AudioProcessor:
|
|
| 157 |
})
|
| 158 |
|
| 159 |
if parsed_lines:
|
|
|
|
| 160 |
socketio.emit('subtitle_final', {
|
| 161 |
'chunk_id': chunk_id,
|
| 162 |
'results': parsed_lines
|
| 163 |
}, room=self.sid)
|
| 164 |
-
print(f"✅ Gemini確定: {parsed_lines[0]['text']}")
|
| 165 |
|
| 166 |
except Exception as e:
|
| 167 |
print(f"⚠️ Geminiエラー: {e}")
|
| 168 |
|
| 169 |
def _process_loop(self):
|
| 170 |
-
CHUNK_DURATION =
|
| 171 |
REQUIRED_BYTES = 16000 * 2 * CHUNK_DURATION
|
| 172 |
pcm_buffer = bytearray()
|
| 173 |
|
| 174 |
while self.running:
|
| 175 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
raw_chunk = self.ffmpeg_proc.stdout.read(4096)
|
| 177 |
if not raw_chunk:
|
| 178 |
time.sleep(0.1)
|
|
@@ -183,12 +177,13 @@ class AudioProcessor:
|
|
| 183 |
|
| 184 |
if len(pcm_buffer) >= REQUIRED_BYTES:
|
| 185 |
to_process = pcm_buffer[:REQUIRED_BYTES]
|
| 186 |
-
pcm_buffer = pcm_buffer[REQUIRED_BYTES:]
|
| 187 |
|
| 188 |
-
# 無音検知
|
| 189 |
try:
|
| 190 |
rms = audioop.rms(to_process, 2)
|
| 191 |
-
if rms < SILENCE_THRESHOLD:
|
|
|
|
| 192 |
except: pass
|
| 193 |
|
| 194 |
# WAV作成
|
|
@@ -198,12 +193,15 @@ class AudioProcessor:
|
|
| 198 |
wf.writeframes(to_process)
|
| 199 |
wav_bytes = wav_io.getvalue()
|
| 200 |
|
| 201 |
-
#
|
|
|
|
|
|
|
|
|
|
| 202 |
chunk_id = str(uuid.uuid4())
|
| 203 |
|
| 204 |
-
# ★並列
|
| 205 |
-
t1 = threading.Thread(target=self._call_groq_fast, args=(wav_bytes, chunk_id))
|
| 206 |
-
t2 = threading.Thread(target=self._call_gemini_accurate, args=(wav_bytes, chunk_id))
|
| 207 |
t1.start()
|
| 208 |
t2.start()
|
| 209 |
|
|
@@ -232,11 +230,15 @@ def handle_audio_stream(data):
|
|
| 232 |
if request.sid in active_processors:
|
| 233 |
active_processors[request.sid].write_data(data)
|
| 234 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
@socketio.on('request_speaker_profiles')
|
| 236 |
def req_prof(): emit('initial_speaker_profiles', {}, room=request.sid)
|
| 237 |
|
| 238 |
-
@socketio.on('reset_speakers')
|
| 239 |
-
def reset(): pass
|
| 240 |
-
|
| 241 |
if __name__ == "__main__":
|
| 242 |
socketio.run(app, host='0.0.0.0', port=7860, allow_unsafe_werkzeug=True)
|
|
|
|
| 23 |
if not GROQ_API_KEY: print("⚠️ GROQ_API_KEYが設定されていません")
|
| 24 |
|
| 25 |
genai.configure(api_key=GEMINI_API_KEY)
|
|
|
|
| 26 |
groq_client = Groq(api_key=GROQ_API_KEY) if GROQ_API_KEY else None
|
| 27 |
|
|
|
|
| 28 |
sys.stdout.reconfigure(line_buffering=True)
|
| 29 |
|
| 30 |
app = Flask(__name__)
|
| 31 |
app.config['SECRET_KEY'] = os.getenv("SECRET_KEY", "secret!")
|
| 32 |
socketio = SocketIO(app, cors_allowed_origins="*", max_http_buffer_size=10 * 1024 * 1024)
|
| 33 |
|
| 34 |
+
# Geminiモデル (確定用)
|
| 35 |
model = genai.GenerativeModel("models/gemini-2.0-flash")
|
| 36 |
|
| 37 |
active_processors = {}
|
|
|
|
| 38 |
SILENCE_THRESHOLD = 500
|
| 39 |
|
| 40 |
class AudioProcessor:
|
|
|
|
| 42 |
self.sid = sid
|
| 43 |
self.running = True
|
| 44 |
self.settings = {'target_lang': 'ja', 'max_speakers': 2}
|
| 45 |
+
self.reset_flag = False # バッファリセット用フラグ
|
| 46 |
|
| 47 |
# FFmpegプロセス
|
| 48 |
self.ffmpeg_proc = subprocess.Popen(
|
|
|
|
| 69 |
def update_settings(self, data):
|
| 70 |
self.settings.update(data)
|
| 71 |
|
| 72 |
+
def trigger_reset(self):
|
| 73 |
+
"""シーク時などにバッファを強制クリアする"""
|
| 74 |
+
self.reset_flag = True
|
| 75 |
+
print(f"🔄 バッファリセット予約: {self.sid}")
|
| 76 |
+
|
| 77 |
def stop(self):
|
| 78 |
self.running = False
|
| 79 |
if self.ffmpeg_proc:
|
|
|
|
| 83 |
except: pass
|
| 84 |
print(f"🛑 プロセッサ停止: {self.sid}")
|
| 85 |
|
| 86 |
+
# --- 速報: Groq (Whisper) ---
|
| 87 |
+
def _call_groq_fast(self, wav_bytes, chunk_id, tgt_name):
|
| 88 |
if not groq_client: return
|
| 89 |
try:
|
| 90 |
+
# Whisperへの指示
|
| 91 |
+
prompt_text = f"Translate everything into {tgt_name}."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
completion = groq_client.audio.transcriptions.create(
|
| 94 |
+
file=("audio.wav", wav_bytes),
|
| 95 |
model="whisper-large-v3",
|
| 96 |
+
prompt=prompt_text,
|
| 97 |
response_format="json",
|
| 98 |
temperature=0.0
|
| 99 |
)
|
| 100 |
|
| 101 |
text = completion.text.strip()
|
| 102 |
if text:
|
| 103 |
+
# 速報を送信
|
| 104 |
socketio.emit('subtitle_preview', {
|
| 105 |
'chunk_id': chunk_id,
|
| 106 |
'text': text
|
| 107 |
}, room=self.sid)
|
| 108 |
+
print(f"🚀 Groq速報: {text[:20]}...")
|
| 109 |
|
| 110 |
except Exception as e:
|
| 111 |
print(f"⚠️ Groqエラー: {e}")
|
| 112 |
|
| 113 |
+
# --- 確定: Gemini ---
|
| 114 |
+
def _call_gemini_accurate(self, wav_bytes, chunk_id, tgt_name, max_spk):
|
| 115 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
prompt = f"""
|
| 117 |
Task: Listen to the audio.
|
| 118 |
1. Identify distinct speakers (Max {max_spk}).
|
|
|
|
| 123 |
|
| 124 |
Rules:
|
| 125 |
- Output ONLY translation.
|
| 126 |
+
- If one person, use "Speaker 1".
|
| 127 |
+
- Adapt tone/context.
|
| 128 |
"""
|
| 129 |
|
| 130 |
response = model.generate_content([
|
|
|
|
| 134 |
text = response.text.strip()
|
| 135 |
|
| 136 |
if text:
|
|
|
|
| 137 |
parsed_lines = []
|
| 138 |
for line in text.split('\n'):
|
| 139 |
if ':' in line:
|
|
|
|
| 144 |
})
|
| 145 |
|
| 146 |
if parsed_lines:
|
| 147 |
+
# 確定を送信
|
| 148 |
socketio.emit('subtitle_final', {
|
| 149 |
'chunk_id': chunk_id,
|
| 150 |
'results': parsed_lines
|
| 151 |
}, room=self.sid)
|
| 152 |
+
print(f"✅ Gemini確定: {parsed_lines[0]['text'][:20]}...")
|
| 153 |
|
| 154 |
except Exception as e:
|
| 155 |
print(f"⚠️ Geminiエラー: {e}")
|
| 156 |
|
| 157 |
def _process_loop(self):
|
| 158 |
+
CHUNK_DURATION = 4 # バッファサイズ (秒)
|
| 159 |
REQUIRED_BYTES = 16000 * 2 * CHUNK_DURATION
|
| 160 |
pcm_buffer = bytearray()
|
| 161 |
|
| 162 |
while self.running:
|
| 163 |
try:
|
| 164 |
+
# バッファリセット判定
|
| 165 |
+
if self.reset_flag:
|
| 166 |
+
pcm_buffer.clear()
|
| 167 |
+
self.reset_flag = False
|
| 168 |
+
print("✨ バッファクリア完了")
|
| 169 |
+
|
| 170 |
raw_chunk = self.ffmpeg_proc.stdout.read(4096)
|
| 171 |
if not raw_chunk:
|
| 172 |
time.sleep(0.1)
|
|
|
|
| 177 |
|
| 178 |
if len(pcm_buffer) >= REQUIRED_BYTES:
|
| 179 |
to_process = pcm_buffer[:REQUIRED_BYTES]
|
| 180 |
+
pcm_buffer = pcm_buffer[REQUIRED_BYTES:] # スライドさせるか、オーバーラップさせるか
|
| 181 |
|
| 182 |
+
# 無音検知 (VAD)
|
| 183 |
try:
|
| 184 |
rms = audioop.rms(to_process, 2)
|
| 185 |
+
if rms < SILENCE_THRESHOLD:
|
| 186 |
+
continue # 無音ならスキップ
|
| 187 |
except: pass
|
| 188 |
|
| 189 |
# WAV作成
|
|
|
|
| 193 |
wf.writeframes(to_process)
|
| 194 |
wav_bytes = wav_io.getvalue()
|
| 195 |
|
| 196 |
+
# 設定取得
|
| 197 |
+
tgt_lang = self.settings.get('target_lang', 'ja')
|
| 198 |
+
tgt_name = "Japanese" if tgt_lang == 'ja' else "English"
|
| 199 |
+
max_spk = self.settings.get('max_speakers', 2)
|
| 200 |
chunk_id = str(uuid.uuid4())
|
| 201 |
|
| 202 |
+
# ★並列処理開始
|
| 203 |
+
t1 = threading.Thread(target=self._call_groq_fast, args=(wav_bytes, chunk_id, tgt_name))
|
| 204 |
+
t2 = threading.Thread(target=self._call_gemini_accurate, args=(wav_bytes, chunk_id, tgt_name, max_spk))
|
| 205 |
t1.start()
|
| 206 |
t2.start()
|
| 207 |
|
|
|
|
| 230 |
if request.sid in active_processors:
|
| 231 |
active_processors[request.sid].write_data(data)
|
| 232 |
|
| 233 |
+
# ★追加: クライアントからのリセット要求
|
| 234 |
+
@socketio.on('reset_stream')
|
| 235 |
+
def handle_stream_reset():
|
| 236 |
+
sid = request.sid
|
| 237 |
+
if sid in active_processors:
|
| 238 |
+
active_processors[sid].trigger_reset()
|
| 239 |
+
|
| 240 |
@socketio.on('request_speaker_profiles')
|
| 241 |
def req_prof(): emit('initial_speaker_profiles', {}, room=request.sid)
|
| 242 |
|
|
|
|
|
|
|
|
|
|
| 243 |
if __name__ == "__main__":
|
| 244 |
socketio.run(app, host='0.0.0.0', port=7860, allow_unsafe_werkzeug=True)
|