Kims12 commited on
Commit
63f8d48
·
verified ·
1 Parent(s): 4eb05a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -37
app.py CHANGED
@@ -1,49 +1,43 @@
1
  import os
 
2
  import gradio as gr
3
- from transformers import AutoTokenizer, AutoModelForCausalLM
4
 
5
- # 환경 변수에서 Hugging Face 토큰을 가져옵니다.
6
- HUGGINGFACE_TOKEN = os.getenv("HUGGINGFACE_TOKEN")
7
 
8
- # 모델과 토크나이저를 로드합니다.
9
- model_id = "CohereForAI/c4ai-command-r-plus-08-2024"
10
- tokenizer = AutoTokenizer.from_pretrained(model_id, use_auth_token=HUGGINGFACE_TOKEN)
11
- model = AutoModelForCausalLM.from_pretrained(model_id, use_auth_token=HUGGINGFACE_TOKEN)
12
-
13
- def translate_code(english_code):
14
  """
15
- 영어 코드를 한국어로 번역하는 함수입니다.
16
  """
17
- # 메시지 포맷팅
18
- messages = [{"role": "user", "content": english_code}]
19
- input_ids = tokenizer.apply_chat_template(
20
- messages,
21
- tokenize=True,
22
- add_generation_prompt=True,
23
- return_tensors="pt"
24
- )
25
-
26
- # 텍스트 생
27
- gen_tokens = model.generate(
28
- input_ids,
29
- max_new_tokens=200,
30
- do_sample=True,
31
- temperature=0.3,
32
- )
33
-
34
- # 생성된 텍스트 디코딩
35
- gen_text = tokenizer.decode(gen_tokens[0], skip_special_tokens=True)
36
-
37
- # 번역된 한국어 텍스트 반환
38
- return gen_text
39
 
40
- # Gradio 인터페이스 설정
41
  iface = gr.Interface(
42
- fn=translate_code,
43
- inputs=gr.inputs.Textbox(lines=10, placeholder="영어 코드를 입력하세요..."),
44
  outputs=gr.outputs.Textbox(),
45
- title="코드 번역기",
46
- description="영어 작성된 코드를 한국어로 번역해드립니다."
 
 
 
 
 
47
  )
48
 
49
  if __name__ == "__main__":
 
1
  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 translate_to_korean(english_text):
 
 
 
 
 
9
  """
10
+ 입력된 영어 텍스트를 한국어로 번역하는 함수
11
  """
12
+ system_message = "You are a helpful assistant that translates English text to Korean."
13
+ try:
14
+ response = openai.ChatCompletion.create(
15
+ model="gpt-4o-mini", # 모델 ID
16
+ messages=[
17
+ {"role": "system", "content": system_message},
18
+ {"role": "user", "content": english_text},
19
+ ],
20
+ max_tokens=1000, # 필요한 경우 조정
21
+ temperature=0.3, # 창의 조절
22
+ top_p=1.0, # 확률 분포 조절
23
+ )
24
+ korean_text = response.choices[0].message['content'].strip()
25
+ return korean_text
26
+ except Exception as e:
27
+ return f"오류가 발생했습니다: {str(e)}"
 
 
 
 
 
 
28
 
29
+ # Gradio 인터페이스 구성
30
  iface = gr.Interface(
31
+ fn=translate_to_korean,
32
+ inputs=gr.inputs.Textbox(lines=10, placeholder="번역할 영어 텍스트를 입력하세요..."),
33
  outputs=gr.outputs.Textbox(),
34
+ title="영어-한국어 번역기",
35
+ description="GPT-4o-mini 모델을 사용하여 영어 텍스트를 한국어로 번역니다.",
36
+ examples=[
37
+ ["Hello, how are you?"],
38
+ ["This is a test sentence for translation."],
39
+ ["OpenAI provides powerful language models."]
40
+ ],
41
  )
42
 
43
  if __name__ == "__main__":