Asilbek14 commited on
Commit
b4f77ad
·
verified ·
1 Parent(s): 08ea239

Integrated a translation model

Browse files
Files changed (1) hide show
  1. app.py +42 -28
app.py CHANGED
@@ -1,8 +1,11 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
 
3
 
4
  # ---------------- CONFIG ----------------
5
  MODEL_REPO = "HuggingFaceH4/zephyr-7b-beta"
 
 
6
  SYSTEM_PROMPT_DEFAULT = (
7
  "You are Zephyr, a concise and polite AI assistant. "
8
  "Answer briefly unless the user specifically asks for detail."
@@ -12,12 +15,40 @@ MAX_NEW_TOKENS_DEFAULT = 128
12
  TEMP_DEFAULT = 0.7
13
  TOP_P_DEFAULT = 0.95
14
 
15
- # Create client (calls Hugging Face Inference API, not local model)
16
  client = InferenceClient(MODEL_REPO)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
  # ---------------- CHAT FUNCTION ----------------
19
  def stream_response(message, chat_history, system_message, max_tokens, temperature, top_p, response_style):
20
- # adjust style
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  if response_style == "Concise":
22
  system_message += " Keep answers short and direct."
23
  elif response_style == "Detailed":
@@ -25,7 +56,6 @@ def stream_response(message, chat_history, system_message, max_tokens, temperatu
25
  elif response_style == "Essay":
26
  system_message += " Write long, structured, essay-style responses."
27
 
28
- # build conversation
29
  messages = [{"role": "system", "content": system_message}] + chat_history
30
  messages.append({"role": "user", "content": message})
31
 
@@ -39,7 +69,6 @@ def stream_response(message, chat_history, system_message, max_tokens, temperatu
39
  ):
40
  token = msg.choices[0].delta.content or ""
41
  response += token
42
- # yield new history in messages format
43
  yield "", chat_history + [
44
  {"role": "user", "content": message},
45
  {"role": "assistant", "content": response}
@@ -50,44 +79,29 @@ def stream_response(message, chat_history, system_message, max_tokens, temperatu
50
  with gr.Blocks(theme=gr.themes.Soft(primary_hue="violet", secondary_hue="pink")) as demo:
51
  gr.Markdown(
52
  """
53
- # 📱 Zephyr-7B (Hosted on Hugging Face Inference API)
54
- Optimized for **mobile-friendly chat**
55
- <span style="opacity:0.7">Powered by HuggingFaceH4/zephyr-7b-beta</span>
56
  """
57
  )
58
 
59
- chatbot = gr.Chatbot(
60
- type="messages", # ✅ use messages format
61
- height=500,
62
- show_copy_button=True,
63
- label="Chat"
64
- )
65
 
66
  with gr.Row():
67
- msg = gr.Textbox(
68
- label="💬 Message",
69
- placeholder="Type your message…",
70
- scale=6
71
- )
72
  send_btn = gr.Button("🚀", variant="primary", scale=1)
73
  clear_btn = gr.Button("🧹", scale=1)
74
 
75
  with gr.Accordion("⚙️ Settings", open=False):
76
- system_prompt = gr.Textbox(
77
- label="System Prompt",
78
- value=SYSTEM_PROMPT_DEFAULT,
79
- lines=3
80
- )
81
  response_style = gr.Dropdown(
82
- ["Concise", "Detailed", "Essay"],
83
- value="Concise",
84
- label="Response Style"
85
  )
86
  temperature = gr.Slider(0.1, 1.5, value=TEMP_DEFAULT, step=0.1, label="Temperature")
87
  top_p = gr.Slider(0.1, 1.0, value=TOP_P_DEFAULT, step=0.05, label="Top-p")
88
  max_tokens = gr.Slider(32, 2048, value=MAX_NEW_TOKENS_DEFAULT, step=16, label="Max new tokens")
89
 
90
- # Events (streaming response)
91
  send_btn.click(
92
  stream_response,
93
  [msg, chatbot, system_prompt, max_tokens, temperature, top_p, response_style],
@@ -101,4 +115,4 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="violet", secondary_hue="pink"))
101
  clear_btn.click(lambda: [], None, chatbot, queue=False)
102
 
103
  if __name__ == "__main__":
104
- demo.launch()
 
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"
7
+ TRANSLATOR_MODEL = "facebook/m2m100_418M" # multilingual translator
8
+
9
  SYSTEM_PROMPT_DEFAULT = (
10
  "You are Zephyr, a concise and polite AI assistant. "
11
  "Answer briefly unless the user specifically asks for detail."
 
15
  TEMP_DEFAULT = 0.7
16
  TOP_P_DEFAULT = 0.95
17
 
18
+ # Clients
19
  client = InferenceClient(MODEL_REPO)
20
+ translator = pipeline("translation", model=TRANSLATOR_MODEL)
21
+
22
+ # ---------------- HELPERS ----------------
23
+ def is_translation_request(message: str) -> bool:
24
+ """
25
+ Heuristics: if user explicitly asks to translate OR if message is not English.
26
+ """
27
+ triggers = ["translate", "traduce", "ترجم", "traduire", "übersetze"]
28
+ if any(t in message.lower() for t in triggers):
29
+ return True
30
+ # naive non-English detection (if >40% chars non-ASCII)
31
+ non_ascii_ratio = sum(1 for c in message if ord(c) > 127) / max(len(message), 1)
32
+ return non_ascii_ratio > 0.4
33
+
34
 
35
  # ---------------- CHAT FUNCTION ----------------
36
  def stream_response(message, chat_history, system_message, max_tokens, temperature, top_p, response_style):
37
+ # check if translation
38
+ if is_translation_request(message):
39
+ try:
40
+ translated = translator(message, src_lang="auto", tgt_lang="en")[0]["translation_text"]
41
+ return "", chat_history + [
42
+ {"role": "user", "content": message},
43
+ {"role": "assistant", "content": translated}
44
+ ]
45
+ except Exception as e:
46
+ return "", chat_history + [
47
+ {"role": "user", "content": message},
48
+ {"role": "assistant", "content": f"⚠️ Translation failed: {str(e)}"}
49
+ ]
50
+
51
+ # Otherwise → normal Zephyr response
52
  if response_style == "Concise":
53
  system_message += " Keep answers short and direct."
54
  elif response_style == "Detailed":
 
56
  elif response_style == "Essay":
57
  system_message += " Write long, structured, essay-style responses."
58
 
 
59
  messages = [{"role": "system", "content": system_message}] + chat_history
60
  messages.append({"role": "user", "content": message})
61
 
 
69
  ):
70
  token = msg.choices[0].delta.content or ""
71
  response += token
 
72
  yield "", chat_history + [
73
  {"role": "user", "content": message},
74
  {"role": "assistant", "content": response}
 
79
  with gr.Blocks(theme=gr.themes.Soft(primary_hue="violet", secondary_hue="pink")) as demo:
80
  gr.Markdown(
81
  """
82
+ # 🌍 Zephyr-7B with Multilingual Support
83
+ - 💬 Normal chat powered by **Zephyr-7B**
84
+ - 🌐 Translation powered by **M2M100** (auto-detects non-English or "translate" requests)
85
  """
86
  )
87
 
88
+ chatbot = gr.Chatbot(type="messages", height=500, show_copy_button=True, label="Chat")
 
 
 
 
 
89
 
90
  with gr.Row():
91
+ msg = gr.Textbox(label="💬 Message", placeholder="Type your message…", scale=6)
 
 
 
 
92
  send_btn = gr.Button("🚀", variant="primary", scale=1)
93
  clear_btn = gr.Button("🧹", scale=1)
94
 
95
  with gr.Accordion("⚙️ Settings", open=False):
96
+ system_prompt = gr.Textbox(label="System Prompt", value=SYSTEM_PROMPT_DEFAULT, lines=3)
 
 
 
 
97
  response_style = gr.Dropdown(
98
+ ["Concise", "Detailed", "Essay"], value="Concise", label="Response Style"
 
 
99
  )
100
  temperature = gr.Slider(0.1, 1.5, value=TEMP_DEFAULT, step=0.1, label="Temperature")
101
  top_p = gr.Slider(0.1, 1.0, value=TOP_P_DEFAULT, step=0.05, label="Top-p")
102
  max_tokens = gr.Slider(32, 2048, value=MAX_NEW_TOKENS_DEFAULT, step=16, label="Max new tokens")
103
 
104
+ # Events
105
  send_btn.click(
106
  stream_response,
107
  [msg, chatbot, system_prompt, max_tokens, temperature, top_p, response_style],
 
115
  clear_btn.click(lambda: [], None, chatbot, queue=False)
116
 
117
  if __name__ == "__main__":
118
+ demo.launch()