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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -84
app.py CHANGED
@@ -1,22 +1,26 @@
1
  import gradio as gr
 
2
  import torch
 
3
  from transformers import AutoTokenizer, AutoModelForCausalLM
4
 
5
 
6
- MODEL_PATH = "ddfws/Rezaeian-StatsAI"
7
 
8
 
9
  print("Loading tokenizer...")
10
 
11
- tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
 
 
12
 
13
 
14
  print("Loading model...")
15
 
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()
@@ -25,154 +29,130 @@ 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
- "x̄": 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
 
44
 
45
 
 
46
  def generate(message, history):
47
 
48
- messages=[]
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(
70
- messages,
71
- tokenize=False,
72
- add_generation_prompt=True
73
- )
74
 
75
 
76
  inputs = tokenizer(
77
  prompt,
78
  return_tensors="pt"
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,
177
- show_api=False
178
  )
 
1
  import gradio as gr
2
+ import spaces
3
  import torch
4
+
5
  from transformers import AutoTokenizer, AutoModelForCausalLM
6
 
7
 
8
+ MODEL_ID = "ddfws/Rezaeian-StatsAI"
9
 
10
 
11
  print("Loading tokenizer...")
12
 
13
+ tokenizer = AutoTokenizer.from_pretrained(
14
+ MODEL_ID
15
+ )
16
 
17
 
18
  print("Loading model...")
19
 
20
  model = AutoModelForCausalLM.from_pretrained(
21
+ MODEL_ID,
22
  device_map="auto",
23
+ dtype=torch.float16
24
  )
25
 
26
  model.eval()
 
29
 
30
 
31
 
32
+ def format_answer(text):
 
 
 
 
 
 
 
 
 
33
 
34
+ # تبدیل چند فرمول رایج آماری به LaTeX
35
+ text = text.replace("β0", "$\\beta_0$")
36
+ text = text.replace("β1", "$\\beta_1$")
37
+ text = text.replace("R2", "$R^2$")
38
+ text = text.replace("σ2", "$\\sigma^2$")
39
+ text = text.replace("x̄", "$\\bar{x}$")
40
 
41
  return text
42
 
43
 
44
 
45
+ @spaces.GPU
46
  def generate(message, history):
47
 
48
+ prompt = ""
 
 
 
 
 
 
 
49
 
50
+ for user, assistant in history:
 
 
 
51
 
52
+ prompt += (
53
+ f"User: {user}\n"
54
+ f"Assistant: {assistant}\n"
55
+ )
 
56
 
57
 
58
+ prompt += f"User: {message}\nAssistant:"
 
 
 
 
59
 
60
 
61
  inputs = tokenizer(
62
  prompt,
63
  return_tensors="pt"
64
+ )
65
+
66
+
67
+ inputs = {
68
+ k:v.to(model.device)
69
+ for k,v in inputs.items()
70
+ }
71
 
72
 
73
  with torch.no_grad():
74
 
75
+ output = model.generate(
76
+
77
  **inputs,
78
+
79
+ max_new_tokens=512,
80
+
81
+ temperature=0.7,
82
+
83
  top_p=0.9,
84
+
85
  do_sample=True
86
  )
87
 
88
 
89
+ result = tokenizer.decode(
90
+ output[0],
91
  skip_special_tokens=True
92
  )
93
 
94
 
95
+ answer = result.split("Assistant:")[-1]
96
+
97
 
98
+ return format_answer(answer)
99
 
100
 
 
101
 
102
+ css = """
103
+
104
+ footer {
105
  display:none !important;
106
  }
107
 
108
 
109
+ .gradio-container {
110
+
111
+ direction:rtl !important;
112
+
113
  }
114
 
115
 
116
+ .message {
117
+
118
  direction:rtl !important;
119
+
120
  text-align:right !important;
 
 
121
 
122
+ unicode-bidi:plaintext;
123
 
 
 
 
124
  }
125
 
126
 
127
+ textarea {
128
+
129
  direction:rtl !important;
 
 
130
 
131
+ text-align:right !important;
132
 
 
 
 
133
  }
134
 
135
 
136
+ code, pre {
137
 
138
+ direction:ltr !important;
139
 
140
+ text-align:left !important;
141
 
 
 
 
 
 
142
  }
 
 
 
 
143
 
144
  """
145
 
146
 
 
 
 
 
 
147
  demo = gr.ChatInterface(
148
  fn=generate,
 
149
  title="Rezaeian StatsAI",
150
+ description="دستیار هوش مصنوعی آمار",
151
+ css=css
 
152
  )
153
 
154
 
155
+
156
  demo.launch(
157
+ server_name="0.0.0.0"
 
 
158
  )