File size: 6,554 Bytes
f9b9d56
83ee74c
574f73e
83ee74c
f9b9d56
574f73e
 
 
0997082
 
f2c0975
 
 
 
 
 
 
0997082
f9b9d56
0997082
 
f2c0975
 
 
0997082
f9b9d56
83ee74c
 
 
 
 
0997082
574f73e
0997082
 
 
 
 
 
83ee74c
 
 
 
 
 
 
 
 
f2c0975
 
 
 
0997082
 
 
 
 
 
 
 
83ee74c
 
0997082
f2c0975
 
 
83ee74c
0997082
 
83ee74c
f2c0975
 
 
 
 
0997082
 
f9b9d56
0997082
574f73e
0997082
574f73e
 
 
 
 
 
 
 
83ee74c
0997082
 
 
f9b9d56
0997082
 
f9b9d56
83ee74c
 
 
 
 
 
 
 
 
 
f9b9d56
0997082
83ee74c
 
 
0997082
f9b9d56
aca4005
f2c0975
aca4005
 
f2c0975
0997082
 
 
 
 
 
 
f9b9d56
0997082
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f9b9d56
0997082
 
 
f2c0975
0997082
 
 
 
f2c0975
 
0997082
 
 
 
f2c0975
 
0997082
 
 
 
 
f2c0975
 
 
0997082
 
 
 
 
 
 
 
 
 
 
f2c0975
 
 
 
 
 
 
 
 
 
 
 
 
 
0997082
 
 
 
 
 
 
 
 
 
 
 
 
 
f9b9d56
0997082
 
 
 
 
 
f9b9d56
 
574f73e
 
f2c0975
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import gradio as gr
from huggingface_hub import InferenceClient
import os
from typing import List, Tuple

# Hugging Face ํ† ํฐ ์„ค์ •
HF_TOKEN = os.getenv("HF_TOKEN")

# Available LLM models
LLM_MODELS = {
    "Mistral": "mistralai/Mistral-7B-Instruct-v0.2",
    "Zephyr": "HuggingFaceH4/zephyr-7b-beta",
    "OpenChat": "openchat/openchat-3.5",
    "Llama2": "meta-llama/Llama-2-7b-chat-hf",
    "Phi": "microsoft/phi-2",
    "Neural": "nvidia/neural-chat-7b-v3-1",
    "Starling": "HuggingFaceH4/starling-lm-7b-alpha"
}

# Default selected models
DEFAULT_MODELS = [
    "mistralai/Mistral-7B-Instruct-v0.2",
    "HuggingFaceH4/zephyr-7b-beta",
    "openchat/openchat-3.5"
]

# Initialize clients with token
clients = {
    model: InferenceClient(model, token=HF_TOKEN) 
    for model in LLM_MODELS.values()
}

def process_file(file) -> str:
    if file is None:
        return ""
    if file.name.endswith(('.txt', '.md')):
        return file.read().decode('utf-8')
    return f"Uploaded file: {file.name}"

def respond_single(
    client,
    message: str,
    history: List[Tuple[str, str]],
    system_message: str,
    max_tokens: int,
    temperature: float,
    top_p: float,
):
    system_prefix = """๋ฐ˜๋“œ์‹œ ํ•œ๊ธ€๋กœ ๋‹ต๋ณ€ํ• ๊ฒƒ. ๋„ˆ๋Š” ์ฃผ์–ด์ง„ ๋‚ด์šฉ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์ƒ์„ธํ•œ ์„ค๋ช…๊ณผ Q&A๋ฅผ ์ œ๊ณตํ•˜๋Š” ์—ญํ• ์ด๋‹ค. 
    ์•„์ฃผ ์นœ์ ˆํ•˜๊ณ  ์ž์„ธํ•˜๊ฒŒ ์„ค๋ช…ํ•˜๋ผ."""
    
    messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}]
    
    for user, assistant in history:
        if user:
            messages.append({"role": "user", "content": user})
        if assistant:
            messages.append({"role": "assistant", "content": assistant})
    
    messages.append({"role": "user", "content": message})
    
    response = ""
    try:
        for msg in client.chat_completion(
            messages,
            max_tokens=max_tokens,
            stream=True,
            temperature=temperature,
            top_p=top_p,
        ):
            if hasattr(msg.choices[0].delta, 'content'):
                token = msg.choices[0].delta.content
                if token is not None:
                    response += token
                    yield response
    except Exception as e:
        yield f"Error: {str(e)}"

