Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,46 +1,34 @@
|
|
1 |
-
# β
Step 1: Install required libraries
|
2 |
-
!pip install transformers gradio --quiet
|
3 |
-
|
4 |
-
# β
Step 2: Import required modules
|
5 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
6 |
import gradio as gr
|
7 |
|
8 |
-
#
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
12 |
|
13 |
-
|
14 |
-
|
|
|
15 |
|
16 |
-
# β
Step 5: Translation function
|
17 |
def translate_text(text):
|
18 |
-
# Detect Urdu using Unicode range
|
19 |
contains_urdu = any('\u0600' <= ch <= '\u06FF' for ch in text)
|
20 |
-
|
21 |
if contains_urdu:
|
22 |
-
|
23 |
-
ur_to_en_model = "Helsinki-NLP/opus-mt-ur-en"
|
24 |
-
tokenizer_ur_en = AutoTokenizer.from_pretrained(ur_to_en_model)
|
25 |
-
model_ur_en = AutoModelForSeq2SeqLM.from_pretrained(ur_to_en_model)
|
26 |
-
ur_to_en_translator = pipeline("translation", model=model_ur_en, tokenizer=tokenizer_ur_en)
|
27 |
-
result = ur_to_en_translator(text, max_length=100)
|
28 |
direction = "Urdu β English"
|
29 |
else:
|
30 |
-
|
31 |
-
result = translator(text, max_length=100)
|
32 |
direction = "English β Urdu"
|
33 |
-
|
34 |
return f"π Translation ({direction}):\n\n" + result[0]['translation_text']
|
35 |
|
36 |
-
# β
Step 6: Gradio UI
|
37 |
with gr.Blocks() as demo:
|
38 |
-
gr.Markdown("## π English β Urdu Translator
|
39 |
-
input_text = gr.Textbox(label="Enter
|
40 |
-
output_text = gr.Textbox(label="
|
41 |
-
|
|
|
42 |
|
43 |
-
|
44 |
|
45 |
-
# β
Step 7: Launch in Colab
|
46 |
-
demo.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
1 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
2 |
import gradio as gr
|
3 |
|
4 |
+
# Load models
|
5 |
+
en_ur_model_name = "Helsinki-NLP/opus-mt-en-ur"
|
6 |
+
ur_en_model_name = "Helsinki-NLP/opus-mt-ur-en"
|
7 |
+
|
8 |
+
en_ur_tokenizer = AutoTokenizer.from_pretrained(en_ur_model_name)
|
9 |
+
en_ur_model = AutoModelForSeq2SeqLM.from_pretrained(en_ur_model_name)
|
10 |
+
translator_en_ur = pipeline("translation", model=en_ur_model, tokenizer=en_ur_tokenizer)
|
11 |
|
12 |
+
ur_en_tokenizer = AutoTokenizer.from_pretrained(ur_en_model_name)
|
13 |
+
ur_en_model = AutoModelForSeq2SeqLM.from_pretrained(ur_en_model_name)
|
14 |
+
translator_ur_en = pipeline("translation", model=ur_en_model, tokenizer=ur_en_tokenizer)
|
15 |
|
|
|
16 |
def translate_text(text):
|
|
|
17 |
contains_urdu = any('\u0600' <= ch <= '\u06FF' for ch in text)
|
|
|
18 |
if contains_urdu:
|
19 |
+
result = translator_ur_en(text, max_length=100)
|
|
|
|
|
|
|
|
|
|
|
20 |
direction = "Urdu β English"
|
21 |
else:
|
22 |
+
result = translator_en_ur(text, max_length=100)
|
|
|
23 |
direction = "English β Urdu"
|
|
|
24 |
return f"π Translation ({direction}):\n\n" + result[0]['translation_text']
|
25 |
|
|
|
26 |
with gr.Blocks() as demo:
|
27 |
+
gr.Markdown("## π English β Urdu Translator using Hugging Face")
|
28 |
+
input_text = gr.Textbox(label="Enter Text (English or Urdu)", placeholder="Type here...")
|
29 |
+
output_text = gr.Textbox(label="Translation Output", interactive=False)
|
30 |
+
translate_button = gr.Button("Translate")
|
31 |
+
translate_button.click(fn=translate_text, inputs=input_text, outputs=output_text)
|
32 |
|
33 |
+
demo.launch()
|
34 |
|
|
|
|