ddfws commited on
Commit
9abf2ee
·
verified ·
1 Parent(s): aa88648

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -122
app.py CHANGED
@@ -8,9 +8,7 @@ MODEL_PATH = "ddfws/Rezaeian-StatsAI"
8
 
9
  print("Loading tokenizer...")
10
 
11
- tokenizer = AutoTokenizer.from_pretrained(
12
- MODEL_PATH
13
- )
14
 
15
 
16
  print("Loading model...")
@@ -18,7 +16,7 @@ print("Loading model...")
18
  model = AutoModelForCausalLM.from_pretrained(
19
  MODEL_PATH,
20
  device_map="auto",
21
- dtype=torch.float16 if torch.cuda.is_available() else torch.float32
22
  )
23
 
24
  model.eval()
@@ -26,20 +24,20 @@ model.eval()
26
  print("MODEL READY")
27
 
28
 
 
29
  def format_text(text):
30
 
31
- # فرمول های رایج آماری
32
  replacements = {
33
  "β0": r"$\beta_0$",
34
  "β1": r"$\beta_1$",
35
  "σ2": r"$\sigma^2$",
36
  "R2": r"$R^2$",
37
- "xbar": r"$\bar{x}$",
38
- "mu": r"$\mu$",
39
  }
40
 
41
  for a,b in replacements.items():
42
- text = text.replace(a,b)
43
 
44
  return text
45
 
@@ -51,27 +49,21 @@ def generate(message, history):
51
 
52
  for user, bot in history:
53
 
54
- messages.append(
55
- {
56
- "role":"user",
57
- "content":user
58
- }
59
- )
60
 
61
- messages.append(
62
- {
63
- "role":"assistant",
64
- "content":bot
65
- }
66
- )
67
 
68
 
69
- messages.append(
70
- {
71
- "role":"user",
72
- "content":message
73
- }
74
- )
75
 
76
 
77
  prompt = tokenizer.apply_chat_template(
@@ -87,157 +79,98 @@ def generate(message, history):
87
  ).to(model.device)
88
 
89
 
90
-
91
  with torch.no_grad():
92
 
93
- output = model.generate(
94
  **inputs,
95
  max_new_tokens=700,
96
  temperature=0.6,
97
  top_p=0.9,
98
- do_sample=True,
99
- repetition_penalty=1.1
100
  )
101
 
102
 
103
-
104
- answer = tokenizer.decode(
105
  output[0][inputs.input_ids.shape[1]:],
106
  skip_special_tokens=True
107
  )
108
 
109
 
110
- answer = format_text(answer)
111
-
112
-
113
- return answer
114
-
115
-
116
 
117
 
118
- css = """
119
 
120
- /* حذف موارد اضافی */
121
 
122
- footer {
123
- display:none !important;
124
  }
125
 
126
 
127
- .gradio-container {
128
-
129
- max-width:1100px !important;
130
- direction:rtl;
131
-
132
  }
133
 
134
 
135
- /* پیام ها */
136
-
137
- .message {
138
-
139
- direction:rtl !important;
140
- text-align:right !important;
141
- unicode-bidi:plaintext;
142
-
143
  }
144
 
145
 
146
- /* متن فارسی */
147
-
148
- .markdown {
149
-
150
- direction:rtl;
151
- text-align:right;
152
-
153
  }
154
 
155
 
156
- /* ورودی */
157
-
158
- textarea {
159
-
160
- direction:rtl !important;
161
- text-align:right !important;
162
-
163
  }
164
 
165
 
166
- /* فرمول ها */
167
-
168
- .katex {
169
-
170
- direction:ltr !important;
171
-
172
  }
173
 
174
 
175
- .MathJax {
176
-
177
- direction:ltr !important;
178
-
179
- }
180
-
181
-
182
- /* کد و انگلیسی */
183
-
184
- code, pre {
185
-
186
- direction:ltr !important;
187
- text-align:left !important;
188
-
189
- }
190
-
191
  """
192
 
193
 
194
- head = """
195
 
196
  <script>
197
- window.MathJax = {
198
- tex: {
199
- inlineMath: [['$', '$']],
200
- displayMath: [['$$','$$']]
201
- }
202
  };
203
  </script>
204
 
205
- <script
206
- src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js">
207
- </script>
208
 
209
  """
210
 
211
 
