DavidBazaldua commited on
Commit
fa2d03d
·
verified ·
1 Parent(s): abe09bc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +182 -49
app.py CHANGED
@@ -35,20 +35,24 @@ DEFAULT_SYSTEM_PROMPT = (
35
  # Prompt building
36
  # ---------------------------------------------------------------------
37
 
 
38
  def build_prompt(system_prompt, context, history, user_message):
39
  messages = []
40
 
41
- if system_prompt.strip():
42
  messages.append({"role": "system", "content": system_prompt})
43
 
44
- if context.strip():
45
- messages.append({
46
- "role": "system",
47
- "content": (
48
- "The following information is additional context. "
49
- "Use it only if relevant:\n" + context
50
- )
51
- })
 
 
 
52
 
53
  for user, assistant in history:
54
  messages.append({"role": "user", "content": user})
@@ -56,18 +60,19 @@ def build_prompt(system_prompt, context, history, user_message):
56
 
57
  messages.append({"role": "user", "content": user_message})
58
 
59
- return tokenizer.apply_chat_template(
60
  messages,
61
  tokenize=False,
62
  add_generation_prompt=True,
63
  )
 
64
 
65
 
66
  def generate_answer(system_prompt, context, message, history, max_tokens, temperature, top_p):
67
  if history is None:
68
  history = []
69
 
70
- if not system_prompt.strip():
71
  system_prompt = DEFAULT_SYSTEM_PROMPT
72
 
73
  max_tokens = int(min(max_tokens, 128))
@@ -92,65 +97,156 @@ def generate_answer(system_prompt, context, message, history, max_tokens, temper
92
 
93
  decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
94
 
95
- # Try to extract only the new part
96
- answer = decoded[len(prompt):].strip() if decoded.startswith(prompt) else decoded.strip()
 
 
97
 
98
  history.append([message, answer])
99
  return answer, history
100
 
101
 
102
  def chat(message, history, system_prompt, context, max_tokens, temperature, top_p):
103
- answer, history = generate_answer(
 
 
 
104
  system_prompt, context, message, history, max_tokens, temperature, top_p
105
  )
 
106
  return "", history
107
 
 
108
  # ---------------------------------------------------------------------
109
- # Minimalist ChatGPT-style UI
110
  # ---------------------------------------------------------------------
111
 
112
  CSS = """
113
- #container {max-width: 1200px; margin-left: auto; margin-right: auto;}
114
- #chat-column {width: 75%;}
115
- #sidebar {width: 25%; padding-left: 20px;}
116
- #input-row {margin-top: 12px;}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
  """
118
 
119
  with gr.Blocks(css=CSS) as demo:
120
- gr.Markdown("<h2 style='font-weight:600;'>Iris – Your Fine-Tuned Llama 3 Assistant</h2>")
 
 
 
 
 
 
 
 
 
 
 
 
121
 
122
- with gr.Row(elem_id="container"):
123
- # LEFT SIDE: CHAT
124
- with gr.Column(elem_id="chat-column"):
125
- chatbot = gr.Chatbot(
126
- height=500,
127
  show_label=False,
128
  )
129
 
130
- with gr.Row(elem_id="input-row"):
131
- msg = gr.Textbox(
132
- placeholder="Send a message...",
133
- scale=8,
134
- show_label=False,
135
- )
136
- send_btn = gr.Button("Send", scale=2)
137
-
138
- # RIGHT SIDE: SIDEBAR
139
- with gr.Column(elem_id="sidebar"):
140
- gr.Markdown("### Settings")
141
-
142
  system_prompt_box = gr.Textbox(
143
- label="System prompt",
144
  value=DEFAULT_SYSTEM_PROMPT,
145
  lines=5,
 
146
  )
147
 
148
- context_box = gr.Textbox(
149
- label="Context",
150
- placeholder="Optional reference text...",
151
- lines=6,
152
- )
153
-
154
  max_tokens_slider = gr.Slider(
155
  label="Max tokens",
156
  minimum=32,
@@ -158,7 +254,6 @@ with gr.Blocks(css=CSS) as demo:
158
  value=128,
159
  step=16,
160
  )
161
-
162
  temperature_slider = gr.Slider(
163
  label="Temperature",
164
  minimum=0.1,
@@ -166,7 +261,6 @@ with gr.Blocks(css=CSS) as demo:
166
  value=0.7,
167
  step=0.1,
168
  )
169
-
170
  top_p_slider = gr.Slider(
171
  label="Top-p",
172
  minimum=0.1,
@@ -175,10 +269,49 @@ with gr.Blocks(css=CSS) as demo:
175
  step=0.05,
176
  )
177
 
178
- clear_btn = gr.Button("Clear chat")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
179
 
180
- # Chat events
181
- inputs = [msg, chatbot, system_prompt_box, context_box, max_tokens_slider, temperature_slider, top_p_slider]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
182
  outputs = [msg, chatbot]
183
 
184
  msg.submit(chat, inputs, outputs)
 
35
  # Prompt building
36
  # ---------------------------------------------------------------------
37
 
38
+
39
  def build_prompt(system_prompt, context, history, user_message):
40
  messages = []
41
 
42
+ if system_prompt and system_prompt.strip():
43
  messages.append({"role": "system", "content": system_prompt})
44
 
45
+ if context and context.strip():
46
+ messages.append(
47
+ {
48
+ "role": "system",
49
+ "content": (
50
+ "The following information is additional context. "
51
+ "Use it only if it is relevant to the user's request:\n"
52
+ f"{context}"
53
+ ),
54
+ }
55
+ )
56
 
57
  for user, assistant in history:
58
  messages.append({"role": "user", "content": user})
 
60
 
61
  messages.append({"role": "user", "content": user_message})
62
 
63
+ prompt = tokenizer.apply_chat_template(
64
  messages,
65
  tokenize=False,
66
  add_generation_prompt=True,
67
  )
68
+ return prompt
69
 
70
 
71
  def generate_answer(system_prompt, context, message, history, max_tokens, temperature, top_p):
72
  if history is None:
73
  history = []
74
 
75
+ if not system_prompt or system_prompt.strip() == "":
76
  system_prompt = DEFAULT_SYSTEM_PROMPT
77
 
78
  max_tokens = int(min(max_tokens, 128))
 
97
 
98
  decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
99
 
100
+ if decoded.startswith(prompt):
101
+ answer = decoded[len(prompt):].strip()
102
+ else:
103
+ answer = decoded.strip()
104
 
105
  history.append([message, answer])
106
  return answer, history
107
 
108
 
109
  def chat(message, history, system_prompt, context, max_tokens, temperature, top_p):
110
+ if history is None:
111
+ history = []
112
+
113
+ _, history = generate_answer(
114
  system_prompt, context, message, history, max_tokens, temperature, top_p
115
  )
116
+ # limpiamos el textbox de input
117
  return "", history
118
 
119
+
120
  # ---------------------------------------------------------------------
121
+ # UI estilo landing / glassmorphism
122
  # ---------------------------------------------------------------------
123
 
124
  CSS = """
125
+ body {
126
+ font-family: system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
127
+ background: radial-gradient(circle at 0% 0%, #ffe3f2 0%, #e2f1ff 40%, #fff6e5 80%);
128
+ }
129
+
130
+ /* centrar contenido y limitar ancho */
131
+ #main-container {
132
+ max-width: 1200px;
133
+ margin: 0 auto;
134
+ }
135
+
136
+ /* columnas izquierda/derecha */
137
+ #left-panel {
138
+ padding-right: 32px;
139
+ }
140
+
141
+ #right-panel {
142
+ display: flex;
143
+ justify-content: center;
144
+ }
145
+
146
+ /* card del chat */
147
+ #chat-card {
148
+ width: 100%;
149
+ max-width: 520px;
150
+ background: rgba(255, 255, 255, 0.82);
151
+ border-radius: 24px;
152
+ padding: 18px 18px 14px 18px;
153
+ box-shadow: 0 18px 45px rgba(15, 23, 42, 0.18);
154
+ backdrop-filter: blur(18px);
155
+ }
156
+
157
+ /* título y subtítulo lado izquierdo */
158
+ #hero-title {
159
+ font-size: 2.4rem;
160
+ line-height: 1.1;
161
+ font-weight: 600;
162
+ color: #111827;
163
+ }
164
+
165
+ #hero-subtitle {
166
+ margin-top: 0.75rem;
167
+ font-size: 1rem;
168
+ color: #4b5563;
169
+ }
170
+
171
+ /* badges */
172
+ .badge {
173
+ display: inline-flex;
174
+ align-items: center;
175
+ padding: 4px 10px;
176
+ border-radius: 999px;
177
+ border: 1px solid rgba(148, 163, 184, 0.6);
178
+ font-size: 0.75rem;
179
+ color: #4b5563;
180
+ background: rgba(255,255,255,0.7);
181
+ }
182
+
183
+ /* labels minimalistas */
184
+ label { font-size: 0.78rem !important; }
185
+
186
+ /* chatbot */
187
+ #chatbot {
188
+ background: transparent;
189
+ border: none;
190
+ }
191
+
192
+ /* quitar el header por defecto del chatbot */
193
+ #chatbot > div:nth-child(1) {
194
+ display: none;
195
+ }
196
+
197
+ /* caja del input */
198
+ #input-row {
199
+ margin-top: 10px;
200
+ }
201
+
202
+ #message-box textarea {
203
+ border-radius: 999px !important;
204
+ }
205
+
206
+ /* botones */
207
+ button {
208
+ border-radius: 999px !important;
209
+ }
210
+
211
+ #send-btn {
212
+ height: 40px;
213
+ }
214
+
215
+ #clear-btn {
216
+ margin-top: 8px;
217
+ }
218
  """
219
 
220
  with gr.Blocks(css=CSS) as demo:
221
+ with gr.Row(elem_id="main-container"):
222
+ # LEFT: hero + context + settings
223
+ with gr.Column(scale=5, elem_id="left-panel"):
224
+ gr.Markdown(
225
+ """
226
+ <div class="badge">Custom AI · Fine-tuned Llama 3</div>
227
+ <h1 id="hero-title">Iris – Custom AI assistant trained on your data.</h1>
228
+ <p id="hero-subtitle">
229
+ Add context on the left, chat on the right.
230
+ Iris will adapt to your instructions and extra data in real time.
231
+ </p>
232
+ """,
233
+ )
234
 
235
+ gr.Markdown("#### Context")
236
+ context_box = gr.Textbox(
237
+ placeholder="Paste any reference text, notes, or documents you want Iris to use as context.",
238
+ lines=7,
 
239
  show_label=False,
240
  )
241
 
242
+ gr.Markdown("#### Behavior")
 
 
 
 
 
 
 
 
 
 
 
243
  system_prompt_box = gr.Textbox(
 
244
  value=DEFAULT_SYSTEM_PROMPT,
245
  lines=5,
246
+ show_label=False,
247
  )
248
 
249
+ gr.Markdown("#### Generation settings")
 
 
 
 
 
250
  max_tokens_slider = gr.Slider(
251
  label="Max tokens",
252
  minimum=32,
 
254
  value=128,
255
  step=16,
256
  )
 
257
  temperature_slider = gr.Slider(
258
  label="Temperature",
259
  minimum=0.1,
 
261
  value=0.7,
262
  step=0.1,
263
  )
 
264
  top_p_slider = gr.Slider(
265
  label="Top-p",
266
  minimum=0.1,
 
269
  step=0.05,
270
  )
271
 
272
+ # RIGHT: chat card
273
+ with gr.Column(scale=7, elem_id="right-panel"):
274
+ with gr.Column(elem_id="chat-card"):
275
+ gr.Markdown(
276
+ """
277
+ <div style="display:flex; align-items:center; gap:6px; margin-bottom:8px;">
278
+ <div style="width:10px;height:10px;border-radius:999px;background:#f97373;"></div>
279
+ <div style="width:10px;height:10px;border-radius:999px;background:#facc15;"></div>
280
+ <div style="width:10px;height:10px;border-radius:999px;background:#4ade80;"></div>
281
+ </div>
282
+ <div style="font-size:0.85rem;color:#6b7280;margin-bottom:6px;">
283
+ Hi there, ask me anything.
284
+ </div>
285
+ """,
286
+ )
287
+
288
+ chatbot = gr.Chatbot(
289
+ height=360,
290
+ show_label=False,
291
+ elem_id="chatbot",
292
+ )
293
 
294
+ with gr.Row(elem_id="input-row"):
295
+ msg = gr.Textbox(
296
+ placeholder="Ask a question...",
297
+ scale=8,
298
+ show_label=False,
299
+ elem_id="message-box",
300
+ )
301
+ send_btn = gr.Button("Send", scale=2, elem_id="send-btn")
302
+
303
+ clear_btn = gr.Button("Clear chat", elem_id="clear-btn")
304
+
305
+ # wiring
306
+ inputs = [
307
+ msg,
308
+ chatbot,
309
+ system_prompt_box,
310
+ context_box,
311
+ max_tokens_slider,
312
+ temperature_slider,
313
+ top_p_slider,
314
+ ]
315
  outputs = [msg, chatbot]
316
 
317
  msg.submit(chat, inputs, outputs)