File size: 9,908 Bytes
39f119b
636d30f
39f119b
 
d05e6bc
5cb8659
d05e6bc
f7a0f58
39f119b
4df5748
f7a0f58
 
5cb8659
39f119b
 
 
 
f2ae859
d5b6e77
39f119b
a48fbd7
5cb8659
a48fbd7
f7a0f58
 
 
abe09bc
f7a0f58
a48fbd7
d05e6bc
5cb8659
d05e6bc
f7a0f58
39f119b
 
fa2d03d
39f119b
fa2d03d
0495e3f
 
 
 
39f119b
 
 
 
f7a0f58
fa2d03d
4df5748
39f119b
fa2d03d
39f119b
0495e3f
4df5748
 
 
f7a0f58
d05e6bc
39f119b
0495e3f
f7a0f58
39f119b
abe09bc
39f119b
f7a0f58
39f119b
 
 
 
d5b6e77
 
abe09bc
fa2d03d
 
 
0495e3f
39f119b
0495e3f
abe09bc
f7a0f58
d05e6bc
e8beb50
d05e6bc
f7a0f58
abe09bc
0495e3f
 
0ff34ff
2b20ef4
4df5748
53f72e7
0ff34ff
2b20ef4
 
00b3b52
4df5748
0ff34ff
b2d12f0
0ff34ff
 
 
2b20ef4
 
fa2d03d
0495e3f
 
4df5748
0495e3f
d05e6bc
3117557
 
e8beb50
b2d12f0
 
e8beb50
b2d12f0
 
4df5748
 
0495e3f
5cb8659
0495e3f
619da60
 
 
0495e3f
00b3b52
34820b1
 
0ff34ff
 
00b3b52
 
 
 
 
0ff34ff
 
 
 
 
 
 
 
 
 
 
 
 
00b3b52
 
 
4df5748
 
53f72e7
2b20ef4
53f72e7
34820b1
00b3b52
4df5748
 
bb0be1c
4df5748
 
5cb8659
0495e3f
619da60
00b3b52
0495e3f
 
 
 
 
fa2d03d
 
0495e3f
2b20ef4
d05e6bc
fa2d03d
0495e3f
 
 
 
 
 
 
d05e6bc
 
fa2d03d
bb0be1c
5cb8659
0495e3f
53f72e7
b2d12f0
0495e3f
bb0be1c
 
5cb8659
bb0be1c
fa2d03d
bb0be1c
0495e3f
00b3b52
4df5748
2b20ef4
0495e3f
d05e6bc
fa2d03d
 
b2d12f0
0495e3f
 
5cb8659
3117557
bb0be1c
0495e3f
 
bb0be1c
d05e6bc
4df5748
b2d12f0
 
5cb8659
3117557
53f72e7
0495e3f
619da60
 
53f72e7
 
619da60
53f72e7
 
5cb8659
53f72e7
 
619da60
 
fa2d03d
d05e6bc
 
 
 
 
 
 
 
 
 
 
 
0495e3f
d05e6bc
 
5cb8659
0495e3f
fa2d03d
 
b61148c
0495e3f
fa2d03d
0495e3f
 
b2d12f0
0495e3f
 
00b3b52
b61148c
d05e6bc
3117557
0495e3f
 
 
3117557
0495e3f
3117557
0495e3f
 
00b3b52
d05e6bc
0495e3f
d05e6bc
0495e3f
5cb8659
0495e3f
 
d05e6bc
0495e3f
fa2d03d
 
0495e3f
d05e6bc
 
0495e3f
fa2d03d
0495e3f
 
fa2d03d
 
0495e3f
 
d05e6bc
 
bb0be1c
5cb8659
fa2d03d
0495e3f
 
 
 
 
636d30f
39f119b
e8beb50
 
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
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
import torch
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer

# ==========================================
# 1. MODEL CONFIGURATION
# ==========================================

MODEL_ID = "DavidBazaldua/llama3_finetuned_transformes"
DEVICE = "cpu"
DTYPE = torch.float32

print(f"Loading model: {MODEL_ID}...")
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
model = AutoModelForCausalLM.from_pretrained(
    MODEL_ID,
    torch_dtype=DTYPE,
    low_cpu_mem_usage=True,
)
model.to(DEVICE)
model.eval()
print("Model loaded successfully.")

DEFAULT_SYSTEM_PROMPT = (
    "You are a helpful, precise AI assistant. "
    "Always answer as briefly as possible. "
    "Respond in English unless the user asks otherwise."
)