212
- demo = gr.ChatInterface(
 
 
213
 
214
- fn=generate,
215
 
 
 
 
216
  title="Rezaeian StatsAI",
217
-
218
- description=
219
- "دستیار هوش مصنوعی آمار و احتمال مهندسی",
220
-
221
  css=css,
222
-
223
- head=head,
224
-
225
- chatbot=gr.Chatbot(
226
- height=650,
227
- show_copy_button=True
228
- ),
229
-
230
- examples=[
231
- "فرمول میانگین نمونه را بنویس",
232
- "رگرسیون خطی را با فرمول توضیح بده",
233
- "آزمون t چیست؟",
234
- "انحراف معیار را محاسبه کن"
235
- ]
236
-
237
  )
238
 
239
 
240
-
241
  demo.launch(
242
  server_name="0.0.0.0",
243
  server_port=7860,
 
8
 
9
  print("Loading tokenizer...")
10
 
11
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
 
 
12
 
13
 
14
  print("Loading model...")
 
16
  model = AutoModelForCausalLM.from_pretrained(
17
  MODEL_PATH,
18
  device_map="auto",
19
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
20
  )
21
 
22
  model.eval()
 
24
  print("MODEL READY")
25
 
26
 
27
+
28
  def format_text(text):
29
 
 
30
  replacements = {
31
  "β0": r"$\beta_0$",
32
  "β1": r"$\beta_1$",
33
  "σ2": r"$\sigma^2$",
34
  "R2": r"$R^2$",
35
+ "": r"$\bar{x}$",
36
+ "mu": r"$\mu$"
37
  }
38
 
39
  for a,b in replacements.items():
40
+ text=text.replace(a,b)
41
 
42
  return text
43
 
 
49
 
50
  for user, bot in history:
51
 
52
+ messages.append({
53
+ "role":"user",
54
+ "content":user
55
+ })
 
 
56
 
57
+ messages.append({
58
+ "role":"assistant",
59
+ "content":bot
60
+ })
 
 
61
 
62
 
63
+ messages.append({
64
+ "role":"user",
65
+ "content":message
66
+ })
 
 
67
 
68
 
69
  prompt = tokenizer.apply_chat_template(
 
79
  ).to(model.device)
80
 
81
 
 
82
  with torch.no_grad():
83
 
84
+ output=model.generate(
85
  **inputs,
86
  max_new_tokens=700,
87
  temperature=0.6,
88
  top_p=0.9,
89
+ do_sample=True
 
90
  )
91
 
92
 
93
+ answer=tokenizer.decode(
 
94
  output[0][inputs.input_ids.shape[1]:],
95
  skip_special_tokens=True
96
  )
97
 
98
 
99
+ return format_text(answer)
 
 
 
 
 
100
 
101
 
 
102
 
103
+ css="""
104
 
105
+ footer{
106
+ display:none !important;
107
  }
108
 
109
 
110
+ .gradio-container{
111
+ direction:rtl;
 
 
 
112
  }
113
 
114
 
115
+ .message{
116
+ direction:rtl !important;
117
+ text-align:right !important;
118
+ unicode-bidi:plaintext;
 
 
 
 
119
  }
120
 
121
 
122
+ .markdown{
123
+ direction:rtl;
124
+ text-align:right;
 
 
 
 
125
  }
126
 
127
 
128
+ textarea{
129
+ direction:rtl !important;
130
+ text-align:right !important;
 
 
 
 
131
  }
132
 
133
 
134
+ code,pre{
135
+ direction:ltr !important;
136
+ text-align:left !important;
 
 
 
137
  }
138
 
139
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
  """
141
 
142
 
143
+ head="""
144
 
145
  <script>
146
+ window.MathJax={
147
+ tex:{
148
+ inlineMath:[['$','$']],
149
+ displayMath:[['$$','$$']]
150
+ }
151
  };
152
  </script>
153
 
154
+ <script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
 
 
155
 
156
  """
157
 
158
 
159
+ chatbot = gr.Chatbot(
160
+ height=600
161
+ )
162
 
 
163
 
164
+ demo = gr.ChatInterface(
165
+ fn=generate,
166
+ chatbot=chatbot,
167
  title="Rezaeian StatsAI",
168
+ description="دستیار هوش مصنوعی آمار و احنمال مهندسی",
 
 
 
169
  css=css,
170
+ head=head
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171
  )
172
 
173
 
 
174
  demo.launch(
175
  server_name="0.0.0.0",
176
  server_port=7860,