Update app.py
Browse files
app.py
CHANGED
@@ -1,79 +1,62 @@
|
|
1 |
-
from flask import Flask, request, jsonify,
|
2 |
-
from
|
3 |
-
from
|
4 |
from transformers import pipeline
|
5 |
from TTS.api import TTS
|
6 |
import tempfile
|
7 |
import os
|
8 |
|
9 |
-
app = Flask(
|
10 |
CORS(app)
|
11 |
|
12 |
-
# Load models
|
13 |
-
whisper_model = WhisperModel("small", device="cpu", compute_type="int8")
|
14 |
-
llm = pipeline("text-generation", model="tiiuae/falcon-rw-1b", max_new_tokens=100)
|
15 |
-
tts = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC", progress_bar=False, gpu=False)
|
16 |
|
17 |
-
|
|
|
|
|
|
|
|
|
18 |
def talk():
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
# Extract only the AI's response (everything after "AI:")
|
37 |
-
try:
|
38 |
-
ai_response = response_raw.split("AI:")[1].strip()
|
39 |
-
except:
|
40 |
-
# Fallback if splitting fails
|
41 |
-
ai_response = response_raw
|
42 |
-
|
43 |
-
# Synthesize speech using only the AI's response
|
44 |
-
tts_audio_path = audio_path.replace(".wav", "_reply.wav")
|
45 |
-
tts.tts_to_file(text=ai_response, file_path=tts_audio_path)
|
46 |
-
|
47 |
-
# Clean up the original audio file
|
48 |
-
try:
|
49 |
-
os.unlink(audio_path)
|
50 |
-
except:
|
51 |
-
pass
|
52 |
-
|
53 |
-
return send_file(tts_audio_path, mimetype="audio/wav")
|
54 |
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
def chat():
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
try:
|
68 |
-
ai_response = response_raw.split("AI:")[1].strip()
|
69 |
-
except:
|
70 |
-
ai_response = response_raw
|
71 |
-
|
72 |
-
return jsonify({"response": ai_response})
|
73 |
|
74 |
@app.route("/")
|
75 |
def index():
|
76 |
-
|
77 |
|
78 |
-
if
|
79 |
-
|
|
|
1 |
+
from flask import Flask, request, jsonify, send\_file
|
2 |
+
from flask\_cors import CORS
|
3 |
+
from faster\_whisper import WhisperModel
|
4 |
from transformers import pipeline
|
5 |
from TTS.api import TTS
|
6 |
import tempfile
|
7 |
import os
|
8 |
|
9 |
+
app = Flask(**name**)
|
10 |
CORS(app)
|
11 |
|
12 |
+
# Load models
|
|
|
|
|
|
|
13 |
|
14 |
+
whisper\_model = WhisperModel("small", device="cpu", compute\_type="int8")
|
15 |
+
llm = pipeline("text-generation", model="tiiuae/falcon-rw-1b", max\_new\_tokens=100)
|
16 |
+
tts = TTS(model\_name="tts\_models/en/ljspeech/tacotron2-DDC", progress\_bar=False, gpu=False)
|
17 |
+
|
18 |
+
@app.route("/talk", methods=\["POST"])
|
19 |
def talk():
|
20 |
+
if "audio" not in request.files:
|
21 |
+
return jsonify({"error": "No audio file"}), 400
|
22 |
+
|
23 |
+
```
|
24 |
+
# Save audio
|
25 |
+
audio_file = request.files["audio"]
|
26 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:
|
27 |
+
audio_path = tmp.name
|
28 |
+
audio_file.save(audio_path)
|
29 |
+
|
30 |
+
# Transcribe
|
31 |
+
segments, _ = whisper_model.transcribe(audio_path)
|
32 |
+
transcription = "".join([seg.text for seg in segments])
|
33 |
+
|
34 |
+
# Generate response
|
35 |
+
response_text = llm(transcription)[0]["generated_text"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
+
# Synthesize speech
|
38 |
+
tts_audio_path = audio_path.replace(".wav", "_reply.wav")
|
39 |
+
tts.tts_to_file(text=response_text, file_path=tts_audio_path)
|
40 |
+
|
41 |
+
return send_file(tts_audio_path, mimetype="audio/wav")
|
42 |
+
```
|
43 |
+
|
44 |
+
@app.route("/chat", methods=\["POST"])
|
45 |
def chat():
|
46 |
+
data = request.get\_json()
|
47 |
+
if not data or "text" not in data:
|
48 |
+
return jsonify({"error": "Missing 'text' in request body"}), 400
|
49 |
+
|
50 |
+
```
|
51 |
+
user_input = data["text"]
|
52 |
+
response = llm(user_input)[0]["generated_text"]
|
53 |
+
|
54 |
+
return jsonify({"response": response})
|
55 |
+
```
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
@app.route("/")
|
58 |
def index():
|
59 |
+
return "Metaverse AI Character API running."
|
60 |
|
61 |
+
if **name** == "**main**":
|
62 |
+
app.run(host="0.0.0.0", port=7860)
|