translate-test / app.py
CSB261's picture
Update app.py
0424bd2 verified
import os
import openai
import gradio as gr
# ํ™˜๊ฒฝ ๋ณ€์ˆ˜์— ์ €์žฅ๋œ API ํ‚ค ์‚ฌ์šฉ
openai.api_key = os.getenv("OPENAI_API_KEY")
def call_api(content, system_message, max_tokens=1024, temperature=0.2, top_p=1.0):
"""
gpt-4o-mini ๋ชจ๋ธ์— ์‚ฌ์šฉ์ž ์ž…๋ ฅ์„ ๋ณด๋‚ด๊ณ  ๋ฒˆ์—ญ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ›์•„์˜จ๋‹ค.
"""
response = openai.ChatCompletion.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": system_message},
{"role": "user", "content": content},
],
max_tokens=max_tokens,
temperature=temperature,
top_p=top_p,
)
return response.choices[0].message['content']
def translate_english_to_korean(code_text):
"""
์˜์–ด๋กœ ์ž‘์„ฑ๋œ ์ฝ”๋“œ๋‚˜ ์„ค๋ช…์„ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ํ•œ๊ตญ์–ด๋กœ ์˜ฎ๊ธด๋‹ค.
"""
system_msg = (
"You are a professional translator who accurately conveys English code "
"comments and explanations into smooth, natural Korean. Preserve code syntax "
"while ensuring natural readability."
)
return call_api(code_text, system_msg)
def main():
"""
Gradio ์ธํ„ฐํŽ˜์ด์Šค ์‹คํ–‰
"""
interface = gr.Interface(
fn=translate_english_to_korean,
inputs=gr.Textbox(lines=10, label="์˜์–ด ์ฝ”๋“œ ์ž…๋ ฅ"),
outputs=gr.Textbox(lines=10, label="๋ฒˆ์—ญ ๊ฒฐ๊ณผ"),
title="์ฝ”๋“œ ์˜์–ด -> ํ•œ๊ตญ์–ด ๋ฒˆ์—ญ๊ธฐ",
description="์˜์–ด๋กœ ์ž‘์„ฑ๋œ ์ฝ”๋“œ๋ฅผ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ํ•œ๊ตญ์–ด๋กœ ์˜ฎ๊ฒจ์ฃผ๋Š” ๊ฐ„๋‹จํ•œ ๋„๊ตฌ์ž…๋‹ˆ๋‹ค."
)
interface.launch(server_name="0.0.0.0", server_port=7860)
if __name__ == "__main__":
main()