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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -64
app.py CHANGED
@@ -7,67 +7,51 @@ import gradio as gr
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()
 
7
  # ---- Load model and tokenizer ----
8
  model_name = "abdulwaheed1/english-to-urdu-translation-mbart"
9
 
10
+ tokenizer = MBart50TokenizerFast.from_pretrained(model_name, src_lang="en_XX", tgt_lang="ur_PK")
11
+ model = MBartForConditionalGeneration.from_pretrained(model_name)
12
+
13
+ # ---- Translation function ----
14
+ def translate_to_urdu(text):
15
+ if not text.strip():
16
+ return "Please enter some English text."
17
+ inputs = tokenizer(text, return_tensors="pt", padding=True)
18
+ translated_tokens = model.generate(**inputs)
19
+ urdu_output = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
20
+ return urdu_output
21
+
22
+ # ---- Gradio Interface ----
23
+ examples = [
24
+ ["How are you?"],
25
+ ["Today is a beautiful day."],
26
+ ["Where are you going?"],
27
+ ["I am learning Artificial Intelligence."],
28
+ ["Thank you very much!"]
29
+ ]
30
+
31
+ app = gr.Interface(
32
+ fn=translate_to_urdu,
33
+ inputs=gr.Textbox(label="Enter English Text", placeholder="Type your English sentence here...", lines=2),
34
+ outputs=gr.Textbox(label="Urdu Translation", lines=2),
35
+ examples=examples,
36
+ title="πŸ•Œ English β†’ Urdu Translator",
37
+ description="""
38
+ <div style='text-align:center;'>
39
+ <h3>Translate English sentences into Urdu using a fine-tuned mBART model.</h3>
40
+ <p style='color:gray;'>Built by <b>Khurram Basharat</b> β€” powered by Transformers & Gradio.</p>
41
+ <p><i>⏳ Please wait a few seconds for the model to load when you first open the app.</i></p>
42
+ </div>
43
+ """,
44
+ theme="soft",
45
+ css="""
46
+ body {
47
+ background: linear-gradient(to bottom right, #dff9fb, #c7ecee);
48
+ font-family: 'Segoe UI', sans-serif;
49
+ }
50
+ .gr-button-primary {
51
+ background-color: #1e3799 !important;
52
+ color: white !important;
53
+ }
54
+ """,
55
+ )
56
+
57
+ app.launch()