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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
import gradio as gr
from huggingface_hub import InferenceClient

# Custom CSS for cool animations and modern design
custom_css = """
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap');

:root {
    --primary: #6366f1;
    --primary-dark: #4f46e5;
    --secondary: #ec4899;
    --background: #0f172a;
    --surface: #1e293b;
    --text: #f1f5f9;
    --text-secondary: #94a3b8;
}

body {
    font-family: 'Poppins', sans-serif;
    background: linear-gradient(135deg, #0f172a 0%, #1e1b4b 100%);
    margin: 0;
    padding: 0;
}

.gradio-container {
    background: transparent !important;
}

.dark .block {
    background: var(--surface) !important;
    border: 1px solid #334155 !important;
    border-radius: 16px !important;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3) !important;
}

.sidebar {
    background: linear-gradient(180deg, var(--surface) 0%, rgba(30, 41, 59, 0.9) 100%) !important;
    border-right: 1px solid #334155 !important;
}

.chatbot {
    background: linear-gradient(135deg, var(--surface) 0%, #1e293b 100%) !important;
    border: 1px solid #334155 !important;
    border-radius: 16px !important;
    min-height: 500px;
}

.message.user {
    background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%) !important;
    color: white !important;
    border-radius: 18px 18px 4px 18px !important;
    margin: 8px 0 !important;
    padding: 12px 16px !important;
    animation: slideInRight 0.3s ease-out;
}

.message.bot {
    background: linear-gradient(135deg, #374151 0%, #4b5563 100%) !important;
    color: var(--text) !important;
    border-radius: 18px 18px 18px 4px !important;
    margin: 8px 0 !important;
    padding: 12px 16px !important;
    animation: slideInLeft 0.3s ease-out;
}

.textbox {
    border-radius: 12px !important;
    border: 1px solid #334155 !important;
    background: var(--surface) !important;
    color: var(--text) !important;
}

.button {
    background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%) !important;
    border: none !important;
    border-radius: 12px !important;
    color: white !important;
    font-weight: 500 !important;
    transition: all 0.3s ease !important;
}

.button:hover {
    transform: translateY(-2px) !important;
    box-shadow: 0 8px 25px rgba(99, 102, 241, 0.4) !important;
}

.slider {
    background: var(--surface) !important;
}

.login-button {
    background: linear-gradient(135deg, var(--secondary) 0%, #db2777 100%) !important;
    border: none !important;
    border-radius: 12px !important;
    color: white !important;
}

@keyframes slideInRight {
    from {
        opacity: 0;
        transform: translateX(30px);
    }
    to {
        opacity: 1;
        transform: translateX(0);
    }
}

@keyframes slideInLeft {
    from {
        opacity: 0;
        transform: translateX(-30px);
    }
    to {
        opacity: 1;
        transform: translateX(0);
    }
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

.gradio-container {
    animation: fadeIn 1s ease-in;
}

.typing-dots {
    display: inline-block;
}

.typing-dots::after {
    content: '...';
    animation: typing 1.5s infinite;
}

@keyframes typing {
    0%, 20% { content: '.'; }
    40% { content: '..'; }
    60%, 100% { content: '...'; }
}

/* Header styling */
.header {
    text-align: center;
    padding: 20px;
    background: linear-gradient(135deg, var(--primary) 0%, var(--secondary) 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

/* Sidebar content styling */
.sidebar-content {
    padding: 20px;
}

.sidebar-content h1 {
    color: var(--text);
    font-size: 24px;
    margin-bottom: 20px;
}

.sidebar-content h3 {
    color: var(--text-secondary);
    font-size: 16px;
    margin-top: 20px;
}

.warning-message {
    background: linear-gradient(135deg, #f59e0b 0%, #d97706 100%);
    color: white;
    padding: 12px;
    border-radius: 12px;
    margin: 10px 0;
    text-align: center;
    animation: pulse 2s infinite;
}

.success-message {
    background: linear-gradient(135deg, #10b981 0%, #059669 100%);
    color: white;
    padding: 12px;
    border-radius: 12px;
    margin: 10px 0;
    text-align: center;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.8; }
}

.login-status {
    min-height: 60px;
    display: flex;
    align-items: center;
    justify-content: center;
}
"""

