ThongCoding commited on
Commit
b1ef4b2
·
verified ·
1 Parent(s): dfa122d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -10
app.py CHANGED
@@ -1,11 +1,14 @@
1
  import gradio as gr
2
  import logging, langdetect
3
  from huggingface_hub import InferenceClient
4
- from textblob import TextBlob
5
 
6
  # Khởi tạo client HF và translator
7
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
 
 
 
9
  logging.basicConfig(level=logging.INFO)
10
 
11
  def respond(
@@ -24,10 +27,7 @@ def respond(
24
  messages = [{"role": "system", "content": system_message}]
25
 
26
  # Dịch câu hỏi của người dùng từ tiếng Việt sang tiếng Anh
27
- message_blob = TextBlob(message)
28
- logging.info(f"User message blob directory: {dir(message_blob)}")
29
-
30
- message_en = str(message_blob.translate(to='en'))
31
 
32
  # Thêm các tin nhắn lịch sử vào messages
33
  for val in history:
@@ -56,12 +56,9 @@ def respond(
56
 
57
  except Exception as e:
58
  return f"Error: {str(e)}"
59
-
60
- response_blob = TextBlob(response)
61
- logging.info(f"Bot message blob directory: {dir(response_blob)}")
62
-
63
- response_vi = str(response_blob.translate(to='vi'))
64
  logging.info(f"Successfully generated text: {response}")
 
65
 
66
  return response if langdetect.detect(response) == 'vi' else response_vi
67
 
 
1
  import gradio as gr
2
  import logging, langdetect
3
  from huggingface_hub import InferenceClient
4
+ from libretranslatepy import LibreTranslateAPI
5
 
6
  # Khởi tạo client HF và translator
7
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
9
+ # Gọi API public
10
+ lt = LibreTranslateAPI("https://libretranslate.de/") # Hoặc server tự host
11
+
12
  logging.basicConfig(level=logging.INFO)
13
 
14
  def respond(
 
27
  messages = [{"role": "system", "content": system_message}]
28
 
29
  # Dịch câu hỏi của người dùng từ tiếng Việt sang tiếng Anh
30
+ message_en = lt.translate(message, "vi", "en")
 
 
 
31
 
32
  # Thêm các tin nhắn lịch sử vào messages
33
  for val in history:
 
56
 
57
  except Exception as e:
58
  return f"Error: {str(e)}"
59
+
 
 
 
 
60
  logging.info(f"Successfully generated text: {response}")
61
+ response_vi = lt.translate(response, "en", "vi")
62
 
63
  return response if langdetect.detect(response) == 'vi' else response_vi
64