import os import gradio as gr import openai # OpenAI API 키 설정 openai.api_key = os.getenv("OPENAI_API_KEY") # OpenAI API 호출 함수 def call_api(content, system_message, max_tokens=100, temperature=0.7, top_p=1.0): response = openai.ChatCompletion.create( model="gpt-4", # 모델 변경: gpt-4 messages=[ {"role": "system", "content": system_message}, {"role": "user", "content": content}, ], max_tokens=max_tokens, temperature=temperature, top_p=top_p, ) return response.choices[0].message['content'] # 번역 함수 def translate_text(input_text): # 입력 텍스트의 언어를 감지하여 시스템 메시지를 동적으로 설정 if any('가' <= char <= '힣' for char in input_text): # 한글이 포함되어 있는 경우 system_message = "Translate the following Korean text to English. Output only the translated text, without any additional context or greetings." else: # 영어로 작성된 경우 system_message = "Translate the following English text to Korean. Output only the translated text, without any additional context or greetings." try: result = call_api(content=input_text, system_message=system_message) return result.strip() except Exception as e: return f"오류 발생: {str(e)}" # Gradio 인터페이스 설정 def gradio_interface(input_text): translated_text = translate_text(input_text) return translated_text # Gradio 앱 생성 iface = gr.Interface( fn=gradio_interface, inputs=gr.Textbox(label="텍스트 입력", lines=2, placeholder="번역할 텍스트를 입력하세요."), outputs=gr.Textbox(label="번역된 텍스트", lines=2), title="언어 번역기", description="gpt-4 모델을 활용하여 입력된 텍스트를 한국어 또는 영어로 번역합니다. 번역된 텍스트만 출력됩니다.", ) if __name__ == "__main__": iface.launch()