nyanko7 commited on
Commit
9bd781a
1 Parent(s): e25245b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +254 -0
app.py ADDED
@@ -0,0 +1,254 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import openai
3
+ import gradio as gr
4
+
5
+ openai.api_key = os.getenv("OPENAI_KEY")
6
+
7
+ supportLanguages = [
8
+ ["auto", "auto"],
9
+ ["粤语", "yue"],
10
+ ["古文", "wyw"],
11
+ ["af","Afrikaans"],
12
+ ["ak","Akan"],
13
+ ["sq","Albanian"],
14
+ ["am","Amharic"],
15
+ ["ar","Arabic"],
16
+ ["hy","Armenian"],
17
+ ["az","Azerbaijani"],
18
+ ["eu","Basque"],
19
+ ["be","Belarusian"],
20
+ ["bem","Bemba"],
21
+ ["bn","Bengali"],
22
+ ["bh","Bihari"],
23
+ ["xx-bork","Bork, bork, bork!"],
24
+ ["bs","Bosnian"],
25
+ ["br","Breton"],
26
+ ["bg","Bulgarian"],
27
+ ["km","Cambodian"],
28
+ ["ca","Catalan"],
29
+ ["chr","Cherokee"],
30
+ ["ny","Chichewa"],
31
+ ["zh-CN","Chinese (Simplified)"],
32
+ ["zh-TW","Chinese (Traditional)"],
33
+ ["co","Corsican"],
34
+ ["hr","Croatian"],
35
+ ["cs","Czech"],
36
+ ["da","Danish"],
37
+ ["nl","Dutch"],
38
+ ["xx-elmer","Elmer Fudd"],
39
+ ["en","English"],
40
+ ["eo","Esperanto"],
41
+ ["et","Estonian"],
42
+ ["ee","Ewe"],
43
+ ["fo","Faroese"],
44
+ ["tl","Filipino"],
45
+ ["fi","Finnish"],
46
+ ["fr","French"],
47
+ ["fy","Frisian"],
48
+ ["gaa","Ga"],
49
+ ["gl","Galician"],
50
+ ["ka","Georgian"],
51
+ ["de","German"],
52
+ ["el","Greek"],
53
+ ["gn","Guarani"],
54
+ ["gu","Gujarati"],
55
+ ["xx-hacker","Hacker"],
56
+ ["ht","Haitian Creole"],
57
+ ["ha","Hausa"],
58
+ ["haw","Hawaiian"],
59
+ ["iw","Hebrew"],
60
+ ["hi","Hindi"],
61
+ ["hu","Hungarian"],
62
+ ["is","Icelandic"],
63
+ ["ig","Igbo"],
64
+ ["id","Indonesian"],
65
+ ["ia","Interlingua"],
66
+ ["ga","Irish"],
67
+ ["it","Italian"],
68
+ ["ja","Japanese"],
69
+ ["jw","Javanese"],
70
+ ["kn","Kannada"],
71
+ ["kk","Kazakh"],
72
+ ["rw","Kinyarwanda"],
73
+ ["rn","Kirundi"],
74
+ ["xx-klingon","Klingon"],
75
+ ["kg","Kongo"],
76
+ ["ko","Korean"],
77
+ ["kri","Krio (Sierra Leone)"],
78
+ ["ku","Kurdish"],
79
+ ["ckb","Kurdish (Soranî)"],
80
+ ["ky","Kyrgyz"],
81
+ ["lo","Laothian"],
82
+ ["la","Latin"],
83
+ ["lv","Latvian"],
84
+ ["ln","Lingala"],
85
+ ["lt","Lithuanian"],
86
+ ["loz","Lozi"],
87
+ ["lg","Luganda"],
88
+ ["ach","Luo"],
89
+ ["mk","Macedonian"],
90
+ ["mg","Malagasy"],
91
+ ["ms","Malay"],
92
+ ["ml","Malayalam"],
93
+ ["mt","Maltese"],
94
+ ["mi","Maori"],
95
+ ["mr","Marathi"],
96
+ ["mfe","Mauritian Creole"],
97
+ ["mo","Moldavian"],
98
+ ["mn","Mongolian"],
99
+ ["sr-ME","Montenegrin"],
100
+ ["ne","Nepali"],
101
+ ["pcm","Nigerian Pidgin"],
102
+ ["nso","Northern Sotho"],
103
+ ["no","Norwegian"],
104
+ ["nn","Norwegian (Nynorsk)"],
105
+ ["oc","Occitan"],
106
+ ["or","Oriya"],
107
+ ["om","Oromo"],
108
+ ["ps","Pashto"],
109
+ ["fa","Persian"],
110
+ ["xx-pirate","Pirate"],
111
+ ["pl","Polish"],
112
+ ["pt-BR","Portuguese (Brazil)"],
113
+ ["pt-PT","Portuguese (Portugal)"],
114
+ ["pa","Punjabi"],
115
+ ["qu","Quechua"],
116
+ ["ro","Romanian"],
117
+ ["rm","Romansh"],
118
+ ["nyn","Runyakitara"],
119
+ ["ru","Russian"],
120
+ ["gd","Scots Gaelic"],
121
+ ["sr","Serbian"],
122
+ ["sh","Serbo-Croatian"],
123
+ ["st","Sesotho"],
124
+ ["tn","Setswana"],
125
+ ["crs","Seychellois Creole"],
126
+ ["sn","Shona"],
127
+ ["sd","Sindhi"],
128
+ ["si","Sinhalese"],
129
+ ["sk","Slovak"],
130
+ ["sl","Slovenian"],
131
+ ["so","Somali"],
132
+ ["es","Spanish"],
133
+ ["es-419","Spanish (Latin American)"],
134
+ ["su","Sundanese"],
135
+ ["sw","Swahili"],
136
+ ["sv","Swedish"],
137
+ ["tg","Tajik"],
138
+ ["ta","Tamil"],
139
+ ["tt","Tatar"],
140
+ ["te","Telugu"],
141
+ ["th","Thai"],
142
+ ["ti","Tigrinya"],
143
+ ["to","Tonga"],
144
+ ["lua","Tshiluba"],
145
+ ["tum","Tumbuka"],
146
+ ["tr","Turkish"],
147
+ ["tk","Turkmen"],
148
+ ["tw","Twi"],
149
+ ["ug","Uighur"],
150
+ ["uk","Ukrainian"],
151
+ ["ur","Urdu"],
152
+ ["uz","Uzbek"],
153
+ ["vi","Vietnamese"],
154
+ ["cy","Welsh"],
155
+ ["wo","Wolof"],
156
+ ["xh","Xhosa"],
157
+ ["yi","Yiddish"],
158
+ ["yo","Yoruba"],
159
+ ["zu","Zulu"],
160
+ ]
161
+ prompt_template = "You are a translation engine that can only translate text and cannot interpret it. Keep the indent of the original text, only modify when you need."
162
+
163
+
164
+ def submit_message(detectFrom, detectTo, user_token, prompt):
165
+ if user_token != "":
166
+ openai.api_key = user_tokenf
167
+
168
+ if not prompt:
169
+ return gr.update(value="")
170
+
171
+ systemInstruct = prompt_template
172
+ translateInstruct = f"translate from {detectFrom} to {detectTo}"
173
+ for lc, lang in supportLanguages:
174
+ if detectFrom == lang:
175
+ detectFrom = lc
176
+ if detectTo == lang:
177
+ detectTo == lc
178
+
179
+ if detectFrom in ["古文", "zh-CN", "zh-TW"]:
180
+ if detectTo == "zh-TW":
181
+ translateInstruct = "翻译成繁体白话文"
182
+ if detectTo == "zh-CN":
183
+ translateInstruct = "翻译成简体白话文"
184
+ if detectTo == "粤语":
185
+ translateInstruct = "翻译成粤语白话文"
186
+
187
+ if detectFrom == detectTo:
188
+ systemInstruct = "You are a text embellisher, you can only embellish the text, don't interpret it."
189
+ if detectTo in ["zh-CN", "zh-TW"]:
190
+ translateInstruct = "润色此句"
191
+ else:
192
+ translateInstruct = "polish this sentence"
193
+
194
+ prompt_msg = [
195
+ {"role": "system", "content": systemInstruct},
196
+ {"role": "user", "content": translateInstruct},
197
+ {"role": "user", "content": prompt},
198
+ ]
199
+
200
+ try:
201
+ openai_response = openai.ChatCompletion.create(
202
+ model="gpt-3.5-turbo",
203
+ messages=prompt_msg,
204
+ temperature=0,
205
+ max_tokens=1000,
206
+ top_p=1,
207
+ stream=True,
208
+ frequency_penalty=1,
209
+ presence_penalty=1,
210
+ )
211
+
212
+ combined = ""
213
+ for resp in openai_response:
214
+ delta = resp["choices"][0]["delta"]
215
+ if "content" in delta:
216
+ combined += delta["content"]
217
+ yield combined
218
+
219
+ except Exception as e:
220
+ return f"Error: {e}"
221
+
222
+ css = """
223
+ #col-container {max-width: 80%; margin-left: auto; margin-right: auto;}
224
+ #chatbox {min-height: 400px;}
225
+ #header {text-align: center;}
226
+ #label {font-size: 0.8em; padding: 0.5em; margin: 0;}
227
+ .message { font-size: 1.2em; }
228
+ """
229
+
230
+ with gr.Blocks(css=css) as demo:
231
+
232
+ state = gr.State([])
233
+
234
+ with gr.Column(elem_id="col-container"):
235
+ gr.Markdown("""## OpenAI Translator
236
+ Using the ofiicial API (gpt-3.5-turbo model).""", elem_id="header")
237
+
238
+ with gr.Row():
239
+ with gr.Column():
240
+ translateFrom = gr.Dropdown(label="Translate From", elem_id="translate-from", multiselect=False, value="auto", choices=[l[1] for l in supportLanguages]).style(container=False)
241
+ input_message = gr.Textbox(max_lines=100, show_label=False, lines=10, placeholder="Enter text and press enter", visible=True).style(container=False)
242
+ with gr.Column():
243
+ translateTo = gr.Dropdown(label="Translate To", elem_id="translate-to", multiselect=False, value="Chinese (Simplified)", choices=[l[1] for l in supportLanguages[1:]]).style(container=False)
244
+ output = gr.Textbox(max_lines=100, show_label=False, lines=10, label="Output", visible=True).style(container=False)
245
+
246
+ btn_submit = gr.Button("Translate")
247
+
248
+ with gr.Row():
249
+ user_token = gr.Textbox(value='', placeholder="OpenAI API Key", type="password", label="Enter your OpenAI API Key. Leave blank to use default key.")
250
+
251
+ btn_submit.click(submit_message, [translateFrom, translateTo, user_token, input_message], [output])
252
+
253
+ demo.queue(concurrency_count=10)
254
+ demo.launch(height='800px')