File size: 2,351 Bytes
e716c34
9a63602
86c1307
9a63602
5fa1a6d
35932c4
9a63602
86c1307
9a63602
801758e
 
 
c00d819
5f3cbcc
9a63602
 
6411c52
9a63602
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80267e7
9a63602
9ede116
35932c4
b8d88fa
9a63602
fbb0cb8
b8d88fa
 
9ede116
b8d88fa
 
9a63602
 
 
b8d88fa
c7553f2
b2277aa
c7553f2
 
ff4497e
5fa1a6d
 
 
 
 
 
 
 
8127acc
 
93a8025
35932c4
e716c34
 
b8d88fa
 
 
 
4e9814e
e716c34
 
599aee6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import gradio as gr
import subprocess
import openai
import time
import re

def translate(text_input, openapi_key):
    openai.api_key = openapi_key
    
    # ๋ผ์ด์„ ์Šค ๋ฌธ์žฅ ์ œ๊ฑฐ
    rm_line = text_input.find('-->')
    text_list = text_input[rm_line+4:].split('\n')
    print(text_list)

    reply = []
    
    for i in range(0,len(text_list),10):
        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.
                ```md
                %s"""%'\n'.join(text_list[i:i+10])

        chat = openai.ChatCompletion.create(
        model = "gpt-3.5-turbo-0301", messages=[
            {"role": "system", 
             "content": content},])
            
        print("์งˆ๋ฌธ")
        print(content)
        print("์‘๋‹ต")
        print(chat.choices[0].message.content)
        
        reply.append(chat.choices[0].message.content)

        time.sleep(30)
        
    return ''.join(reply)

inputs = [
    gr.inputs.Textbox(lines=2, label="Input Open API Key"),
    gr.inputs.File(label="Upload MDX File")
]

outputs = gr.outputs.Textbox(label="Translation")

def translate_with_upload(text, file):
    
    openapi_key = text
    
    if file is not None:
        text_input = ""
        with open(file.name, 'r') as f:
            text_input += f.read()
            text_input += '\n'
        print(text_input)
        # ํ…์ŠคํŠธ์—์„œ ์ฝ”๋“œ ๋ธ”๋ก์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
        text_input = re.sub(r'```.*?```', '', text_input, flags=re.DOTALL)
        
        text_input = re.sub(r'^\|.*\|$\n?', '', text_input, flags=re.MULTILINE)
        
        # ํ…์ŠคํŠธ์—์„œ ๋นˆ ์ค„์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
        text_input = re.sub(r'^\n', '', text_input, flags=re.MULTILINE)
        text_input = re.sub(r'\n\n+', '\n\n', text_input)
    else:
        text_input = ""

    return translate(text_input, openapi_key)

prompt_translate = gr.Interface(
    fn=translate_with_upload,
    inputs=inputs,
    outputs=outputs,
    title="ChatGPT Korean Prompt Translation",
    description="Translate your text into Korean using the GPT-3 model.", verbose=True
)

prompt_translate.launch()