File size: 1,750 Bytes
2da52c5
 
 
3ee4aca
1485e11
864b0b9
 
1485e11
1da6765
1485e11
1da6765
 
1485e11
3ee4aca
bd6643e
 
 
 
1485e11
 
bd6643e
1485e11
 
bd6643e
1485e11
 
 
 
 
bd6643e
 
2da52c5
1485e11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1da6765
04017ae
1485e11
 
 
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
import os
import openai
import gradio as gr

# Hugging Face Spaces์—์„œ ์ €์žฅํ•ด๋‘” Secrets(ํ™˜๊ฒฝ๋ณ€์ˆ˜) ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
openai.api_key = os.getenv("OPENAI_API_KEY")

def translate_to_english(korean_text):
    """
    ChatGPT API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•œ๊ตญ์–ด๋ฅผ ์˜์–ด๋กœ ๋ฒˆ์—ญํ•˜๋Š” ํ•จ์ˆ˜
    """
    if not korean_text.strip():
        return "๋ฒˆ์—ญํ•  ๋ฌธ์žฅ์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”."

    try:
        response = openai.ChatCompletion.create(
            model="gpt-3.5-turbo",
            messages=[
                {"role": "system", "content": "You are a professional translator who translates Korean into natural English."},
                {"role": "user", "content": korean_text},
            ],
            temperature=0.2,
            max_tokens=200
        )
        
        # API ์‘๋‹ต์—์„œ ๋ฒˆ์—ญ๋œ ํ…์ŠคํŠธ ์ถ”์ถœ
        english_text = response["choices"][0]["message"]["content"].strip()
        return english_text

    except Exception as e:
        return f"์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}"

def main():
    with gr.Blocks() as demo:
        gr.Markdown("## ํ•œ๊ตญ์–ด โ†’ ์˜์–ด ๋ฒˆ์—ญ๊ธฐ (ChatGPT API ํ™œ์šฉ)")
        with gr.Row():
            with gr.Column():
                input_text = gr.Textbox(label="ํ•œ๊ตญ์–ด ์ž…๋ ฅ", placeholder="ํ•œ๊ตญ์–ด ๋ฌธ์žฅ์„ ์ž…๋ ฅํ•˜์„ธ์š”")
            with gr.Column():
                output_text = gr.Textbox(label="์˜์–ด ๋ฒˆ์—ญ ๊ฒฐ๊ณผ")
        
        translate_button = gr.Button("๋ฒˆ์—ญํ•˜๊ธฐ")
        
        translate_button.click(
            fn=translate_to_english,
            inputs=input_text,
            outputs=output_text
        )
        
    return demo

if __name__ == "__main__":
    demo = main()
    # ssr ์˜ต์…˜ ์ œ๊ฑฐ
    demo.launch(share=True)