ThongCoding commited on
Commit
abc08ee
·
verified ·
1 Parent(s): eb4ecfd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -29
app.py CHANGED
@@ -2,9 +2,7 @@ import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  from deep_translator import GoogleTranslator
4
 
5
- """
6
- 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
7
- """
8
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
9
  translator_vi2en = GoogleTranslator(source='vi', target='en')
10
  translator_en2vi = GoogleTranslator(source='en', target='vi')
@@ -13,45 +11,54 @@ def respond(
13
  message,
14
  history: list[tuple[str, str]],
15
  system_message,
16
- max_tokens,
17
- temperature,
18
- top_p,
19
  ):
 
20
  messages = [{"role": "system", "content": system_message}]
21
-
 
22
  message_en = translator_vi2en.translate(message)
23
 
 
24
  for val in history:
25
- if val[0]:
26
  messages.append({"role": "user", "content": val[0]})
27
- if val[1]:
28
  messages.append({"role": "assistant", "content": val[1]})
29
 
 
30
  messages.append({"role": "user", "content": message_en})
31
 
32
  response = ""
33
 
34
- for message in client.chat_completion(
35
- messages,
36
- max_tokens=max_tokens,
37
- stream=True,
38
- temperature=temperature,
39
- top_p=top_p,
40
- ):
41
- token = message.choices[0].delta.content
42
-
43
- response += token
44
- response_vi = translator_en2vi.translate(response)
45
-
46
- return response_vi
47
 
 
 
 
 
 
 
 
 
 
 
48
 
49
- """
50
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
51
- """
52
- demo = gr.ChatInterface(
53
- respond
54
- )
55
 
56
  if __name__ == "__main__":
57
- demo.launch()
 
2
  from huggingface_hub import InferenceClient
3
  from deep_translator import GoogleTranslator
4
 
5
+ # Khởi tạo client HF và translator
 
 
6
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
7
  translator_vi2en = GoogleTranslator(source='vi', target='en')
8
  translator_en2vi = GoogleTranslator(source='en', target='vi')
 
11
  message,
12
  history: list[tuple[str, str]],
13
  system_message,
14
+ max_tokens=128,
15
+ temperature=0.5,
16
+ top_p=0.9,
17
  ):
18
+ # Tạo list tin nhắn bắt đầu từ hệ thống
19
  messages = [{"role": "system", "content": system_message}]
20
+
21
+ # Dịch câu hỏi của người dùng từ tiếng Việt sang tiếng Anh
22
  message_en = translator_vi2en.translate(message)
23
 
24
+ # Thêm các tin nhắn lịch sử vào messages
25
  for val in history:
26
+ if val[0]: # Tin nhắn của người dùng
27
  messages.append({"role": "user", "content": val[0]})
28
+ if val[1]: # Tin nhắn của trợ lý
29
  messages.append({"role": "assistant", "content": val[1]})
30
 
31
+ # Thêm câu hỏi của người dùng vào cuối lịch sử
32
  messages.append({"role": "user", "content": message_en})
33
 
34
  response = ""
35
 
36
+ # Gửi yêu cầu tới mô hình và nhận kết quả
37
+ try:
38
+ for message in client.chat_completion(
39
+ model="HuggingFaceH4/zephyr-7b-beta",
40
+ messages=messages,
41
+ max_tokens=max_tokens,
42
+ temperature=temperature,
43
+ top_p=top_p,
44
+ ):
45
+ # Tiến hành nhận phản hồi từ mô hình
46
+ token = message["choices"][0]["delta"]["content"]
47
+ response += token
 
48
 
49
+ except Exception as e:
50
+ return f"Error: {str(e)}"
51
+
52
+ # Dịch câu trả lời về tiếng Việt
53
+ try:
54
+ response_vi = translator_en2vi.translate(response)
55
+ except Exception:
56
+ response_vi = "(Không thể dịch câu trả lời về tiếng Việt)"
57
+
58
+ return response_vi
59
 
60
+ # Gradio UI
61
+ demo = gr.ChatInterface(fn=respond)
 
 
 
 
62
 
63
  if __name__ == "__main__":
64
+ demo.launch()