-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtalker.py
377 lines (306 loc) · 14.5 KB
/
talker.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#talker.py
from flask import Blueprint, render_template, request, jsonify, url_for, send_from_directory, abort, send_file
from models import Agent, User, Timeframe, Conversation, db
from flask_login import login_required, current_user
import openai
import os
import logging
from logging.handlers import RotatingFileHandler
import json
import datetime
import random
talker_blueprint = Blueprint('talker_blueprint', __name__, template_folder='templates')
handler = RotatingFileHandler('logs/talker.log', maxBytes=10000, backupCount=3)
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
logger.addHandler(handler)
client = openai.OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
@talker_blueprint.route("/talker/<agent_id>")
@login_required
def talker(agent_id):
agent = None
agent_image_data = None
agent_type = None
try:
# Check if the agent exists in the User's agents_data
user_agent_data = next((agent for agent in current_user.agents_data
if str(agent.get('id', '')) == str(agent_id)),
None)
if user_agent_data:
agent = Agent(id=user_agent_data['id'],
user_id=current_user.id,
data=user_agent_data)
agent_type = 'agent'
photo_path = agent.data.get('photo_path', '')
photo_filename = photo_path.split('/')[-1]
agent_image_data = current_user.images_data.get(photo_filename, '')
# Check if the agent exists in the Agent model
if not agent:
agent = Agent.query.filter_by(user_id=current_user.id,
id=str(agent_id)).first()
if agent:
agent_type = 'agent'
photo_path = agent.data.get('photo_path', '')
photo_filename = photo_path.split('/')[-1]
agent_image_data = current_user.images_data.get(photo_filename, '')
# Check if the agent exists in any of the user's timeframes
if not agent:
for timeframe in current_user.timeframes:
timeframe_agents_data = json.loads(timeframe.agents_data)
timeframe_agent_data = next(
(agent for agent in timeframe_agents_data
if str(agent.get('id', '')) == str(agent_id)), None)
if timeframe_agent_data:
agent = Agent(id=timeframe_agent_data['id'],
user_id=current_user.id,
data=timeframe_agent_data)
agent_type = 'timeframe'
photo_path = agent.data.get('photo_path', '')
photo_filename = photo_path.split('/')[-1]
agent_image_data = json.loads(summary_image_data).get(
photo_filename, '')
break
if not agent:
return "Agent not found", 404
conversations = Conversation.query.filter(
Conversation.user_id == current_user.id,
Conversation.agents.cast(db.Text).contains(agent.id)).all()
return render_template("talker.html",
agent_id=agent.id,
agent_jobtitle=agent.data.get('jobtitle', ''),
agent_summary=agent.data.get('summary', ''),
agent_image_data=agent_image_data,
agent_type=agent_type,
conversations=conversations)
except Exception as e:
logger.exception(f"Error in talker route: {str(e)}")
return "An error occurred", 500
@talker_blueprint.route("/transcribe", methods=["POST"])
@login_required
def transcribe():
logger.info(f"Transcribe route called with request files: {request.files}")
if 'audio' not in request.files:
logger.error("No audio file provided in the request")
return jsonify({"error": "No audio file provided"}), 400
audio_file = request.files['audio']
logger.info(f"Received audio file: {audio_file}")
assert audio_file.filename.endswith(('.mp3', '.wav')), "Audio file must be in MP3 or WAV format"
user_folder = current_user.folder_path
os.makedirs(user_folder, exist_ok=True)
audio_file_path = os.path.join(user_folder, 'temp_audio.mp3')
audio_file.save(audio_file_path)
logger.info(f"Audio file saved to: {audio_file_path}")
try:
with open(audio_file_path, 'rb') as f:
logger.info("Sending audio file to OpenAI for transcription")
transcription = client.audio.transcriptions.create(model="whisper-1",
file=f,
language="en")
logger.info(f"Received transcription: {transcription}")
agent_id = request.form["agent_id"]
logger.info(f"Agent ID: {agent_id}")
agent = get_agent_by_id(agent_id, current_user)
assert agent is not None, f"Agent not found for ID: {agent_id}"
logger.info(f"Found agent: {agent}")
conversation_id = request.form.get("conversation_id")
conversation_name = request.form.get("conversation_name")
logger.info(f"Conversation ID: {conversation_id}, Conversation Name: {conversation_name}")
conversation_messages = []
if conversation_id and conversation_id != 'undefined':
try:
conversation_id = int(conversation_id)
conversation = Conversation.query.get(conversation_id)
if conversation and conversation.user_id == current_user.id:
conversation_messages = conversation.messages
conversation_messages.append({"role": "user", "content": transcription.text})
db.session.commit()
logger.info(f"Appended user message to conversation {conversation_id}")
except (ValueError, AssertionError) as e:
logger.warning(f"Error processing conversation: {str(e)}")
conversation_id = None
if not conversation_id:
conversation_id = 'temp'
conversation_name = conversation_name or 'Temporary Conversation'
conversation_messages.append({"role": "user", "content": transcription.text})
logger.info("Using temporary conversation")
response_text = chat_with_model(conversation_messages, agent.data, transcription.text)
conversation_messages.append({"role": "assistant", "content": response_text})
audio_url = text_to_speech(response_text, agent_id)
return jsonify({
"conversation_id": conversation_id,
"conversation_name": conversation_name,
"user_text": transcription.text,
"ai_text": response_text,
"audio_url": audio_url
})
except Exception as e:
logger.exception(f"Error in transcription: {str(e)}")
return jsonify({"error": str(e)}), 500
@talker_blueprint.route("/chat", methods=["POST"])
@login_required
def chat():
user_message = request.form["user_message"]
agent_id = request.form["agent_id"]
conversation_id = request.form.get("conversation_id")
conversation_name = request.form.get("conversation_name")
agent = get_agent_by_id(agent_id, current_user)
if not agent:
return jsonify({"error": "Agent not found"}), 404
if not conversation_id or conversation_id == 'null':
conversation = Conversation(user_id=current_user.id,
name=conversation_name,
agents=[agent_id],
messages=[])
db.session.add(conversation)
db.session.commit()
conversation_id = conversation.id
else:
try:
conversation_id = int(conversation_id)
conversation = Conversation.query.get(conversation_id)
if conversation and conversation.user_id == current_user.id:
conversation.messages.append({"role": "user", "content": user_message})
db.session.commit()
else:
return jsonify({"error": "Conversation not found"}), 404
except ValueError:
return jsonify({"error": "Invalid conversation ID"}), 400
try:
response_text = chat_with_model(conversation.messages, agent.data, user_message)
conversation.messages.append({"role": "user", "content": user_message})
conversation.messages.append({"role": "assistant", "content": response_text})
db.session.commit()
audio_url = text_to_speech(response_text, agent_id)
return jsonify({
"conversation_id": conversation_id,
"conversation_name": conversation.name,
"user_text": user_message,
"ai_text": response_text,
"audio_url": audio_url
})
except Exception as e:
logger.error(f"Chat error: {str(e)}")
return jsonify({"error": str(e)}), 500
def chat_with_model(conversation_messages, agent_data, user_message, max_tokens=2000, top_p=0.5, temperature=0.9):
logger.info(f"Entering chat_with_model for agent {agent_data['id']}")
with open("abe/talker.json", "r") as f:
prompts = json.load(f)
gpt_system_prompt = prompts['gpt_system_prompt'].format(
agent_id=agent_data['id'],
agent_jobtitle=agent_data.get('jobtitle', ''),
agent_summary=agent_data.get('summary', ''),
agent_persona=agent_data.get('persona', ''),
agent_relationships=agent_data.get('relationships', '')
)
messages = [{"role": "system", "content": gpt_system_prompt}]
# Append the conversation history to the messages list with labels
if conversation_messages:
messages.append({"role": "system", "content": "Conversation History:"})
for i, message in enumerate(conversation_messages, start=1):
if message["role"] == "user":
messages.append({"role": "system", "content": f"User Message {i}: {message['content']}"})
else:
messages.append({"role": "system", "content": f"AI Response {i}: {message['content']}"})
messages.append({"role": "system", "content": "Current User Message:"})
# Add the current user message to the messages list
messages.append({"role": "user", "content": user_message})
logger.info(f"Sending {len(messages)} messages to the model")
logger.debug(f"System prompt: {gpt_system_prompt}")
logger.debug(f"User prompt: {messages[-1]['content']}")
try:
response = client.chat.completions.create(
model="gpt-4-turbo",
messages=messages,
max_tokens=max_tokens,
top_p=top_p,
temperature=temperature
)
logger.info("Model response received successfully")
logger.debug(f"Model response: {response}")
return response.choices[0].message.content
except Exception as e:
logger.exception(f"Error in chat_with_model: {str(e)}")
return "Failed to generate response."
finally:
logger.info("Exiting chat_with_model")
@talker_blueprint.route('/audio/<path:filename>')
def serve_audio(filename):
user_folder = current_user.folder_path
file_path = os.path.join(user_folder, filename)
if os.path.exists(file_path):
return send_file(file_path, mimetype='audio/mpeg')
else:
abort(404)
def text_to_speech(text, agent_id):
try:
user_folder = current_user.folder_path
os.makedirs(user_folder, exist_ok=True)
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
file_path = os.path.join(user_folder, f'response_{timestamp}.mp3')
agent = get_agent_by_id(agent_id, current_user)
if agent:
voice = agent.voice
else:
voice = 'echo' # Default voice if agent not found
with client.audio.speech.with_streaming_response.create(
model="tts-1", voice=voice, input=text) as response:
with open(file_path, 'wb') as audio_file:
for chunk in response.iter_bytes():
audio_file.write(chunk)
audio_url = url_for('talker_blueprint.serve_audio', filename=f'response_{timestamp}.mp3', _external=True)
return audio_url
except Exception as e:
logger.error(f"Text-to-speech error: {str(e)}")
return None
def truncate_messages(messages, max_chars):
total_chars = sum(len(msg["content"]) for msg in messages)
if total_chars <= max_chars:
return messages
truncated_messages = []
remaining_chars = max_chars
for msg in reversed(messages):
if len(msg["content"]) <= remaining_chars:
truncated_messages.insert(0, msg)
remaining_chars -= len(msg["content"])
else:
break
return truncated_messages
@talker_blueprint.route('/get_conversation_messages/<conversation_id>')
@login_required
def get_conversation_messages(conversation_id):
try:
# Convert conversation_id to int if not 'null', else handle the error or use a default.
if conversation_id.lower() == 'null':
return jsonify({'error': 'Invalid conversation ID'}), 400
conversation_id = int(conversation_id) # This will raise ValueError if conversion fails
conversation = Conversation.query.get(conversation_id)
if conversation and conversation.user_id == current_user.id:
return jsonify({'messages': conversation.messages, 'name': conversation.name})
else:
return jsonify({'error': 'Conversation not found'}), 404
except ValueError:
return jsonify({'error': 'Invalid conversation ID'}), 400
@talker_blueprint.route('/update_conversation_name/<conversation_id>', methods=["POST"])
@login_required
def update_conversation_name(conversation_id):
new_name = request.form.get("name")
conversation = Conversation.query.get(conversation_id)
if conversation and conversation.user_id == current_user.id:
conversation.name = new_name
db.session.commit()
return jsonify({"success": True})
else:
return jsonify({"error": "Conversation not found"}), 404
def get_agent_by_id(agent_id, user):
agent = Agent.query.filter_by(user_id=user.id, id=str(agent_id)).first()
if agent:
return agent
user_agent_data = next((agent for agent in user.agents_data if str(agent.get('id', '')) == str(agent_id)), None)
if user_agent_data:
return Agent(id=user_agent_data['id'], user_id=user.id, data=user_agent_data, voice=user_agent_data.get('voice', 'echo'))
for timeframe in user.timeframes:
timeframe_agents_data = json.loads(timeframe.agents_data)
timeframe_agent_data = next((agent for agent in timeframe_agents_data if str(agent.get('id', '')) == str(agent_id)), None)
if timeframe_agent_data:
return Agent(id=timeframe_agent_data['id'], user_id=user.id, data=timeframe_agent_data, voice=timeframe_agent_data.get('voice', 'echo'))
return None