-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
62 lines (49 loc) · 1.68 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from dotenv import load_dotenv
from flask import Flask, render_template, request, jsonify
from openai import OpenAI
import os
from langchain import hub
from langchain_community.chat_models import ChatOpenAI
from langchain.agents import AgentExecutor, create_openai_tools_agent
from langchain_core.messages import AIMessage, HumanMessage
from tools import agent_tools
app = Flask(__name__)
client = OpenAI()
llm = ChatOpenAI(model="gpt-4")
prompt = hub.pull("hwchase17/openai-tools-agent")
agent = create_openai_tools_agent(llm, agent_tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=agent_tools, verbose=True)
chat_history = []
@app.route('/')
def index():
return render_template('index.html')
@app.route('/handle_question', methods=['POST'])
def handle_question():
audio = request.files['audio']
audio_path = "audio.webm"
audio.save(audio_path)
audio_file = open("audio.webm", "rb")
question = client.audio.transcriptions.create(
model="whisper-1",
file=audio_file,
response_format="text"
)
print(f"The question is: {question}")
chat_history.append(HumanMessage(content=question))
agent_input = {
"input": question,
"chat_history": chat_history,
}
response = agent_executor.invoke(agent_input)
output = response['output']
chat_history.append(AIMessage(content=output))
speech_path = "static/speech.mp3"
speech_response = client.audio.speech.create(
model="tts-1",
voice="nova",
input=output
)
speech_response.stream_to_file(speech_path)
return jsonify({'speech_url': speech_path})
if __name__ == '__main__':
app.run(debug=True)