LouisMonawe commited on
Commit
c30b612
·
1 Parent(s): bfe256d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -11
app.py CHANGED
@@ -104,19 +104,19 @@
104
 
105
  # translator.launch()
106
 
 
 
 
107
  import os
108
  from huggingface_hub import InferenceClient
109
  import gradio as gr
110
  from dotenv import load_dotenv
111
 
112
- # Load environment variables
113
  load_dotenv()
114
- HF_TOKEN = os.getenv("HF_TOKEN")
115
 
116
- # Initialize InferenceClient with your token
117
  client = InferenceClient(token=HF_TOKEN)
118
 
119
- # Language to ISO 639-3 codes (used for NLLB-200)
120
  LANGUAGES = {
121
  "English → Afrikaans": "afr",
122
  "English → Xhosa": "xho",
@@ -132,7 +132,10 @@ LANGUAGES = {
132
  MODEL_NAME = "facebook/nllb-200-distilled-600M"
133
 
134
 
135
- def translate(input_text, language_label):
 
 
 
136
  language_code = LANGUAGES[language_label]
137
  formatted_input = f">>{language_code}<< {input_text}"
138
 
@@ -140,8 +143,8 @@ def translate(input_text, language_label):
140
  response = client.text_generation(
141
  prompt=formatted_input,
142
  model=MODEL_NAME,
143
- # max_new_tokens=500, # Adjust based on expected translation length
144
- # wait_for_model=True, # Avoids cold-start issues (Pro users skip queues)
145
  )
146
  return response.strip()
147
  except Exception as e:
@@ -152,16 +155,22 @@ def translate(input_text, language_label):
152
  translator = gr.Interface(
153
  fn=translate,
154
  inputs=[
155
- gr.Textbox(label="Input Text", placeholder="Type text here..."),
156
- gr.Dropdown(list(LANGUAGES.keys()), label="Select Language Target"),
157
  ],
158
  outputs=gr.Textbox(label="Translation"),
159
- title="Translademia",
160
- description="Translate English text to South African languages using Meta's NLLB-200 model.",
 
 
 
 
161
  )
162
 
163
  translator.launch()
164
 
 
 
165
 
166
  # import requests
167
  # import gradio as gr
 
104
 
105
  # translator.launch()
106
 
107
+
108
+ # love
109
+
110
  import os
111
  from huggingface_hub import InferenceClient
112
  import gradio as gr
113
  from dotenv import load_dotenv
114
 
 
115
  load_dotenv()
116
+ HF_TOKEN = os.getenv("HF_TOKEN") # Ensure this is a PRO token
117
 
 
118
  client = InferenceClient(token=HF_TOKEN)
119
 
 
120
  LANGUAGES = {
121
  "English → Afrikaans": "afr",
122
  "English → Xhosa": "xho",
 
132
  MODEL_NAME = "facebook/nllb-200-distilled-600M"
133
 
134
 
135
+ def translate(input_text: str, language_label: str) -> str:
136
+ if not input_text.strip():
137
+ return "Error: Please enter text to translate."
138
+
139
  language_code = LANGUAGES[language_label]
140
  formatted_input = f">>{language_code}<< {input_text}"
141
 
 
143
  response = client.text_generation(
144
  prompt=formatted_input,
145
  model=MODEL_NAME,
146
+ max_new_tokens=500,
147
+ do_sample=False, # Disable randomness for consistent translations
148
  )
149
  return response.strip()
150
  except Exception as e:
 
155
  translator = gr.Interface(
156
  fn=translate,
157
  inputs=[
158
+ gr.Textbox(label="Input Text", placeholder="Type English text here..."),
159
+ gr.Dropdown(list(LANGUAGES.keys()), label="Target Language"),
160
  ],
161
  outputs=gr.Textbox(label="Translation"),
162
+ title="NLLB-200 Translator (South African Languages)",
163
+ description="Translate English to South African languages using Meta's NLLB-200 model.",
164
+ examples=[
165
+ ["Hello, how are you?", "English → Afrikaans"],
166
+ ["What is your name?", "English → Zulu"],
167
+ ],
168
  )
169
 
170
  translator.launch()
171
 
172
+ # hate
173
+
174
 
175
  # import requests
176
  # import gradio as gr