Update app.py
Browse files
app.py
CHANGED
@@ -1,21 +1,62 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
|
|
|
|
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
inputs = [
|
8 |
-
gr.inputs.Textbox(lines=
|
9 |
gr.inputs.File(label="Upload MDX File")
|
10 |
]
|
11 |
|
12 |
outputs = gr.outputs.Textbox(label="Translation")
|
13 |
|
14 |
def translate_with_upload(text, file):
|
|
|
|
|
|
|
15 |
if file is not None:
|
16 |
-
# νμΌ
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
prompt_translate = gr.Interface(
|
21 |
fn=translate_with_upload,
|
|
|
1 |
import gradio as gr
|
2 |
+
import subprocess
|
3 |
+
import openapi
|
4 |
+
import time
|
5 |
|
6 |
+
def translate(text_input, openapi_key):
|
7 |
+
openai.api_key = openapi_key
|
8 |
+
|
9 |
+
text_list = text_input.split('\n')[12:]
|
10 |
|
11 |
+
reply = []
|
12 |
+
|
13 |
+
for i in range(0,len(text_list)+9,10):
|
14 |
+
content = """What do these sentences about Hugging Face Transformers (a machine learning library) mean in Korean? Please do not translate the word after a π€ emoji as it is a product name. Please ignore the video and image and translate only the sentences I provided. Ignore the contents of the iframe tag.
|
15 |
+
```md
|
16 |
+
%s"""%'\n'.join(text_list[i:i+10])
|
17 |
+
|
18 |
+
chat = openai.ChatCompletion.create(
|
19 |
+
model = "gpt-3.5-turbo-0301", messages=[
|
20 |
+
{"role": "system",
|
21 |
+
"content": content},])
|
22 |
+
|
23 |
+
print("μ§λ¬Έ")
|
24 |
+
print(content)
|
25 |
+
print("μλ΅")
|
26 |
+
print(chat.choices[0].message.content)
|
27 |
+
|
28 |
+
reply.append(chat.choices[0].message.content)
|
29 |
+
|
30 |
+
time.sleep(20)
|
31 |
+
|
32 |
+
return reply
|
33 |
|
34 |
inputs = [
|
35 |
+
gr.inputs.Textbox(lines=2, label="Input Open API Key"),
|
36 |
gr.inputs.File(label="Upload MDX File")
|
37 |
]
|
38 |
|
39 |
outputs = gr.outputs.Textbox(label="Translation")
|
40 |
|
41 |
def translate_with_upload(text, file):
|
42 |
+
|
43 |
+
openapi_key = text
|
44 |
+
|
45 |
if file is not None:
|
46 |
+
# νμΌ μ΄λ¦μ μΆμΆν©λλ€.
|
47 |
+
filename = os.path.basename(file.name)
|
48 |
+
# μλ‘μ΄ νμΌ μ΄λ¦μ μμ±ν©λλ€.
|
49 |
+
new_filename = f"text_{filename}"
|
50 |
+
# sed λͺ
λ Ήμ΄λ₯Ό μ¬μ©νμ¬ νμΌμ λ³νν©λλ€.
|
51 |
+
subprocess.run(f"sed '/```/,/```/d' {filename} | sed '/^|.*|$/d' | sed '/^$/N;/^\n$/D' > {new_filename}", shell=True)
|
52 |
+
# μλ‘μ΄ νμΌμ μ΄μ΄μ λ΄μ©μ μ½μ΄μ΅λλ€.
|
53 |
+
with open(new_filename, 'r') as f:
|
54 |
+
text_input = f.read()
|
55 |
+
else:
|
56 |
+
# νμΌμ΄ μ
λ‘λλμ§ μμ κ²½μ°, λΉ λ¬Έμμ΄λ‘ μ΄κΈ°νν©λλ€.
|
57 |
+
text_input = ""
|
58 |
+
|
59 |
+
return translate(text_input, openapi_key)
|
60 |
|
61 |
prompt_translate = gr.Interface(
|
62 |
fn=translate_with_upload,
|