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)
|