Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,105 +8,91 @@ from transformers import (
|
|
| 8 |
)
|
| 9 |
|
| 10 |
|
| 11 |
-
|
| 12 |
|
| 13 |
|
| 14 |
print("Loading tokenizer...")
|
| 15 |
|
| 16 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 17 |
-
MODEL_ID
|
| 18 |
-
)
|
| 19 |
|
| 20 |
|
| 21 |
-
print("Loading
|
| 22 |
|
| 23 |
|
| 24 |
bnb_config = BitsAndBytesConfig(
|
| 25 |
load_in_4bit=True,
|
| 26 |
-
bnb_4bit_quant_type="nf4",
|
| 27 |
bnb_4bit_compute_dtype=torch.float16,
|
|
|
|
| 28 |
bnb_4bit_use_double_quant=True
|
| 29 |
)
|
| 30 |
|
| 31 |
|
| 32 |
model = AutoModelForCausalLM.from_pretrained(
|
| 33 |
-
|
| 34 |
quantization_config=bnb_config,
|
| 35 |
device_map="auto"
|
| 36 |
)
|
| 37 |
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
print("Model loaded successfully")
|
| 42 |
|
| 43 |
|
| 44 |
-
|
| 45 |
-
You are Rezaeian-StatsAI.
|
| 46 |
-
You are a Persian university assistant specialized in Engineering Statistics and Probability.
|
| 47 |
-
Answer questions in Persian.
|
| 48 |
-
Solve problems step by step.
|
| 49 |
-
Use clear mathematical explanations.
|
| 50 |
-
"""
|
| 51 |
|
|
|
|
| 52 |
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
<|im_end|>
|
| 59 |
|
| 60 |
-
<|im_start|>user
|
| 61 |
-
{question}
|
| 62 |
-
<|im_end|>
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
|
| 68 |
inputs = tokenizer(
|
| 69 |
-
|
| 70 |
return_tensors="pt"
|
| 71 |
).to(model.device)
|
| 72 |
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
do_sample=True,
|
| 81 |
-
pad_token_id=tokenizer.eos_token_id
|
| 82 |
-
)
|
| 83 |
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
skip_special_tokens=True
|
| 88 |
)
|
| 89 |
|
| 90 |
|
| 91 |
-
|
| 92 |
-
text = text.split("assistant")[-1]
|
| 93 |
-
|
| 94 |
|
| 95 |
-
return text.strip()
|
| 96 |
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
fn=answer,
|
| 101 |
-
inputs=gr.Textbox(
|
| 102 |
-
lines=3,
|
| 103 |
-
placeholder="سوال آماری خود را بنویسید..."
|
| 104 |
-
),
|
| 105 |
-
outputs=gr.Textbox(
|
| 106 |
-
lines=10
|
| 107 |
-
),
|
| 108 |
title="Rezaeian-StatsAI",
|
| 109 |
-
description="
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
)
|
| 111 |
|
| 112 |
|
|
|
|
| 8 |
)
|
| 9 |
|
| 10 |
|
| 11 |
+
model_id = "ddfws/Rezaeian-StatsAI"
|
| 12 |
|
| 13 |
|
| 14 |
print("Loading tokenizer...")
|
| 15 |
|
| 16 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
|
|
|
|
|
|
| 17 |
|
| 18 |
|
| 19 |
+
print("Loading model...")
|
| 20 |
|
| 21 |
|
| 22 |
bnb_config = BitsAndBytesConfig(
|
| 23 |
load_in_4bit=True,
|
|
|
|
| 24 |
bnb_4bit_compute_dtype=torch.float16,
|
| 25 |
+
bnb_4bit_quant_type="nf4",
|
| 26 |
bnb_4bit_use_double_quant=True
|
| 27 |
)
|
| 28 |
|
| 29 |
|
| 30 |
model = AutoModelForCausalLM.from_pretrained(
|
| 31 |
+
model_id,
|
| 32 |
quantization_config=bnb_config,
|
| 33 |
device_map="auto"
|
| 34 |
)
|
| 35 |
|
| 36 |
|
| 37 |
+
print("Model loaded!")
|
|
|
|
|
|
|
| 38 |
|
| 39 |
|
| 40 |
+
def chat(message, history):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
messages = []
|
| 43 |
|
| 44 |
+
for user, assistant in history:
|
| 45 |
+
messages.append(
|
| 46 |
+
{"role":"user","content":user}
|
| 47 |
+
)
|
| 48 |
+
messages.append(
|
| 49 |
+
{"role":"assistant","content":assistant}
|
| 50 |
+
)
|
| 51 |
|
| 52 |
+
messages.append(
|
| 53 |
+
{"role":"user","content":message}
|
| 54 |
+
)
|
|
|
|
| 55 |
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
text = tokenizer.apply_chat_template(
|
| 58 |
+
messages,
|
| 59 |
+
tokenize=False,
|
| 60 |
+
add_generation_prompt=True
|
| 61 |
+
)
|
| 62 |
|
| 63 |
|
| 64 |
inputs = tokenizer(
|
| 65 |
+
text,
|
| 66 |
return_tensors="pt"
|
| 67 |
).to(model.device)
|
| 68 |
|
| 69 |
|
| 70 |
+
output = model.generate(
|
| 71 |
+
**inputs,
|
| 72 |
+
max_new_tokens=512,
|
| 73 |
+
temperature=0.7,
|
| 74 |
+
do_sample=True
|
| 75 |
+
)
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
|
| 78 |
+
result = tokenizer.decode(
|
| 79 |
+
output[0][inputs.input_ids.shape[1]:],
|
| 80 |
skip_special_tokens=True
|
| 81 |
)
|
| 82 |
|
| 83 |
|
| 84 |
+
return result
|
|
|
|
|
|
|
| 85 |
|
|
|
|
| 86 |
|
| 87 |
|
| 88 |
+
demo = gr.ChatInterface(
|
| 89 |
+
fn=chat,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
title="Rezaeian-StatsAI",
|
| 91 |
+
description="""
|
| 92 |
+
فارسی AI آماری ساخته شده توسط امیرحسین رضائیان.
|
| 93 |
+
Fine-tuned on university statistics notes.
|
| 94 |
+
Base model: Qwen2.5-7B-Instruct.
|
| 95 |
+
"""
|
| 96 |
)
|
| 97 |
|
| 98 |
|