narin45 commited on
Commit
9be8f0b
·
verified ·
1 Parent(s): 4ba9759

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -64
app.py CHANGED
@@ -1,32 +1,14 @@
1
  import gradio as gr
2
- import sys # این خط را هم اضافه کنید
3
-
4
- print(f"--- Checking Gradio Version ---")
5
- print(f"Gradio version found: {gr.__version__}")
6
- print(f"-------------------------------")
7
- # sys.exit() # اگر میخواهید فقط نسخه را چک کنید و خارج شوید، این خط را از کامنت درآورید
8
-
9
- # --- بقیه کدهای شما از اینجا شروع می‌شود ---
10
-
11
-
12
-
13
-
14
-
15
-
16
  from huggingface_hub import InferenceClient
17
 
18
  """
19
- 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
20
  """
21
- # توجه: اگر از مدل خاصی استفاده می‌کنید که نیاز به توکن دارد،
22
- # باید توکن خود را به صورت امن مدیریت کنید (مثلاً از طریق متغیرهای محیطی)
23
- # client = InferenceClient("Qwen/Qwen2.5-72B-Instruct", token="YOUR_HF_TOKEN")
24
  client = InferenceClient("Qwen/Qwen2.5-72B-Instruct")
25
 
26
-
27
  def respond(
28
  message,
29
- history: list[tuple[str, str]],
30
  system_message,
31
  max_tokens,
32
  temperature,
@@ -35,62 +17,54 @@ def respond(
35
  messages = [{"role": "system", "content": system_message}]
36
 
37
  for val in history:
38
- if val[0]:
39
- messages.append({"role": "user", "content": val[0]})
40
- if val[1]:
41
- messages.append({"role": "assistant", "content": val[1]})
42
 
43
  messages.append({"role": "user", "content": message})
44
 
45
  response = ""
46
-
47
- try:
48
- for message_chunk in client.chat_completion(
49
- messages,
50
- max_tokens=max_tokens,
51
- stream=True,
52
- temperature=temperature,
53
- top_p=top_p,
54
- ):
55
- # بررسی کنید که آیا محتوا وجود دارد و None نیست
56
- token = message_chunk.choices[0].delta.content
57
- if token:
58
- response += token
59
- yield response
60
- except Exception as e:
61
- # در صورت بروز خطا، یک پیام خطا نمایش دهید
62
- yield f"خطا در ارتباط با مدل: {e}"
63
- # یا می‌توانید از یک پیام عمومی‌تر استفاده کنید:
64
- # yield "متاسفانه مشکلی در پردازش درخواست شما پیش آمد."
65
-
 
 
 
 
 
 
66
 
67
  """
68
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
69
  """
70
  demo = gr.ChatInterface(
71
  respond,
72
- # --- تغییرات اصلی اینجا هستند ---
73
- rtl=True, # فعال کردن حالت راست‌چین برای کل رابط کاربری
74
- placeholder="پیام خود را بنویسید...", # تغییر متن پیش‌فرض کادر ورودی
75
- # ---------------------------------
76
  additional_inputs=[
77
- gr.Textbox(value="You are a friendly Chatbot.", label="پیام سیستمی"), # لیبل فارسی
78
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="حداکثر توکن‌های جدید"), # لیبل فارسی
79
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="دما (Temperature)"), # لیبل فارسی
80
- gr.Slider(
81
- minimum=0.1,
82
- maximum=1.0,
83
- value=0.95,
84
- step=0.05,
85
- label="Top-p (نمونه‌برداری هسته‌ای)", # لیبل فارسی
86
- ),
87
  ],
88
- title="چت‌بات فارسی با Qwen", # عنوان اختیاری برای تب مرورگر یا بالای صفحه
89
- description="یک رابط کاربری چت با مدل Qwen 2.5 72B Instruct", # توضیحات اختیاری
90
  )
91
 
92
-
93
  if __name__ == "__main__":
94
- # برای اجرای عمومی‌تر، می‌توانید هاست را 0.0.0.0 قرار دهید
95
- # demo.launch(server_name="0.0.0.0", server_port=7860)
96
  demo.launch()
 
1
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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[dict], # تغییر فرمت تاریخچه به OpenAI-style برای هماهنگی با type="messages"
12
  system_message,
13
  max_tokens,
14
  temperature,
 
17
  messages = [{"role": "system", "content": system_message}]
18
 
19
  for val in history:
20
+ messages.append(val)
 
 
 
21
 
22
  messages.append({"role": "user", "content": message})
23
 
24
  response = ""
25
+ for message in client.chat_completion(
26
+ messages,
27
+ max_tokens=max_tokens,
28
+ stream=True,
29
+ temperature=temperature,
30
+ top_p=top_p,
31
+ ):
32
+ token = message.choices[0].delta.content
33
+ response += token
34
+ yield response
35
+
36
+ # CSS برای راست‌چین، فارسی کردن، و مخفی کردن additional_inputs
37
+ css = """
38
+ .gr-button, .gr-input, .gr-textbox, .gr-chatbot {
39
+ direction: rtl !important;
40
+ text-align: right !important;
41
+ font-family: 'Tahoma', sans-serif !important;
42
+ }
43
+ .gr-markdown h1, .gr-markdown p {
44
+ direction: rtl !important;
45
+ text-align: right !important;
46
+ }
47
+ #additional-inputs {
48
+ display: none !important; /* مخفی کردن additional_inputs */
49
+ }
50
+ """
51
 
52
  """
53
+ برای اطلاعات بیشتر درباره سفارشی‌سازی ChatInterface، مستندات گرادیو رو ببینید: https://www.gradio.app/docs/chatinterface
54
  """
55
  demo = gr.ChatInterface(
56
  respond,
57
+ chatbot=gr.Chatbot(label="گفت‌وگو", type="messages"), # تنظیم type به messages
58
+ textbox=gr.Textbox(placeholder="پیام خود را اینجا بنویسید...", label="ورودی"),
 
 
59
  additional_inputs=[
60
+ gr.Textbox(value="You are Qwen2.5-72B-Instruct, created by Alibaba Cloud. Do not mention Claude or Anthropic.", label="پیام سیستمی"),
61
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="حداکثر توکن‌های جدید"),
62
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="دما"),
63
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="تاپ-پی (نمونه‌برداری هسته‌ای)"),
 
 
 
 
 
 
64
  ],
65
+ additional_inputs_accordion="تنظیمات پیشرفته", # تغییر به رشته
66
+ css=css,
67
  )
68
 
 
69
  if __name__ == "__main__":
 
 
70
  demo.launch()