Spaces:
Sleeping
Sleeping
v1.0
Browse files- app.py +78 -0
- languages.py +42 -0
- model.py +36 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from model import run
|
| 3 |
+
from languages import language_names
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
example_text = "Now let's make my mum's favourite. So three mars bars into the pan. Then we add the tuna and just stir for a bit, just let the chocolate and fish infuse. A sprinkle of olive oil and some tomato ketchup. Now smell that. Oh boy this is going to be incredible."
|
| 8 |
+
css = """
|
| 9 |
+
body {
|
| 10 |
+
background: linear-gradient(135deg, #0f2027, #203a43, #2c5364);
|
| 11 |
+
}
|
| 12 |
+
.gradio-container {
|
| 13 |
+
font-family: 'Inter', sans-serif;
|
| 14 |
+
}
|
| 15 |
+
textarea {
|
| 16 |
+
border-radius: 12px !important;
|
| 17 |
+
font-size: 16px !important;
|
| 18 |
+
}
|
| 19 |
+
button {
|
| 20 |
+
border-radius: 999px !important;
|
| 21 |
+
font-weight: 600 !important;
|
| 22 |
+
}
|
| 23 |
+
"""
|
| 24 |
+
|
| 25 |
+
with gr.Blocks(
|
| 26 |
+
theme=gr.themes.Soft(
|
| 27 |
+
primary_hue="indigo",
|
| 28 |
+
secondary_hue="cyan",
|
| 29 |
+
neutral_hue="slate"
|
| 30 |
+
),
|
| 31 |
+
css=css
|
| 32 |
+
) as app:
|
| 33 |
+
|
| 34 |
+
gr.Markdown(
|
| 35 |
+
"""
|
| 36 |
+
# 🌍 Universal Translator
|
| 37 |
+
Translate texts quickly between multiple languages.
|
| 38 |
+
Simple and functional.
|
| 39 |
+
"""
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
with gr.Row():
|
| 43 |
+
with gr.Column(scale=1):
|
| 44 |
+
source_language = gr.Dropdown(
|
| 45 |
+
choices=language_names,
|
| 46 |
+
value="English",
|
| 47 |
+
label="Source Language"
|
| 48 |
+
)
|
| 49 |
+
input_text = gr.Textbox(
|
| 50 |
+
label="Original text",
|
| 51 |
+
placeholder="Please enter the text...",
|
| 52 |
+
lines=8,
|
| 53 |
+
value=example_text
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
with gr.Column(scale=1):
|
| 57 |
+
target_language = gr.Dropdown(
|
| 58 |
+
choices=language_names,
|
| 59 |
+
value="Portuguese",
|
| 60 |
+
label="Target Language"
|
| 61 |
+
)
|
| 62 |
+
output_text = gr.Textbox(
|
| 63 |
+
label="Translation",
|
| 64 |
+
lines=8,
|
| 65 |
+
interactive=False
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
translate_btn = gr.Button("Translate ✨")
|
| 69 |
+
|
| 70 |
+
translate_btn.click(
|
| 71 |
+
run,
|
| 72 |
+
inputs=[input_text, target_language],
|
| 73 |
+
outputs=output_text
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
if __name__ == "__main__":
|
| 78 |
+
app.queue(max_size=10).launch(mcp_server=True)
|
languages.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
languages = {
|
| 2 |
+
"Chinese": "zh",
|
| 3 |
+
"English": "en",
|
| 4 |
+
"French": "fr",
|
| 5 |
+
"Portuguese": "pt",
|
| 6 |
+
"Spanish": "es",
|
| 7 |
+
"Japanese": "ja",
|
| 8 |
+
"Turkish": "tr",
|
| 9 |
+
"Russian": "ru",
|
| 10 |
+
"Arabic": "ar",
|
| 11 |
+
"Korean": "ko",
|
| 12 |
+
"Thai": "th",
|
| 13 |
+
"Italian": "it",
|
| 14 |
+
"German": "de",
|
| 15 |
+
"Vietnamese": "vi",
|
| 16 |
+
"Malay": "ms",
|
| 17 |
+
"Indonesian": "id",
|
| 18 |
+
"Filipino": "tl",
|
| 19 |
+
"Hindi": "hi",
|
| 20 |
+
"Traditional Chinese": "zh-Hant",
|
| 21 |
+
"Polish": "pl",
|
| 22 |
+
"Czech": "cs",
|
| 23 |
+
"Dutch": "nl",
|
| 24 |
+
"Khmer": "km",
|
| 25 |
+
"Burmese": "my",
|
| 26 |
+
"Persian": "fa",
|
| 27 |
+
"Gujarati": "gu",
|
| 28 |
+
"Urdu": "ur",
|
| 29 |
+
"Telugu": "te",
|
| 30 |
+
"Marathi": "mr",
|
| 31 |
+
"Hebrew": "he",
|
| 32 |
+
"Bengali": "bn",
|
| 33 |
+
"Tamil": "ta",
|
| 34 |
+
"Ukrainian": "uk",
|
| 35 |
+
"Tibetan": "bo",
|
| 36 |
+
"Kazakh": "kk",
|
| 37 |
+
"Mongolian": "mn",
|
| 38 |
+
"Uyghur": "ug",
|
| 39 |
+
"Cantonese": "yue"
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
language_names = list(languages.keys())
|
model.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from llama_cpp import Llama
|
| 2 |
+
from huggingface_hub import hf_hub_download
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
repo_id = "tencent/HY-MT1.5-1.8B-GGUF"
|
| 7 |
+
filename = "HY-MT1.5-1.8B-Q8_0.gguf"
|
| 8 |
+
local_dir = "./model"
|
| 9 |
+
model_path = hf_hub_download(repo_id=repo_id, filename=filename, local_dir=local_dir)
|
| 10 |
+
|
| 11 |
+
llm = Llama(
|
| 12 |
+
model_path=model_path,
|
| 13 |
+
n_ctx=4096, # Corresponde ao -n 4096
|
| 14 |
+
verbose=False # Reduz o log excessivo no console
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def run(
|
| 20 |
+
text: str = "It’s on the house.",
|
| 21 |
+
target_language: str = "Portuguese",
|
| 22 |
+
):
|
| 23 |
+
prompt = f"Translate the following segment into {target_language}, without additional explanation.\n\n{text}"
|
| 24 |
+
|
| 25 |
+
output = llm(
|
| 26 |
+
prompt,
|
| 27 |
+
max_tokens=4096,
|
| 28 |
+
temperature=0.7, # --temp 0.7
|
| 29 |
+
top_k=20, # --top-k 20
|
| 30 |
+
top_p=0.6, # --top-p 0.6
|
| 31 |
+
repeat_penalty=1.05, # --repeat-penalty 1.05
|
| 32 |
+
stop=["\n"] # Opcional: para parar após a tradução
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
result = output["choices"][0]["text"].strip()
|
| 36 |
+
return result
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
huggingface_hub
|
| 2 |
+
|
| 3 |
+
gradio
|
| 4 |
+
https://github.com/rafael-vasconcellos/llama-cpp-python-wheels/releases/download/v0.0.2/llama_cpp_python-0.3.16-cp310-cp310-linux_x86_64.whl
|