Update app.py
Browse files
app.py
CHANGED
|
@@ -1,55 +1,55 @@
|
|
| 1 |
import os
|
| 2 |
import openai
|
| 3 |
import gradio as gr
|
| 4 |
-
import logging
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
logging.disable(logging.CRITICAL)
|
| 8 |
-
|
| 9 |
-
# 1) OpenAI API ํค ์ค์
|
| 10 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 11 |
|
| 12 |
-
def
|
| 13 |
"""
|
| 14 |
-
ํ๊ตญ์ด
|
| 15 |
"""
|
| 16 |
if not korean_text.strip():
|
| 17 |
-
return "๋ฒ์ญํ
|
| 18 |
|
| 19 |
try:
|
| 20 |
response = openai.ChatCompletion.create(
|
| 21 |
model="gpt-3.5-turbo",
|
| 22 |
messages=[
|
| 23 |
-
{
|
| 24 |
-
|
| 25 |
-
"content": "You are a professional translator that translates Korean into English naturally."
|
| 26 |
-
},
|
| 27 |
-
{
|
| 28 |
-
"role": "user",
|
| 29 |
-
"content": korean_text
|
| 30 |
-
}
|
| 31 |
],
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
top_p=0.9
|
| 35 |
)
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
| 38 |
except Exception as e:
|
| 39 |
return f"์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
|
| 40 |
|
| 41 |
-
|
| 42 |
-
gr.
|
| 43 |
-
|
| 44 |
-
with gr.
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
if __name__ == "__main__":
|
| 53 |
-
|
| 54 |
-
# ssr
|
| 55 |
-
demo.launch(share=True
|
|
|
|
| 1 |
import os
|
| 2 |
import openai
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
|
| 5 |
+
# Hugging Face Spaces์์ ์ ์ฅํด๋ Secrets(ํ๊ฒฝ๋ณ์) ๋ถ๋ฌ์ค๊ธฐ
|
|
|
|
|
|
|
|
|
|
| 6 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 7 |
|
| 8 |
+
def translate_to_english(korean_text):
|
| 9 |
"""
|
| 10 |
+
ChatGPT API๋ฅผ ์ฌ์ฉํ์ฌ ํ๊ตญ์ด๋ฅผ ์์ด๋ก ๋ฒ์ญํ๋ ํจ์
|
| 11 |
"""
|
| 12 |
if not korean_text.strip():
|
| 13 |
+
return "๋ฒ์ญํ ๋ฌธ์ฅ์ ์
๋ ฅํด์ฃผ์ธ์."
|
| 14 |
|
| 15 |
try:
|
| 16 |
response = openai.ChatCompletion.create(
|
| 17 |
model="gpt-3.5-turbo",
|
| 18 |
messages=[
|
| 19 |
+
{"role": "system", "content": "You are a professional translator who translates Korean into natural English."},
|
| 20 |
+
{"role": "user", "content": korean_text},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
],
|
| 22 |
+
temperature=0.2,
|
| 23 |
+
max_tokens=200
|
|
|
|
| 24 |
)
|
| 25 |
+
|
| 26 |
+
# API ์๋ต์์ ๋ฒ์ญ๋ ํ
์คํธ ์ถ์ถ
|
| 27 |
+
english_text = response["choices"][0]["message"]["content"].strip()
|
| 28 |
+
return english_text
|
| 29 |
+
|
| 30 |
except Exception as e:
|
| 31 |
return f"์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
|
| 32 |
|
| 33 |
+
def main():
|
| 34 |
+
with gr.Blocks() as demo:
|
| 35 |
+
gr.Markdown("## ํ๊ตญ์ด โ ์์ด ๋ฒ์ญ๊ธฐ (ChatGPT API ํ์ฉ)")
|
| 36 |
+
with gr.Row():
|
| 37 |
+
with gr.Column():
|
| 38 |
+
input_text = gr.Textbox(label="ํ๊ตญ์ด ์
๋ ฅ", placeholder="ํ๊ตญ์ด ๋ฌธ์ฅ์ ์
๋ ฅํ์ธ์")
|
| 39 |
+
with gr.Column():
|
| 40 |
+
output_text = gr.Textbox(label="์์ด ๋ฒ์ญ ๊ฒฐ๊ณผ")
|
| 41 |
+
|
| 42 |
+
translate_button = gr.Button("๋ฒ์ญํ๊ธฐ")
|
| 43 |
+
|
| 44 |
+
translate_button.click(
|
| 45 |
+
fn=translate_to_english,
|
| 46 |
+
inputs=input_text,
|
| 47 |
+
outputs=output_text
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
return demo
|
| 51 |
|
| 52 |
if __name__ == "__main__":
|
| 53 |
+
demo = main()
|
| 54 |
+
# ssr ์ต์
์ ๊ฑฐ
|
| 55 |
+
demo.launch(share=True)
|