Reality123b commited on
Commit
f147126
·
verified ·
1 Parent(s): 3291aee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -12
app.py CHANGED
@@ -1,14 +1,38 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
 
 
3
 
4
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  def check_custom_responses(message: str) -> str:
7
  """Check for specific patterns and return custom responses."""
8
- # Convert message to lowercase for case-insensitive matching
9
  message_lower = message.lower()
10
-
11
- # Dictionary of custom responses
12
  custom_responses = {
13
  "what is ur name?": "xylaria",
14
  "what is your name?": "xylaria",
@@ -19,15 +43,11 @@ def check_custom_responses(message: str) -> str:
19
  "how many r is in strawberry": "3",
20
  "who is ur dev": "sk md saad amin",
21
  "who is ur developer": "sk md saad amin",
22
-
23
-
24
  }
25
 
26
- # Check if message matches any custom patterns
27
  for pattern, response in custom_responses.items():
28
  if pattern in message_lower:
29
  return response
30
-
31
  return None
32
 
33
  def respond(
@@ -44,17 +64,22 @@ def respond(
44
  yield custom_response
45
  return
46
 
47
- # If no custom response, proceed with normal chat completion
48
- messages = [{"role": "system", "content": system_message}]
49
 
 
 
50
  for val in history:
51
  if val[0]:
52
- messages.append({"role": "user", "content": val[0]})
 
 
53
  if val[1]:
54
  messages.append({"role": "assistant", "content": val[1]})
55
 
56
- messages.append({"role": "user", "content": message})
57
 
 
58
  response = ""
59
  for message in client.chat_completion(
60
  messages,
@@ -65,7 +90,13 @@ def respond(
65
  ):
66
  token = message.choices[0].delta.content
67
  response += token
68
- yield response
 
 
 
 
 
 
69
 
70
  demo = gr.ChatInterface(
71
  respond,
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
+ from googletrans import Translator
4
+ from langdetect import detect
5
 
6
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
7
+ translator = Translator()
8
+
9
+ def detect_and_translate(text: str, target_lang='en') -> tuple[str, str]:
10
+ """
11
+ Detect language and translate to target language if needed.
12
+ Returns tuple of (translated_text, detected_language)
13
+ """
14
+ try:
15
+ detected_lang = detect(text)
16
+ if detected_lang != target_lang:
17
+ translation = translator.translate(text, dest=target_lang)
18
+ return translation.text, detected_lang
19
+ return text, detected_lang
20
+ except:
21
+ return text, 'en' # Fallback to original text if translation fails
22
+
23
+ def translate_to_original(text: str, original_lang: str) -> str:
24
+ """Translate response back to original language if needed"""
25
+ if original_lang != 'en':
26
+ try:
27
+ translation = translator.translate(text, dest=original_lang)
28
+ return translation.text
29
+ except:
30
+ return text
31
+ return text
32
 
33
  def check_custom_responses(message: str) -> str:
34
  """Check for specific patterns and return custom responses."""
 
35
  message_lower = message.lower()
 
 
36
  custom_responses = {
37
  "what is ur name?": "xylaria",
38
  "what is your name?": "xylaria",
 
43
  "how many r is in strawberry": "3",
44
  "who is ur dev": "sk md saad amin",
45
  "who is ur developer": "sk md saad amin",
 
 
46
  }
47
 
 
48
  for pattern, response in custom_responses.items():
49
  if pattern in message_lower:
50
  return response
 
51
  return None
52
 
53
  def respond(
 
64
  yield custom_response
65
  return
66
 
67
+ # Detect language and translate to English if needed
68
+ translated_msg, detected_lang = detect_and_translate(message)
69
 
70
+ # Prepare conversation history
71
+ messages = [{"role": "system", "content": system_message}]
72
  for val in history:
73
  if val[0]:
74
+ # Translate user message from history if needed
75
+ trans_user_msg, _ = detect_and_translate(val[0])
76
+ messages.append({"role": "user", "content": trans_user_msg})
77
  if val[1]:
78
  messages.append({"role": "assistant", "content": val[1]})
79
 
80
+ messages.append({"role": "user", "content": translated_msg})
81
 
82
+ # Get response from model
83
  response = ""
84
  for message in client.chat_completion(
85
  messages,
 
90
  ):
91
  token = message.choices[0].delta.content
92
  response += token
93
+
94
+ # Translate accumulated response if original message wasn't in English
95
+ if detected_lang != 'en':
96
+ translated_response = translate_to_original(response, detected_lang)
97
+ yield translated_response
98
+ else:
99
+ yield response
100
 
101
  demo = gr.ChatInterface(
102
  respond,