ddfws commited on
Commit
094abed
·
verified ·
1 Parent(s): c97e898

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -17
app.py CHANGED
@@ -3,7 +3,8 @@ import torch
3
 
4
  from transformers import (
5
  AutoTokenizer,
6
- AutoModelForCausalLM
 
7
  )
8
 
9
 
@@ -11,30 +12,45 @@ MODEL_ID = "ddfws/Rezaeian-StatsAI"
11
 
12
 
13
  print("Loading tokenizer...")
14
- tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
- print("Loading model...")
18
 
19
  model = AutoModelForCausalLM.from_pretrained(
20
  MODEL_ID,
21
- torch_dtype=torch.float16,
22
  device_map="auto"
23
  )
24
 
25
 
26
  model.eval()
27
 
 
 
28
 
29
  system_prompt = """
30
  You are Rezaeian-StatsAI.
31
- You are a university statistics and probability assistant.
32
- Answer in Persian.
33
- Solve engineering statistics problems step by step.
 
34
  """
35
 
36
 
37
- def chat(question):
38
 
39
  prompt = f"""
40
  <|im_start|>system
@@ -57,33 +73,40 @@ def chat(question):
57
 
58
  with torch.no_grad():
59
 
60
- output = model.generate(
61
  **inputs,
62
  max_new_tokens=512,
63
  temperature=0.7,
64
- do_sample=True
 
65
  )
66
 
67
 
68
- answer = tokenizer.decode(
69
- output[0],
70
  skip_special_tokens=True
71
  )
72
 
73
 
74
- return answer.split("assistant")[-1]
 
 
 
 
 
75
 
76
 
77
  demo = gr.Interface(
78
- fn=chat,
79
  inputs=gr.Textbox(
80
- label="سوال آماری"
 
81
  ),
82
  outputs=gr.Textbox(
83
- label="پاسخ مدل"
84
  ),
85
  title="Rezaeian-StatsAI",
86
- description="AI assistant for Engineering Statistics"
87
  )
88
 
89
 
 
3
 
4
  from transformers import (
5
  AutoTokenizer,
6
+ AutoModelForCausalLM,
7
+ BitsAndBytesConfig
8
  )
9
 
10
 
 
12
 
13
 
14
  print("Loading tokenizer...")
 
15
 
16
+ tokenizer = AutoTokenizer.from_pretrained(
17
+ MODEL_ID
18
+ )
19
+
20
+
21
+ print("Loading 4bit model...")
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
  MODEL_ID,
34
+ quantization_config=bnb_config,
35
  device_map="auto"
36
  )
37
 
38
 
39
  model.eval()
40
 
41
+ print("Model loaded successfully")
42
+
43
 
44
  system_prompt = """
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
+ def answer(question):
54
 
55
  prompt = f"""
56
  <|im_start|>system
 
73
 
74
  with torch.no_grad():
75
 
76
+ outputs = model.generate(
77
  **inputs,
78
  max_new_tokens=512,
79
  temperature=0.7,
80
+ do_sample=True,
81
+ pad_token_id=tokenizer.eos_token_id
82
  )
83
 
84
 
85
+ text = tokenizer.decode(
86
+ outputs[0],
87
  skip_special_tokens=True
88
  )
89
 
90
 
91
+ if "assistant" in text:
92
+ text = text.split("assistant")[-1]
93
+
94
+
95
+ return text.strip()
96
+
97
 
98
 
99
  demo = gr.Interface(
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="Persian Engineering Statistics AI"
110
  )
111
 
112