Spaces:
Sleeping
Sleeping
import gradio as gr | |
import openai | |
import os | |
def respond_chatgpt_qna( | |
question: str, | |
system_message: str, | |
max_tokens: int, | |
temperature: float, | |
top_p: float | |
): | |
""" | |
OpenAI의 gpt-4o-mini 모델을 이용해 질문에 대한 답변을 반환하는 함수. | |
""" | |
openai_token = os.getenv("OPENAI_TOKEN") | |
if not openai_token: | |
return "OpenAI API 토큰이 필요합니다." | |
openai.api_key = openai_token | |
messages = [ | |
{"role": "system", "content": system_message}, | |
{"role": "user", "content": question} | |
] | |
try: | |
response = openai.ChatCompletion.create( | |
model="gpt-4o-mini", | |
messages=messages, | |
max_tokens=max_tokens, | |
temperature=temperature, | |
top_p=top_p, | |
) | |
assistant_message = response.choices[0].message['content'] | |
return assistant_message | |
except Exception as e: | |
return f"오류가 발생했습니다: {str(e)}" | |
def merge_and_call(tone: str, ref1: str, ref2: str, ref3: str): | |
""" | |
사용자가 선택한 말투와 참조글들을 하나의 프롬프트로 합쳐 gpt-4o-mini 모델에 전달하는 함수. | |
""" | |
# 간단한 프롬프트 생성 | |
question = f"말투: {tone}\n참조글 1: {ref1}\n참조글 2: {ref2}\n참조글 3: {ref3}" | |
# 고급 설정은 코드 내부에 기본값으로 지정 (UI에는 노출되지 않음) | |
system_message = "아래의 참조글들을 참고하여 블로그 글을 생성하라." | |
max_tokens = 2000 | |
temperature = 0.7 | |
top_p = 0.95 | |
return respond_chatgpt_qna( | |
question=question, | |
system_message=system_message, | |
max_tokens=max_tokens, | |
temperature=temperature, | |
top_p=top_p | |
) | |
with gr.Blocks() as demo: | |
gr.Markdown("# 블로그 생성기") | |
# 입력 항목 구성 | |
tone_radio = gr.Radio( | |
choices=["친근하게", "일반적인", "전문적인"], | |
label="말투바꾸기", | |
value="일반적인" | |
) | |
ref1_text = gr.Textbox(label="참조글 1", lines=5) | |
ref2_text = gr.Textbox(label="참조글 2", lines=5) | |
ref3_text = gr.Textbox(label="참조글 3", lines=5) | |
answer_output = gr.Textbox(label="결과", lines=10, interactive=False) | |
submit_button = gr.Button("전송") | |
submit_button.click( | |
fn=merge_and_call, | |
inputs=[tone_radio, ref1_text, ref2_text, ref3_text], | |
outputs=answer_output | |
) | |
if __name__ == "__main__": | |
demo.launch() |