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