# ==========================================
# 2. CHAT LOGIC
# ==========================================

def build_prompt(system_prompt, context, history, user_message):
    messages = []
    if system_prompt and system_prompt.strip():
        messages.append({"role": "system", "content": system_prompt})
    if context and context.strip():
        messages.append({
            "role": "system",
            "content": f"Use the following context if relevant:\n{context}"
        })
    for user, assistant in history:
        messages.append({"role": "user", "content": user})
        messages.append({"role": "assistant", "content": assistant})
    messages.append({"role": "user", "content": user_message})

    prompt = tokenizer.apply_chat_template(
        messages, tokenize=False, add_generation_prompt=True,
    )
    return prompt

def chat_logic(message, history, system_prompt, context, max_tokens, temperature, top_p):
    if not message: return "", history
    if history is None: history = []
    if not system_prompt: system_prompt = DEFAULT_SYSTEM_PROMPT

    max_tokens = int(min(max_tokens, 1024))
    prompt = build_prompt(system_prompt, context, history, message)
    inputs = tokenizer(prompt, return_tensors="pt", add_special_tokens=False).to(DEVICE)

    with torch.no_grad():
        outputs = model.generate(
            **inputs,
            max_new_tokens=max_tokens,
            do_sample=True,
            temperature=float(temperature),
            top_p=float(top_p),
            pad_token_id=tokenizer.eos_token_id,
        )

    decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
    if decoded.startswith(prompt):
        answer = decoded[len(prompt):].strip()
    else:
        answer = decoded.split("assistant\n")[-1].strip()

    history.append((message, answer))
    return "", history

# ==========================================
# 3. UI DESIGN (FINAL FIX)
# ==========================================

CSS = """
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;600;700&display=swap');

/* --- GLOBAL RESET --- */
:root, .dark, body, .gradio-container {
    --body-text-color: #000000 !important;
    --body-background-fill: transparent !important;
    --background-fill-primary: transparent !important;
    --background-fill-secondary: transparent !important;
    --block-background-fill: transparent !important;
    --block-border-color: transparent !important;
    --input-background-fill: #ffffff !important;
    
    /* COLORS */
    --primary-500: #000000 !important;
    --primary-600: #000000 !important;
    --secondary-500: #000000 !important;
}

body {
    font-family: 'Inter', sans-serif !important;
    background: radial-gradient(circle at 10% 10%, #ffeef8 0%, #edf6ff 50%, #f7f0ff 90%) !important;
    color: #000000 !important;
    margin: 0; padding: 0;
    min-height: 100vh;
}

/* HIDE GRADIO FOOTER (CSS is enough) */
footer {
    display: none !important;
    visibility: hidden !important;
}

h1 { font-weight: 700 !important; color: #000000 !important; letter-spacing: -0.02em; }
p, span, label { color: #374151 !important; }

/* --- LEFT PANEL --- */
.config-card {
    background: rgba(255, 255, 255, 0.4) !important; 
    border: 1px solid rgba(255, 255, 255, 0.5) !important;
    backdrop-filter: blur(10px);
    border-radius: 24px;
    padding: 24px !important;
}

.config-card div, .config-card .gr-form, .config-card .gr-box, 
.config-card .gr-group, .config-card .gr-block, .config-card .gr-panel {
    background: transparent !important;
    border: none !important;
    box-shadow: none !important;
}

.config-card label span, 
.config-card .label-wrap span,
.config-card .block-title {
    background-color: transparent !important;
    background: transparent !important;
    color: #000000 !important;
    border: none !important;
    font-weight: 700 !important;
    font-size: 0.85rem !important;
    margin-bottom: 4px !important;
    padding: 0 !important;
}

.config-card textarea, 
.config-card input[type="text"], 
.config-card input[type="number"] {
    background-color: #ffffff !important;
    color: #000000 !important;
    border: 1px solid #e5e7eb !important;
    border-radius: 12px !important;
    box-shadow: 0 1px 2px rgba(0,0,0,0.05) !important;
}

input[type=range] {
    accent-color: #000000 !important;
    filter: grayscale(100%) brightness(0) !important;
}

/* --- RIGHT PANEL: CHAT --- */
.chat-window {
    background: rgba(255, 255, 255, 0.95) !important;
    border: 1px solid rgba(255, 255, 255, 0.9);
    backdrop-filter: blur(20px);
    border-radius: 24px;
    box-shadow: 0 20px 50px rgba(0,0,0,0.05);
    overflow: hidden;
    padding: 0 !important;
}

.window-header {
    display: flex; gap: 8px; align-items: center;
    padding: 18px 24px 10px 24px;
}
.dot { width: 12px; height: 12px; border-radius: 50%; }
.red { background-color: #ff5f56; }
.yellow { background-color: #ffbd2e; }
.green { background-color: #27c93f; }

#chatbot {
    background: transparent !important;
    border: none !important;
    height: 550px !important;
}

/* --- CHAT BUBBLES --- */
.message.user {
    background-color: #000000 !important;
    color: #ffffff !important;
    border-radius: 18px 18px 4px 18px !important;
    font-weight: 500;
}
.message.user p, .message.user span, .message.user div {
    color: #ffffff !important;
}

.message.bot {
    background-color: #ffffff !important;
    color: #000000 !important;
    border: 1px solid #e5e7eb !important;
    border-radius: 18px 18px 18px 4px !important;
    box-shadow: 0 2px 5px rgba(0,0,0,0.02);
}

/* --- INPUT BOX --- */
.chat-input-area {
    padding: 16px 24px 24px 24px;
    display: flex; gap: 10px; align-items: flex-end;
}

#msg-box textarea {
    background: #ffffff !important;
    padding: 12px 18px !important;
    border: 1px solid #e5e7eb !important;
    color: #000000 !important;
    max-height: 120px !important;
    overflow-y: auto !important;
    border-radius: 24px !important;
}

#send-btn {
    background: transparent !important;
    color: #000000 !important;
    border: none !important;
    box-shadow: none !important;
    font-size: 1.6rem;
    padding: 0 10px !important;
    width: auto !important;
    margin-bottom: 8px;
}
#send-btn:hover {
    transform: translateX(3px);
    color: #333;
}
"""

