Update app.py
Browse files
app.py
CHANGED
|
@@ -7,9 +7,9 @@ import gradio as gr
|
|
| 7 |
# 로컬 환경에서는 "export OPENAI_API_KEY=..." 로 설정해둡니다.
|
| 8 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 9 |
|
| 10 |
-
def
|
| 11 |
"""
|
| 12 |
-
|
| 13 |
max_tokens, temperature, top_p 설정을 적용하여 ChatGPT API를 호출합니다.
|
| 14 |
"""
|
| 15 |
if not text.strip():
|
|
@@ -21,11 +21,11 @@ def translate_english_to_korean(text, max_tokens, temperature, top_p):
|
|
| 21 |
messages=[
|
| 22 |
{
|
| 23 |
"role": "system",
|
| 24 |
-
"content": "You are a helpful assistant that translates
|
| 25 |
},
|
| 26 |
{
|
| 27 |
"role": "user",
|
| 28 |
-
"content": f"Translate the following text
|
| 29 |
}
|
| 30 |
],
|
| 31 |
max_tokens=max_tokens,
|
|
@@ -41,52 +41,15 @@ def translate_english_to_korean(text, max_tokens, temperature, top_p):
|
|
| 41 |
# (2) Gradio 인터페이스 설계
|
| 42 |
def main():
|
| 43 |
with gr.Blocks() as demo:
|
| 44 |
-
gr.Markdown("#
|
| 45 |
|
| 46 |
with gr.Row():
|
| 47 |
with gr.Column():
|
| 48 |
input_text = gr.Textbox(
|
| 49 |
-
label="
|
| 50 |
lines=5,
|
| 51 |
-
placeholder="번역할
|
| 52 |
)
|
| 53 |
max_tokens_slider = gr.Slider(
|
| 54 |
minimum=50,
|
| 55 |
-
|
| 56 |
-
value=200,
|
| 57 |
-
step=1,
|
| 58 |
-
label="max_tokens"
|
| 59 |
-
)
|
| 60 |
-
temperature_slider = gr.Slider(
|
| 61 |
-
minimum=0.0,
|
| 62 |
-
maximum=1.0,
|
| 63 |
-
value=0.7,
|
| 64 |
-
step=0.1,
|
| 65 |
-
label="temperature"
|
| 66 |
-
)
|
| 67 |
-
top_p_slider = gr.Slider(
|
| 68 |
-
minimum=0.0,
|
| 69 |
-
maximum=1.0,
|
| 70 |
-
value=1.0,
|
| 71 |
-
step=0.1,
|
| 72 |
-
label="top_p"
|
| 73 |
-
)
|
| 74 |
-
translate_button = gr.Button("번역하기")
|
| 75 |
-
|
| 76 |
-
with gr.Column():
|
| 77 |
-
output_text = gr.Textbox(
|
| 78 |
-
label="한국어 번역 결과",
|
| 79 |
-
lines=5
|
| 80 |
-
)
|
| 81 |
-
|
| 82 |
-
translate_button.click(
|
| 83 |
-
fn=translate_english_to_korean,
|
| 84 |
-
inputs=[input_text, max_tokens_slider, temperature_slider, top_p_slider],
|
| 85 |
-
outputs=[output_text]
|
| 86 |
-
)
|
| 87 |
-
|
| 88 |
-
return demo
|
| 89 |
-
|
| 90 |
-
if __name__ == "__main__":
|
| 91 |
-
demo_app = main()
|
| 92 |
-
demo_app.launch()
|
|
|
|
| 7 |
# 로컬 환경에서는 "export OPENAI_API_KEY=..." 로 설정해둡니다.
|
| 8 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 9 |
|
| 10 |
+
def translate_korean_to_english(text, max_tokens, temperature, top_p):
|
| 11 |
"""
|
| 12 |
+
한국어 텍스트를 영어로 번역하는 함수.
|
| 13 |
max_tokens, temperature, top_p 설정을 적용하여 ChatGPT API를 호출합니다.
|
| 14 |
"""
|
| 15 |
if not text.strip():
|
|
|
|
| 21 |
messages=[
|
| 22 |
{
|
| 23 |
"role": "system",
|
| 24 |
+
"content": "You are a helpful assistant that translates Korean to English."
|
| 25 |
},
|
| 26 |
{
|
| 27 |
"role": "user",
|
| 28 |
+
"content": f"Translate the following text from Korean to English in a natural manner:\n{text}"
|
| 29 |
}
|
| 30 |
],
|
| 31 |
max_tokens=max_tokens,
|
|
|
|
| 41 |
# (2) Gradio 인터페이스 설계
|
| 42 |
def main():
|
| 43 |
with gr.Blocks() as demo:
|
| 44 |
+
gr.Markdown("# 한국어 → 영어 번역기\n(ChatGPT API 활용)")
|
| 45 |
|
| 46 |
with gr.Row():
|
| 47 |
with gr.Column():
|
| 48 |
input_text = gr.Textbox(
|
| 49 |
+
label="한국어 텍스트 입력",
|
| 50 |
lines=5,
|
| 51 |
+
placeholder="번역할 한국어 텍스트를 입력하세요."
|
| 52 |
)
|
| 53 |
max_tokens_slider = gr.Slider(
|
| 54 |
minimum=50,
|
| 55 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|