Spaces:
Sleeping
Sleeping
refactor(chat): use error_redirect template for history errors
Browse files- routes/chat.py +23 -28
routes/chat.py
CHANGED
|
@@ -5,8 +5,6 @@ from logs import get_user_history, save_log
|
|
| 5 |
|
| 6 |
chat_bp = Blueprint('chat', __name__)
|
| 7 |
|
| 8 |
-
model = configure_genai()
|
| 9 |
-
|
| 10 |
|
| 11 |
@chat_bp.route('/chat/<int:user_id>', methods=['GET', 'POST'])
|
| 12 |
def chat(user_id):
|
|
@@ -16,41 +14,38 @@ def chat(user_id):
|
|
| 16 |
history = get_user_history(user_id)
|
| 17 |
|
| 18 |
if not history:
|
| 19 |
-
return ''
|
| 20 |
-
<html>
|
| 21 |
-
<head>
|
| 22 |
-
<meta http-equiv="refresh" content="2;url=/" />
|
| 23 |
-
</head>
|
| 24 |
-
<body>
|
| 25 |
-
<h2>Ops, algo deu errado por aqui.</h2>
|
| 26 |
-
<p>Vou te levar de volta para a página inicial.</p>
|
| 27 |
-
</body>
|
| 28 |
-
</html>
|
| 29 |
-
'''
|
| 30 |
|
| 31 |
response = None
|
| 32 |
|
| 33 |
if request.method == 'POST':
|
| 34 |
question = request.form.get('question')
|
| 35 |
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
f"sobre obras literárias, personagens, enredos, gêneros e autores."
|
| 41 |
-
f" O nome do usuário é {history.get('name', f'Usuário {user_id}')}"
|
| 42 |
-
f". As preferências dele são: "
|
| 43 |
-
f"{', '.join(history.get('preferences', [])) or 'nenhuma'}. "
|
| 44 |
-
f"Histórico: {history}. "
|
| 45 |
-
f"Pergunta: {question} "
|
| 46 |
-
f"Responda na mesma língua da pergunta, com até 500 caracteres. "
|
| 47 |
-
f"Fale diretamente com o usuário."
|
| 48 |
-
)
|
| 49 |
|
| 50 |
-
|
| 51 |
-
response = gemini_response.text
|
| 52 |
|
| 53 |
-
|
|
|
|
|
|
|
| 54 |
|
| 55 |
formatted_response = None
|
| 56 |
if response:
|
|
|
|
| 5 |
|
| 6 |
chat_bp = Blueprint('chat', __name__)
|
| 7 |
|
|
|
|
|
|
|
| 8 |
|
| 9 |
@chat_bp.route('/chat/<int:user_id>', methods=['GET', 'POST'])
|
| 10 |
def chat(user_id):
|
|
|
|
| 14 |
history = get_user_history(user_id)
|
| 15 |
|
| 16 |
if not history:
|
| 17 |
+
return render_template('error_redirect.html')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
response = None
|
| 20 |
|
| 21 |
if request.method == 'POST':
|
| 22 |
question = request.form.get('question')
|
| 23 |
|
| 24 |
+
try:
|
| 25 |
+
model = configure_genai()
|
| 26 |
+
session = model.start_chat(enable_automatic_function_calling=True)
|
| 27 |
+
|
| 28 |
+
prompt = (
|
| 29 |
+
f"Você é uma especialista em livros e pode conversar "
|
| 30 |
+
f"livremente sobre obras literárias, personagens, enredos, "
|
| 31 |
+
f"gêneros e autores. O nome do usuário é "
|
| 32 |
+
f"{history.get('name', f'Usuário {user_id}')}. "
|
| 33 |
+
f"As preferências dele são: "
|
| 34 |
+
f"{', '.join(history.get('preferences', [])) or 'nenhuma'}. "
|
| 35 |
+
f"Histórico: {history}. "
|
| 36 |
+
f"Pergunta: {question}. Responda na mesma língua da pergunta,"
|
| 37 |
+
f" com até 500 caracteres."
|
| 38 |
+
f"Fale diretamente com o usuário."
|
| 39 |
+
)
|
| 40 |
|
| 41 |
+
gemini_response = session.send_message(prompt)
|
| 42 |
+
response = gemini_response.text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
save_log(user_id, history, response)
|
|
|
|
| 45 |
|
| 46 |
+
except Exception as e:
|
| 47 |
+
print(f"Erro ao usar a IA: {e}")
|
| 48 |
+
response = "⚠️ A IA está indisponível, retorne em 24h."
|
| 49 |
|
| 50 |
formatted_response = None
|
| 51 |
if response:
|