fereen5 commited on
Commit
f3992c8
Β·
verified Β·
1 Parent(s): ad25f94

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -29
app.py CHANGED
@@ -85,29 +85,32 @@ def translate_file(file, src_lang, tgt_lang, max_length, temperature):
85
  except Exception as e:
86
  return f"File translation error: {e}"
87
 
88
- # App UI
89
- with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
 
 
 
 
 
 
90
  gr.Markdown("""
91
- <div style="text-align: center; font-size: 28px; font-weight: bold; margin-bottom: 20px;">
92
- 🌍 PolyLinguaAI: Translate Across Worlds
93
- </div>
94
- <div style="text-align: center; font-size: 16px; margin-bottom: 40px;">
95
- Translate between 12+ global languages using the powerful NLLB model.
96
- </div>
97
  """)
98
 
99
  with gr.Tab("🌐 Text Translator"):
100
  with gr.Row():
101
- src_lang = gr.Dropdown(list(LANGUAGE_CODES.keys()), label="From", value="English")
102
- swap = gr.Button("⇄", size="sm")
103
- tgt_lang = gr.Dropdown(list(LANGUAGE_CODES.keys()), label="To", value="Korean")
104
 
105
- input_text = gr.Textbox(lines=5, label="Input Text", placeholder="Type or paste text to translate...")
106
- output_text = gr.Textbox(lines=5, label="Translated Output", interactive=False)
 
107
 
108
  with gr.Row():
109
- translate = gr.Button("πŸ” Translate", variant="primary")
110
- clear = gr.Button("🧼 Clear")
111
 
112
  with gr.Accordion("βš™οΈ Advanced Settings", open=False):
113
  max_length = gr.Slider(10, 512, value=128, step=1, label="Max Length")
@@ -117,33 +120,34 @@ with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
117
  history_json = gr.JSON(label="Recent Translations")
118
  with gr.Row():
119
  refresh = gr.Button("πŸ”„ Refresh")
120
- clear_history = gr.Button("🧹 Clear")
121
 
122
  with gr.Tab("πŸ“ File Translator"):
123
- file_input = gr.File(label="Upload .txt File", file_types=[".txt"])
124
- file_src = gr.Dropdown(list(LANGUAGE_CODES.keys()), label="From", value="English")
125
- file_tgt = gr.Dropdown(list(LANGUAGE_CODES.keys()), label="To", value="Korean")
126
- file_result = gr.Textbox(label="Translated File Output")
127
- file_translate = gr.Button("Translate File", variant="primary")
128
 
129
- with gr.Accordion("βš™οΈ File Settings", open=False):
130
  f_max_length = gr.Slider(10, 512, value=128, step=1, label="Max Length")
131
  f_temp = gr.Slider(0.1, 2.0, value=0.7, step=0.1, label="Temperature")
132
 
 
133
  translate.click(cached_translate, [input_text, src_lang, tgt_lang, max_length, temperature], output_text)
134
  clear.click(lambda: ("", ""), None, [input_text, output_text])
135
  swap.click(swap_langs, [src_lang, tgt_lang], [src_lang, tgt_lang])
136
  refresh.click(lambda: history.get(), None, history_json)
137
  clear_history.click(lambda: history.clear() or [], None, history_json)
138
  file_translate.click(lambda file, src, tgt, ml, t: translate_file(file.read(), src, tgt, ml, t),
139
- [file_input, file_src, file_tgt, f_max_length, f_temp],
140
- file_result)
141
 
142
  gr.Markdown(f"""
143
- <hr/>
144
- <div style="font-size: 14px; text-align: center;">
145
- βš™οΈ Model: <code>{model_name}</code> | Device: <code>{device}</code> | Cache: 512 entries
146
- </div>
147
  """)
148
 
149
- demo.launch()
 
 
85
  except Exception as e:
86
  return f"File translation error: {e}"
87
 
