Spaces:
Running
Running
File size: 1,638 Bytes
dce9570 b916cdf dce9570 c575e18 dce9570 572d4e2 a361b87 dce9570 a12d6fd dce9570 |
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 |
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)
# Подключаем шаблоны Jinja2
templates = Jinja2Templates(directory="templates")
# Главная страница с текстом "server is running"
@app.get("/", response_class=HTMLResponse)
async def read_root(request: Request):
return templates.TemplateResponse("index.html", {"request": request, "text": "server is running"})
@app.post('/update_text/')
async def update_text(data: TextData):
received_text = data.text
return {"received_text": received_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
|