narin45 commited on
Commit
8da2e31
·
verified ·
1 Parent(s): c17a5b4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -38
app.py CHANGED
@@ -5,7 +5,7 @@ client = InferenceClient("Qwen/Qwen2.5-72B-Instruct")
5
 
6
  def respond(
7
  message,
8
- history: list[tuple[str, str]],
9
  system_message,
10
  max_tokens,
11
  temperature,
@@ -13,16 +13,13 @@ def respond(
13
  ):
14
  messages = [{"role": "system", "content": system_message}]
15
 
 
16
  for val in history:
17
- if val[0]:
18
- messages.append({"role": "user", "content": val[0]})
19
- if val[1]:
20
- messages.append({"role": "assistant", "content": val[1]})
21
 
22
  messages.append({"role": "user", "content": message})
23
 
24
  response = ""
25
-
26
  for message in client.chat_completion(
27
  messages,
28
  max_tokens=max_tokens,
@@ -57,9 +54,12 @@ js = """
57
  // تنظیم تاریخچه اولیه تو چت‌بات
58
  const chatbot = document.querySelector('.chatbot');
59
  if (chatbot) {
60
- history.forEach(([userMsg, botMsg]) => {
61
- chatbot.innerHTML += `<p><b>شما:</b> ${userMsg}</p>`;
62
- chatbot.innerHTML += `<p><b>چت‌بات:</b> ${botMsg}</p>`;
 
 
 
63
  });
64
  }
65
  }
@@ -70,14 +70,16 @@ js = """
70
  if (chatbot) {
71
  const messages = chatbot.querySelectorAll('p');
72
  const history = [];
73
- let userMsg = null;
74
  messages.forEach((msg) => {
75
  if (msg.innerHTML.startsWith('<b>شما:</b>')) {
76
- userMsg = msg.innerHTML.replace('<b>شما:</b> ', '');
77
- } else if (msg.innerHTML.startsWith('<b>چت‌بات:</b>') && userMsg) {
78
- const botMsg = msg.innerHTML.replace('<b>چت‌بات:</b> ', '');
79
- history.push([userMsg, botMsg]);
80
- userMsg = null;
 
 
81
  }
82
  });
83
  localStorage.setItem('chatHistory', JSON.stringify(history));
@@ -91,8 +93,8 @@ js = """
91
  }
92
 
93
  // پاک کردن تاریخچه با دکمه "پاک کردن"
94
- const clearBtn = document.querySelector('.gr-button[aria-label="پاک کردن"]');
95
- if (clearBtn) {
96
  clearBtn.addEventListener('click', () => {
97
  localStorage.removeItem('chatHistory');
98
  });
@@ -100,27 +102,30 @@ js = """
100
  }
101
  """
102
 
103
- demo = gr.ChatInterface(
104
- respond,
105
- title="چت‌بات Qwen2.5-72B-Instruct",
106
- description="به چت‌بات خوش اومدید! پیام خودتون رو بنویسید.",
107
- chatbot=gr.Chatbot(label="گفت‌وگو"),
108
- textbox=gr.Textbox(placeholder="پیام خود را اینجا بنویسید...", label="ورودی"),
109
- submit_btn="ارسال",
110
- retry_btn="دوباره امتحان کن",
111
- undo_btn="لغو",
112
- clear_btn="پاک کردن",
113
- css=css,
114
- additional_inputs=[
115
- gr.Textbox(value="You are Qwen2.5-72B-Instruct, created by Alibaba Cloud. Do not mention Claude or Anthropic.", label="پیام سیستمی"),
116
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="حداکثر توکن‌های جدید"),
117
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="دما"),
118
- gr.Slider(minimum=0.1, maximum=1.0, value=0.9, step=0.05, label="تاپ-پی (نمونه‌برداری هسته‌ای)"),
119
- ],
120
- additional_inputs_accordion=False,
121
- # اضافه کردن جاوااسکریپت برای مدیریت Local Storage
122
- custom_js=js,
123
- )
 
 
 
124
 
125
  if __name__ == "__main__":
126
  demo.launch()
 
5
 
6
  def respond(
7
  message,
8
+ history: list[dict], # تغییر فرمت تاریخچه به OpenAI-style
9
  system_message,
10
  max_tokens,
11
  temperature,
 
13
  ):
14
  messages = [{"role": "system", "content": system_message}]
15
 
16
+ # اضافه کردن تاریخچه به messages
17
  for val in history:
18
+ messages.append(val)
 
 
 
19
 
20
  messages.append({"role": "user", "content": message})
21
 
22
  response = ""
 
23
  for message in client.chat_completion(
24
  messages,
25
  max_tokens=max_tokens,
 
54
  // تنظیم تاریخچه اولیه تو چت‌بات
55
  const chatbot = document.querySelector('.chatbot');
56
  if (chatbot) {
57
+ history.forEach(msg => {
58
+ if (msg.role === 'user') {
59
+ chatbot.innerHTML += `<p><b>شما:</b> ${msg.content}</p>`;
60
+ } else if (msg.role === 'assistant') {
61
+ chatbot.innerHTML += `<p><b>چت‌بات:</b> ${msg.content}</p>`;
62
+ }
63
  });
64
  }
65
  }
 
70
  if (chatbot) {
71
  const messages = chatbot.querySelectorAll('p');
72
  const history = [];
73
+ let lastRole = null;
74
  messages.forEach((msg) => {
75
  if (msg.innerHTML.startsWith('<b>شما:</b>')) {
76
+ const content = msg.innerHTML.replace('<b>شما:</b> ', '');
77
+ history.push({"role": "user", "content": content});
78
+ lastRole = 'user';
79
+ } else if (msg.innerHTML.startsWith('<b>چت‌بات:</b>')) {
80
+ const content = msg.innerHTML.replace('<b>چت‌بات:</b> ', '');
81
+ history.push({"role": "assistant", "content": content});
82
+ lastRole = 'assistant';
83
  }
84
  });
85
  localStorage.setItem('chatHistory', JSON.stringify(history));
 
93
  }
94
 
95
  // پاک کردن تاریخچه با دکمه "پاک کردن"
96
+ const clearBtn = document.querySelector('.gr-button');
97
+ if (clearBtn && clearBtn.textContent === 'پاک کردن') {
98
  clearBtn.addEventListener('click', () => {
99
  localStorage.removeItem('chatHistory');
100
  });
 
102
  }
103
  """
104
 
105
+ with gr.Blocks(css=css) as demo:
106
+ gr.Markdown("### چت‌بات Qwen2.5-72B-Instruct")
107
+ gr.Markdown("به چت‌بات خوش اومدید! پیام خودتون رو بنویسید.")
108
+ chatbot = gr.Chatbot(label="گفت‌وگو", type="messages") # تنظیم type به messages
109
+ msg = gr.Textbox(placeholder="پیام خود را اینجا بنویسید...", label="ورودی")
110
+ with gr.Row():
111
+ submit_btn = gr.Button("ارسال")
112
+ clear_btn = gr.Button("پاک کردن")
113
+ retry_btn = gr.Button("دوباره امتحان ک��")
114
+ undo_btn = gr.Button("لغو")
115
+ # لود جاوااسکریپت
116
+ gr.HTML(f'<script>{js}</script>')
117
+ # مخفی کردن additional_inputs
118
+ system_message = gr.Textbox(value="You are Qwen2.5-72B-Instruct, created by Alibaba Cloud. Do not mention Claude or Anthropic.", label="پیام سیستمی", visible=False)
119
+ max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="حداکثر توکن‌های جدید", visible=False)
120
+ temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="دما", visible=False)
121
+ top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.9, step=0.05, label="تاپ-پی (نمونه‌برداری هسته‌ای)", visible=False)
122
+
123
+ # اتصال دکمه‌ها
124
+ msg.submit(respond, [msg, chatbot, system_message, max_tokens, temperature, top_p], chatbot)
125
+ submit_btn.click(respond, [msg, chatbot, system_message, max_tokens, temperature, top_p], chatbot)
126
+ clear_btn.click(lambda: None, None, chatbot, queue=False)
127
+ retry_btn.click(respond, [msg, chatbot, system_message, max_tokens, temperature, top_p], chatbot)
128
+ undo_btn.click(lambda: None, None, chatbot, queue=False)
129
 
130
  if __name__ == "__main__":
131
  demo.launch()