khurrambasharat commited on
Commit
928de04
Β·
verified Β·
1 Parent(s): ce1467d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -26
app.py CHANGED
@@ -1,32 +1,73 @@
 
 
1
 
2
- # ---- English β†’ Urdu Translator App ----
3
  from transformers import MBart50TokenizerFast, MBartForConditionalGeneration
4
  import gradio as gr
5
 
6
- # Load model and tokenizer
7
  model_name = "abdulwaheed1/english-to-urdu-translation-mbart"
8
- tokenizer = MBart50TokenizerFast.from_pretrained(model_name, src_lang="en_XX", tgt_lang="ur_PK")
9
- model = MBartForConditionalGeneration.from_pretrained(model_name)
10
-
11
- # Translation function
12
- def translate_to_urdu(text):
13
- if not text.strip():
14
- return "Please enter some English text."
15
- inputs = tokenizer(text, return_tensors="pt", padding=True)
16
- translated_tokens = model.generate(**inputs)
17
- urdu_output = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
18
- return urdu_output
19
-
20
- # Create a simple dashboard with Gradio
21
- app = gr.Interface(
22
- fn=translate_to_urdu,
23
- inputs=gr.Textbox(label="Enter English Text", placeholder="Type your English sentence here..."),
24
- outputs=gr.Textbox(label="Urdu Translation"),
25
- title="πŸ•Œ English β†’ Urdu Translator",
26
- description="This is my First fine-tuned mBART model to translate English sentences into Urdu.",
27
- theme="soft"
28
- )
29
-
30
- # Launch app
31
- app.launch(share=True) # use share=True to get a public link accessible in Chrome
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "0"
3
 
 
4
  from transformers import MBart50TokenizerFast, MBartForConditionalGeneration
5
  import gradio as gr
6
 
7
+ # ---- Load model and tokenizer ----
8
  model_name = "abdulwaheed1/english-to-urdu-translation-mbart"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
+ with gr.Blocks(title="English β†’ Urdu Translator") as app:
11
+ gr.Markdown(
12
+ """
13
+ <div style="text-align:center; padding: 10px;">
14
+ <h1 style="color:#1e3799;">πŸ•Œ English β†’ Urdu Translator</h1>
15
+ <p style="font-size:18px;">Translate English sentences into beautiful Urdu text using a fine-tuned mBART model.</p>
16
+ </div>
17
+ """,
18
+ )
19
+
20
+ with gr.Row():
21
+ with gr.Column(scale=1):
22
+ gr.Markdown("⏳ <i>Loading model... please wait 10–20 seconds on first launch.</i>")
23
+
24
+ # Load the model only once (outside function)
25
+ tokenizer = MBart50TokenizerFast.from_pretrained(model_name, src_lang="en_XX", tgt_lang="ur_PK")
26
+ model = MBartForConditionalGeneration.from_pretrained(model_name)
27
+
28
+ # ---- Translation function ----
29
+ def translate_to_urdu(text):
30
+ if not text.strip():
31
+ return "Please enter some English text."
32
+ inputs = tokenizer(text, return_tensors="pt", padding=True)
33
+ translated_tokens = model.generate(**inputs)
34
+ urdu_output = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
35
+ return urdu_output
36
+
37
+ # ---- Gradio Interface ----
38
+ input_box = gr.Textbox(
39
+ label="Enter English Text",
40
+ placeholder="Type your English sentence here...",
41
+ lines=2,
42
+ )
43
+ output_box = gr.Textbox(
44
+ label="Urdu Translation",
45
+ lines=2,
46
+ )
47
+
48
+ example_texts = [
49
+ ["How are you?"],
50
+ ["Today is a beautiful day."],
51
+ ["Where are you going?"],
52
+ ["I am learning Artificial Intelligence."],
53
+ ["Thank you very much!"]
54
+ ]
55
+
56
+ gr.Interface(
57
+ fn=translate_to_urdu,
58
+ inputs=input_box,
59
+ outputs=output_box,
60
+ examples=example_texts,
61
+ title="πŸ•Œ English β†’ Urdu Translator",
62
+ description="Built by Khurram Basharat β€” powered by mBART model fine-tuned for English to Urdu translation.",
63
+ theme="soft",
64
+ css="""
65
+ body {
66
+ background: linear-gradient(to bottom right, #dff9fb, #c7ecee);
67
+ font-family: 'Segoe UI', sans-serif;
68
+ }
69
+ .gr-button-primary {
70
+ background-color: #1e3799 !important;
71
+ }
72
+ """,
73
+ ).launch()