Sangwon78 commited on
Commit
864b0b9
ยท
verified ยท
1 Parent(s): dac7e1c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -21
app.py CHANGED
@@ -3,31 +3,31 @@ import openai
3
  import gradio as gr
4
  from datasets import load_dataset
5
 
6
- # OpenAI API ํ‚ค ์„ค์ •
7
- openai.api_key = os.getenv("OPENAI_API_KEY")
8
-
9
- # ๋ฐ์ดํ„ฐ์…‹ ๋กœ๋“œ(๋ฐ๋ชจ์šฉ)
10
  data = load_dataset(
11
  "meta-llama/Llama-3.3-70B-Instruct-evals",
12
  name="Llama-3.3-70B-Instruct-evals__mbpp_plus__details",
13
  split="latest"
14
  )
15
 
 
 
 
16
  def translate_english_to_korean(text, max_tokens=256, temperature=1.0, top_p=1.0):
17
  """
18
- ์˜์–ด ๋ฌธ์žฅ์„ ํ•œ๊ตญ์–ด๋กœ ๋ฒˆ์—ญํ•ฉ๋‹ˆ๋‹ค.
19
-
20
- :param text: ๋ฒˆ์—ญํ•  ์˜์–ด ๋ฌธ์žฅ
21
- :param max_tokens: ์ƒ์„ฑํ•  ์‘๋‹ต์˜ ์ตœ๋Œ€ ํ† ํฐ ์ˆ˜
22
- :param temperature: ์‘๋‹ต์˜ ์ฐฝ์˜์„ฑ(0~2)
23
- :param top_p: ์ƒ˜ํ”Œ๋ง ์‹œ ํ™•๋ฅ ์˜ ๋ˆ„์  ๋ถ„ํฌ ์ž„๊ณ„๊ฐ’(0~1)
24
- :return: ํ•œ๊ตญ์–ด ๋ฒˆ์—ญ๋ฌธ
25
  """
 
26
  if not text.strip():
27
  return "๋ฒˆ์—ญํ•  ๋ฌธ์žฅ์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”."
28
 
