Spaces:
Sleeping
Sleeping
Commit
•
dce9570
1
Parent(s):
96d2b4f
Update main.py
Browse files
main.py
CHANGED
@@ -1,13 +1,46 @@
|
|
1 |
-
from fastapi import FastAPI,
|
2 |
-
from fastapi.responses import
|
|
|
|
|
|
|
3 |
|
4 |
app = FastAPI()
|
5 |
|
|
|
|
|
|
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
return {"output": "output"}
|
10 |
|
|
|
|
|
|
|
|
|
|
|
11 |
@app.get("/", response_class=HTMLResponse)
|
12 |
-
async def
|
13 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, Request, File, UploadFile
|
2 |
+
from fastapi.responses import HTMLResponse
|
3 |
+
from fastapi.staticfiles import StaticFiles
|
4 |
+
from fastapi.templating import Jinja2Templates
|
5 |
+
import os
|
6 |
|
7 |
app = FastAPI()
|
8 |
|
9 |
+
# Директория для временного хранения загруженных файлов
|
10 |
+
UPLOAD_DIRECTORY = "uploads"
|
11 |
+
os.makedirs(UPLOAD_DIRECTORY, exist_ok=True)
|
12 |
|
13 |
+
# Подключаем статические файлы (например, CSS)
|
14 |
+
app.mount("/static", StaticFiles(directory="static"), name="static")
|
|
|
15 |
|
16 |
+
# Подключаем шаблоны Jinja2
|
17 |
+
templates = Jinja2Templates(directory="templates")
|
18 |
+
|
19 |
+
|
20 |
+
# Главная страница с текстом "server is running"
|
21 |
@app.get("/", response_class=HTMLResponse)
|
22 |
+
async def read_root():
|
23 |
+
return templates.TemplateResponse("index.html", {"request": request, "text": "server is running"})
|
24 |
+
|
25 |
+
|
26 |
+
# Первый POST эндпоинт для приема текста
|
27 |
+
@app.post("/update_text/")
|
28 |
+
async def update_text(text: str):
|
29 |
+
return {"text": text}
|
30 |
+
|
31 |
+
|
32 |
+
# Второй POST эндпоинт для приема файла и текстового поля prompt
|
33 |
+
@app.post("/upload_file/")
|
34 |
+
async def upload_file(prompt: str, file: UploadFile = File(...)):
|
35 |
+
# Сохраняем файл в директорию UPLOAD_DIRECTORY
|
36 |
+
file_location = os.path.join(UPLOAD_DIRECTORY, file.filename)
|
37 |
+
with open(file_location, "wb") as file_object:
|
38 |
+
file_object.write(file.file.read())
|
39 |
+
|
40 |
+
# Отправляем запрос на первый эндпоинт с текстом равным имени загруженного файла
|
41 |
+
response = await update_text(file.filename)
|
42 |
+
|
43 |
+
# Удаляем загруженный файл
|
44 |
+
os.remove(file_location)
|
45 |
+
|
46 |
+
return response
|