88
+ # Custom CSS to improve UI
89
+ gradio_style = """
90
+ .gr-button { border-radius: 12px !important; padding: 10px 20px !important; font-weight: bold; }
91
+ textarea, input[type=text] { border: 2px solid #00ADB5 !important; border-radius: 10px; transition: 0.2s; }
92
+ textarea:focus, input[type=text]:focus { border-color: #FF5722 !important; box-shadow: 0 0 8px #FF5722 !important; }
93
+ """
94
+
95
+ with gr.Blocks(css=gradio_style, theme=gr.themes.Soft()) as demo:
96
  gr.Markdown("""
97
+ # 🌍 PolyLinguaAI: Translate Across Worlds
98
+ Translate instantly between 12+ languages using Facebook's NLLB model.
 
 
 
 
99
  """)
100
 
101
  with gr.Tab("🌐 Text Translator"):
102
  with gr.Row():
103
+ src_lang = gr.Dropdown(list(LANGUAGE_CODES.keys()), label="🌐 From", value="English")
104
+ swap = gr.Button("⇄")
105
+ tgt_lang = gr.Dropdown(list(LANGUAGE_CODES.keys()), label="🎯 To", value="Korean")
106
 
107
+ with gr.Row():
108
+ input_text = gr.Textbox(lines=3, label="✍️ Input Text")
109
+ output_text = gr.Textbox(lines=3, label="πŸ“€ Translated Output", interactive=False)
110
 
111
  with gr.Row():
112
+ translate = gr.Button("πŸš€ Translate", variant="primary")
113
+ clear = gr.Button("🧽 Clear")
114
 
115
  with gr.Accordion("βš™οΈ Advanced Settings", open=False):
116
  max_length = gr.Slider(10, 512, value=128, step=1, label="Max Length")
 
120
  history_json = gr.JSON(label="Recent Translations")
121
  with gr.Row():
122
  refresh = gr.Button("πŸ”„ Refresh")
123
+ clear_history = gr.Button("🧹 Clear History")
124
 
125
  with gr.Tab("πŸ“ File Translator"):
126
+ file_input = gr.File(label="πŸ“‚ Upload .txt File", file_types=[".txt"])
127
+ file_src = gr.Dropdown(list(LANGUAGE_CODES.keys()), label="πŸ“Œ From", value="English")
128
+ file_tgt = gr.Dropdown(list(LANGUAGE_CODES.keys()), label="πŸ“Œ To", value="Korean")
129
+ file_translate = gr.Button("πŸ“„ Translate File", variant="primary")
130
+ file_result = gr.Textbox(label="πŸ“‘ File Output", lines=10, interactive=False)
131
 
132
+ with gr.Accordion("βš™οΈ Advanced Settings", open=False):
133
  f_max_length = gr.Slider(10, 512, value=128, step=1, label="Max Length")
134
  f_temp = gr.Slider(0.1, 2.0, value=0.7, step=0.1, label="Temperature")
135
 
136
+ # Events
137
  translate.click(cached_translate, [input_text, src_lang, tgt_lang, max_length, temperature], output_text)
138
  clear.click(lambda: ("", ""), None, [input_text, output_text])
139
  swap.click(swap_langs, [src_lang, tgt_lang], [src_lang, tgt_lang])
140
  refresh.click(lambda: history.get(), None, history_json)
141
  clear_history.click(lambda: history.clear() or [], None, history_json)
142
  file_translate.click(lambda file, src, tgt, ml, t: translate_file(file.read(), src, tgt, ml, t),
143
+ [file_input, file_src, file_tgt, f_max_length, f_temp], file_result)
 
144
 
145
  gr.Markdown(f"""
146
+ ### πŸ›  Model Info
147
+ - Model: `{model_name}`
148
+ - Device: `{device}`
149
+ - Cached Translations: 512
150
  """)
151
 
152
+ if __name__ == "__main__":
153
+ demo.launch(share=True)