File size: 3,895 Bytes
7ec8374
769836d
7ec8374
769836d
 
bacaeb4
c6c15ce
769836d
 
 
 
 
 
 
 
 
1cb0a98
c6c15ce
 
769836d
1cb0a98
 
 
 
 
769836d
1cb0a98
 
 
 
8cdd6a2
1cb0a98
 
 
 
 
 
 
 
 
769836d
8cdd6a2
 
 
 
 
 
 
 
769836d
 
7ec8374
f669ed3
769836d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ec8374
 
 
 
 
 
 
c6c15ce
7ec8374
 
 
 
 
 
c6c15ce
769836d
 
 
 
 
 
 
 
 
c6c15ce
769836d
 
c6c15ce
c5dc213
c6c15ce
769836d
 
 
c6c15ce
 
 
 
c5dc213
c6c15ce
c5dc213
c6c15ce
 
 
 
 
 
 
 
 
c5dc213
c6c15ce
 
c5dc213
c6c15ce
 
7ec8374
 
 
769836d
7ec8374
769836d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ec8374
 
 
 
 
 
 
c6c15ce
 
 
769836d
7ec8374
 
 
769836d
 
7ec8374
 
769836d
 
1cb0a98
 
 
 
 
c6c15ce
1cb0a98
769836d
 
1cb0a98
769836d
7ec8374
1cb0a98
c6c15ce
1cb0a98
 
 
7ec8374
 
c5dc213
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
import gradio as gr
import spaces
from huggingface_hub import InferenceClient
from transformers import pipeline

LOCAL_MODEL = "Qwen/Qwen3-0.6B"
REMOTE_MODEL = "openai/gpt-oss-20b"

pipe = pipeline(
    "text-generation",
    model=LOCAL_MODEL,
    dtype="auto",
    device="cuda",
)

fancy_css = """
.gradio-container {
    width: 96% !important;
    max-width: none !important;
}
#app-title {
    text-align: center;
    margin-bottom: 4px;
}
#app-subtitle {
    text-align: center;
    color: var(--body-text-color-subdued);
    margin-bottom: 24px;
}
#chat-container {
    width: 100%;
    border: 1px solid var(--border-color-primary);
    border-radius: 12px;
    padding: 16px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
}
#model-note {
    font-size: 0.9em;
    color: var(--body-text-color-subdued);
    margin-top: 8px;
}
@media (max-width: 768px) {
    .gradio-container {
        width: 98% !important;
    }
    #chat-container {
        padding: 8px;
    }
}
"""


@spaces.GPU
def local_generate(
    messages,
    max_tokens,
    temperature,
    top_p,
):
    outputs = pipe(
        messages,
        max_new_tokens=max_tokens,
        do_sample=True,
        temperature=temperature,
        top_p=top_p,
    )

    return outputs[0]["generated_text"][-1]["content"]


def respond(
    message,
    history: list[dict[str, str]],
    system_message,
    max_tokens,
    temperature,
    top_p,
    use_local_model,
    hf_token: gr.OAuthToken,
):
    messages = [{"role": "system", "content": system_message}]
    messages.extend(history)
    messages.append({"role": "user", "content": message})

    if use_local_model:
        print("[MODE] local")

        response = local_generate(
            messages,
            max_tokens,
            temperature,
            top_p,
        )

        yield response
        return

    print("[MODE] api")

    if hf_token is None or not getattr(hf_token, "token", None):
        yield "⚠️ Please log in with your Hugging Face account first."
        return

    client = InferenceClient(
        token=hf_token.token,
        model=REMOTE_MODEL,
    )

    response = ""

    for chunk in client.chat_completion(
        messages,
        max_tokens=max_tokens,
        stream=True,
        temperature=temperature,
        top_p=top_p,
    ):
        choices = chunk.choices
        token = ""

        if len(choices) and choices[0].delta.content:
            token = choices[0].delta.content

        response += token
        yield response


chatbot = gr.ChatInterface(
    fn=respond,
    additional_inputs=[
        gr.Textbox(
            value="You are a friendly Chatbot.",
            label="System message",
        ),
        gr.Slider(
            minimum=1,
            maximum=2048,
            value=512,
            step=1,
            label="Max new tokens",
        ),
        gr.Slider(
            minimum=0.1,
            maximum=2.0,
            value=0.7,
            step=0.1,
            label="Temperature",
        ),
        gr.Slider(
            minimum=0.1,
            maximum=1.0,
            value=0.95,
            step=0.05,
            label="Top-p (nucleus sampling)",
        ),
        gr.Checkbox(
            label="Use Local Model",
            value=False,
        ),
    ],
)


with gr.Blocks(css=fancy_css) as demo:
    with gr.Sidebar():
        gr.LoginButton()

    gr.Markdown(
        "# 🌟 Fancy AI Chatbot",
        elem_id="app-title",
    )

    gr.Markdown(
        "A fancier version of the standard Huggging Face chatbot template.",
        elem_id="app-subtitle",
    )

    with gr.Column(elem_id="chat-container"):
        chatbot.render()

        gr.Markdown(
            "Use **Additional inputs** to switch between the API model and the locally executed model.",
            elem_id="model-note",
        )


if __name__ == "__main__":
    demo.launch()