huynhkimthien commited on
Commit
1a94ea5
·
verified ·
1 Parent(s): 9787f53

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -36
app.py CHANGED
@@ -78,42 +78,54 @@ async def chat(request: ChatRequest):
78
 
79
  # Endpoint voice chat + TTS
80
  @app.post("/voice_chat")
81
- async def voice_chat(file: UploadFile = File(...)):
82
- file_location = f"temp_{file.filename}"
83
- with open(file_location, "wb") as f:
84
- f.write(await file.read())
85
-
86
- result = whisper_model.transcribe(file_location, language="vi")
87
- user_text = result["text"]
88
- os.remove(file_location)
89
-
90
- # Kiểm tra yêu cầu mở nhạc
91
- if any(kw in user_text.lower() for kw in ["nghe nhạc", "mở bài hát", "bài hát", "bài"]):
92
- song_name = extract_song_name(user_text)
93
- if song_name:
94
- wav_path = download_youtube_as_wav(song_name)
95
- if wav_path:
96
- return FileResponse(wav_path, media_type="audio/wav", filename="song.wav")
97
- else:
98
- return {"error": "Không tìm thấy hoặc tải được bài hát."}
99
-
100
- # Nếu không phải yêu cầu mở nhạc xử như
101
- conversation.append({"role": "user", "content": user_text})
102
- text = tokenizer.apply_chat_template(conversation, tokenize=False, add_generation_prompt=True)
103
- model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
104
- response_text = generate_full_response(model_inputs)
105
- conversation.append({"role": "assistant", "content": response_text})
106
-
107
- tts = gTTS(response_text, lang="vi")
108
- audio_file = "response.mp3"
109
- tts.save(audio_file)
110
-
111
- return {
112
- "user_text": user_text,
113
- "response": response_text,
114
- "audio_url": "/get_audio"
115
- }
116
-
 
 
 
 
 
 
 
 
 
 
 
 
117
  # Endpoint trả về file âm thanh
118
  @app.get("/get_audio")
119
  async def get_audio():
 
78
 
79
  # Endpoint voice chat + TTS
80
  @app.post("/voice_chat")
81
+ async def voice_chat(request: Request):
82
+ try:
83
+ raw_audio = await request.body()
84
+ sample_rate = 16000
85
+ audio_np = np.frombuffer(raw_audio, dtype=np.int16)
86
+
87
+ # Chuyển thành WAV
88
+ wav_io = io.BytesIO()
89
+ wav.write(wav_io, sample_rate, audio_np)
90
+ wav_io.seek(0)
91
+
92
+ with open("temp_audio.wav", "wb") as f:
93
+ f.write(wav_io.read())
94
+
95
+ # Whisper nhận dạng
96
+ result = whisper_model.transcribe("temp_audio.wav", language="vi")
97
+ user_text = result["text"]
98
+
99
+ # Kiểm tra yêu cầu mở nhạc
100
+ if any(kw in user_text.lower() for kw in ["nghe nhạc", "mở bài hát", "bài hát", "bài"]):
101
+ song_name = extract_song_name(user_text)
102
+ if song_name:
103
+ wav_path = download_youtube_as_wav(song_name)
104
+ if wav_path:
105
+ return FileResponse(wav_path, media_type="audio/wav", filename="song.wav")
106
+ else:
107
+ return JSONResponse({"error": "Không tìm thấy hoặc tải được bài hát."}, status_code=404)
108
+
109
+ # Xử lý hội thoại
110
+ conversation.append({"role": "user", "content": user_text})
111
+ text = tokenizer.apply_chat_template(conversation, tokenize=False, add_generation_prompt=True)
112
+ model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
113
+ response_text = generate_full_response(model_inputs)
114
+ conversation.append({"role": "assistant", "content": response_text})
115
+
116
+ # TTS
117
+ tts = gTTS(response_text, lang="vi")
118
+ audio_file = "response.mp3"
119
+ tts.save(audio_file)
120
+
121
+ return {
122
+ "user_text": user_text,
123
+ "response": response_text,
124
+ "audio_url": "/get_audio"
125
+ }
126
+
127
+ except Exception as e:
128
+ return JSONResponse({"error": str(e)}, status_code=500)
129
  # Endpoint trả về file âm thanh
130
  @app.get("/get_audio")
131
  async def get_audio():