Spaces:
Running
Running
Update app.py
Browse files
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 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 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()
|