File size: 3,257 Bytes
4eb05a1
63f8d48
8110c29
c5eb776
 
 
 
8110c29
63f8d48
 
4eb05a1
63f8d48
4eb05a1
63f8d48
4eb05a1
c5eb776
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63f8d48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8110c29
63f8d48
e7a27a4
 
c5eb776
e7a27a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8110c29
 
 
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
78
79
80
81
82
83
84
85
86
87
88
89
import os
import openai
import gradio as gr
from dotenv import load_dotenv

# ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ๋กœ๋“œ
load_dotenv()

# OpenAI API ํ‚ค๋ฅผ ํ™˜๊ฒฝ ๋ณ€์ˆ˜์—์„œ ๊ฐ€์ ธ์˜ค๊ธฐ
openai.api_key = os.getenv("OPENAI_API_KEY")

def translate_to_korean(english_text):
    """
    ์ž…๋ ฅ๋œ ์˜์–ด ํ…์ŠคํŠธ๋ฅผ ํ•œ๊ตญ์–ด๋กœ ๋ฒˆ์—ญํ•˜๋Š” ํ•จ์ˆ˜
    """
    system_message = (
        "You are an assistant that translates English text to Korean. "
        "Ensure that the translation adheres to the following guidelines:\n\n"
        "1. Prioritize natural and fluent Korean over literal translations.\n"
        "2. Maintain grammatical accuracy:\n"
        "   - Prefer active voice over passive voice.\n"
        "   - Minimize the use of pronouns.\n"
        "   - Prefer verbs and adjectives over nominalizations.\n"
        "   - Use simple present or present perfect tense instead of present continuous.\n"
        "   - Maintain Subject-Object-Verb order.\n"
        "   - Avoid using expressions like \"~์—ˆ์–ด์š”\".\n"
        "3. Ensure a natural flow that matches the context of the subject.\n"
        "4. Choose vocabulary that is appropriate for the topic and situation.\n"
        "5. Reflect emotional nuances to create empathetic expressions.\n"
        "6. Maintain a balance between literal and free translation.\n"
        "7. Avoid repeating topic keywords.\n"
        "8. Ensure the translation does not appear to be written by AI.\n"
    )
    
    try:
        response = openai.ChatCompletion.create(
            model="gpt-4o-mini",  # ๋ชจ๋ธ ID
            messages=[
                {"role": "system", "content": system_message},
                {"role": "user", "content": english_text},
            ],
            max_tokens=1000,      # ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์กฐ์ •
            temperature=0.3,      # ์ฐฝ์˜์„ฑ ์กฐ์ ˆ
            top_p=1.0,            # ํ™•๋ฅ  ๋ถ„ํฌ ์กฐ์ ˆ
        )
        korean_text = response.choices[0].message['content'].strip()
        return korean_text
    except Exception as e:
        return f"์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}"

# Gradio ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌ์„ฑ
with gr.Blocks() as iface:
    gr.Markdown("# ์˜์–ด-ํ•œ๊ตญ์–ด ๋ฒˆ์—ญ๊ธฐ")
    gr.Markdown("GPT-4o-mini ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์˜์–ด ํ…์ŠคํŠธ๋ฅผ ํ•œ๊ตญ์–ด๋กœ ๋ฒˆ์—ญํ•ฉ๋‹ˆ๋‹ค. ๋ฒˆ์—ญ ์‹œ ์ž์—ฐ์Šค๋Ÿฝ๊ณ  ๋งค๋„๋Ÿฌ์šด ํ•œ๊ตญ์–ด๋กœ ์ œ๊ณต๋ฉ๋‹ˆ๋‹ค.")
    
    with gr.Row():
        with gr.Column():
            english_input = gr.Textbox(
                lines=10, 
                placeholder="๋ฒˆ์—ญํ•  ์˜์–ด ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”...",
                label="์˜์–ด ํ…์ŠคํŠธ"
            )
        with gr.Column():
            korean_output = gr.Textbox(
                lines=10, 
                label="ํ•œ๊ตญ์–ด ๋ฒˆ์—ญ"
            )
    
    translate_button = gr.Button("๋ฒˆ์—ญํ•˜๊ธฐ")
    
    translate_button.click(
        fn=translate_to_korean, 
        inputs=english_input, 
        outputs=korean_output
    )
    
    gr.Examples(
        examples=[
            "Hello, how are you?",
            "This is a test sentence for translation.",
            "OpenAI provides powerful language models."
        ],
        inputs=english_input,
        outputs=korean_output
    )

if __name__ == "__main__":
    iface.launch()