File size: 4,241 Bytes
744eef2
 
 
 
 
895e085
744eef2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
895e085
 
744eef2
 
 
 
 
 
 
895e085
 
 
744eef2
895e085
 
 
744eef2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
895e085
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
744eef2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import gradio as gr
from huggingface_hub import InferenceClient
import os
import random
import logging
import openai  # OpenAI API๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์ถ”๊ฐ€

# ๋กœ๊น… ์„ค์ •
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": "gpt-4o-mini"  # GPT-4o Mini ๋ชจ๋ธ ์ถ”๊ฐ€
}

# HuggingFace ํ† ํฐ ์„ค์ •
hf_token = os.getenv("HF_TOKEN")
if not hf_token:
    raise ValueError("HF_TOKEN ํ™˜๊ฒฝ ๋ณ€์ˆ˜๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")

# OpenAI API ํด๋ผ์ด์–ธํŠธ ์„ค์ •
openai.api_key = os.getenv("OPENAI_API_KEY")

def call_hf_api(prompt, reference_text, max_tokens, temperature, top_p, model):
    if model == "gpt-4o-mini":
        return call_openai_api(prompt, max_tokens, temperature, top_p)
    
    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, max_tokens, temperature, top_p):
    try:
        response = openai.ChatCompletion.create(
            model="gpt-4o-mini",
            messages=[
                {"role": "user", "content": 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"OpenAI ์‘๋‹ต ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {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

# Gradio ์ธํ„ฐํŽ˜์ด์Šค ์„ค์ •
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)