Spaces:
Sleeping
Sleeping
File size: 3,823 Bytes
e9d4432 cbc0c5c e9d4432 e6e0ed7 e9d4432 fd4e0bb 236e88e e6e0ed7 5a41e3f 21ac88c 3baf495 e9d4432 aa343dc 092d445 e9d4432 71fc2ba e6e0ed7 b6face9 e9d4432 b6face9 e9d4432 fd4e0bb 7bb6709 236e88e cbc0c5c 5a41e3f 8180af3 5a41e3f f7ef4d6 21ac88c f7ef4d6 21ac88c 5a41e3f f7ef4d6 3cba2b2 f7ef4d6 3cba2b2 f7ef4d6 b6face9 e9d4432 f7ef4d6 cbc0c5c 4995549 3baf495 aa343dc 4995549 cbc0c5c e9d4432 092d445 | 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 | import os
from flask import Flask, request, render_template, session, send_file, jsonify
import io
import requests
import base64
import uuid
from functions import (
sentiment_analysis,
generate_text,
summarize_text,
chat_with_agent,
text_stats,
shuffle_words, to_leetspeak, compare_texts, transliterate_ru_to_en
)
import traceback
app = Flask(__name__)
UPLOAD_FOLDER = 'uploads'
RESULTS_FOLDER = 'static/results'
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
os.makedirs(RESULTS_FOLDER, exist_ok=True)
app.secret_key = os.getenv("SECRET_KEY", os.urandom(24))
@app.route('/', methods = ['GET', 'POST'])
def index():
result_text = None
error = None
chat_history = session.get('chat_history', [])
if request.method == 'POST':
action = request.form.get('action')
if action == 'sentiment':
text = request.form.get('text', '')
if text:
result_text = sentiment_analysis(text)
else:
error = "Введите текст"
elif action =="generate":
prompt = request.form.get('text', '')
if prompt:
result_text=generate_text(prompt)
else:
error = 'Введите промпт'
elif action=="summarize":
text=request.form.get('text', '')
if text:
result_text=summarize_text(text)
else:
error = "Введите текст для пересказа"
elif action=="chat":
pass
elif action =="stats":
text=request.form.get('text','')
if text:
result_text = text_stats(text)
else:
error="Введите текст"
elif action =="shuffle":
text=request.form.get('text','')
if text:
result_text = shuffle_words(text)
else:
error="Введите текст"
elif action =="leetspeak":
text=request.form.get('text','')
if text:
result_text = to_leetspeak(text)
else:
error="Введите текст"
elif action =="compare":
text=request.form.get('text','')
text2 = request.form.get('text2', '')
if text and text2:
result_text = compare_texts(text, text2)
else:
error="Введите оба текста"
elif action =="transliterate":
text=request.form.get('text','')
if text:
result_text = transliterate_ru_to_en(text)
else:
error="Введите текст"
return render_template('index.html',
result_text=result_text,
error=error,
chat_history=chat_history)
@app.route('/chat', methods=['POST'])
def chat_endpoint():
"""AJAX - эндпоинn для общения с ИИ - агентом."""
try:
data = request.get_json()
if not data:
return jsonify({'error': 'Неверный запрос'}), 400
user_message=data.get('message', '').strip()
if not user_message:
return jsonify({'error':'сообщение не может быть пустым'}), 400
history = session.get('chat_history',[])
reply, new_history=chat_with_agent(history, user_message)
session['chat_history']=new_history
return jsonify({'reply':reply})
except Exception as e:
print(f"Ошибка в /chat: {str(e)}")
traceback.print_exc()
return jsonify({'error': str(e)}), 500
if __name__=='__main__':
app.run(debug=True, host='0.0.0.0', port=7860) |