Spaces:
Runtime error
Runtime error
Download app.py from louie8841/python-translator: direct link, hf CLI and curl.
- Browser
- Download file 1.63 kB
-
https://huggingface.co/spaces/louie8841/python-translator/resolve/main/app.py
- Command line
-
hf download hf://spaces/louie8841/python-translator/app.py
-
curl -L -o app.py https://huggingface.co/spaces/louie8841/python-translator/resolve/main/app.py
1.63 kB
| import gradio as gr | |
| import urllib.parse | |
| import urllib.request | |
| import json | |
| # ์ธ์ด ์ฝ๋ ๋ชฉ๋ก | |
| languages = { | |
| "Auto (์๋ ๊ฐ์ง)": "auto", | |
| "English": "en", | |
| "Korean": "ko", | |
| "Japanese": "ja", | |
| "Chinese (Simplified)": "zh-CN", | |
| "Spanish": "es", | |
| "French": "fr", | |
| "German": "de", | |
| "Russian": "ru", | |
| "Italian": "it", | |
| "Portuguese": "pt", | |
| "Vietnamese": "vi", | |
| "Thai": "th", | |
| "Arabic": "ar", | |
| "Indonesian": "id", | |
| } | |
| # ๋ฒ์ญ ํจ์ | |
| def translate_text(text, src_lang, target_lang): | |
| if not text.strip(): | |
| return "โ ๏ธ ๋ฒ์ญํ ๋ด์ฉ์ ์ ๋ ฅํ์ธ์." | |
| try: | |
| query = urllib.parse.quote(text) | |
| src_code = languages[src_lang] | |
| tgt_code = languages[target_lang] | |
| url = f"https://translate.googleapis.com/translate_a/single?client=gtx&sl={src_code}&tl={tgt_code}&dt=t&q={query}" | |
| with urllib.request.urlopen(url) as response: | |
| data = json.loads(response.read().decode("utf-8")) | |
| translated_text = data[0][0][0] | |
| return translated_text | |
| except Exception as e: | |
| return f"โ ์ค๋ฅ: {e}" | |
| # Gradio UI | |
| demo = gr.Interface( | |
| fn=translate_text, | |
| inputs=[ | |
| gr.Textbox(label="๋ฒ์ญํ ๋ฌธ์ฅ ์ ๋ ฅ", placeholder="์: Hello World"), | |
| gr.Dropdown(list(languages.keys()), value="Auto (์๋ ๊ฐ์ง)", label="์๋ณธ ์ธ์ด ์ ํ"), | |
| gr.Dropdown(list(languages.keys()), value="Korean", label="๋ฒ์ญํ ์ธ์ด ์ ํ"), | |
| ], | |
| outputs=gr.Textbox(label="๋ฒ์ญ ๊ฒฐ๊ณผ"), | |
| title="Gradio ๋ฒ์ญ๊ธฐ", | |
| flagging_mode="never" # Flag ๋ฒํผ ์ ๊ฑฐ | |
| ) | |
| demo.launch() | |