Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import requests | |
| import os | |
| # Получаем токен из переменной окружения | |
| HF_TOKEN = os.getenv("HF_TOKEN") | |
| # Функция для запроса к Hugging Face Inference API | |
| def translate(text, target_language): | |
| headers = { | |
| "Authorization": f"Bearer {HF_TOKEN}" | |
| } | |
| data = { | |
| "inputs": text, | |
| "parameters": {"target_language": target_language}, | |
| } | |
| response = requests.post("https://api-inference.huggingface.co/models/facebook/mbart-large-50-many-to-many-mmt", headers=headers, json=data) | |
| return response | |
| # Список доступных языков для перевода | |
| languages = [ | |
| "ar_AR", "cs_CZ", "de_DE", "en_XX", "es_XX", "et_EE", "fi_FI", | |
| "fr_XX", "gu_IN", "hi_IN", "it_IT", "ja_XX", "ko_KR", "mr_IN", | |
| "nl_XX", "ro_RO", "ru_RU", "sv_SE", "tr_TR", "zh_CN" | |
| ] | |
| with gr.Blocks() as demo: | |
| with gr.Row(): | |
| text_input = gr.Textbox(lines=3, placeholder="Введите текст для перевода") | |
| language_dropdown = gr.Dropdown(choices=languages, value="en_XX", label="Выберите язык перевода") | |
| with gr.Row(): | |
| translate_button = gr.Button("Перевести") | |
| with gr.Row(): | |
| output = gr.Textbox(label="Перевод") | |
| translate_button.click(fn=translate, inputs=[text_input, language_dropdown], outputs=output) | |
| demo.launch() |