Spaces:
Sleeping
Sleeping
| import os | |
| import json | |
| import requests | |
| import gradio as gr | |
| def translate(source, direction): | |
| if not source or not direction: | |
| return "Please enter valid text and select the mode!" | |
| # WARNING, this token is a test token for new developers, and it should be replaced by your token | |
| payload = { | |
| "source": source, | |
| "trans_type": direction, | |
| "request_id": "demo", | |
| "detect": True, | |
| } | |
| headers = { | |
| "content-type": "application/json", | |
| "x-authorization": f"token {os.getenv('token')}", | |
| } | |
| try: | |
| response = requests.request( | |
| "POST", | |
| "http://api.interpreter.caiyunai.com/v1/translator", | |
| data=json.dumps(payload), | |
| headers=headers, | |
| ) | |
| return json.loads(response.text)["target"] | |
| except Exception as e: | |
| return f"{e}" | |
| if __name__ == "__main__": | |
| gr.Interface( | |
| fn=translate, | |
| inputs=[ | |
| gr.TextArea( | |
| label="Input text area", | |
| placeholder="Type the text here...", | |
| show_copy_button=True, | |
| ), | |
| gr.Dropdown(choices=["auto2en", "auto2zh", "auto2ja"], label="Mode"), | |
| ], | |
| outputs=gr.TextArea(label="Translation results", show_copy_button=True), | |
| flagging_mode="never", | |
| examples=[ | |
| ["彩云小译は最高の翻訳サービスです", "auto2en"], | |
| ["Lingocloud is the best translation service.", "auto2zh"], | |
| ], | |
| cache_examples=False, | |
| ).launch() | |