Update app.py
Browse files
app.py
CHANGED
@@ -1,62 +1,57 @@
|
|
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 |
|
14 |
-
|
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 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
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
|
45 |
def chat():
|
46 |
-
data = request.
|
47 |
-
if not data or "text" not in data:
|
48 |
-
return jsonify({"error": "Missing 'text' in request body"}), 400
|
49 |
|
50 |
-
|
51 |
-
|
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
|
62 |
-
app.run(host="0.0.0.0", port=7860)
|
|
|
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 |
+
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 |
+
@app.route("/talk", methods=["POST"])
|
|
|
|
|
|
|
|
|
18 |
def talk():
|
19 |
+
if "audio" not in request.files:
|
20 |
+
return jsonify({"error": "No audio file"}), 400
|
21 |
|
22 |
+
# Save audio
|
23 |
+
audio_file = request.files["audio"]
|
24 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:
|
25 |
+
audio_path = tmp.name
|
26 |
+
audio_file.save(audio_path)
|
|
|
27 |
|
28 |
+
# Transcribe
|
29 |
+
segments, _ = whisper_model.transcribe(audio_path)
|
30 |
+
transcription = "".join([seg.text for seg in segments])
|
31 |
|
32 |
+
# Generate response
|
33 |
+
response_text = llm(transcription)[0]["generated_text"]
|
34 |
|
35 |
+
# Synthesize speech
|
36 |
+
tts_audio_path = audio_path.replace(".wav", "_reply.wav")
|
37 |
+
tts.tts_to_file(text=response_text, file_path=tts_audio_path)
|
38 |
|
39 |
+
return send_file(tts_audio_path, mimetype="audio/wav")
|
|
|
40 |
|
41 |
+
@app.route("/chat", methods=["POST"])
|
42 |
def chat():
|
43 |
+
data = request.get_json()
|
44 |
+
if not data or "text" not in data:
|
45 |
+
return jsonify({"error": "Missing 'text' in request body"}), 400
|
46 |
|
47 |
+
user_input = data["text"]
|
48 |
+
response = llm(user_input)[0]["generated_text"]
|
|
|
49 |
|
50 |
+
return jsonify({"response": response})
|
|
|
51 |
|
52 |
@app.route("/")
|
53 |
def index():
|
54 |
+
return "Metaverse AI Character API running."
|
55 |
|
56 |
+
if __name__ == "__main__":
|
57 |
+
app.run(host="0.0.0.0", port=7860)
|