AamerAkhter commited on
Commit
a9c44f0
·
verified ·
1 Parent(s): c3d6235

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -20
app.py CHANGED
@@ -1,50 +1,49 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
- # Global variables to hold models (initially empty)
 
5
  en_to_ur_pipeline = None
6
  ur_to_en_pipeline = None
7
 
8
- def load_en_ur_model():
9
- """Loads the English to Urdu model only when needed."""
10
  global en_to_ur_pipeline
11
  if en_to_ur_pipeline is None:
12
- print("Downloading/Loading English to Urdu model...")
13
  en_to_ur_pipeline = pipeline("translation", model="Helsinki-NLP/opus-mt-en-ur")
14
  return en_to_ur_pipeline
15
 
16
- def load_ur_en_model():
17
- """Loads the Urdu to English model only when needed."""
18
  global ur_to_en_pipeline
19
  if ur_to_en_pipeline is None:
20
- print("Downloading/Loading Urdu to English model...")
21
  ur_to_en_pipeline = pipeline("translation", model="Helsinki-NLP/opus-mt-ur-en")
22
  return ur_to_en_pipeline
23
 
24
  def translate_text(text, direction):
25
- if not text.strip():
26
- return "Please enter some text to translate."
27
 
28
  try:
29
- # Load the specific model requested
30
  if direction == "English to Urdu":
31
- model = load_en_ur_model()
32
  result = model(text)
33
  return result[0]['translation_text']
34
 
35
  elif direction == "Urdu to English":
36
- model = load_ur_en_model()
37
  result = model(text)
38
  return result[0]['translation_text']
39
 
40
  except Exception as e:
41
- return f"Error: {str(e)}"
42
 
43
- # --- Create User Interface ---
44
- # Fixed: Removed 'theme' from Blocks() to fix the warning in your logs
45
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
46
  gr.Markdown("# 🇬🇧 English <-> 🇵🇰 Urdu Translator")
47
- gr.Markdown("Translate text instantly using AI models.")
48
 
49
  with gr.Row():
50
  with gr.Column():
@@ -61,11 +60,11 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
61
  submit_btn = gr.Button("Translate", variant="primary")
62
 
63
  with gr.Column():
 
64
  output_text = gr.Textbox(
65
  label="Translated Output",
66
- lines=5,
67
- interactive=False,
68
- show_copy_button=True
69
  )
70
 
71
  submit_btn.click(
 
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
+ # --- Global Variables for Lazy Loading ---
5
+ # We set them to None initially so the app starts instantly
6
  en_to_ur_pipeline = None
7
  ur_to_en_pipeline = None
8
 
9
+ def load_en_ur():
10
+ """Load English to Urdu model only when requested"""
11
  global en_to_ur_pipeline
12
  if en_to_ur_pipeline is None:
13
+ print("Loading English -> Urdu model...")
14
  en_to_ur_pipeline = pipeline("translation", model="Helsinki-NLP/opus-mt-en-ur")
15
  return en_to_ur_pipeline
16
 
17
+ def load_ur_en():
18
+ """Load Urdu to English model only when requested"""
19
  global ur_to_en_pipeline
20
  if ur_to_en_pipeline is None:
21
+ print("Loading Urdu -> English model...")
22
  ur_to_en_pipeline = pipeline("translation", model="Helsinki-NLP/opus-mt-ur-en")
23
  return ur_to_en_pipeline
24
 
25
  def translate_text(text, direction):
26
+ if not text or not text.strip():
27
+ return "Please enter text to translate."
28
 
29
  try:
30
+ # Load only the model we need right now
31
  if direction == "English to Urdu":
32
+ model = load_en_ur()
33
  result = model(text)
34
  return result[0]['translation_text']
35
 
36
  elif direction == "Urdu to English":
37
+ model = load_ur_en()
38
  result = model(text)
39
  return result[0]['translation_text']
40
 
41
  except Exception as e:
42
+ return f"Error during translation: {str(e)}"
43
 
44
+ # --- Create The App Interface ---
45
+ with gr.Blocks() as demo:
 
46
  gr.Markdown("# 🇬🇧 English <-> 🇵🇰 Urdu Translator")
 
47
 
48
  with gr.Row():
49
  with gr.Column():
 
60
  submit_btn = gr.Button("Translate", variant="primary")
61
 
62
  with gr.Column():
63
+ # Removed show_copy_button to prevent errors
64
  output_text = gr.Textbox(
65
  label="Translated Output",
66
+ lines=5,
67
+ interactive=False
 
68
  )
69
 
70
  submit_btn.click(