Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,13 @@
|
|
| 1 |
from flask import Flask, request, jsonify
|
| 2 |
from transformers import pipeline
|
| 3 |
import logging
|
| 4 |
-
import re
|
| 5 |
|
| 6 |
logging.basicConfig(level=logging.INFO)
|
| 7 |
logger = logging.getLogger(__name__)
|
| 8 |
|
| 9 |
app = Flask(__name__)
|
| 10 |
|
| 11 |
-
# Инициализация pipeline
|
| 12 |
try:
|
| 13 |
chatbot = pipeline(
|
| 14 |
"text-generation",
|
|
@@ -16,93 +15,56 @@ try:
|
|
| 16 |
device_map="auto",
|
| 17 |
model_kwargs={"torch_dtype": "auto"}
|
| 18 |
)
|
| 19 |
-
logger.info("
|
| 20 |
except Exception as e:
|
| 21 |
-
logger.error(f"Error
|
| 22 |
-
chatbot =
|
| 23 |
-
"text-generation",
|
| 24 |
-
model="microsoft/DialoGPT-medium",
|
| 25 |
-
device_map="auto"
|
| 26 |
-
)
|
| 27 |
-
logger.info("Fallback English model loaded")
|
| 28 |
-
|
| 29 |
-
def clean_response(text, user_message):
|
| 30 |
-
"""Очистка ответа от мусора"""
|
| 31 |
-
# Удаляем промпт если он остался
|
| 32 |
-
text = text.replace(f"Пользователь: {user_message}", "").replace("Ассистент:", "").strip()
|
| 33 |
-
|
| 34 |
-
# Удаляем HTML entities
|
| 35 |
-
text = re.sub(r'&[a-z]+;', '', text)
|
| 36 |
-
|
| 37 |
-
# Удаляем URL
|
| 38 |
-
text = re.sub(r'http\S+', '', text)
|
| 39 |
-
|
| 40 |
-
# Удаляем странные числовые последовательности
|
| 41 |
-
text = re.sub(r'\d{8,}', '', text)
|
| 42 |
-
|
| 43 |
-
# Берем только первую осмысленную часть до следующего "Пользователь:" или цифр
|
| 44 |
-
if "Пользователь:" in text:
|
| 45 |
-
text = text.split("Пользователь:")[0]
|
| 46 |
-
|
| 47 |
-
# Удаляем лишние переносы и пробелы
|
| 48 |
-
text = re.sub(r'\n+', ' ', text)
|
| 49 |
-
text = re.sub(r'\s+', ' ', text).strip()
|
| 50 |
-
|
| 51 |
-
# Обрезаем слишком длинные ответы
|
| 52 |
-
if len(text) > 200:
|
| 53 |
-
sentences = re.split(r'[.!?]', text)
|
| 54 |
-
if len(sentences) > 1:
|
| 55 |
-
text = '.'.join(sentences[:2]) + '.'
|
| 56 |
-
else:
|
| 57 |
-
text = text[:200] + '...'
|
| 58 |
-
|
| 59 |
-
return text
|
| 60 |
|
| 61 |
@app.route('/health', methods=['GET'])
|
| 62 |
def health_check():
|
| 63 |
-
return jsonify({"status": "healthy", "
|
| 64 |
|
| 65 |
@app.route('/chat', methods=['POST'])
|
| 66 |
def chat():
|
| 67 |
try:
|
|
|
|
|
|
|
|
|
|
| 68 |
data = request.get_json()
|
|
|
|
| 69 |
|
| 70 |
-
if not
|
| 71 |
-
return jsonify({"error": "
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
prompt = f"""Ты - полезный AI ассистент. Дай полный ответ и по делу на вопрос пользователя, но не слишком длинный.
|
| 78 |
|
| 79 |
Пользователь: {user_message}
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
# Генерация с строгими параметрами
|
| 83 |
response = chatbot(
|
| 84 |
prompt,
|
| 85 |
-
max_new_tokens=100,
|
| 86 |
-
temperature=0.
|
| 87 |
do_sample=True,
|
| 88 |
-
top_p=0.
|
| 89 |
-
repetition_penalty=1.
|
| 90 |
num_return_sequences=1,
|
| 91 |
pad_token_id=chatbot.tokenizer.eos_token_id,
|
| 92 |
-
truncation=True
|
| 93 |
-
no_repeat_ngram_size=2 # Предотвращает повторение фраз
|
| 94 |
)
|
| 95 |
-
|
| 96 |
-
# Извлекаем
|
| 97 |
generated_text = response[0]['generated_text']
|
| 98 |
assistant_response = generated_text.replace(prompt, "").strip()
|
| 99 |
-
assistant_response = clean_response(assistant_response, user_message)
|
| 100 |
|
| 101 |
-
#
|
| 102 |
-
if
|
| 103 |
-
assistant_response = "
|
| 104 |
|
| 105 |
-
logger.info(f"
|
| 106 |
|
| 107 |
return jsonify({
|
| 108 |
"response": assistant_response,
|
|
@@ -110,18 +72,12 @@ def chat():
|
|
| 110 |
})
|
| 111 |
|
| 112 |
except Exception as e:
|
| 113 |
-
logger.error(f"Error
|
| 114 |
-
return jsonify({"error": "Internal
|
| 115 |
|
| 116 |
@app.route('/', methods=['GET'])
|
| 117 |
def home():
|
| 118 |
-
return jsonify({
|
| 119 |
-
"message": "Chat API is running",
|
| 120 |
-
"endpoints": {
|
| 121 |
-
"health": "GET /health",
|
| 122 |
-
"chat": "POST /chat"
|
| 123 |
-
}
|
| 124 |
-
})
|
| 125 |
|
| 126 |
if __name__ == '__main__':
|
| 127 |
app.run(host='0.0.0.0', port=7860, debug=False)
|
|
|
|
| 1 |
from flask import Flask, request, jsonify
|
| 2 |
from transformers import pipeline
|
| 3 |
import logging
|
|
|
|
| 4 |
|
| 5 |
logging.basicConfig(level=logging.INFO)
|
| 6 |
logger = logging.getLogger(__name__)
|
| 7 |
|
| 8 |
app = Flask(__name__)
|
| 9 |
|
| 10 |
+
# Инициализация pipeline
|
| 11 |
try:
|
| 12 |
chatbot = pipeline(
|
| 13 |
"text-generation",
|
|
|
|
| 15 |
device_map="auto",
|
| 16 |
model_kwargs={"torch_dtype": "auto"}
|
| 17 |
)
|
| 18 |
+
logger.info("Model loaded successfully")
|
| 19 |
except Exception as e:
|
| 20 |
+
logger.error(f"Error: {e}")
|
| 21 |
+
chatbot = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
@app.route('/health', methods=['GET'])
|
| 24 |
def health_check():
|
| 25 |
+
return jsonify({"status": "healthy", "model_loaded": chatbot is not None})
|
| 26 |
|
| 27 |
@app.route('/chat', methods=['POST'])
|
| 28 |
def chat():
|
| 29 |
try:
|
| 30 |
+
if not chatbot:
|
| 31 |
+
return jsonify({"error": "Model not loaded"}), 500
|
| 32 |
+
|
| 33 |
data = request.get_json()
|
| 34 |
+
user_message = data.get('message', '').strip()
|
| 35 |
|
| 36 |
+
if not user_message:
|
| 37 |
+
return jsonify({"error": "Empty message"}), 400
|
| 38 |
+
|
| 39 |
+
logger.info(f"Received: {user_message}")
|
| 40 |
+
|
| 41 |
+
# Четкий промпт для адекватного общения
|
| 42 |
+
prompt = f"""Ты - профессиональный помощник по Telegram стикерам. Отвечай вежливо и по делу.
|
|
|
|
| 43 |
|
| 44 |
Пользователь: {user_message}
|
| 45 |
+
Помощник:"""
|
| 46 |
+
|
|
|
|
| 47 |
response = chatbot(
|
| 48 |
prompt,
|
| 49 |
+
max_new_tokens=100,
|
| 50 |
+
temperature=0.7,
|
| 51 |
do_sample=True,
|
| 52 |
+
top_p=0.9,
|
| 53 |
+
repetition_penalty=1.1,
|
| 54 |
num_return_sequences=1,
|
| 55 |
pad_token_id=chatbot.tokenizer.eos_token_id,
|
| 56 |
+
truncation=True
|
|
|
|
| 57 |
)
|
| 58 |
+
|
| 59 |
+
# Извлекаем ответ
|
| 60 |
generated_text = response[0]['generated_text']
|
| 61 |
assistant_response = generated_text.replace(prompt, "").strip()
|
|
|
|
| 62 |
|
| 63 |
+
# Очищаем ответ
|
| 64 |
+
if "Пользователь:" in assistant_response:
|
| 65 |
+
assistant_response = assistant_response.split("Пользователь:")[0].strip()
|
| 66 |
|
| 67 |
+
logger.info(f"Response: {assistant_response}")
|
| 68 |
|
| 69 |
return jsonify({
|
| 70 |
"response": assistant_response,
|
|
|
|
| 72 |
})
|
| 73 |
|
| 74 |
except Exception as e:
|
| 75 |
+
logger.error(f"Error: {e}")
|
| 76 |
+
return jsonify({"error": "Internal error"}), 500
|
| 77 |
|
| 78 |
@app.route('/', methods=['GET'])
|
| 79 |
def home():
|
| 80 |
+
return jsonify({"message": "Sticker assistant API"})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
if __name__ == '__main__':
|
| 83 |
app.run(host='0.0.0.0', port=7860, debug=False)
|