CSB261 commited on
Commit
0424bd2
ยท
verified ยท
1 Parent(s): f5ff822

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -17
app.py CHANGED
@@ -2,12 +2,15 @@ import os
2
  import openai
3
  import gradio as gr
4
 
5
- # OpenAI API ํด๋ผ์ด์–ธํŠธ ์„ค์ •
6
  openai.api_key = os.getenv("OPENAI_API_KEY")
7
 
8
- def call_api(content, system_message, max_tokens=1000, temperature=0.5, top_p=1.0):
 
 
 
9
  response = openai.ChatCompletion.create(
10
- model="gpt-4o-mini", # ๋˜๋Š” ๋‹ค๋ฅธ ๋ชจ๋ธ ID ์‚ฌ์šฉ
11
  messages=[
12
  {"role": "system", "content": system_message},
13
  {"role": "user", "content": content},
@@ -18,20 +21,29 @@ def call_api(content, system_message, max_tokens=1000, temperature=0.5, top_p=1.
18
  )
19
  return response.choices[0].message['content']
20
 
21
- def translate_code(english_code):
22
- system_message = "You are a helpful assistant that translates code comments and documentation from English to Korean."
23
- user_content = f"Please translate the following code from English to Korean:\n\n{english_code}"
24
- korean_translation = call_api(user_content, system_message)
25
- return korean_translation
 
 
 
 
 
26
 
27
- # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์„ค์ •
28
- iface = gr.Interface(
29
- fn=translate_code,
30
- inputs=gr.Textbox(lines=20, placeholder="๋ฒˆ์—ญํ•  ์˜์–ด ์ฝ”๋“œ๋ฅผ ์—ฌ๊ธฐ์— ์ž…๋ ฅํ•˜์„ธ์š”."),
31
- outputs=gr.Textbox(),
32
- title="์ฝ”๋“œ ์˜์–ด-ํ•œ๊ตญ์–ด ๋ฒˆ์—ญ๊ธฐ",
33
- description="์˜์–ด๋กœ ์ž‘์„ฑ๋œ ์ฝ”๋“œ๋ฅผ ํ•œ๊ตญ์–ด๋กœ ๋ฒˆ์—ญํ•ฉ๋‹ˆ๋‹ค. ๋ฒˆ์—ญํ•  ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ๋ฒˆ์—ญ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜์„ธ์š”."
34
- )
 
 
 
 
35
 
36
  if __name__ == "__main__":
37
- iface.launch()
 
2
  import openai
3
  import gradio as gr
4
 
5
+ # ํ™˜๊ฒฝ ๋ณ€์ˆ˜์— ์ €์žฅ๋œ API ํ‚ค ์‚ฌ์šฉ
6
  openai.api_key = os.getenv("OPENAI_API_KEY")
7
 
8
+ def call_api(content, system_message, max_tokens=1024, temperature=0.2, top_p=1.0):
9
+ """
10
+ gpt-4o-mini ๋ชจ๋ธ์— ์‚ฌ์šฉ์ž ์ž…๋ ฅ์„ ๋ณด๋‚ด๊ณ  ๋ฒˆ์—ญ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ›์•„์˜จ๋‹ค.
11
+ """
12
  response = openai.ChatCompletion.create(
13
+ model="gpt-4o-mini",
14
  messages=[
15
  {"role": "system", "content": system_message},
16
  {"role": "user", "content": content},
 
21
  )
22
  return response.choices[0].message['content']
23
 
24
+ def translate_english_to_korean(code_text):
25
+ """
26
+ ์˜์–ด๋กœ ์ž‘์„ฑ๋œ ์ฝ”๋“œ๋‚˜ ์„ค๋ช…์„ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ํ•œ๊ตญ์–ด๋กœ ์˜ฎ๊ธด๋‹ค.
27
+ """
28
+ system_msg = (
29
+ "You are a professional translator who accurately conveys English code "
30
+ "comments and explanations into smooth, natural Korean. Preserve code syntax "
31
+ "while ensuring natural readability."
32
+ )
33
+ return call_api(code_text, system_msg)
34
 
35
+ def main():
36
+ """
37
+ Gradio ์ธํ„ฐํŽ˜์ด์Šค ์‹คํ–‰
38
+ """
39
+ interface = gr.Interface(
40
+ fn=translate_english_to_korean,
41
+ inputs=gr.Textbox(lines=10, label="์˜์–ด ์ฝ”๋“œ ์ž…๋ ฅ"),
42
+ outputs=gr.Textbox(lines=10, label="๋ฒˆ์—ญ ๊ฒฐ๊ณผ"),
43
+ title="์ฝ”๋“œ ์˜์–ด -> ํ•œ๊ตญ์–ด ๋ฒˆ์—ญ๊ธฐ",
44
+ description="์˜์–ด๋กœ ์ž‘์„ฑ๋œ ์ฝ”๋“œ๋ฅผ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ํ•œ๊ตญ์–ด๋กœ ์˜ฎ๊ฒจ์ฃผ๋Š” ๊ฐ„๋‹จํ•œ ๋„๊ตฌ์ž…๋‹ˆ๋‹ค."
45
+ )
46
+ interface.launch(server_name="0.0.0.0", server_port=7860)
47
 
48
  if __name__ == "__main__":
49
+ main()