Pontonkid commited on
Commit
d868715
·
verified ·
1 Parent(s): bba77ef

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import requests
4
+ import gradio as gr
5
+
6
+
7
+ def translate(source, direction):
8
+ if not source or not direction:
9
+ return "Please enter valid text and select the mode!"
10
+
11
+ # WARNING, this token is a test token for new developers, and it should be replaced by your token
12
+ payload = {
13
+ "source": source,
14
+ "trans_type": direction,
15
+ "request_id": "demo",
16
+ "detect": True,
17
+ }
18
+ headers = {
19
+ "content-type": "application/json",
20
+ "x-authorization": f"token {os.getenv('token')}",
21
+ }
22
+ try:
23
+ response = requests.request(
24
+ "POST",
25
+ "http://api.interpreter.caiyunai.com/v1/translator",
26
+ data=json.dumps(payload),
27
+ headers=headers,
28
+ )
29
+
30
+ return json.loads(response.text)["target"]
31
+
32
+ except Exception as e:
33
+ return f"{e}"
34
+
35
+
36
+ if __name__ == "__main__":
37
+ gr.Interface(
38
+ fn=translate,
39
+ inputs=[
40
+ gr.TextArea(
41
+ label="Input text area",
42
+ placeholder="Type the text here...",
43
+ show_copy_button=True,
44
+ ),
45
+ gr.Dropdown(choices=["auto2en", "auto2zh", "auto2ja"], label="Mode"),
46
+ ],
47
+ outputs=gr.TextArea(label="Translation results", show_copy_button=True),
48
+ flagging_mode="never",
49
+
50
+ ).launch()