Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
@@ -6,32 +11,55 @@ from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
| 6 |
MODEL_ID = "ddfws/Rezaeian-StatsAI"
|
| 7 |
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
|
| 12 |
|
| 13 |
-
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
dtype=torch.float16,
|
| 19 |
-
trust_remote_code=True
|
| 20 |
-
)
|
| 21 |
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
print("Model loaded!")
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
prompt = ""
|
| 30 |
|
| 31 |
-
for
|
| 32 |
-
prompt +=
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
prompt +=
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
|
| 37 |
inputs = tokenizer(
|
|
@@ -44,30 +72,31 @@ def chat(message, history):
|
|
| 44 |
|
| 45 |
output = model.generate(
|
| 46 |
**inputs,
|
| 47 |
-
max_new_tokens=
|
| 48 |
temperature=0.7,
|
| 49 |
top_p=0.9,
|
| 50 |
do_sample=True
|
| 51 |
)
|
| 52 |
|
| 53 |
|
| 54 |
-
|
| 55 |
output[0],
|
| 56 |
skip_special_tokens=True
|
| 57 |
)
|
| 58 |
|
| 59 |
|
| 60 |
-
answer =
|
| 61 |
|
| 62 |
return answer
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
demo = gr.ChatInterface(
|
| 67 |
-
fn=
|
| 68 |
title="Rezaeian StatsAI",
|
| 69 |
description="AI Assistant"
|
| 70 |
)
|
| 71 |
|
| 72 |
|
| 73 |
-
|
|
|
|
|
|
| 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 |
MODEL_ID = "ddfws/Rezaeian-StatsAI"
|
| 12 |
|
| 13 |
|
| 14 |
+
tokenizer = None
|
| 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 |
+
print("Loading model...")
|
| 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 |
+
model.eval()
|
| 41 |
+
|
| 42 |
+
print("Model loaded!")
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def generate(message, history):
|
| 47 |
+
|
| 48 |
+
load_model()
|
| 49 |
|
| 50 |
prompt = ""
|
| 51 |
|
| 52 |
+
for h in history:
|
| 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(
|
|
|
|
| 72 |
|
| 73 |
output = model.generate(
|
| 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 |
output[0],
|
| 84 |
skip_special_tokens=True
|
| 85 |
)
|
| 86 |
|
| 87 |
|
| 88 |
+
answer = text.split("Assistant:")[-1]
|
| 89 |
|
| 90 |
return answer
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
demo = gr.ChatInterface(
|
| 95 |
+
fn=generate,
|
| 96 |
title="Rezaeian StatsAI",
|
| 97 |
description="AI Assistant"
|
| 98 |
)
|
| 99 |
|
| 100 |
|
| 101 |
+
if __name__ == "__main__":
|
| 102 |
+
demo.launch()
|