Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,9 @@ from pydantic import BaseModel
|
|
| 4 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 5 |
import torch
|
| 6 |
import uvicorn
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
|
@@ -11,18 +14,21 @@ model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
|
| 11 |
|
| 12 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 13 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 14 |
-
|
| 15 |
model.eval()
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
class Message(BaseModel):
|
| 19 |
message: str
|
| 20 |
|
| 21 |
|
| 22 |
def generate_ai(message: str):
|
| 23 |
-
|
| 24 |
prompt = f"User: {message}\nAssistant:"
|
| 25 |
-
|
| 26 |
inputs = tokenizer(prompt, return_tensors="pt")
|
| 27 |
|
| 28 |
with torch.no_grad():
|
|
@@ -40,27 +46,61 @@ def generate_ai(message: str):
|
|
| 40 |
return reply
|
| 41 |
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
@app.get("/")
|
| 44 |
async def root():
|
| 45 |
return PlainTextResponse("AI server работает")
|
| 46 |
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
@app.
|
| 50 |
-
async def
|
| 51 |
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
|
|
|
|
|
|
|
| 55 |
|
|
|
|
| 56 |
|
| 57 |
-
# GET (для Minecraft)
|
| 58 |
-
@app.get("/ask")
|
| 59 |
-
async def ask(message: str):
|
| 60 |
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
return PlainTextResponse(reply)
|
| 64 |
|
| 65 |
|
| 66 |
if __name__ == "__main__":
|
|
|
|
| 4 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 5 |
import torch
|
| 6 |
import uvicorn
|
| 7 |
+
import threading
|
| 8 |
+
import time
|
| 9 |
+
from collections import OrderedDict
|
| 10 |
|
| 11 |
app = FastAPI()
|
| 12 |
|
|
|
|
| 14 |
|
| 15 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 16 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
|
|
|
| 17 |
model.eval()
|
| 18 |
|
| 19 |
+
# 🔹 "База данных" (макс 40 сообщений)
|
| 20 |
+
MAX_HISTORY = 40
|
| 21 |
+
db = OrderedDict() # message -> {"status": "pending/done", "reply": str}
|
| 22 |
+
|
| 23 |
+
# 🔹 Очередь
|
| 24 |
+
queue = []
|
| 25 |
|
| 26 |
class Message(BaseModel):
|
| 27 |
message: str
|
| 28 |
|
| 29 |
|
| 30 |
def generate_ai(message: str):
|
|
|
|
| 31 |
prompt = f"User: {message}\nAssistant:"
|
|
|
|
| 32 |
inputs = tokenizer(prompt, return_tensors="pt")
|
| 33 |
|
| 34 |
with torch.no_grad():
|
|
|
|
| 46 |
return reply
|
| 47 |
|
| 48 |
|
| 49 |
+
# 🔥 Фоновый обработчик
|
| 50 |
+
def worker():
|
| 51 |
+
while True:
|
| 52 |
+
if queue:
|
| 53 |
+
message = queue.pop(0)
|
| 54 |
+
|
| 55 |
+
# Генерация
|
| 56 |
+
reply = generate_ai(message)
|
| 57 |
+
|
| 58 |
+
# Сохраняем результат
|
| 59 |
+
if message in db:
|
| 60 |
+
db[message]["status"] = "done"
|
| 61 |
+
db[message]["reply"] = reply
|
| 62 |
+
|
| 63 |
+
time.sleep(0.1)
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
# запускаем поток
|
| 67 |
+
threading.Thread(target=worker, daemon=True).start()
|
| 68 |
+
|
| 69 |
+
|
| 70 |
@app.get("/")
|
| 71 |
async def root():
|
| 72 |
return PlainTextResponse("AI server работает")
|
| 73 |
|
| 74 |
|
| 75 |
+
# 🔹 отправка запроса
|
| 76 |
+
@app.get("/ask")
|
| 77 |
+
async def ask(message: str):
|
| 78 |
|
| 79 |
+
# если уже есть — не добавляем повторно
|
| 80 |
+
if message not in db:
|
| 81 |
+
db[message] = {"status": "pending", "reply": ""}
|
| 82 |
+
queue.append(message)
|
| 83 |
|
| 84 |
+
# ограничение до 40
|
| 85 |
+
if len(db) > MAX_HISTORY:
|
| 86 |
+
db.popitem(last=False) # удаляем старый
|
| 87 |
|
| 88 |
+
return PlainTextResponse("accepted")
|
| 89 |
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
+
# 🔹 получение ответа
|
| 92 |
+
@app.get("/get")
|
| 93 |
+
async def get(message: str):
|
| 94 |
+
|
| 95 |
+
if message not in db:
|
| 96 |
+
return PlainTextResponse("not found")
|
| 97 |
+
|
| 98 |
+
data = db[message]
|
| 99 |
+
|
| 100 |
+
if data["status"] == "pending":
|
| 101 |
+
return PlainTextResponse("processing")
|
| 102 |
|
| 103 |
+
return PlainTextResponse(data["reply"])
|
| 104 |
|
| 105 |
|
| 106 |
if __name__ == "__main__":
|