Spaces:
Runtime error
Runtime error
| 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() | |