Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,36 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from fastapi.responses import PlainTextResponse
|
| 3 |
from pydantic import BaseModel
|
| 4 |
-
from
|
| 5 |
from deep_translator import GoogleTranslator
|
| 6 |
-
import torch
|
| 7 |
import uvicorn
|
| 8 |
import threading
|
| 9 |
import time
|
| 10 |
from collections import OrderedDict
|
|
|
|
|
|
|
| 11 |
|
| 12 |
app = FastAPI()
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
MAX_HISTORY = 40
|
| 21 |
NUM_WORKERS = 3
|
|
@@ -27,47 +42,36 @@ class Message(BaseModel):
|
|
| 27 |
message: str
|
| 28 |
|
| 29 |
|
| 30 |
-
# 🔥
|
| 31 |
-
def split_text(text, max_len=
|
| 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 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
**inputs,
|
| 45 |
-
max_new_tokens=300,
|
| 46 |
-
min_new_tokens=30,
|
| 47 |
-
do_sample=True,
|
| 48 |
-
temperature=0.7,
|
| 49 |
-
top_p=0.9,
|
| 50 |
-
streamer=streamer,
|
| 51 |
-
eos_token_id=tokenizer.eos_token_id
|
| 52 |
)
|
| 53 |
|
| 54 |
-
thread = threading.Thread(target=model.generate, kwargs=gen_kwargs)
|
| 55 |
-
thread.start()
|
| 56 |
-
|
| 57 |
partial = ""
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
partial +=
|
| 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()
|
| 71 |
|
| 72 |
final_text = split_text(translated) + " full generated"
|
| 73 |
|
|
@@ -77,7 +81,7 @@ def generate_ai_stream(message: str):
|
|
| 77 |
return final_text
|
| 78 |
|
| 79 |
|
| 80 |
-
# 🔥
|
| 81 |
def worker():
|
| 82 |
while True:
|
| 83 |
if queue:
|
|
@@ -95,17 +99,17 @@ def worker():
|
|
| 95 |
time.sleep(0.01)
|
| 96 |
|
| 97 |
|
| 98 |
-
# 🔥
|
| 99 |
for _ in range(NUM_WORKERS):
|
| 100 |
threading.Thread(target=worker, daemon=True).start()
|
| 101 |
|
| 102 |
|
| 103 |
@app.get("/")
|
| 104 |
async def root():
|
| 105 |
-
return PlainTextResponse("AI server
|
| 106 |
|
| 107 |
|
| 108 |
-
#
|
| 109 |
@app.get("/ask")
|
| 110 |
async def ask(message: str):
|
| 111 |
|
|
@@ -122,7 +126,7 @@ async def ask(message: str):
|
|
| 122 |
return PlainTextResponse("accepted")
|
| 123 |
|
| 124 |
|
| 125 |
-
#
|
| 126 |
@app.get("/get")
|
| 127 |
async def get(message: str):
|
| 128 |
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from fastapi.responses import PlainTextResponse
|
| 3 |
from pydantic import BaseModel
|
| 4 |
+
from llama_cpp import Llama
|
| 5 |
from deep_translator import GoogleTranslator
|
|
|
|
| 6 |
import uvicorn
|
| 7 |
import threading
|
| 8 |
import time
|
| 9 |
from collections import OrderedDict
|
| 10 |
+
import os
|
| 11 |
+
import requests
|
| 12 |
|
| 13 |
app = FastAPI()
|
| 14 |
|
| 15 |
+
# 🔥 MODEL DOWNLOAD
|
| 16 |
+
MODEL_URL = "https://huggingface.co/TheBloke/phi-2-GGUF/resolve/main/phi-2.Q4_K_M.gguf"
|
| 17 |
+
MODEL_PATH = "phi-2.gguf"
|
| 18 |
+
|
| 19 |
+
if not os.path.exists(MODEL_PATH):
|
| 20 |
+
print("Downloading model... (это может занять время)")
|
| 21 |
+
with requests.get(MODEL_URL, stream=True) as r:
|
| 22 |
+
with open(MODEL_PATH, "wb") as f:
|
| 23 |
+
for chunk in r.iter_content(chunk_size=8192):
|
| 24 |
+
if chunk:
|
| 25 |
+
f.write(chunk)
|
| 26 |
+
print("Model downloaded!")
|
| 27 |
+
|
| 28 |
+
# 🔥 LOAD MODEL
|
| 29 |
+
llm = Llama(
|
| 30 |
+
model_path=MODEL_PATH,
|
| 31 |
+
n_ctx=2048,
|
| 32 |
+
n_threads=4
|
| 33 |
+
)
|
| 34 |
|
| 35 |
MAX_HISTORY = 40
|
| 36 |
NUM_WORKERS = 3
|
|
|
|
| 42 |
message: str
|
| 43 |
|
| 44 |
|
| 45 |
+
# 🔥 split text
|
| 46 |
+
def split_text(text, max_len=25):
|
| 47 |
return "\n".join([text[i:i+max_len] for i in range(0, len(text), max_len)])
|
| 48 |
|
| 49 |
|
| 50 |
+
# 🔥 GENERATION
|
| 51 |
def generate_ai_stream(message: str):
|
| 52 |
prompt = f"User: {message}\nAssistant: Answer clearly and fully:\n"
|
| 53 |
|
| 54 |
+
output = llm(
|
| 55 |
+
prompt,
|
| 56 |
+
max_tokens=300,
|
| 57 |
+
stop=["User:", "\n\n"],
|
| 58 |
+
stream=True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
)
|
| 60 |
|
|
|
|
|
|
|
|
|
|
| 61 |
partial = ""
|
| 62 |
|
| 63 |
+
for chunk in output:
|
| 64 |
+
token = chunk["choices"][0]["text"]
|
| 65 |
+
partial += token
|
| 66 |
|
| 67 |
if message in db:
|
| 68 |
db[message]["reply"] = split_text(partial)
|
| 69 |
|
| 70 |
+
# 🔥 перевод
|
| 71 |
try:
|
| 72 |
translated = GoogleTranslator(source='en', target='ru').translate(partial.strip())
|
| 73 |
except:
|
| 74 |
+
translated = partial.strip()
|
| 75 |
|
| 76 |
final_text = split_text(translated) + " full generated"
|
| 77 |
|
|
|
|
| 81 |
return final_text
|
| 82 |
|
| 83 |
|
| 84 |
+
# 🔥 WORKER
|
| 85 |
def worker():
|
| 86 |
while True:
|
| 87 |
if queue:
|
|
|
|
| 99 |
time.sleep(0.01)
|
| 100 |
|
| 101 |
|
| 102 |
+
# 🔥 START WORKERS
|
| 103 |
for _ in range(NUM_WORKERS):
|
| 104 |
threading.Thread(target=worker, daemon=True).start()
|
| 105 |
|
| 106 |
|
| 107 |
@app.get("/")
|
| 108 |
async def root():
|
| 109 |
+
return PlainTextResponse("AI server running (Phi-2 GGUF + llama.cpp)")
|
| 110 |
|
| 111 |
|
| 112 |
+
# 🔥 ASK
|
| 113 |
@app.get("/ask")
|
| 114 |
async def ask(message: str):
|
| 115 |
|
|
|
|
| 126 |
return PlainTextResponse("accepted")
|
| 127 |
|
| 128 |
|
| 129 |
+
# 🔥 GET
|
| 130 |
@app.get("/get")
|
| 131 |
async def get(message: str):
|
| 132 |
|