Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from huggingface_hub import InferenceClient | |
| import openai | |
| ############################# | |
| # [기본코드] - 수정/삭제 불가 | |
| ############################# | |
| # 제거할 모델들을 MODELS 사전에서 제외 | |
| MODELS = { | |
| "Zephyr 7B Beta": "HuggingFaceH4/zephyr-7b-beta", | |
| "Meta Llama 3.1 8B": "meta-llama/Meta-Llama-3.1-8B-Instruct", | |
| "Meta-Llama 3.1 70B-Instruct": "meta-llama/Meta-Llama-3.1-70B-Instruct", | |
| "Microsoft": "microsoft/Phi-3-mini-4k-instruct", | |
| "Mixtral 8x7B": "mistralai/Mistral-7B-Instruct-v0.3", | |
| "Mixtral Nous-Hermes": "NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO", | |
| "Aya-23-35B": "CohereForAI/aya-23-35B" | |
| } | |
| # Cohere Command R+ 모델 ID 정의 | |
| COHERE_MODEL = "CohereForAI/c4ai-command-r-plus-08-2024" | |
| def get_client(model_name, hf_token): | |
| """ | |
| 모델 이름에 맞춰 InferenceClient 생성. | |
| hf_token을 UI에서 입력받은 값으로 사용하도록 변경. | |
| """ | |
| if not hf_token: | |
| raise ValueError("HuggingFace API 토큰이 필요합니다.") | |
| if model_name in MODELS: | |
| model_id = MODELS[model_name] | |
| elif model_name == "Cohere Command R+": | |
| model_id = COHERE_MODEL | |
| else: | |
| raise ValueError("유효하지 않은 모델 이름입니다.") | |
| return InferenceClient(model_id, token=hf_token) | |
| def respond_hf_qna( | |
| question: str, | |
| model_name: str, | |
| max_tokens: int, | |
| temperature: float, | |
| top_p: float, | |
| system_message: str, | |
| hf_token: str | |
| ): | |
| """ | |
| HuggingFace 모델(Zephyr 등)에 대해 한 번의 질문(question)에 대한 답변을 반환하는 함수. | |
| """ | |
| try: | |
| client = get_client(model_name, hf_token) | |
| except ValueError as e: | |
| return f"오류: {str(e)}" | |
| # 시스템 메시지 + 유저 질문을 한 번만 전달 | |
| messages = [ | |
| {"role": "system", "content": system_message}, | |
| {"role": "user", "content": question} | |
| ] | |
| try: | |
| # 스트리밍 대신 전체 답변(비스트리밍) 호출 | |
| response = client.chat_completion( | |
| messages, | |
| max_tokens=max_tokens, | |
| temperature=temperature, | |
| top_p=top_p, | |
| stream=False, # 스트리밍 비활성화 | |
| ) | |
| assistant_message = response.choices[0].message.content | |
| return assistant_message | |
| except Exception as e: | |
| return f"오류가 발생했습니다: {str(e)}" | |
| def respond_cohere_qna( | |
| question: str, | |
| system_message: str, | |
| max_tokens: int, | |
| temperature: float, | |
| top_p: float, | |
| hf_token: str | |
| ): | |
| """ | |
| Cohere Command R+ 모델을 이용해 한 번의 질문(question)에 대한 답변을 반환하는 함수. | |
| """ | |
| model_name = "Cohere Command R+" | |
| try: | |
| client = get_client(model_name, hf_token) | |
| except ValueError as e: | |
| return f"오류: {str(e)}" | |
| messages = [ | |
| {"role": "system", "content": system_message}, | |
| {"role": "user", "content": question} | |
| ] | |
| try: | |
| response_full = client.chat_completion( | |
| messages, | |
| max_tokens=max_tokens, | |
| temperature=temperature, | |
| top_p=top_p, | |
| ) | |
| assistant_message = response_full.choices[0].message.content | |
| return assistant_message | |
| except Exception as e: | |
| return f"오류가 발생했습니다: {str(e)}" | |
| def respond_chatgpt_qna( | |
| question: str, | |
| system_message: str, | |
| max_tokens: int, | |
| temperature: float, | |
| top_p: float, | |
| openai_token: str | |
| ): | |
| """ | |
| ChatGPT(OpenAI) 모델을 이용해 한 번의 질문(question)에 대한 답변을 반환하는 함수. | |
| """ | |
| 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)}" | |
| ############################# | |
| # [기본코드] UI 부분 - 수정/삭제 불가 | |
| ############################# | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# Prompting AI - 일반 문답형 데모") | |
| gr.Markdown("언어모델별 문답형 테스트 데모입니다. 한 번에 한 질문씩만 주고받습니다.") | |
| with gr.Row(): | |
| hf_token_box = gr.Textbox( | |
| label="HuggingFace 토큰 (비공개)", | |
| type="password", | |
| placeholder="HuggingFace API 토큰을 입력하세요..." | |
| ) | |
| openai_token_box = gr.Textbox( | |
| label="OpenAI 토큰 (비공개)", | |
| type="password", | |
| placeholder="OpenAI API 토큰을 입력하세요..." | |
| ) | |
| # --- Tab: 일반 모델 --- | |
| with gr.Tab("일반 모델"): | |
| model_name = gr.Radio( | |
| choices=list(MODELS.keys()), | |
| label="Language Model (HuggingFace)", | |
| value="Zephyr 7B Beta" | |
| ) | |
| max_tokens = gr.Slider(minimum=0, maximum=2000, value=500, step=100, label="Max Tokens") | |
| temperature = gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.05, label="Temperature") | |
| top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p") | |
| system_message = gr.Textbox( | |
| value="""반드시 한글로 답변할 것. | |
| 너는 최고의 비서이다. | |
| 내가 요구하는것들을 최대한 자세하고 정확하게 답변하라. | |
| """, | |
| label="System Message", | |
| lines=3 | |
| ) | |
| question_input = gr.Textbox(label="질문을 입력하세요") | |
| answer_output = gr.Textbox(label="답변", interactive=False) | |
| submit_button = gr.Button("전송") | |
| submit_button.click( | |
| fn=respond_hf_qna, | |
| inputs=[ | |
| question_input, | |
| model_name, | |
| max_tokens, | |
| temperature, | |
| top_p, | |
| system_message, | |
| hf_token_box | |
| ], | |
| outputs=answer_output | |
| ) | |
| # --- Tab: Cohere Command R+ --- | |
| with gr.Tab("Cohere Command R+"): | |
| cohere_system_message = gr.Textbox( | |
| value="""반드시 한글로 답변할 것. | |
| 너는 최고의 비서이다. | |
| 내가 요구하는것들을 최대한 자세하고 정확하게 답변하라. | |
| """, | |
| label="System Message", | |
| lines=3 | |
| ) | |
| cohere_max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max Tokens") | |
| cohere_temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature") | |
| cohere_top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-P") | |
| cohere_question_input = gr.Textbox(label="질문을 입력하세요") | |
| cohere_answer_output = gr.Textbox(label="답변", interactive=False) | |
| cohere_submit_button = gr.Button("전송") | |
| cohere_submit_button.click( | |
| fn=respond_cohere_qna, | |
| inputs=[ | |
| cohere_question_input, | |
| cohere_system_message, | |
| cohere_max_tokens, | |
| cohere_temperature, | |
| cohere_top_p, | |
| hf_token_box | |
| ], | |
| outputs=cohere_answer_output | |
| ) | |
| # --- Tab: ChatGPT --- | |
| with gr.Tab("ChatGPT"): | |
| chatgpt_system_message = gr.Textbox( | |
| value="""반드시 한글로 답변할 것. | |
| 너는 ChatGPT, OpenAI에서 개발한 언어 모델이다. | |
| 내가 요구하는 것을 최대한 자세하고 정확하게 답변하라. | |
| """, | |
| label="System Message", | |
| lines=3 | |
| ) | |
| chatgpt_max_tokens = gr.Slider(minimum=1, maximum=4096, value=1024, step=1, label="Max Tokens") | |
| chatgpt_temperature = gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.05, label="Temperature") | |
| chatgpt_top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-P") | |
| chatgpt_question_input = gr.Textbox(label="질문을 입력하세요") | |
| chatgpt_answer_output = gr.Textbox(label="답변", interactive=False) | |
| chatgpt_submit_button = gr.Button("전송") | |
| chatgpt_submit_button.click( | |
| fn=respond_chatgpt_qna, | |
| inputs=[ | |
| chatgpt_question_input, | |
| chatgpt_system_message, | |
| chatgpt_max_tokens, | |
| chatgpt_temperature, | |
| chatgpt_top_p, | |
| openai_token_box | |
| ], | |
| outputs=chatgpt_answer_output | |
| ) | |
| ################################################# | |
| # [클로드 플레이그라운드] - [참조코드] 일부 발췌 | |
| ################################################# | |
| with gr.Tab("클로드 플레이그라운드"): | |
| gr.Markdown("클로드 모델과의 간단한 플레이그라운드입니다.") | |
| # Claude용 토큰 | |
| claude_token_box = gr.Textbox( | |
| label="Claude 토큰 (비공개)", | |
| type="password", | |
| placeholder="Claude API 토큰을 입력하세요..." | |
| ) | |
| claude_system_message = gr.Textbox( | |
| label="System Message", | |
| value="반드시 한글로 답변할 것.\n너는 Anthropic에서 개발한 클로드이다.\n최대한 정확하고 친절하게 답변하라.\n", | |
| lines=3 | |
| ) | |
| claude_max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max Tokens") | |
| claude_temperature = gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.05, label="Temperature") | |
| claude_top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p") | |
| claude_question_input = gr.Textbox(label="질문을 입력하세요") | |
| claude_answer_output = gr.Textbox(label="답변", interactive=False) | |
| claude_submit_button = gr.Button("전송") | |
| # Claude 응답 함수 (최소 기능만 사용) | |
| def respond_claude_qna( | |
| question: str, | |
| system_message: str, | |
| max_tokens: int, | |
| temperature: float, | |
| top_p: float, | |
| claude_api_key: str | |
| ): | |
| """ | |
| [참조코드]에서 최소한의 클로드 호출 부분만 발췌한 함수. | |
| 플레이그라운드 형태로 한 번의 질문에 대한 답변을 반환. | |
| """ | |
| import anthropic | |
| # 토큰이 없으면 에러 | |
| if not claude_api_key: | |
| return "Claude API 토큰이 필요합니다." | |
| try: | |
| claude_client = anthropic.Anthropic(api_key=claude_api_key) | |
| # 클로드에게 보낼 프롬프트 구성 | |
| # Anthropic 공식 예시를 참고하여 HUMAN_PROMPT / AI_PROMPT 사용 | |
| prompt = ( | |
| f"{anthropic.HUMAN_PROMPT}\n" | |
| f"{system_message}\n\n" | |
| f"질문: {question}\n" | |
| f"{anthropic.AI_PROMPT}" | |
| ) | |
| response = claude_client.completions.create( | |
| model="claude-3-haiku-20240307", | |
| max_tokens_to_sample=max_tokens, | |
| temperature=temperature, | |
| top_p=top_p, | |
| prompt=prompt | |
| ) | |
| return response.completion.strip() | |
| except Exception as e: | |
| return f"오류가 발생했습니다: {str(e)}" | |
| claude_submit_button.click( | |
| fn=respond_claude_qna, | |
| inputs=[ | |
| claude_question_input, | |
| claude_system_message, | |
| claude_max_tokens, | |
| claude_temperature, | |
| claude_top_p, | |
| claude_token_box | |
| ], | |
| outputs=claude_answer_output | |
| ) | |
| ############################# | |
| # 메인 실행부 | |
| ############################# | |
| if __name__ == "__main__": | |
| demo.launch() | |