Asilbek14 commited on
Commit
d02b539
Β·
verified Β·
1 Parent(s): 2d6f15d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -14
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  from transformers import pipeline
 
4
 
5
  # ---------------- CONFIG ----------------
6
  MODEL_REPO = "HuggingFaceH4/zephyr-7b-beta"
@@ -33,21 +34,25 @@ def is_translation_request(message: str) -> bool:
33
  return non_ascii_ratio > 0.4
34
 
35
 
 
 
 
 
 
 
 
 
 
 
36
  # ---------------- CHAT FUNCTION ----------------
37
  def stream_response(message, chat_history, system_message, max_tokens, temperature, top_p, response_style):
38
  # check if translation
39
  if is_translation_request(message):
40
- try:
41
- translated = translator(message, src_lang="auto", tgt_lang="en")[0]["translation_text"]
42
- chat_history.append({"role": "user", "content": message})
43
- chat_history.append({"role": "assistant", "content": translated})
44
- yield "", chat_history
45
- return
46
- except Exception as e:
47
- chat_history.append({"role": "user", "content": message})
48
- chat_history.append({"role": "assistant", "content": f"⚠️ Translation failed: {str(e)}"})
49
- yield "", chat_history
50
- return
51
 
52
  # Otherwise β†’ normal Zephyr response
53
  if response_style == "Concise":
@@ -80,18 +85,17 @@ def stream_response(message, chat_history, system_message, max_tokens, temperatu
80
  # finally clear input box once after streaming is done
81
  yield "", chat_history
82
 
 
83
  # ---------------- UI ----------------
84
  with gr.Blocks(theme=gr.themes.Soft(primary_hue="violet", secondary_hue="pink")) as demo:
85
  gr.Markdown(
86
  """
87
  # πŸ€– Zephyr-7B Chat + 🌍 Translator
88
  Welcome! This app combines **Zephyr-7B** (for smart chat) with **M2M100** (for multilingual translation).
89
-
90
  **How to use:**
91
  - πŸ’¬ Ask *anything* β†’ Zephyr will reply.
92
  - 🌐 Paste non-English text or say "translate" β†’ auto translation into English.
93
  - βš™οΈ Adjust settings in the panel if you want different styles.
94
-
95
  ---
96
  """
97
  )
@@ -138,4 +142,4 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="violet", secondary_hue="pink"))
138
  gr.Markdown("πŸ”— Built with ❀️ using [Zephyr-7B](https://huggingface.co/HuggingFaceH4/zephyr-7b-beta) & [M2M100](https://huggingface.co/facebook/m2m100_418M).")
139
 
140
  if __name__ == "__main__":
141
- demo.launch()
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  from transformers import pipeline
4
+ from langdetect import detect # πŸ”₯ new for auto language detection
5
 
6
  # ---------------- CONFIG ----------------
7
  MODEL_REPO = "HuggingFaceH4/zephyr-7b-beta"
 
34
  return non_ascii_ratio > 0.4
35
 
36
 
37
+ def translate_text(text: str) -> str:
38
+ """Detects language and translates to English."""
39
+ try:
40
+ src_lang = detect(text) # auto-detect like "ru", "es", "fr"
41
+ result = translator(text, src_lang=src_lang, tgt_lang="en")
42
+ return result[0]["translation_text"]
43
+ except Exception as e:
44
+ return f"⚠️ Translation failed: {str(e)}"
45
+
46
+
47
  # ---------------- CHAT FUNCTION ----------------
48
  def stream_response(message, chat_history, system_message, max_tokens, temperature, top_p, response_style):
49
  # check if translation
50
  if is_translation_request(message):
51
+ translated = translate_text(message)
52
+ chat_history.append({"role": "user", "content": message})
53
+ chat_history.append({"role": "assistant", "content": translated})
54
+ yield "", chat_history
55
+ return
 
 
 
 
 
 
56
 
57
  # Otherwise β†’ normal Zephyr response
58
  if response_style == "Concise":
 
85
  # finally clear input box once after streaming is done
86
  yield "", chat_history
87
 
88
+
89
  # ---------------- UI ----------------
90
  with gr.Blocks(theme=gr.themes.Soft(primary_hue="violet", secondary_hue="pink")) as demo:
91
  gr.Markdown(
92
  """
93
  # πŸ€– Zephyr-7B Chat + 🌍 Translator
94
  Welcome! This app combines **Zephyr-7B** (for smart chat) with **M2M100** (for multilingual translation).
 
95
  **How to use:**
96
  - πŸ’¬ Ask *anything* β†’ Zephyr will reply.
97
  - 🌐 Paste non-English text or say "translate" β†’ auto translation into English.
98
  - βš™οΈ Adjust settings in the panel if you want different styles.
 
99
  ---
100
  """
101
  )
 
142
  gr.Markdown("πŸ”— Built with ❀️ using [Zephyr-7B](https://huggingface.co/HuggingFaceH4/zephyr-7b-beta) & [M2M100](https://huggingface.co/facebook/m2m100_418M).")
143
 
144
  if __name__ == "__main__":
145
+ demo.launch()