Update app.py
Browse files
app.py
CHANGED
@@ -1,24 +1,23 @@
|
|
1 |
import os
|
|
|
|
|
2 |
import tempfile
|
3 |
-
from flask import Flask, request, jsonify, send_file
|
4 |
-
from gradio_client import Client
|
5 |
-
from openai_chat_module import OpenaiChatModule
|
6 |
-
from text2speech import VITSApiTTS
|
7 |
|
8 |
app = Flask(__name__)
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
13 |
if 'audio' not in request.files:
|
14 |
return jsonify({"error": "No audio file provided"})
|
15 |
|
16 |
file = request.files['audio']
|
17 |
filename = request.form.get('filename')
|
18 |
-
modelid = request.form.get('modelid', default=2)
|
19 |
-
|
20 |
-
if not filename:
|
21 |
-
return jsonify({"error": "No filename parameter provided"})
|
22 |
|
23 |
# Create a temporary directory
|
24 |
temp_dir = tempfile.mkdtemp()
|
@@ -27,36 +26,16 @@ def upload_and_return_temp_file():
|
|
27 |
destination_path = os.path.join(temp_dir, f"{filename}.wav")
|
28 |
file.save(destination_path)
|
29 |
print(destination_path)
|
30 |
-
|
31 |
-
|
32 |
-
client = Client()
|
33 |
result = client.predict(
|
|
|
34 |
destination_path, # str (filepath or URL to file) in 'Input' Audio component
|
35 |
api_name="/predict"
|
36 |
)
|
37 |
-
|
38 |
-
openai_chat_module = OpenaiChatModule('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')
|
39 |
-
text = openai_chat_module.chat_with_origin_model(result)
|
40 |
-
print(text)
|
41 |
-
vits_tts = VITSApiTTS(modelid)
|
42 |
-
audio_data = vits_tts.text_to_speech_and_play(text)
|
43 |
-
|
44 |
-
# Save audio data as a file in the temporary directory
|
45 |
-
audio_file_path = os.path.join(temp_dir, ""+filename+".mp3")
|
46 |
-
with open(audio_file_path, "wb") as f:
|
47 |
-
f.write(audio_data)
|
48 |
-
print(audio_file_path)
|
49 |
-
# Return the audio file as a response with the correct content type
|
50 |
-
response = send_file(audio_file_path, mimetype="audio/mp3")
|
51 |
-
|
52 |
-
# # # Cleanup: Delete temporary files and directory
|
53 |
os.remove(destination_path)
|
54 |
os.rmdir(temp_dir)
|
55 |
-
|
56 |
-
return response
|
57 |
-
|
58 |
-
# except Exception as e:
|
59 |
-
# return jsonify({"error": str(e)})
|
60 |
|
61 |
if __name__ == '__main__':
|
62 |
-
app.run(host='0.0.0.0', port=5000
|
|
|
1 |
import os
|
2 |
+
from flask import Flask, request, jsonify
|
3 |
+
import gradio as gr,Client
|
4 |
import tempfile
|
|
|
|
|
|
|
|
|
5 |
|
6 |
app = Flask(__name__)
|
7 |
|
8 |
+
def process_input(input_text):
|
9 |
+
# 处理函数
|
10 |
+
result = "Processed: " + input_text
|
11 |
+
return result
|
12 |
+
|
13 |
+
@app.route('/api', methods=['POST'])
|
14 |
+
def api():
|
15 |
+
# try:
|
16 |
if 'audio' not in request.files:
|
17 |
return jsonify({"error": "No audio file provided"})
|
18 |
|
19 |
file = request.files['audio']
|
20 |
filename = request.form.get('filename')
|
|
|
|
|
|
|
|
|
21 |
|
22 |
# Create a temporary directory
|
23 |
temp_dir = tempfile.mkdtemp()
|
|
|
26 |
destination_path = os.path.join(temp_dir, f"{filename}.wav")
|
27 |
file.save(destination_path)
|
28 |
print(destination_path)
|
29 |
+
|
30 |
+
client = Client("https://xxiani-speechbrain-asr-wav2vec2-transformer-aishells.hf.space/")
|
|
|
31 |
result = client.predict(
|
32 |
+
|
33 |
destination_path, # str (filepath or URL to file) in 'Input' Audio component
|
34 |
api_name="/predict"
|
35 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
os.remove(destination_path)
|
37 |
os.rmdir(temp_dir)
|
38 |
+
return jsonify({'result': result})
|
|
|
|
|
|
|
|
|
39 |
|
40 |
if __name__ == '__main__':
|
41 |
+
app.run(host='0.0.0.0', port=5000)
|