Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,49 +1,43 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
|
| 5 |
-
# 환경 변수에서
|
| 6 |
-
|
| 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 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 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=
|
| 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__":
|