def respond(
    message,
    history: list[dict[str, str]],
    system_message,
    max_tokens,
    temperature,
    top_p,
    hf_token: gr.OAuthToken,
):
    """
    For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
    """
    if not hf_token:
        yield "πŸ” Please login with your Hugging Face token to start chatting! Click the login button in the sidebar."
        return

    try:
        client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")

        messages = [{"role": "system", "content": system_message}]
        messages.extend(history)
        messages.append({"role": "user", "content": message})

        response = ""

        for message in client.chat_completion(
            messages,
            max_tokens=max_tokens,
            stream=True,
            temperature=temperature,
            top_p=top_p,
        ):
            choices = message.choices
            token = ""
            if len(choices) and choices[0].delta.content:
                token = choices[0].delta.content

            response += token
            yield response
            
    except Exception as e:
        yield f"❌ Error: {str(e)}"


# Enhanced system message for helpful assistance
system_message = """You are a helpful and friendly AI assistant. Your purpose is to understand user needs and provide useful, accurate assistance. Be conversational, supportive, and focus on being genuinely helpful in any situation. Keep responses clear and practical."""

# Create the main interface
with gr.Blocks(
    css=custom_css, 
    theme=gr.themes.Soft(primary_hue="indigo", secondary_hue="pink"),
    title="πŸ€– AI Assistant"
) as demo:
    
    gr.Markdown("""
    <div class="header">
        <h1>πŸ€– AI Assistant</h1>
        <p>Welcome! I'm here to help you with anything you need. Ask me questions, seek advice, or just chat!</p>
    </div>
    """)
    
    with gr.Row():
        with gr.Column(scale=1, min_width=280):
            # Sidebar content
            gr.Markdown("""
            <div class="sidebar-content">
                <h1>🎯 Settings</h1>
            </div>
            """)
            
            # Login section
            gr.Markdown("### πŸ” Authentication")
            login_btn = gr.LoginButton()
            
            # Login status display
            login_status = gr.Markdown(
                """
                <div class="warning-message">
                    πŸ” Please login to use the AI assistant
                </div>
                """,
                elem_classes="login-status"
            )
            
            # Settings
            gr.Markdown("### βš™οΈ Parameters")
            system_input = gr.Textbox(
                value=system_message, 
                label="πŸ€– System Role", 
                lines=3,
                interactive=True
            )
            
            max_tokens_input = gr.Slider(
                minimum=1, 
                maximum=2048, 
                value=512, 
                step=1, 
                label="πŸ“Š Max Tokens"
            )
            
            temperature_input = gr.Slider(
                minimum=0.1, 
                maximum=4.0, 
                value=0.7, 
                step=0.1, 
                label="🌑️ Temperature"
            )
            
            top_p_input = gr.Slider(
                minimum=0.1,
                maximum=1.0,
                value=0.95,
                step=0.05,
                label="🎯 Top-p (nucleus sampling)",
            )
            
            gr.Markdown("""
            <div class="sidebar-content">
                <h3>πŸ’‘ Tips</h3>
                <ul style="color: var(--text-secondary); font-size: 14px;">
                    <li>Higher temperature = more creative</li>
                    <li>Lower temperature = more focused</li>
                    <li>Adjust max tokens for response length</li>
                    <li>Login with Hugging Face token required</li>
                </ul>
            </div>
            """)
        
        with gr.Column(scale=4):
            # Chat interface without examples to avoid caching issues
            chatbot = gr.ChatInterface(
                respond,
                type="messages",
                additional_inputs=[
                    system_input,
                    max_tokens_input,
                    temperature_input,
                    top_p_input
                ],
                submit_btn="πŸš€ Send",
                title=None,  # We already have our own title
                description=None  # We already have our own description
            )

    # JavaScript to update login status
    demo.load(
        None,
        None,
        None,
        js="""
        () => {
            // Check if user is logged in on page load
            const checkLogin = () => {
                const token = localStorage.getItem('oauth-token');
                const statusEl = document.querySelector('.login-status');
                if (token && statusEl) {
                    statusEl.innerHTML = '<div class="success-message">βœ… Successfully logged in! You can now chat with the AI.</div>';
                }
            };
            
            // Check initially
            checkLogin();
            
            // Set up interval to check for login status changes
            setInterval(checkLogin, 1000);
        }
        """
    )

if __name__ == "__main__":
    demo.launch(
        show_api=False,
        share=False,
        server_name="0.0.0.0",
        server_port=7860
    )