narin45 commited on
Commit
488ccc7
·
verified ·
1 Parent(s): a4fda93

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -16
app.py CHANGED
@@ -2,14 +2,13 @@ import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
  """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
  """
7
  client = InferenceClient("Qwen/Qwen2.5-72B-Instruct")
8
 
9
-
10
  def respond(
11
  message,
12
- history: list[tuple[str, str]],
13
  system_message,
14
  max_tokens,
15
  temperature,
@@ -39,26 +38,87 @@ def respond(
39
  response += token
40
  yield response
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
  """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
  """
46
  demo = gr.ChatInterface(
47
  respond,
 
 
48
  additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
  ],
 
 
60
  )
61
 
62
-
63
  if __name__ == "__main__":
64
- demo.launch()
 
2
  from huggingface_hub import InferenceClient
3
 
4
  """
5
+ برای اطلاعات بیشتر درباره پشتیبانی Inference API از huggingface_hub، مستندات رو بررسی کنید: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
  """
7
  client = InferenceClient("Qwen/Qwen2.5-72B-Instruct")
8
 
 
9
  def respond(
10
  message,
11
+ history: list[tuple[str, str]], # فرمت تاریخچه همون تاپل می‌مونه
12
  system_message,
13
  max_tokens,
14
  temperature,
 
38
  response += token
39
  yield response
40
 
41
+ # CSS برای راست‌چین و فارسی کردن
42
+ css = """
43
+ .gr-button, .gr-input, .gr-textbox, .gr-chatbot {
44
+ direction: rtl !important;
45
+ text-align: right !important;
46
+ font-family: 'Tahoma', sans-serif !important;
47
+ }
48
+ .gr-markdown h1, .gr-markdown p {
49
+ direction: rtl !important;
50
+ text-align: right !important;
51
+ }
52
+ """
53
+
54
+ # جاوااسکریپت برای مدیریت Local Storage
55
+ js = """
56
+ () => {
57
+ // لود تاریخچه از Local Storage موقع شروع
58
+ const savedHistory = localStorage.getItem('chatHistory');
59
+ if (savedHistory) {
60
+ const history = JSON.parse(savedHistory);
61
+ const chatbot = document.querySelector('.chatbot');
62
+ if (chatbot) {
63
+ history.forEach(([userMsg, botMsg]) => {
64
+ chatbot.innerHTML += `<p><b>شما:</b> ${userMsg}</p>`;
65
+ chatbot.innerHTML += `<p><b>چت‌بات:</b> ${botMsg}</p>`;
66
+ });
67
+ }
68
+ }
69
+
70
+ // ذخیره تاریخچه بعد از هر پیام
71
+ const observer = new MutationObserver((mutations) => {
72
+ const chatbot = document.querySelector('.chatbot');
73
+ if (chatbot) {
74
+ const messages = chatbot.querySelectorAll('p');
75
+ const history = [];
76
+ let userMsg = null;
77
+ messages.forEach((msg) => {
78
+ if (msg.innerHTML.startsWith('<b>شما:</b>')) {
79
+ userMsg = msg.innerHTML.replace('<b>شما:</b> ', '');
80
+ } else if (msg.innerHTML.startsWith('<b>چت‌بات:</b>') && userMsg) {
81
+ const botMsg = msg.innerHTML.replace('<b>چت‌بات:</b> ', '');
82
+ history.push([userMsg, botMsg]);
83
+ userMsg = null;
84
+ }
85
+ });
86
+ localStorage.setItem('chatHistory', JSON.stringify(history));
87
+ }
88
+ });
89
+
90
+ // مشاهده تغییرات تو چت‌بات
91
+ const chatbotContainer = document.querySelector('.chatbot');
92
+ if (chatbotContainer) {
93
+ observer.observe(chatbotContainer, { childList: true, subtree: true });
94
+ }
95
+
96
+ // پاک کردن تاریخچه با دکمه "Clear"
97
+ const clearBtn = document.querySelector('.gr-button[aria-label="Clear"]');
98
+ if (clearBtn) {
99
+ clearBtn.addEventListener('click', () => {
100
+ localStorage.removeItem('chatHistory');
101
+ });
102
+ }
103
+ }
104
+ """
105
 
106
  """
107
+ برای اطلاعات بیشتر درباره سفارشی‌سازی ChatInterface، مستندات گرادیو رو ببینید: https://www.gradio.app/docs/chatinterface
108
  """
109
  demo = gr.ChatInterface(
110
  respond,
111
+ chatbot=gr.Chatbot(label="گفت‌وگو"),
112
+ textbox=gr.Textbox(placeholder="پیام خود را اینجا بنویسید...", label="ورودی"),
113
  additional_inputs=[
114
+ gr.Textbox(value="You are Qwen2.5-72B-Instruct, created by Alibaba Cloud. Do not mention Claude or Anthropic.", label="پیام سیستمی"),
115
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="حداکثر توکن‌های جدید"),
116
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="دما"),
117
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="تاپ-پی (نمونه‌برداری هسته‌ای)"),
 
 
 
 
 
 
118
  ],
119
+ additional_inputs_accordion=False, # مخفی کردن additional_inputs
120
+ css=css,
121
  )
122
 
 
123
  if __name__ == "__main__":
124
+ demo.launch()