LouisMonawe commited on
Commit
1cd99d0
·
1 Parent(s): bb88812

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -10
app.py CHANGED
@@ -2,7 +2,7 @@
2
  import requests
3
  import gradio as gr
4
 
5
- # Supported languages for the Space API
6
  LANGUAGES = {
7
  "English → Afrikaans": ("en", "af"),
8
  "English → Xhosa": ("en", "xh"),
@@ -19,21 +19,25 @@ API_URL = "https://sepioo-facebook-translation.hf.space/translate"
19
 
20
 
21
  def translate(input_text, language_label):
22
- source_lang, target_lang = LANGUAGES[language_label]
23
 
24
- payload = {"source": source_lang, "target": target_lang, "text": input_text}
 
 
 
 
25
 
26
  response = requests.post(API_URL, json=payload)
27
 
28
  if response.status_code != 200:
29
- return f"Error: {response.status_code} - {response.text}"
30
 
31
  try:
32
  data = response.json()
33
- if "translation" in data:
34
- return data["translation"]
35
- elif "error" in data:
36
- return f"API Error: {data['error']}"
37
  else:
38
  return "Unexpected response format."
39
  except Exception as e:
@@ -48,8 +52,8 @@ translator = gr.Interface(
48
  gr.Dropdown(list(LANGUAGES.keys()), label="Select Language Target"),
49
  ],
50
  outputs=gr.Textbox(label="Translation"),
51
- title="Translademia (South African Languages Edition)",
52
- description="Translate English into supported South African languages using a Hugging Face Space backend.",
53
  )
54
 
55
  translator.launch()
 
2
  import requests
3
  import gradio as gr
4
 
5
+ # Language dropdown: Display label (from_code, to_code)
6
  LANGUAGES = {
7
  "English → Afrikaans": ("en", "af"),
8
  "English → Xhosa": ("en", "xh"),
 
19
 
20
 
21
  def translate(input_text, language_label):
22
+ from_lang, to_lang = LANGUAGES[language_label]
23
 
24
+ payload = {
25
+ "from_language": from_lang,
26
+ "to_language": to_lang,
27
+ "input_text": input_text,
28
+ }
29
 
30
  response = requests.post(API_URL, json=payload)
31
 
32
  if response.status_code != 200:
33
+ return f"Error {response.status_code}: {response.text}"
34
 
35
  try:
36
  data = response.json()
37
+ if "translate" in data:
38
+ return data["translate"]
39
+ elif "detail" in data:
40
+ return f"Validation error: {data['detail']}"
41
  else:
42
  return "Unexpected response format."
43
  except Exception as e:
 
52
  gr.Dropdown(list(LANGUAGES.keys()), label="Select Language Target"),
53
  ],
54
  outputs=gr.Textbox(label="Translation"),
55
+ title="Translademia (Powered by Hugging Face Space)",
56
+ description="Translate English to South African languages using sepioo's public Space.",
57
  )
58
 
59
  translator.launch()