def respond_all(
    message: str,
    file,
    history1: List[Tuple[str, str]],
    history2: List[Tuple[str, str]],
    history3: List[Tuple[str, str]],
    selected_models: List[str],
    system_message: str,
    max_tokens: int,
    temperature: float,
    top_p: float,
):
    if file:
        file_content = process_file(file)
        message = f"{message}\n\nFile content:\n{file_content}"

    while len(selected_models) < 3:
        selected_models.append(selected_models[-1])

    def generate(client, history):
        return respond_single(
            client,
            message,
            history,
            system_message,
            max_tokens,
            temperature,
            top_p,
        )

    return (
        generate(clients[selected_models[0]], history1),
        generate(clients[selected_models[1]], history2),
        generate(clients[selected_models[2]], history3),
    )

css = """
footer {visibility: hidden}
"""

with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css) as demo:
    with gr.Row():
        model_choices = gr.Checkboxgroup(
            choices=list(LLM_MODELS.values()),
            value=DEFAULT_MODELS,
            label="Select Models (Choose up to 3)",
            interactive=True
        )

    with gr.Row():
        with gr.Column():
            chat1 = gr.ChatInterface(
                lambda message, history: None,
                chatbot=gr.Chatbot(height=400, label="Chat 1"),
                textbox=False,
            )
        with gr.Column():
            chat2 = gr.ChatInterface(
                lambda message, history: None,
                chatbot=gr.Chatbot(height=400, label="Chat 2"),
                textbox=False,
            )
        with gr.Column():
            chat3 = gr.ChatInterface(
                lambda message, history: None,
                chatbot=gr.Chatbot(height=400, label="Chat 3"),
                textbox=False,
            )

    with gr.Row():
        with gr.Column():
            system_message = gr.Textbox(
                value="๋‹น์‹ ์€ ์นœ์ ˆํ•œ AI ์–ด์‹œ์Šคํ„ดํŠธ์ž…๋‹ˆ๋‹ค.",
                label="System message"
            )
            max_tokens = gr.Slider(
                minimum=1,
                maximum=8000,
                value=4000,
                step=1,
                label="Max new tokens"
            )
            temperature = gr.Slider(
                minimum=0,
                maximum=1,
                value=0.7,
                step=0.1,
                label="Temperature"
            )
            top_p = gr.Slider(
                minimum=0,
                maximum=1,
                value=0.9,
                step=0.05,
                label="Top-p"
            )
            
    with gr.Row():
        file_input = gr.File(label="Upload File (optional)")
        msg_input = gr.Textbox(
            show_label=False,
            placeholder="Enter text and press enter",
            container=False
        )

    examples = [
        ["์ƒ์„ธํ•œ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์„ ๋งˆ์น˜ ํ™”๋ฉด์„ ๋ณด๋ฉด์„œ ์„ค๋ช…ํ•˜๋“ฏ์ด 4000 ํ† ํฐ ์ด์ƒ ์ž์„ธํžˆ ์„ค๋ช…ํ•˜๋ผ"],
        ["FAQ 20๊ฑด์„ ์ƒ์„ธํ•˜๊ฒŒ ์ž‘์„ฑํ•˜๋ผ. 4000ํ† ํฐ ์ด์ƒ ์‚ฌ์šฉํ•˜๋ผ."],
        ["์‚ฌ์šฉ ๋ฐฉ๋ฒ•๊ณผ ์ฐจ๋ณ„์ , ํŠน์ง•, ๊ฐ•์ ์„ ์ค‘์‹ฌ์œผ๋กœ 4000 ํ† ํฐ ์ด์ƒ ์œ ํŠœ๋ธŒ ์˜์ƒ ์Šคํฌ๋ฆฝํŠธ ํ˜•ํƒœ๋กœ ์ž‘์„ฑํ•˜๋ผ"],
        ["๋ณธ ์„œ๋น„์Šค๋ฅผ SEO ์ตœ์ ํ™”ํ•˜์—ฌ ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ๋กœ 4000 ํ† ํฐ ์ด์ƒ ์ž‘์„ฑํ•˜๋ผ"],
        ["๊ณ„์† ์ด์–ด์„œ ๋‹ต๋ณ€ํ•˜๋ผ"],
    ]
    
    gr.Examples(
        examples=examples,
        inputs=msg_input,
        cache_examples=False
    )
        
    def submit_message(message, file):
        return respond_all(
            message,
            file,
            chat1.chatbot.value,
            chat2.chatbot.value,
            chat3.chatbot.value,
            model_choices.value,
            system_message.value,
            max_tokens.value,
            temperature.value,
            top_p.value,
        )

    msg_input.submit(
        submit_message,
        [msg_input, file_input],
        [chat1.chatbot, chat2.chatbot, chat3.chatbot],
        api_name="submit"
    )

if __name__ == "__main__":
    if not HF_TOKEN:
        print("Warning: HF_TOKEN environment variable is not set")
    demo.launch()