29
  response = openai.ChatCompletion.create(
30
- model="gpt-3.5-turbo", # ํ˜น์€ gpt-4 ๋“ฑ ์›ํ•˜๋Š” ๋ชจ๋ธ
31
  messages=[
32
  {
33
  "role": "system",
@@ -40,32 +40,62 @@ def translate_english_to_korean(text, max_tokens=256, temperature=1.0, top_p=1.0
40
  ],
41
  max_tokens=int(max_tokens),
42
  temperature=float(temperature),
43
- top_p=float(top_p),
44
  )
45
 
 
46
  return response.choices[0].message.content.strip()
47
 
48
  def build_app():
49
  """
50
- Gradio ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌ์„ฑํ•ฉ๋‹ˆ๋‹ค.
51
  """
52
  with gr.Blocks() as demo:
53
- gr.Markdown("## ์˜์–ด โ†’ ํ•œ๊ตญ์–ด ๋ฒˆ์—ญ๊ธฐ\n์ž์—ฐ์Šค๋Ÿฝ๊ณ  ๋งค๋„๋Ÿฌ์šด ๋ฒˆ์—ญ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.")
54
 
55
  with gr.Row():
56
  with gr.Column():
57
- text_input = gr.Textbox(label="์˜์–ด ๋ฌธ์žฅ ์ž…๋ ฅ", placeholder="๋ฒˆ์—ญํ•  ์˜์–ด ๋ฌธ์žฅ์„ ์ž…๋ ฅํ•˜์„ธ์š”.")
58
- max_tokens_slider = gr.Slider(minimum=1, maximum=2048, step=1, value=256, label="max_tokens")
59
- temperature_slider = gr.Slider(minimum=0, maximum=2, step=0.1, value=1.0, label="temperature")
60
- top_p_slider = gr.Slider(minimum=0, maximum=1, step=0.01, value=1.0, label="top_p")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  translate_button = gr.Button("๋ฒˆ์—ญํ•˜๊ธฐ")
62
 
63
  with gr.Column():
64
- output_text = gr.Textbox(label="ํ•œ๊ตญ์–ด ๋ฒˆ์—ญ ๊ฒฐ๊ณผ", interactive=False)
 
 
 
 
65
 
 
66
  translate_button.click(
67
  fn=translate_english_to_korean,
68
- inputs=[text_input, max_tokens_slider, temperature_slider, top_p_slider],
69
  outputs=output_text,
70
  )
71
 
 
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
+ # 2. OpenAI API ํ‚ค ์„ค์ •
14
+ openai.api_key = os.getenv("OPENAI_API_KEY")
15
+
16
  def translate_english_to_korean(text, max_tokens=256, temperature=1.0, top_p=1.0):
17
  """
18
+ ์˜์–ด ๋ฌธ์žฅ์„ ํ•œ๊ตญ์–ด๋กœ ๋ฒˆ์—ญํ•˜๋Š” ํ•จ์ˆ˜.
19
+ ํŒŒ๋ผ๋ฏธํ„ฐ:
20
+ - text: ๋ฒˆ์—ญํ•  ์˜์–ด ๋ฌธ์žฅ
21
+ - max_tokens: ์ƒ์„ฑํ•  ๋ฒˆ์—ญ๋ฌธ์˜ ์ตœ๋Œ€ ํ† ํฐ ์ˆ˜
22
+ - temperature: ์ฐฝ์˜์„ฑ(0~2)
23
+ - top_p: ์ƒ˜ํ”Œ๋ง ํ™•๋ฅ ์˜ ๋ˆ„์  ๋ถ„ํฌ ์ž„๊ณ„๊ฐ’(0~1)
 
24
  """
25
+ # ์ž…๋ ฅ์ด ๋น„์–ด์žˆ์œผ๋ฉด ์•ˆ๋‚ด ๋ฉ”์‹œ์ง€
26
  if not text.strip():
27
  return "๋ฒˆ์—ญํ•  ๋ฌธ์žฅ์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”."
28
 
29
  response = openai.ChatCompletion.create(
30
+ model="gpt-3.5-turbo", # ํ•„์š”ํ•œ ๊ฒฝ์šฐ gpt-4๋กœ ๊ต์ฒด ๊ฐ€๋Šฅ
31
  messages=[
32
  {
33
  "role": "system",
 
40
  ],
41
  max_tokens=int(max_tokens),
42
  temperature=float(temperature),
43
+ top_p=float(top_p)
44
  )
45
 
46
+ # ChatCompletion ์‘๋‹ต์—์„œ ๋ฒˆ์—ญ ๊ฒฐ๊ณผ ์ถ”์ถœ
47
  return response.choices[0].message.content.strip()
48
 
49
  def build_app():
50
  """
51
+ Gradio Blocks๋ฅผ ์ด์šฉํ•ด UI๋ฅผ ๋นŒ๋“œํ•˜๋Š” ํ•จ์ˆ˜.
52
  """
53
  with gr.Blocks() as demo:
54
+ gr.Markdown("## ์˜์–ด โ†’ ํ•œ๊ตญ์–ด ๋ฒˆ์—ญ๊ธฐ\n\n์ž์—ฐ์Šค๋Ÿฝ๊ณ  ๋งค๋„๋Ÿฌ์šด ๋ฒˆ์—ญ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.")
55
 
56
  with gr.Row():
57
  with gr.Column():
58
+ # ์‚ฌ์šฉ์ž ์ž…๋ ฅ ์˜์—ญ
59
+ input_text = gr.Textbox(
60
+ label="์˜์–ด ๋ฌธ์žฅ ์ž…๋ ฅ",
61
+ placeholder="๋ฒˆ์—ญํ•  ์˜์–ด ๋ฌธ์žฅ์„ ์ž…๋ ฅํ•˜์„ธ์š”."
62
+ )
63
+
64
+ max_tokens_slider = gr.Slider(
65
+ minimum=1,
66
+ maximum=2048,
67
+ step=1,
68
+ value=256,
69
+ label="max_tokens"
70
+ )
71
+ temperature_slider = gr.Slider(
72
+ minimum=0,
73
+ maximum=2,
74
+ step=0.1,
75
+ value=1.0,
76
+ label="temperature"
77
+ )
78
+ top_p_slider = gr.Slider(
79
+ minimum=0,
80
+ maximum=1,
81
+ step=0.01,
82
+ value=1.0,
83
+ label="top_p"
84
+ )
85
+
86
  translate_button = gr.Button("๋ฒˆ์—ญํ•˜๊ธฐ")
87
 
88
  with gr.Column():
89
+ # ๊ฒฐ๊ณผ ์ถœ๋ ฅ ์˜์—ญ
90
+ output_text = gr.Textbox(
91
+ label="ํ•œ๊ตญ์–ด ๋ฒˆ์—ญ ๊ฒฐ๊ณผ",
92
+ interactive=False
93
+ )
94
 
95
+ # ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ ๋ฒˆ์—ญ ํ•จ์ˆ˜ ํ˜ธ์ถœ
96
  translate_button.click(
97
  fn=translate_english_to_korean,
98
+ inputs=[input_text, max_tokens_slider, temperature_slider, top_p_slider],
99
  outputs=output_text,
100
  )
101