-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
165 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from models import db | ||
|
||
class FileModel(db.Model): | ||
__tablename__ = 'files' | ||
|
||
id = db.Column(db.Integer, primary_key=True) | ||
file_path = db.Column(db.String, nullable=False) # Путь к файлу | ||
|
||
def __repr__(self): | ||
return f"<FileMessage(id={self.id}, filepath={self.filepath})>" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import traceback | ||
from flask import request, jsonify, Blueprint | ||
from services.conversation_service import ConversationService | ||
|
||
from services.file_service import FileService | ||
|
||
api = Blueprint('api', __name__) | ||
|
||
|
||
@api.route('/message/upload', methods=['POST']) | ||
def send_message_with_file(): | ||
""" | ||
Создает задачу с учетом контекста вопроса пользователя к отправленному файлу. | ||
Принимает conversation_id, message и файл. | ||
Возвращает ID созданной задачи. | ||
""" | ||
|
||
conversation_id = request.form.get('conversation_id') | ||
message = request.form.get('message') | ||
file = request.files.get('file') | ||
|
||
if not conversation_id or not message or not file: | ||
return jsonify({"error": "Missing required fields"}), 400 | ||
|
||
|
||
file = FileService.save_file(file) | ||
|
||
try: | ||
task_id = ConversationService.from_id(int(conversation_id)).handle_file(file, message) | ||
return jsonify({"task_id": task_id}), 202 | ||
|
||
except Exception as e: | ||
print(traceback.format_exc()) | ||
return jsonify({"error": str(e)}), 400 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import os | ||
|
||
from werkzeug.utils import secure_filename | ||
|
||
from models import db | ||
from models.file_model import FileModel | ||
|
||
UPLOAD_FOLDER = 'uploads' | ||
|
||
class FileService: | ||
|
||
@staticmethod | ||
def check_upload_folder(self): | ||
if not os.path.exists(UPLOAD_FOLDER): | ||
os.makedirs(UPLOAD_FOLDER) | ||
|
||
|
||
@staticmethod | ||
def save_file(file): | ||
|
||
filename = secure_filename(file.filename) | ||
filepath = os.path.join(UPLOAD_FOLDER, filename) | ||
file.save(filepath) | ||
|
||
new_file_message = FileModel( | ||
file_path=filepath | ||
) | ||
|
||
db.session.add(new_file_message) | ||
db.session.commit() | ||
|
||
return new_file_message |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
from wtforms import Form, StringField, validators, IntegerField | ||
from flask_wtf import FlaskForm | ||
from wtforms import IntegerField, StringField, FileField | ||
from wtforms.validators import DataRequired, Length | ||
|
||
|
||
class UploadForm(Form): | ||
conversation_id = IntegerField('conversation_id') | ||
question = StringField('question', [validators.Length(min=1)]) | ||
class UploadForm(FlaskForm): | ||
conversation_id = IntegerField('Conversation ID', validators=[DataRequired()]) | ||
message = StringField('Message', validators=[DataRequired(), Length(min=1)]) | ||
file = FileField('File', validators=[DataRequired()]) |