Update app.py
Browse files
app.py
CHANGED
|
@@ -24,7 +24,7 @@ os.makedirs(TEMP_DIR, exist_ok=True)
|
|
| 24 |
# Mô hình sử dụng
|
| 25 |
STT_MODEL = "openai/whisper-tiny.en"
|
| 26 |
TTS_MODEL = "facebook/mms-tts-eng"
|
| 27 |
-
CHAT_MODEL = "mistralai/Mistral-7B-Instruct-v0.1" #
|
| 28 |
|
| 29 |
# Giao diện HTML
|
| 30 |
HTML_CONTENT = """
|
|
@@ -64,7 +64,7 @@ async def text_to_speech(text: str) -> str:
|
|
| 64 |
output_path = os.path.join(TEMP_DIR, "output.wav")
|
| 65 |
try:
|
| 66 |
print(f"🔄 Đang xử lý TTS với model {TTS_MODEL}...")
|
| 67 |
-
response = client.text_to_speech(
|
| 68 |
with open(output_path, "wb") as f:
|
| 69 |
f.write(response)
|
| 70 |
with open(output_path, "rb") as f:
|
|
@@ -78,7 +78,7 @@ async def speech_to_text(file: UploadFile) -> str:
|
|
| 78 |
try:
|
| 79 |
print(f"🔄 Đang xử lý STT với model {STT_MODEL}...")
|
| 80 |
audio_data = await file.read()
|
| 81 |
-
response = client.automatic_speech_recognition(
|
| 82 |
return response.get("text", "Không nghe được gì.")
|
| 83 |
except Exception as e:
|
| 84 |
print(f"❌ Lỗi STT: {e}")
|
|
@@ -87,7 +87,7 @@ async def speech_to_text(file: UploadFile) -> str:
|
|
| 87 |
async def generate_text(prompt: str) -> str:
|
| 88 |
try:
|
| 89 |
print(f"🔄 Đang xử lý hội thoại với model {CHAT_MODEL}...")
|
| 90 |
-
response = client.text_generation(
|
| 91 |
return response.get("generated_text", "Xin lỗi, tôi gặp lỗi khi xử lý câu hỏi của bạn.").strip()
|
| 92 |
except Exception as e:
|
| 93 |
print(f"❌ Lỗi xử lý hội thoại: {e}")
|
|
@@ -113,4 +113,4 @@ async def chatbot(prompt: str = Form(None), file: UploadFile = None):
|
|
| 113 |
|
| 114 |
if __name__ == "__main__":
|
| 115 |
print("🚀 Khởi động FastAPI Server...")
|
| 116 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 24 |
# Mô hình sử dụng
|
| 25 |
STT_MODEL = "openai/whisper-tiny.en"
|
| 26 |
TTS_MODEL = "facebook/mms-tts-eng"
|
| 27 |
+
CHAT_MODEL = "mistralai/Mistral-7B-Instruct-v0.1" # Model nhẹ hơn
|
| 28 |
|
| 29 |
# Giao diện HTML
|
| 30 |
HTML_CONTENT = """
|
|
|
|
| 64 |
output_path = os.path.join(TEMP_DIR, "output.wav")
|
| 65 |
try:
|
| 66 |
print(f"🔄 Đang xử lý TTS với model {TTS_MODEL}...")
|
| 67 |
+
response = client.text_to_speech(TTS_MODEL, text)
|
| 68 |
with open(output_path, "wb") as f:
|
| 69 |
f.write(response)
|
| 70 |
with open(output_path, "rb") as f:
|
|
|
|
| 78 |
try:
|
| 79 |
print(f"🔄 Đang xử lý STT với model {STT_MODEL}...")
|
| 80 |
audio_data = await file.read()
|
| 81 |
+
response = client.automatic_speech_recognition(STT_MODEL, audio_data)
|
| 82 |
return response.get("text", "Không nghe được gì.")
|
| 83 |
except Exception as e:
|
| 84 |
print(f"❌ Lỗi STT: {e}")
|
|
|
|
| 87 |
async def generate_text(prompt: str) -> str:
|
| 88 |
try:
|
| 89 |
print(f"🔄 Đang xử lý hội thoại với model {CHAT_MODEL}...")
|
| 90 |
+
response = client.text_generation(CHAT_MODEL, prompt, max_new_tokens=100)
|
| 91 |
return response.get("generated_text", "Xin lỗi, tôi gặp lỗi khi xử lý câu hỏi của bạn.").strip()
|
| 92 |
except Exception as e:
|
| 93 |
print(f"❌ Lỗi xử lý hội thoại: {e}")
|
|
|
|
| 113 |
|
| 114 |
if __name__ == "__main__":
|
| 115 |
print("🚀 Khởi động FastAPI Server...")
|
| 116 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|