-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
149 lines (125 loc) · 6.14 KB
/
Copy pathapp.py
File metadata and controls
149 lines (125 loc) · 6.14 KB
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
import logging
import os
import tempfile
from flask import Flask, render_template, jsonify, request
from openai import OpenAI
from flask_cors import CORS
from utils.nlp import DialogContext
from utils.command_processor import process_command
from models import init_db
# Настройка логирования для внешних библиотек
logging.getLogger('werkzeug').setLevel(logging.INFO)
logging.getLogger('sqlalchemy').setLevel(logging.WARNING)
# Настройка логирования
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
# Настройка OpenAI API
client = OpenAI(api_key=os.environ.get('OPENAI_API_KEY'))
def create_app():
"""Create and configure the Flask application"""
app = Flask(__name__)
try:
# Конфигурация приложения
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY', 'dev-key-1234')
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # Ограничение размера файла: 16MB
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///instance/terra.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# Инициализация CORS
CORS(app)
# Инициализация базы данных
init_db(app)
# Создаем глобальный объект для хранения контекста диалога
app.dialog_context = DialogContext()
logger.info("Application initialized successfully")
except Exception as e:
logger.error(f"Error initializing application: {str(e)}")
raise
@app.route('/')
def index():
"""Render the main page"""
return render_template('index.html')
@app.route('/process_audio', methods=['POST'])
def process_audio():
"""Process audio file using Whisper API"""
tmp_file_path = None
try:
logger.debug("Processing audio request")
if 'audio' not in request.files:
logger.warning("No audio file in request")
return jsonify({
'status': 'error',
'command_type': 'error',
'result': 'Аудио файл не найден'
}), 400
audio_file = request.files['audio']
if not audio_file.filename:
logger.warning("Empty audio filename")
return jsonify({
'status': 'error',
'command_type': 'error',
'result': 'Пустой аудио файл'
}), 400
# Сохраняем временный файл
try:
with tempfile.NamedTemporaryFile(delete=False, suffix='.webm') as tmp_file:
logger.debug(f"Saving temporary file: {tmp_file.name}")
audio_file.save(tmp_file.name)
tmp_file_path = tmp_file.name
except Exception as e:
logger.error(f"Error saving temporary file: {str(e)}")
return jsonify({
'status': 'error',
'command_type': 'error',
'result': 'Ошибка при сохранении аудио файла'
}), 500
try:
# Отправляем файл в Whisper API
with open(tmp_file_path, 'rb') as audio:
logger.info("Sending audio to Whisper API")
transcript = client.audio.transcriptions.create(
file=audio,
model="whisper-1",
language="ru"
)
# Получаем распознанный текст
text = transcript.text.lower().strip()
logger.info(f"Whisper API response: {text}")
# Анализируем текст и получаем тип команды
logger.debug(f"Анализируем текст после распознавания: '{text}'")
command_type, entities = app.dialog_context.analyze_text(text)
logger.info(f"Распознан тип команды: {command_type}, сущности: {entities}")
# Обрабатываем команду через процессор команд
result = process_command(command_type, entities)
logger.info(f"Результат обработки команды: {result}")
logger.debug(f"Отправляем ответ клиенту: {result}")
return jsonify({
'status': 'success',
'command_type': command_type,
'result': result
})
except Exception as e:
logger.error(f"Error processing audio with Whisper API: {str(e)}")
return jsonify({
'status': 'error',
'command_type': 'error',
'result': 'Ошибка при распознавании речи'
}), 500
except Exception as e:
logger.error(f"Unexpected error processing audio: {str(e)}")
return jsonify({
'status': 'error',
'command_type': 'error',
'result': 'Произошла неожиданная ошибка при обработке аудио'
}), 500
finally:
# Удаляем временный файл
if tmp_file_path and os.path.exists(tmp_file_path):
try:
os.unlink(tmp_file_path)
logger.debug(f"Temporary file removed: {tmp_file_path}")
except Exception as e:
logger.error(f"Error removing temporary file: {str(e)}")
return app