Sangwon78 commited on
Commit
2da52c5
ยท
verified ยท
1 Parent(s): 77a177b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -0
app.py CHANGED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ 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",
34
+ "content": (
35
+ "You are a helpful assistant that translates English text to Korean. "
36
+ "Please provide a natural, high-quality translation."
37
+ ),
38
+ },
39
+ {"role": "user", "content": text},
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
+
72
+ return demo
73
+
74
+ if __name__ == "__main__":
75
+ demo = build_app()
76
+ demo.launch()