Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ from fastapi import FastAPI
|
|
| 2 |
from fastapi.responses import PlainTextResponse
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
|
|
|
|
| 5 |
import torch
|
| 6 |
import uvicorn
|
| 7 |
import threading
|
|
@@ -26,6 +27,12 @@ class Message(BaseModel):
|
|
| 26 |
message: str
|
| 27 |
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
def generate_ai_stream(message: str):
|
| 30 |
prompt = f"User: {message}\nAssistant: Answer clearly and fully:\n"
|
| 31 |
|
|
@@ -49,15 +56,20 @@ def generate_ai_stream(message: str):
|
|
| 49 |
|
| 50 |
partial = ""
|
| 51 |
|
| 52 |
-
# 🔥
|
| 53 |
for text in streamer:
|
| 54 |
partial += text
|
| 55 |
|
| 56 |
if message in db:
|
| 57 |
-
db[message]["reply"] = partial
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
|
| 60 |
-
final_text = partial.strip() + " full generated"
|
| 61 |
|
| 62 |
if message in db:
|
| 63 |
db[message]["reply"] = final_text
|
|
@@ -65,6 +77,7 @@ def generate_ai_stream(message: str):
|
|
| 65 |
return final_text
|
| 66 |
|
| 67 |
|
|
|
|
| 68 |
def worker():
|
| 69 |
while True:
|
| 70 |
if queue:
|
|
@@ -82,6 +95,7 @@ def worker():
|
|
| 82 |
time.sleep(0.01)
|
| 83 |
|
| 84 |
|
|
|
|
| 85 |
for _ in range(NUM_WORKERS):
|
| 86 |
threading.Thread(target=worker, daemon=True).start()
|
| 87 |
|
|
@@ -91,6 +105,7 @@ async def root():
|
|
| 91 |
return PlainTextResponse("AI server работает")
|
| 92 |
|
| 93 |
|
|
|
|
| 94 |
@app.get("/ask")
|
| 95 |
async def ask(message: str):
|
| 96 |
|
|
@@ -107,6 +122,7 @@ async def ask(message: str):
|
|
| 107 |
return PlainTextResponse("accepted")
|
| 108 |
|
| 109 |
|
|
|
|
| 110 |
@app.get("/get")
|
| 111 |
async def get(message: str):
|
| 112 |
|
|
|
|
| 2 |
from fastapi.responses import PlainTextResponse
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
|
| 5 |
+
from deep_translator import GoogleTranslator
|
| 6 |
import torch
|
| 7 |
import uvicorn
|
| 8 |
import threading
|
|
|
|
| 27 |
message: str
|
| 28 |
|
| 29 |
|
| 30 |
+
# 🔥 РАЗБИВКА ТЕКСТА
|
| 31 |
+
def split_text(text, max_len=25):
|
| 32 |
+
return "\n".join([text[i:i+max_len] for i in range(0, len(text), max_len)])
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
# 🔥 СТРИМИНГ ГЕНЕРАЦИИ
|
| 36 |
def generate_ai_stream(message: str):
|
| 37 |
prompt = f"User: {message}\nAssistant: Answer clearly and fully:\n"
|
| 38 |
|
|
|
|
| 56 |
|
| 57 |
partial = ""
|
| 58 |
|
| 59 |
+
# 🔥 ПОТОКОВАЯ ГЕНЕРАЦИЯ
|
| 60 |
for text in streamer:
|
| 61 |
partial += text
|
| 62 |
|
| 63 |
if message in db:
|
| 64 |
+
db[message]["reply"] = split_text(partial)
|
| 65 |
+
|
| 66 |
+
# 🔥 ПЕРЕВОД В КОНЦЕ
|
| 67 |
+
try:
|
| 68 |
+
translated = GoogleTranslator(source='en', target='ru').translate(partial.strip())
|
| 69 |
+
except:
|
| 70 |
+
translated = partial.strip() # fallback если перевод упал
|
| 71 |
|
| 72 |
+
final_text = split_text(translated) + " full generated"
|
|
|
|
| 73 |
|
| 74 |
if message in db:
|
| 75 |
db[message]["reply"] = final_text
|
|
|
|
| 77 |
return final_text
|
| 78 |
|
| 79 |
|
| 80 |
+
# 🔥 ВОРКЕР
|
| 81 |
def worker():
|
| 82 |
while True:
|
| 83 |
if queue:
|
|
|
|
| 95 |
time.sleep(0.01)
|
| 96 |
|
| 97 |
|
| 98 |
+
# 🔥 ЗАПУСК НЕСКОЛЬКИХ ПОТОКОВ
|
| 99 |
for _ in range(NUM_WORKERS):
|
| 100 |
threading.Thread(target=worker, daemon=True).start()
|
| 101 |
|
|
|
|
| 105 |
return PlainTextResponse("AI server работает")
|
| 106 |
|
| 107 |
|
| 108 |
+
# 🔹 ASK
|
| 109 |
@app.get("/ask")
|
| 110 |
async def ask(message: str):
|
| 111 |
|
|
|
|
| 122 |
return PlainTextResponse("accepted")
|
| 123 |
|
| 124 |
|
| 125 |
+
# 🔹 GET (стриминг)
|
| 126 |
@app.get("/get")
|
| 127 |
async def get(message: str):
|
| 128 |
|