wowimdaniel commited on
Commit
5ca0d32
·
verified ·
1 Parent(s): 123d75f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -0
app.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, BackgroundTasks
2
+ from transformers import pipeline
3
+ import uuid
4
+
5
+ app = FastAPI()
6
+
7
+ # Загружаем легкую модель (она скачается сама при запуске)
8
+ pipe = pipeline("text-generation", model="gpt2")
9
+
10
+ tasks = {}
11
+
12
+ @app.post("/generate")
13
+ async def generate(prompt: str, background_tasks: BackgroundTasks):
14
+ task_id = str(uuid.uuid4())
15
+ tasks[task_id] = {"status": "pending", "result": None}
16
+
17
+ def run_ai(tid, p):
18
+ try:
19
+ # Генерация 50 новых токенов
20
+ res = pipe(p, max_new_tokens=50, truncation=True)
21
+ tasks[tid] = {"status": "ready", "result": res[0]['generated_text']}
22
+ except Exception as e:
23
+ tasks[tid] = {"status": "error", "result": str(e)}
24
+
25
+ background_tasks.add_task(run_ai, task_id, prompt)
26
+ return {"task_id": task_id}
27
+
28
+ @app.get("/check/{task_id}")
29
+ async def check(task_id: str):
30
+ return tasks.get(task_id, {"status": "not_found", "result": None})