Sangwon78 commited on
Commit
1485e11
ยท
verified ยท
1 Parent(s): f73f343

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -34
app.py CHANGED
@@ -1,55 +1,55 @@
1
  import os
2
  import openai
3
  import gradio as gr
4
- import logging
5
 
6
- # ๋ถˆํ•„์š”ํ•œ ๋กœ๊ทธ(Info, Warning ๋“ฑ)๋ฅผ ๋ณด์ด์ง€ ์•Š๋„๋ก ์„ค์ •
7
- logging.disable(logging.CRITICAL)
8
-
9
- # 1) OpenAI API ํ‚ค ์„ค์ •
10
  openai.api_key = os.getenv("OPENAI_API_KEY")
11
 
12
- def translate_kor_to_eng(korean_text):
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
- "role": "system",
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
- max_tokens=128,
33
- temperature=0.7,
34
- top_p=0.9
35
  )
36
- translated_text = response["choices"][0]["message"]["content"].strip()
37
- return translated_text
 
 
 
38
  except Exception as e:
39
  return f"์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}"
40
 
41
- with gr.Blocks() as demo:
42
- gr.Markdown("## ํ•œ๊ตญ์–ด -> ์˜์–ด ๋ฒˆ์—ญ๊ธฐ (๋กœ๊ทธ ์ตœ์†Œํ™” ๋ฒ„์ „)")
43
- with gr.Row():
44
- with gr.Column():
45
- input_text = gr.Textbox(label="๋ฒˆ์—ญํ•  ํ•œ๊ตญ์–ด ์ž…๋ ฅ", lines=3)
46
- translate_button = gr.Button("๋ฒˆ์—ญํ•˜๊ธฐ")
47
- with gr.Column():
48
- output_text = gr.Textbox(label="์˜์–ด ๋ฒˆ์—ญ ๊ฒฐ๊ณผ", lines=3)
49
-
50
- translate_button.click(fn=translate_kor_to_eng, inputs=input_text, outputs=output_text)
 
 
 
 
 
 
 
 
51
 
52
  if __name__ == "__main__":
53
- # share=True: ๊ณต์šฉ URL ์ƒ์„ฑ
54
- # ssr=False: SSR ๊ด€๋ จ ์‹คํ—˜์  ๊ธฐ๋Šฅ ๋น„ํ™œ์„ฑํ™”(๋กœ๊ทธ ์ถœ๋ ฅ ์ค„์ž„)
55
- demo.launch(share=True, ssr=False)
 
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)