Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,87 +1,54 @@
|
|
| 1 |
-
from flask import Flask, request
|
| 2 |
-
import io
|
| 3 |
from flask_cors import CORS # Necessary for cross-domain requests
|
|
|
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
CORS(app) # This is used to allow cross-domain requests during development
|
| 7 |
|
| 8 |
-
|
| 9 |
-
from groq import Groq
|
| 10 |
-
|
| 11 |
-
from texttospeech import text_to_speech
|
| 12 |
-
# gets API Key from environment variable OPENAI_API_KEY
|
| 13 |
client = Groq(
|
| 14 |
api_key="Your_Groq_Key",
|
| 15 |
)
|
| 16 |
-
openai_client = OpenAI(api_key="Your_OpenAI_KEY")
|
| 17 |
|
| 18 |
-
INIT_MESG=[
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
]
|
| 36 |
-
history_messages = INIT_MESG
|
| 37 |
-
|
| 38 |
-
@app.route('/synthesize-speech', methods=['POST'])
|
| 39 |
-
def synthesize_speech():
|
| 40 |
-
data = request.json
|
| 41 |
-
text = data['text']
|
| 42 |
-
|
| 43 |
-
# Create an audio buffer
|
| 44 |
-
#sound = io.BytesIO()
|
| 45 |
-
|
| 46 |
-
sound = "output.mp3"
|
| 47 |
-
|
| 48 |
-
voice_response = openai_client.audio.speech.create(
|
| 49 |
-
model="tts-1",
|
| 50 |
-
voice="alloy",
|
| 51 |
-
input=text,
|
| 52 |
-
)
|
| 53 |
-
|
| 54 |
-
voice_response.stream_to_file(sound)
|
| 55 |
-
|
| 56 |
-
#text_to_speech(text, sound)
|
| 57 |
-
return send_file(
|
| 58 |
-
sound,
|
| 59 |
-
mimetype="audio/mpeg"
|
| 60 |
-
)
|
| 61 |
|
| 62 |
@app.route('/process-speech', methods=['POST'])
|
| 63 |
def process_speech():
|
| 64 |
-
|
| 65 |
data = request.json
|
| 66 |
user_text = data['text']
|
| 67 |
-
history_messages.append({"role":"user", "content":user_text})
|
|
|
|
| 68 |
completion = client.chat.completions.create(
|
| 69 |
-
|
| 70 |
model="mixtral-8x7b-32768",
|
| 71 |
messages=history_messages,
|
| 72 |
-
|
| 73 |
-
|
| 74 |
ai_response = completion.choices[0].message.content
|
| 75 |
-
history_messages.append({"role":"assistant", "content":ai_response})
|
| 76 |
-
|
| 77 |
-
|
| 78 |
return {'response': ai_response}
|
| 79 |
|
| 80 |
@app.route('/start-speech', methods=['POST'])
|
| 81 |
def start_speech():
|
| 82 |
-
|
| 83 |
-
history_messages = INIT_MESG
|
| 84 |
return {'response': 'OK'}
|
| 85 |
|
| 86 |
if __name__ == '__main__':
|
| 87 |
-
app.run(debug=True)
|
|
|
|
| 1 |
+
from flask import Flask, request
|
|
|
|
| 2 |
from flask_cors import CORS # Necessary for cross-domain requests
|
| 3 |
+
from groq import Groq
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
CORS(app) # This is used to allow cross-domain requests during development
|
| 7 |
|
| 8 |
+
# Initialize Groq client
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
client = Groq(
|
| 10 |
api_key="Your_Groq_Key",
|
| 11 |
)
|
|
|
|
| 12 |
|
| 13 |
+
INIT_MESG = [
|
| 14 |
+
{
|
| 15 |
+
"role": "user",
|
| 16 |
+
"content": """
|
| 17 |
+
You are a voice chatbot that responds to human user's speech input.
|
| 18 |
+
The speech input texts are sometimes broken or hard stop due to the listening mechanism.
|
| 19 |
+
If the message you read is not complete, please ask the user to repeat or complete politely and concisely.
|
| 20 |
+
Remember you are speaking not writing, so please use oral expression in plain language.
|
| 21 |
+
""",
|
| 22 |
+
},
|
| 23 |
+
{
|
| 24 |
+
"role": "assistant",
|
| 25 |
+
"content": "OK, I understood.",
|
| 26 |
+
},
|
| 27 |
+
]
|
| 28 |
+
|
| 29 |
+
history_messages = INIT_MESG.copy()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
@app.route('/process-speech', methods=['POST'])
|
| 32 |
def process_speech():
|
|
|
|
| 33 |
data = request.json
|
| 34 |
user_text = data['text']
|
| 35 |
+
history_messages.append({"role": "user", "content": user_text})
|
| 36 |
+
|
| 37 |
completion = client.chat.completions.create(
|
|
|
|
| 38 |
model="mixtral-8x7b-32768",
|
| 39 |
messages=history_messages,
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
ai_response = completion.choices[0].message.content
|
| 43 |
+
history_messages.append({"role": "assistant", "content": ai_response})
|
| 44 |
+
|
|
|
|
| 45 |
return {'response': ai_response}
|
| 46 |
|
| 47 |
@app.route('/start-speech', methods=['POST'])
|
| 48 |
def start_speech():
|
| 49 |
+
global history_messages
|
| 50 |
+
history_messages = INIT_MESG.copy()
|
| 51 |
return {'response': 'OK'}
|
| 52 |
|
| 53 |
if __name__ == '__main__':
|
| 54 |
+
app.run(debug=True)
|