|
import gradio as gr |
|
from huggingface_hub import InferenceClient |
|
import openai |
|
import os |
|
import random |
|
import logging |
|
|
|
|
|
logging.basicConfig(filename='language_model_playground.log', level=logging.DEBUG, |
|
format='%(asctime)s - %(levelname)s - %(message)s') |
|
|
|
|
|
MODELS = { |
|
"Zephyr 7B Beta": "HuggingFaceH4/zephyr-7b-beta", |
|
"DeepSeek Coder V2": "deepseek-ai/DeepSeek-Coder-V2-Instruct", |
|
"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", |
|
"Cohere Command R+": "CohereForAI/c4ai-command-r-plus", |
|
"Aya-23-35B": "CohereForAI/aya-23-35B", |
|
"GPT-4o Mini": "openai/gpt-4o-mini" |
|
} |
|
|
|
|
|
hf_token = os.getenv("HF_TOKEN") |
|
if not hf_token: |
|
raise ValueError("HF_TOKEN νκ²½ λ³μκ° μ€μ λμ§ μμμ΅λλ€.") |
|
|
|
|
|
openai.api_key = os.getenv("OPENAI_API_KEY") |
|
if not openai.api_key: |
|
raise ValueError("OPENAI_API_KEY νκ²½ λ³μκ° μ€μ λμ§ μμμ΅λλ€.") |
|
|
|
def call_hf_api(prompt, reference_text, max_tokens, temperature, top_p, model): |
|
if model == "openai/gpt-4o-mini": |
|
return call_openai_api(prompt, reference_text, max_tokens, temperature, top_p) |
|
else: |
|
client = InferenceClient(model=model, token=hf_token) |
|
combined_prompt = f"{prompt}\n\nμ°Έκ³ ν
μ€νΈ:\n{reference_text}" |
|
random_seed = random.randint(0, 1000000) |
|
|
|
try: |
|
response = client.text_generation( |
|
combined_prompt, |
|
max_new_tokens=max_tokens, |
|
temperature=temperature, |
|
top_p=top_p, |
|
seed=random_seed |
|
) |
|
return response |
|
except Exception as e: |
|
logging.error(f"HuggingFace API νΈμΆ μ€ μ€λ₯ λ°μ: {str(e)}") |
|
return f"μλ΅ μμ± μ€ μ€λ₯ λ°μ: {str(e)}. λμ€μ λ€μ μλν΄ μ£ΌμΈμ." |
|
|
|
def call_openai_api(prompt, reference_text, max_tokens, temperature, top_p): |
|
system_message = "You are a helpful assistant." |
|
combined_prompt = f"{prompt}\n\nμ°Έκ³ ν
μ€νΈ:\n{reference_text}" |
|
|
|
try: |
|
response = openai.ChatCompletion.create( |
|
model="gpt-4o-mini", |
|
messages=[ |
|
{"role": "system", "content": system_message}, |
|
{"role": "user", "content": combined_prompt}, |
|
], |
|
max_tokens=max_tokens, |
|
temperature=temperature, |
|
top_p=top_p, |
|
) |
|
return response.choices[0].message['content'] |
|
except Exception as e: |
|
logging.error(f"OpenAI API νΈμΆ μ€ μ€λ₯ λ°μ: {str(e)}") |
|
return f"μλ΅ μμ± μ€ μ€λ₯ λ°μ: {str(e)}. λμ€μ λ€μ μλν΄ μ£ΌμΈμ." |
|
|
|
def generate_response(prompt, reference_text, max_tokens, temperature, top_p, model): |
|
response = call_hf_api(prompt, reference_text, max_tokens, temperature, top_p, MODELS[model]) |
|
response_html = f""" |
|
<h3>μμ±λ μλ΅:</h3> |
|
<div style='max-height: 500px; overflow-y: auto; white-space: pre-wrap; word-wrap: break-word;'> |
|
{response} |
|
</div> |
|
""" |
|
return response_html |
|
|
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("## μΈμ΄ λͺ¨λΈ ν둬ννΈ νλ μ΄κ·ΈλΌμ΄λ") |
|
|
|
with gr.Column(): |
|
model_radio = gr.Radio(choices=list(MODELS.keys()), value="Zephyr 7B Beta", label="μΈμ΄ λͺ¨λΈ μ ν") |
|
prompt_input = gr.Textbox(label="ν둬ννΈ μ
λ ₯", lines=5) |
|
reference_text_input = gr.Textbox(label="μ°Έκ³ ν
μ€νΈ μ
λ ₯", lines=5) |
|
|
|
with gr.Row(): |
|
max_tokens_slider = gr.Slider(minimum=0, maximum=5000, value=2000, step=100, label="μ΅λ ν ν° μ") |
|
temperature_slider = gr.Slider(minimum=0, maximum=1, value=0.75, step=0.05, label="μ¨λ") |
|
top_p_slider = gr.Slider(minimum=0, maximum=1, value=0.95, step=0.05, label="Top P") |
|
|
|
generate_button = gr.Button("μλ΅ μμ±") |
|
response_output = gr.HTML(label="μμ±λ μλ΅") |
|
|
|
|
|
generate_button.click( |
|
generate_response, |
|
inputs=[prompt_input, reference_text_input, max_tokens_slider, temperature_slider, top_p_slider, model_radio], |
|
outputs=response_output |
|
) |
|
|
|
|
|
demo.launch(share=True) |
|
|