Sangwon78 commited on
Commit
3ee4aca
ยท
verified ยท
1 Parent(s): 04017ae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -40
app.py CHANGED
@@ -1,20 +1,22 @@
1
  import os
2
  import openai
3
  import gradio as gr
 
 
 
 
 
 
 
 
4
 
5
- # (1) OpenAI Key ์„ค์ •
6
- # Hugging Face Spaces์—์„œ๋Š” "Repository > Settings > Secrets"์— OPENAI_API_KEY๋ฅผ ๋“ฑ๋กํ•˜๊ฑฐ๋‚˜
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():
16
  return "๋ฒˆ์—ญํ•  ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”."
17
-
18
  try:
19
  response = openai.ChatCompletion.create(
20
  model="gpt-3.5-turbo",
@@ -25,23 +27,21 @@ def translate_korean_to_english(text, max_tokens, temperature, top_p):
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,
32
- temperature=temperature,
33
- top_p=top_p
34
  )
35
- # ChatCompletion์—์„œ ์ƒ์„ฑ๋œ ๋ฒˆ์—ญ๋ฌธ ์ถ”์ถœ
36
- result = response.choices[0].message.content.strip()
37
- return result
38
  except Exception as e:
39
  return f"์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}"
40
 
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():
@@ -50,27 +50,6 @@ def main():
50
  lines=5,
51
  placeholder="๋ฒˆ์—ญํ•  ํ•œ๊ตญ์–ด ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”."
52
  )
53
- max_tokens_slider = gr.Slider(
54
- minimum=50,
55
- maximum=2048,
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():
@@ -81,7 +60,7 @@ def main():
81
 
82
  translate_button.click(
83
  fn=translate_korean_to_english,
84
- inputs=[input_text, max_tokens_slider, temperature_slider, top_p_slider],
85
  outputs=[output_text]
86
  )
87
 
@@ -89,4 +68,6 @@ def main():
89
 
90
  if __name__ == "__main__":
91
  demo_app = main()
 
 
92
  demo_app.launch()
 
1
  import os
2
  import openai
3
  import gradio as gr
4
+ from datasets import load_dataset
5
+
6
+ # (1) ๋ฐ์ดํ„ฐ์…‹ ๋กœ๋“œ
7
+ data = load_dataset(
8
+ "meta-llama/Llama-3.3-70B-Instruct-evals",
9
+ name="Llama-3.3-70B-Instruct-evals__mbpp_plus__details",
10
+ split="latest"
11
+ )
12
 
 
 
 
13
  openai.api_key = os.getenv("OPENAI_API_KEY")
14
 
15
+ def translate_korean_to_english(text):
16
+ # ๋ณธ๋ฌธ ์˜ˆ์‹œ. ์‹ค์ œ๋Š” max_tokens, temperature ๋“ฑ ์˜ต์…˜ ๋งž์ถฐ์ฃผ์„ธ์š”.
 
 
 
17
  if not text.strip():
18
  return "๋ฒˆ์—ญํ•  ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”."
19
+
20
  try:
21
  response = openai.ChatCompletion.create(
22
  model="gpt-3.5-turbo",
 
27
  },
28
  {
29
  "role": "user",
30
+ "content": f"Translate the following text from Korean to English:\n{text}"
31
  }
32
  ],
33
+ max_tokens=200,
34
+ temperature=0.7,
35
+ top_p=1.0
36
  )
37
+ return response.choices[0].message.content.strip()
 
 
38
  except Exception as e:
39
  return f"์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}"
40
 
41
+ # (2) Gradio ์ธํ„ฐํŽ˜์ด์Šค
42
  def main():
43
  with gr.Blocks() as demo:
44
+ gr.Markdown("# ํ•œ๊ตญ์–ด โ†’ ์˜์–ด ๋ฒˆ์—ญ๊ธฐ (ChatGPT API)")
45
 
46
  with gr.Row():
47
  with gr.Column():
 
50
  lines=5,
51
  placeholder="๋ฒˆ์—ญํ•  ํ•œ๊ตญ์–ด ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”."
52
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  translate_button = gr.Button("๋ฒˆ์—ญํ•˜๊ธฐ")
54
 
55
  with gr.Column():
 
60
 
61
  translate_button.click(
62
  fn=translate_korean_to_english,
63
+ inputs=[input_text],
64
  outputs=[output_text]
65
  )
66
 
 
68
 
69
  if __name__ == "__main__":
70
  demo_app = main()
71
+ # share=True ์˜ต์…˜์„ ์ฃผ๋ฉด ์™ธ๋ถ€์—์„œ ์ ‘์† ๊ฐ€๋Šฅํ•œ ๋งํฌ๊ฐ€ ์ƒ๊น๋‹ˆ๋‹ค.
72
+ # ssr=False๋กœ SSR์„ ๋น„ํ™œ์„ฑํ™”ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.
73
  demo_app.launch()