Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,3 @@
|
|
| 1 |
-
import os
|
| 2 |
-
|
| 3 |
-
# جلوگیری از مشکل spaces watchdog
|
| 4 |
-
os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
|
| 5 |
-
|
| 6 |
import gradio as gr
|
| 7 |
import torch
|
| 8 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
@@ -11,55 +6,34 @@ from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
| 11 |
MODEL_ID = "ddfws/Rezaeian-StatsAI"
|
| 12 |
|
| 13 |
|
| 14 |
-
|
| 15 |
-
model = None
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
def load_model():
|
| 19 |
-
|
| 20 |
-
global tokenizer, model
|
| 21 |
-
|
| 22 |
-
if model is None:
|
| 23 |
-
|
| 24 |
-
print("Loading tokenizer...")
|
| 25 |
-
|
| 26 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 27 |
-
MODEL_ID
|
| 28 |
-
)
|
| 29 |
-
|
| 30 |
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 34 |
-
MODEL_ID,
|
| 35 |
-
device_map="auto",
|
| 36 |
-
dtype=torch.float16,
|
| 37 |
-
trust_remote_code=True
|
| 38 |
-
)
|
| 39 |
|
| 40 |
-
|
| 41 |
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
|
|
|
| 44 |
|
|
|
|
| 45 |
|
| 46 |
-
def generate(message, history):
|
| 47 |
|
| 48 |
-
|
| 49 |
|
| 50 |
prompt = ""
|
| 51 |
|
| 52 |
-
for
|
| 53 |
-
prompt +=
|
| 54 |
-
"User: " + h[0] +
|
| 55 |
-
"\nAssistant: " + h[1] +
|
| 56 |
-
"\n"
|
| 57 |
-
)
|
| 58 |
|
| 59 |
-
prompt +=
|
| 60 |
-
"User: " + message +
|
| 61 |
-
"\nAssistant:"
|
| 62 |
-
)
|
| 63 |
|
| 64 |
|
| 65 |
inputs = tokenizer(
|
|
@@ -70,33 +44,31 @@ def generate(message, history):
|
|
| 70 |
|
| 71 |
with torch.no_grad():
|
| 72 |
|
| 73 |
-
|
| 74 |
**inputs,
|
| 75 |
max_new_tokens=256,
|
| 76 |
temperature=0.7,
|
| 77 |
-
top_p=0.9,
|
| 78 |
do_sample=True
|
| 79 |
)
|
| 80 |
|
| 81 |
|
| 82 |
text = tokenizer.decode(
|
| 83 |
-
|
| 84 |
skip_special_tokens=True
|
| 85 |
)
|
| 86 |
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
return answer
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
demo = gr.ChatInterface(
|
| 95 |
-
fn=
|
| 96 |
-
title="Rezaeian StatsAI"
|
| 97 |
-
description="AI Assistant"
|
| 98 |
)
|
| 99 |
|
| 100 |
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
|
|
| 6 |
MODEL_ID = "ddfws/Rezaeian-StatsAI"
|
| 7 |
|
| 8 |
|
| 9 |
+
print("Loading tokenizer...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 12 |
+
MODEL_ID
|
| 13 |
+
)
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
print("Loading model...")
|
| 17 |
|
| 18 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 19 |
+
MODEL_ID,
|
| 20 |
+
device_map="auto",
|
| 21 |
+
dtype=torch.float16
|
| 22 |
+
)
|
| 23 |
|
| 24 |
+
model.eval()
|
| 25 |
|
| 26 |
+
print("MODEL READY")
|
| 27 |
|
|
|
|
| 28 |
|
| 29 |
+
def chat(message, history):
|
| 30 |
|
| 31 |
prompt = ""
|
| 32 |
|
| 33 |
+
for user, assistant in history:
|
| 34 |
+
prompt += f"User: {user}\nAssistant: {assistant}\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
prompt += f"User: {message}\nAssistant:"
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
|
| 39 |
inputs = tokenizer(
|
|
|
|
| 44 |
|
| 45 |
with torch.no_grad():
|
| 46 |
|
| 47 |
+
result = model.generate(
|
| 48 |
**inputs,
|
| 49 |
max_new_tokens=256,
|
| 50 |
temperature=0.7,
|
|
|
|
| 51 |
do_sample=True
|
| 52 |
)
|
| 53 |
|
| 54 |
|
| 55 |
text = tokenizer.decode(
|
| 56 |
+
result[0],
|
| 57 |
skip_special_tokens=True
|
| 58 |
)
|
| 59 |
|
| 60 |
|
| 61 |
+
return text.split("Assistant:")[-1]
|
|
|
|
|
|
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
demo = gr.ChatInterface(
|
| 66 |
+
fn=chat,
|
| 67 |
+
title="Rezaeian StatsAI"
|
|
|
|
| 68 |
)
|
| 69 |
|
| 70 |
|
| 71 |
+
demo.launch(
|
| 72 |
+
server_name="0.0.0.0",
|
| 73 |
+
server_port=7860
|
| 74 |
+
)
|