from fastapi import FastAPI, Request, File, UploadFile from fastapi.responses import HTMLResponse from fastapi.staticfiles import StaticFiles from fastapi.templating import Jinja2Templates import os app = FastAPI() # Директория для временного хранения загруженных файлов UPLOAD_DIRECTORY = "uploads" os.makedirs(UPLOAD_DIRECTORY, exist_ok=True) # Подключаем статические файлы (например, CSS) app.mount("/static", StaticFiles(directory="static"), name="static") # Подключаем шаблоны Jinja2 templates = Jinja2Templates(directory="templates") # Главная страница с текстом "server is running" @app.get("/", response_class=HTMLResponse) async def read_root(): return templates.TemplateResponse("index.html", {"request": request, "text": "server is running"}) # Первый POST эндпоинт для приема текста @app.post("/update_text/") async def update_text(text: str): return {"text": text} # Второй POST эндпоинт для приема файла и текстового поля prompt @app.post("/upload_file/") async def upload_file(prompt: str, file: UploadFile = File(...)): # Сохраняем файл в директорию UPLOAD_DIRECTORY file_location = os.path.join(UPLOAD_DIRECTORY, file.filename) with open(file_location, "wb") as file_object: file_object.write(file.file.read()) # Отправляем запрос на первый эндпоинт с текстом равным имени загруженного файла response = await update_text(file.filename) # Удаляем загруженный файл os.remove(file_location) return response