ddfws commited on
Commit
1c206a7
·
verified ·
1 Parent(s): 51f1f7f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -29
app.py CHANGED
@@ -34,13 +34,18 @@ def generate(message, history):
34
  prompt = ""
35
 
36
  for item in history:
 
37
  if isinstance(item, dict):
 
38
  if item["role"] == "user":
39
  prompt += f"User: {item['content']}\n"
 
40
  else:
41
  prompt += f"Assistant: {item['content']}\n"
 
42
  else:
43
- prompt += f"User: {item[0]}\nAssistant: {item[1]}\n"
 
44
 
45
 
46
  prompt += f"User: {message}\nAssistant:"
@@ -78,90 +83,131 @@ def generate(message, history):
78
 
79
  answer = result.split("Assistant:")[-1]
80
 
 
81
  return answer.strip()
82
 
83
 
84
 
85
  css = """
 
86
  body {
87
  direction: rtl;
88
  }
89
 
 
90
  .gradio-container {
91
- direction: rtl;
92
- text-align: right;
 
 
93
  }
94
 
 
95
  textarea {
 
96
  direction: rtl !important;
97
  text-align: right !important;
 
98
  }
99
 
 
100
  .message {
101
- direction: rtl;
102
- text-align: right;
 
 
103
  }
104
 
 
105
  """
106
 
107
 
108
  with gr.Blocks(css=css) as demo:
109
 
 
110
  gr.Markdown(
111
  """
112
  # 🤖 Rezaeian StatsAI
113
- ### دستیار هوش مصنوعی آمار و تحلیل داده
 
114
  """
115
  )
116
 
117
 
118
  chatbot = gr.Chatbot(
119
- height=600
 
120
  )
121
 
122
 
123
- msg = gr.Textbox(
124
- placeholder="سوال خود را بنویسید...",
125
- lines=2
126
- )
127
 
128
 
129
- clear = gr.Button("پاک کردن")
 
 
 
 
130
 
131
 
132
- def respond(message, history):
 
 
 
133
 
134
- answer = generate(message, history)
135
 
136
- history.append(
137
- {
138
- "role":"user",
139
- "content":message
140
- }
 
 
 
 
 
 
 
141
  )
142
 
143
- history.append(
 
 
144
  {
145
- "role":"assistant",
146
- "content":answer
 
 
 
 
 
147
  }
148
- )
 
 
149
 
150
  return "", history
151
 
152
 
153
 
154
- msg.submit(
155
- respond,
156
- [msg, chatbot],
157
- [msg, chatbot]
 
 
 
 
 
 
 
158
  )
159
 
160
 
161
  clear.click(
162
  lambda: [],
163
- None,
164
- chatbot
165
  )
166
 
167
 
 
34
  prompt = ""
35
 
36
  for item in history:
37
+
38
  if isinstance(item, dict):
39
+
40
  if item["role"] == "user":
41
  prompt += f"User: {item['content']}\n"
42
+
43
  else:
44
  prompt += f"Assistant: {item['content']}\n"
45
+
46
  else:
47
+ prompt += f"User: {item[0]}\n"
48
+ prompt += f"Assistant: {item[1]}\n"
49
 
50
 
51
  prompt += f"User: {message}\nAssistant:"
 
83
 
84
  answer = result.split("Assistant:")[-1]
85
 
86
+
87
  return answer.strip()
88
 
89
 
90
 
91
  css = """
92
+
93
  body {
94
  direction: rtl;
95
  }
96
 
97
+
98
  .gradio-container {
99
+
100
+ max-width: 900px !important;
101
+ margin: auto !important;
102
+
103
  }
104
 
105
+
106
  textarea {
107
+
108
  direction: rtl !important;
109
  text-align: right !important;
110
+
111
  }
112
 
113
+
114
  .message {
115
+
116
+ direction: rtl !important;
117
+ text-align: right !important;
118
+
119
  }
120
 
121
+
122
  """
123
 
124
 
125
  with gr.Blocks(css=css) as demo:
126
 
127
+
128
  gr.Markdown(
129
  """
130
  # 🤖 Rezaeian StatsAI
131
+
132
+ دستیار هوش مصنوعی آمار و تحلیل داده
133
  """
134
  )
135
 
136
 
137
  chatbot = gr.Chatbot(
138
+ height=450,
139
+ type="messages"
140
  )
141
 
142
 
143
+ with gr.Row():
 
 
 
144
 
145
 
146
+ textbox = gr.Textbox(
147
+ placeholder="سوال خود را بنویسید...",
148
+ scale=8,
149
+ container=False
150
+ )
151
 
152
 
153
+ send = gr.Button(
154
+ "ارسال",
155
+ scale=1
156
+ )
157
 
 
158
 
159
+ clear = gr.Button(
160
+ "پاک کردن",
161
+ scale=1
162
+ )
163
+
164
+
165
+
166
+ def chat(message, history):
167
+
168
+ answer = generate(
169
+ message,
170
+ history
171
  )
172
 
173
+
174
+ history = history + [
175
+
176
  {
177
+ "role": "user",
178
+ "content": message
179
+ },
180
+
181
+ {
182
+ "role": "assistant",
183
+ "content": answer
184
  }
185
+
186
+ ]
187
+
188
 
189
  return "", history
190
 
191
 
192
 
193
+ send.click(
194
+ chat,
195
+ inputs=[textbox, chatbot],
196
+ outputs=[textbox, chatbot]
197
+ )
198
+
199
+
200
+ textbox.submit(
201
+ chat,
202
+ inputs=[textbox, chatbot],
203
+ outputs=[textbox, chatbot]
204
  )
205
 
206
 
207
  clear.click(
208
  lambda: [],
209
+ inputs=None,
210
+ outputs=chatbot
211
  )
212
 
213