khurrambasharat commited on
Commit
4a9c2ef
Β·
verified Β·
1 Parent(s): 2b64045

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -42
app.py CHANGED
@@ -1,80 +1,73 @@
1
  import os
2
  os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "0"
3
 
4
- from transformers import (
5
- MBart50TokenizerFast,
6
- MBartForConditionalGeneration,
7
- AutoConfig
8
- )
9
  import gradio as gr
10
 
11
- # ---- Load model and tokenizer ----
12
  model_name = "Mudasir692/mbart-eng-ur"
13
 
 
14
  config = AutoConfig.from_pretrained(model_name)
15
-
16
- # Fix missing or invalid parameters
17
- if getattr(config, "early_stopping", None) is None:
18
  config.early_stopping = True
19
- if getattr(config, "max_length", None) is None:
20
- config.max_length = 128 # βœ… set a safe limit
21
- if getattr(config, "num_beams", None) is None:
22
- config.num_beams = 4
23
 
24
- tokenizer = MBart50TokenizerFast.from_pretrained(model_name, src_lang="en_XX", tgt_lang="ur_PK")
25
  model = MBartForConditionalGeneration.from_pretrained(model_name, config=config)
26
 
 
 
 
 
 
 
 
27
  # ---- Translation function ----
28
- def translate_to_urdu(text):
29
  if not text.strip():
30
  return "Please enter some English text."
31
 
 
 
 
 
32
  inputs = tokenizer(text, return_tensors="pt", padding=True)
33
- translated_tokens = model.generate(
34
- **inputs,
35
- max_length=128, # βœ… explicitly set again
36
- num_beams=4,
37
- early_stopping=True
38
- )
39
- urdu_output = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
40
- return urdu_output
41
 
42
- # ---- Gradio Interface ----
43
  examples = [
44
- ["How are you?"],
45
- ["Today is a beautiful day."],
46
- ["Where are you going?"],
47
- ["I am learning Artificial Intelligence."],
48
- ["Thank you very much!"]
49
  ]
50
 
 
51
  app = gr.Interface(
52
- fn=translate_to_urdu,
53
- inputs=gr.Textbox(label="Enter English Text", placeholder="Type your English sentence here...", lines=2),
54
- outputs=gr.Textbox(label="Urdu Translation", lines=2),
 
 
 
55
  examples=examples,
56
- title="πŸ•Œ English β†’ Urdu Translator",
57
  description="""
58
  <div style='text-align:center;'>
59
- <h3>Translate English sentences into Urdu using a fine-tuned mBART model.</h3>
60
  <p style='color:gray;'>Built by <b>Khurram Basharat</b> β€” powered by Transformers & Gradio.</p>
61
- <p><i>⏳ Please wait a few seconds for the model to load when you first open the app.</i></p>
62
  </div>
63
  """,
64
- theme="soft",
65
  css="""
66
  body {
67
- background: linear-gradient(135deg, #d1f2eb, #a3e4d7, #d6eaf8);
68
  font-family: 'Segoe UI', sans-serif;
69
- background-attachment: fixed;
70
  }
71
  .gr-button-primary {
72
  background-color: #1e3799 !important;
73
  color: white !important;
74
- border-radius: 8px !important;
75
- }
76
- h1, h3 {
77
- color: #0a3d62 !important;
78
  }
79
  """,
80
  )
 
1
  import os
2
  os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "0"
3
 
4
+ from transformers import MBart50TokenizerFast, MBartForConditionalGeneration, AutoConfig
 
 
 
 
5
  import gradio as gr
6
 
7
+ # ---- Load model & tokenizer ----
8
  model_name = "Mudasir692/mbart-eng-ur"
9
 
10
+ # Fix config issue
11
  config = AutoConfig.from_pretrained(model_name)
12
+ if config.early_stopping is None:
 
 
13
  config.early_stopping = True
 
 
 
 
14
 
15
+ tokenizer = MBart50TokenizerFast.from_pretrained(model_name)
16
  model = MBartForConditionalGeneration.from_pretrained(model_name, config=config)
17
 
18
+ # ---- Language mapping ----
19
+ LANG_CODES = {
20
+ "Urdu": "ur_PK",
21
+ "Arabic": "ar_AR",
22
+ "Hindi": "hi_IN",
23
+ }
24
+
25
  # ---- Translation function ----
26
+ def translate_text(text, target_lang):
27
  if not text.strip():
28
  return "Please enter some English text."
29
 
30
+ tgt_lang_code = LANG_CODES.get(target_lang, "ur_PK")
31
+ tokenizer.src_lang = "en_XX"
32
+ tokenizer.tgt_lang = tgt_lang_code
33
+
34
  inputs = tokenizer(text, return_tensors="pt", padding=True)
35
+ translated_tokens = model.generate(**inputs)
36
+ output = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
37
+ return output
 
 
 
 
 
38
 
39
+ # ---- Examples ----
40
  examples = [
41
+ ["How are you?", "Urdu"],
42
+ ["Where are you going?", "Arabic"],
43
+ ["This is my new project.", "Hindi"],
 
 
44
  ]
45
 
46
+ # ---- Gradio Interface ----
47
  app = gr.Interface(
48
+ fn=translate_text,
49
+ inputs=[
50
+ gr.Textbox(label="Enter English Text", placeholder="Type your English sentence here...", lines=2),
51
+ gr.Dropdown(["Urdu", "Arabic", "Hindi"], label="Select Target Language", value="Urdu")
52
+ ],
53
+ outputs=gr.Textbox(label="Translation", lines=2),
54
  examples=examples,
55
+ title="🌍 Multi-Language Translator",
56
  description="""
57
  <div style='text-align:center;'>
58
+ <h3>Translate English sentences into Urdu, Arabic, or Hindi using a fine-tuned mBART model.</h3>
59
  <p style='color:gray;'>Built by <b>Khurram Basharat</b> β€” powered by Transformers & Gradio.</p>
60
+ <p><i>⏳ The model loads on first use, please wait a few seconds.</i></p>
61
  </div>
62
  """,
 
63
  css="""
64
  body {
65
+ background: linear-gradient(to bottom right, #f1f2f6, #dff9fb);
66
  font-family: 'Segoe UI', sans-serif;
 
67
  }
68
  .gr-button-primary {
69
  background-color: #1e3799 !important;
70
  color: white !important;
 
 
 
 
71
  }
72
  """,
73
  )