Spaces:
Running
Running
sachin
commited on
Commit
·
34414a3
1
Parent(s):
0c3deeb
fix-translate
Browse files
app.py
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import json
|
4 |
+
|
5 |
+
# List of supported languages
|
6 |
+
LANGUAGES = [
|
7 |
+
"Assamese (asm_Beng)", "Kashmiri (Arabic) (kas_Arab)", "Punjabi (pan_Guru)",
|
8 |
+
"Bengali (ben_Beng)", "Kashmiri (Devanagari) (kas_Deva)", "Sanskrit (san_Deva)",
|
9 |
+
"Bodo (brx_Deva)", "Maithili (mai_Deva)", "Santali (sat_Olck)",
|
10 |
+
"Dogri (doi_Deva)", "Malayalam (mal_Mlym)", "Sindhi (Arabic) (snd_Arab)",
|
11 |
+
"English (eng_Latn)", "Marathi (mar_Deva)", "Sindhi (Devanagari) (snd_Deva)",
|
12 |
+
"Konkani (gom_Deva)", "Manipuri (Bengali) (mni_Beng)", "Tamil (tam_Taml)",
|
13 |
+
"Gujarati (guj_Gujr)", "Manipuri (Meitei) (mni_Mtei)", "Telugu (tel_Telu)",
|
14 |
+
"Hindi (hin_Deva)", "Nepali (npi_Deva)", "Urdu (urd_Arab)",
|
15 |
+
"Kannada (kan_Knda)", "Odia (ory_Orya)"
|
16 |
+
]
|
17 |
+
|
18 |
+
# Function to extract language code from selection
|
19 |
+
def get_lang_code(lang_string):
|
20 |
+
return lang_string.split("(")[-1].rstrip(")")
|
21 |
+
|
22 |
+
def translate_api(sentences, src_lang, tgt_lang):
|
23 |
+
url = "https://slabstech-dhwani-server-workshop.hf.space/v1/translate"
|
24 |
+
|
25 |
+
headers = {
|
26 |
+
"accept": "application/json",
|
27 |
+
"Content-Type": "application/json"
|
28 |
+
}
|
29 |
+
|
30 |
+
# Convert sentences string to list if it's a string
|
31 |
+
if isinstance(sentences, str):
|
32 |
+
try:
|
33 |
+
sentences_list = json.loads(sentences)
|
34 |
+
except json.JSONDecodeError:
|
35 |
+
sentences_list = [sentences]
|
36 |
+
else:
|
37 |
+
sentences_list = sentences
|
38 |
+
|
39 |
+
payload = {
|
40 |
+
"sentences": sentences_list,
|
41 |
+
"src_lang": get_lang_code(src_lang),
|
42 |
+
"tgt_lang": get_lang_code(tgt_lang)
|
43 |
+
}
|
44 |
+
|
45 |
+
try:
|
46 |
+
response = requests.post(url, headers=headers, json=payload)
|
47 |
+
response.raise_for_status()
|
48 |
+
return response.json()
|
49 |
+
except requests.exceptions.HTTPError as e:
|
50 |
+
return {"error": f"HTTP Error: {str(e)}"}
|
51 |
+
except requests.exceptions.RequestException as e:
|
52 |
+
return {"error": f"Request Error: {str(e)}"}
|
53 |
+
|
54 |
+
# Create Gradio interface
|
55 |
+
with gr.Blocks(title="Translation API Interface") as demo:
|
56 |
+
gr.Markdown("# Translation API Interface")
|
57 |
+
gr.Markdown("Enter sentences and select languages to translate.")
|
58 |
+
|
59 |
+
with gr.Row():
|
60 |
+
with gr.Column():
|
61 |
+
# Input components
|
62 |
+
sentences_input = gr.Textbox(
|
63 |
+
label="Sentences",
|
64 |
+
placeholder='Enter sentences as JSON array or single sentence (e.g., ["Hello", "Good morning"] or "Hello")',
|
65 |
+
lines=3,
|
66 |
+
value='["Hi"]'
|
67 |
+
)
|
68 |
+
src_lang_input = gr.Dropdown(
|
69 |
+
label="Source Language",
|
70 |
+
choices=LANGUAGES,
|
71 |
+
value="English (eng_Latn)"
|
72 |
+
)
|
73 |
+
tgt_lang_input = gr.Dropdown(
|
74 |
+
label="Target Language",
|
75 |
+
choices=LANGUAGES,
|
76 |
+
value="Kannada (kan_Knda)"
|
77 |
+
)
|
78 |
+
|
79 |
+
submit_btn = gr.Button("Translate")
|
80 |
+
|
81 |
+
with gr.Column():
|
82 |
+
# Output component
|
83 |
+
output = gr.JSON(label="Translation Response")
|
84 |
+
|
85 |
+
# Connect the button click to the API function
|
86 |
+
submit_btn.click(
|
87 |
+
fn=translate_api,
|
88 |
+
inputs=[sentences_input, src_lang_input, tgt_lang_input],
|
89 |
+
outputs=output
|
90 |
+
)
|
91 |
+
|
92 |
+
# Launch the interface
|
93 |
+
if __name__ == "__main__":
|
94 |
+
demo.launch()
|