File size: 1,646 Bytes
a0a5234
 
 
 
0424bd2
a0a5234
 
0424bd2
 
 
 
a0a5234
0424bd2
a0a5234
 
 
 
 
 
 
 
 
 
0424bd2
 
 
 
 
 
 
 
 
 
a0a5234
0424bd2
 
 
 
 
 
 
 
 
 
 
 
a0a5234
 
0424bd2
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
import os
import openai
import gradio as gr

# ํ™˜๊ฒฝ ๋ณ€์ˆ˜์— ์ €์žฅ๋œ API ํ‚ค ์‚ฌ์šฉ
openai.api_key = os.getenv("OPENAI_API_KEY")

def call_api(content, system_message, max_tokens=1024, temperature=0.2, top_p=1.0):
    """
    gpt-4o-mini ๋ชจ๋ธ์— ์‚ฌ์šฉ์ž ์ž…๋ ฅ์„ ๋ณด๋‚ด๊ณ  ๋ฒˆ์—ญ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ›์•„์˜จ๋‹ค.
    """
    response = openai.ChatCompletion.create(
        model="gpt-4o-mini",
        messages=[
            {"role": "system", "content": system_message},
            {"role": "user", "content": content},
        ],
        max_tokens=max_tokens,
        temperature=temperature,
        top_p=top_p,
    )
    return response.choices[0].message['content']

def translate_english_to_korean(code_text):
    """
    ์˜์–ด๋กœ ์ž‘์„ฑ๋œ ์ฝ”๋“œ๋‚˜ ์„ค๋ช…์„ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ํ•œ๊ตญ์–ด๋กœ ์˜ฎ๊ธด๋‹ค.
    """
    system_msg = (
        "You are a professional translator who accurately conveys English code "
        "comments and explanations into smooth, natural Korean. Preserve code syntax "
        "while ensuring natural readability."
    )
    return call_api(code_text, system_msg)

def main():
    """
    Gradio ์ธํ„ฐํŽ˜์ด์Šค ์‹คํ–‰
    """
    interface = gr.Interface(
        fn=translate_english_to_korean,
        inputs=gr.Textbox(lines=10, label="์˜์–ด ์ฝ”๋“œ ์ž…๋ ฅ"),
        outputs=gr.Textbox(lines=10, label="๋ฒˆ์—ญ ๊ฒฐ๊ณผ"),
        title="์ฝ”๋“œ ์˜์–ด -> ํ•œ๊ตญ์–ด ๋ฒˆ์—ญ๊ธฐ",
        description="์˜์–ด๋กœ ์ž‘์„ฑ๋œ ์ฝ”๋“œ๋ฅผ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ํ•œ๊ตญ์–ด๋กœ ์˜ฎ๊ฒจ์ฃผ๋Š” ๊ฐ„๋‹จํ•œ ๋„๊ตฌ์ž…๋‹ˆ๋‹ค."
    )
    interface.launch(server_name="0.0.0.0", server_port=7860)

if __name__ == "__main__":
    main()