WINDOW_HEADER = """
<div class="window-header">
    <div class="dot red"></div>
    <div class="dot yellow"></div>
    <div class="dot green"></div>
    <span style="margin-left: 12px; font-size: 0.85rem; color: #6b7280; font-weight: 500;">Iris Assistant</span>
</div>
"""

with gr.Blocks(theme=gr.themes.Soft(), css=CSS, title="Custom AI") as demo:
    
    with gr.Row(elem_id="main-container"):
        
        # --- LEFT PANEL ---
        with gr.Column(scale=4):
            gr.Markdown(
                """
                # MX Iris AI Chatbot
                """
            )
            
            with gr.Group(elem_classes="config-card"):
                gr.Markdown("### ⚙️ Settings")
                
                context_input = gr.Textbox(
                    label="Context (Data)",
                    placeholder="Paste context, notes, or data here...",
                    lines=5
                )
                
                system_prompt_input = gr.Textbox(
                    label="System Prompt",
                    value=DEFAULT_SYSTEM_PROMPT,
                    lines=2
                )
                
                with gr.Row():
                    temp_slider = gr.Slider(0.1, 1.5, value=0.7, step=0.1, label="Temperature")
                    tokens_slider = gr.Slider(64, 1024, value=256, step=64, label="Max Tokens")
                
                top_p_slider = gr.Slider(0.1, 1.0, value=0.9, step=0.05, label="Top-P")

        # --- RIGHT PANEL ---
        with gr.Column(scale=6):
            with gr.Column(elem_classes="chat-window"):
                gr.HTML(WINDOW_HEADER)
                
                chatbot = gr.Chatbot(
                    elem_id="chatbot",
                    show_label=False,
                    avatar_images=(None, "https://cdn-icons-png.flaticon.com/512/4712/4712027.png"),
                    height=550,
                    layout="bubble"
                )
                
                with gr.Row(elem_classes="chat-input-area"):
                    msg = gr.Textbox(
                        show_label=False,
                        placeholder="Ask a question...",
                        scale=9,
                        elem_id="msg-box",
                        container=False,
                        autofocus=True,
                        lines=1
                    )
                    send_btn = gr.Button("➝", scale=1, elem_id="send-btn")

    inputs_list = [msg, chatbot, system_prompt_input, context_input, tokens_slider, temp_slider, top_p_slider]
    msg.submit(chat_logic, inputs_list, [msg, chatbot])
    send_btn.click(chat_logic, inputs_list, [msg, chatbot])

if __name__ == "__main__":
    # Removed incorrect show_footer argument. CSS handles footer hiding.
    demo.launch(show_api=False)