File size: 2,490 Bytes
32a7553
249a64a
 
32a7553
7c5fedb
249a64a
32a7553
249a64a
 
7c5fedb
249a64a
 
7c5fedb
 
 
 
 
 
 
249a64a
 
7c5fedb
249a64a
 
 
 
 
 
 
 
 
 
 
 
7c5fedb
e5df068
7c5fedb
249a64a
 
7c5fedb
 
 
 
e5df068
 
7c5fedb
 
 
 
 
 
 
 
 
 
e5df068
249a64a
7c5fedb
 
 
 
 
249a64a
 
32a7553
769b8f7
249a64a
 
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
import gradio as gr
import os
import openai

# OpenAI API ν‚€λ₯Ό μ„€μ •ν•©λ‹ˆλ‹€.
openai.api_key = os.getenv("OPENAI_API_KEY")

def translate_code_to_korean(code):
    """
    μž…λ ₯된 μ˜μ–΄ μ½”λ“œλ₯Ό μžμ—°μŠ€λŸ½κ³  μ •ν™•ν•œ ν•œκ΅­μ–΄λ‘œ λ²ˆμ—­ν•©λ‹ˆλ‹€.
    """
    system_message = (
        "당신은 ν”„λ‘œκ·Έλž˜λ° κ΄€λ ¨ μ˜μ–΄ ν…μŠ€νŠΈλ₯Ό ν•œκ΅­μ–΄λ‘œ λ²ˆμ—­ν•˜λŠ” AIμž…λ‹ˆλ‹€. "
        "λ²ˆμ—­μ€ λ¬Έλ²•μ μœΌλ‘œ μ •ν™•ν•˜κ³  μžμ—°μŠ€λŸ¬μš΄ 흐름을 μš°μ„ μœΌλ‘œ ν•˜λ©°, 기술적 의미λ₯Ό λͺ…ν™•ν•˜κ²Œ 전달해야 ν•©λ‹ˆλ‹€. "
        "- ν”Όλ™ν˜• λŒ€μ‹  λŠ₯λ™ν˜• λ¬Έμž₯을 μ‚¬μš©ν•©λ‹ˆλ‹€. \n"
        "- λŒ€λͺ…사λ₯Ό μ΅œμ†Œν™”ν•˜κ³ , 동사와 ν˜•μš©μ‚¬λ₯Ό μš°μ„ μ μœΌλ‘œ μ‚¬μš©ν•©λ‹ˆλ‹€. \n"
        "- ν˜„μž¬μ§„ν–‰ν˜•λ³΄λ‹€ λ‹¨μˆœ ν˜„μž¬λ‚˜ μ™„λ£Œν˜•μ„ μ‚¬μš©ν•©λ‹ˆλ‹€. \n"
        "- μ£Όμ œμ™€ 상황에 λ§žλŠ” μ–΄νœ˜λ₯Ό μ„ νƒν•˜λ©°, 직역과 μ˜μ—­ μ‚¬μ΄μ˜ κ· ν˜•μ„ μœ μ§€ν•©λ‹ˆλ‹€. \n"
        "- λ²ˆμ—­λœ ν…μŠ€νŠΈλŠ” AIκ°€ μž‘μ„±ν•œ λŠλ‚Œμ„ μ£Όμ§€ μ•Šμ•„μ•Ό ν•©λ‹ˆλ‹€."
    )
    try:
        # OpenAI APIλ₯Ό ν˜ΈμΆœν•©λ‹ˆλ‹€.
        response = openai.ChatCompletion.create(
            model="gpt-4o-mini",
            messages=[
                {"role": "system", "content": system_message},
                {"role": "user", "content": code},
            ],
            max_tokens=1500,
            temperature=0.7,
            top_p=0.9,
        )
        return response.choices[0].message['content']
    except Exception as e:
        return f"였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {str(e)}"

# Gradio μΈν„°νŽ˜μ΄μŠ€λ₯Ό κ΅¬μ„±ν•©λ‹ˆλ‹€.
def interface():
    with gr.Blocks() as app:
        gr.Markdown("""
        # μ½”λ“œ λ²ˆμ—­κΈ°
        μ˜μ–΄λ‘œ μž‘μ„±λœ μ½”λ“œλ₯Ό μž…λ ₯ν•˜λ©΄ μžμ—°μŠ€λŸ½κ³  μ •ν™•ν•œ ν•œκ΅­μ–΄λ‘œ λ²ˆμ—­ν•΄ λ“œλ¦½λ‹ˆλ‹€.
        """)
        
        with gr.Row():
            input_code = gr.Textbox(
                label="λ²ˆμ—­ν•  μ˜μ–΄ μ½”λ“œ",
                lines=10,
                placeholder="여기에 μ½”λ“œλ₯Ό μž…λ ₯ν•˜μ„Έμš”."
            )
            output_translation = gr.Textbox(
                label="λ²ˆμ—­ κ²°κ³Ό",
                lines=10,
                interactive=False
            )
        
        translate_button = gr.Button("λ²ˆμ—­ μ‹€ν–‰")
        translate_button.click(
            translate_code_to_korean,
            inputs=[input_code],
            outputs=[output_translation]
        )
        
    return app

if __name__ == "__main__":
    app = interface()
    app.launch()