DavidBazaldua commited on
Commit
00b3b52
·
verified ·
1 Parent(s): d05e6bc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -42
app.py CHANGED
@@ -3,14 +3,14 @@ import gradio as gr
3
  from transformers import AutoModelForCausalLM, AutoTokenizer
4
 
5
  # ==========================================
6
- # 1. CONFIGURACIÓN DEL MODELO
7
  # ==========================================
8
 
9
  MODEL_ID = "DavidBazaldua/llama3_finetuned_transformes"
10
- DEVICE = "cpu" # Cambia a "cuda" si tienes GPU NVIDIA
11
  DTYPE = torch.float32
12
 
13
- print(f"Cargando modelo: {MODEL_ID}...")
14
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
15
  model = AutoModelForCausalLM.from_pretrained(
16
  MODEL_ID,
@@ -19,7 +19,7 @@ model = AutoModelForCausalLM.from_pretrained(
19
  )
20
  model.to(DEVICE)
21
  model.eval()
22
- print("Modelo cargado exitosamente.")
23
 
24
  DEFAULT_SYSTEM_PROMPT = (
25
  "You are a helpful, precise AI assistant. "
@@ -28,7 +28,7 @@ DEFAULT_SYSTEM_PROMPT = (
28
  )
29
 
30
  # ==========================================
31
- # 2. LÓGICA DEL CHAT
32
  # ==========================================
33
 
34
  def build_prompt(system_prompt, context, history, user_message):
@@ -37,19 +37,19 @@ def build_prompt(system_prompt, context, history, user_message):
37
  if system_prompt and system_prompt.strip():
38
  messages.append({"role": "system", "content": system_prompt})
39
 
40
- # 2. Contexto Extra
41
  if context and context.strip():
42
  messages.append({
43
  "role": "system",
44
  "content": f"Use the following context if relevant:\n{context}"
45
  })
46
 
47
- # 3. Historial de conversación
48
  for user, assistant in history:
49
  messages.append({"role": "user", "content": user})
50
  messages.append({"role": "assistant", "content": assistant})
51
 
52
- # 4. Mensaje actual
53
  messages.append({"role": "user", "content": user_message})
54
 
55
  prompt = tokenizer.apply_chat_template(
@@ -69,7 +69,6 @@ def chat_logic(message, history, system_prompt, context, max_tokens, temperature
69
  if not system_prompt:
70
  system_prompt = DEFAULT_SYSTEM_PROMPT
71
 
72
- # Configuración de seguridad
73
  max_tokens = int(min(max_tokens, 1024))
74
 
75
  prompt = build_prompt(system_prompt, context, history, message)
@@ -88,73 +87,91 @@ def chat_logic(message, history, system_prompt, context, max_tokens, temperature
88
 
89
  decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
90
 
91
- # Limpiar la respuesta para no repetir el prompt
92
  if decoded.startswith(prompt):
93
  answer = decoded[len(prompt):].strip()
94
  else:
95
- # Fallback por si el modelo repite cosas raras
96
  answer = decoded.split("assistant\n")[-1].strip()
97
 
98
  history.append((message, answer))
99
  return "", history
100
 
101
  # ==========================================
102
- # 3. INTERFAZ GRÁFICA (CSS + LAYOUT)
103
  # ==========================================
104
 
105
- # CSS corregido para forzar modo claro y estilo Glass
106
  CSS = """
107
  @import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;600;700&display=swap');
108
 
109
- /* --- FORZAR VARIABLES CLARAS (Anti-Dark Mode) --- */
110
  :root, .dark, body, .gradio-container {
111
  --body-text-color: #1f2937 !important;
112
- --body-background-fill: transparent !important;
113
  --background-fill-primary: transparent !important;
114
  --background-fill-secondary: transparent !important;
115
  --block-background-fill: transparent !important;
116
- --block-border-color: rgba(255,255,255, 0.4) !important;
117
  --input-background-fill: rgba(255, 255, 255, 0.9) !important;
118
  --input-border-color: #e5e7eb !important;
 
119
  }
120
 
121
  body {
122
  font-family: 'Inter', sans-serif !important;
123
- /* Gradiente Pastel de fondo */
124
  background: radial-gradient(circle at 10% 10%, #ffeef8 0%, #edf6ff 50%, #f7f0ff 90%) !important;
125
  color: #1f2937 !important;
126
  margin: 0; padding: 0;
127
  min-height: 100vh;
128
  }
129
 
130
- /* Tipografía */
131
  h1 { font-weight: 700 !important; color: #111827 !important; letter-spacing: -0.02em; }
132
  p, span, label { color: #4b5563 !important; }
133
 
134
- /* --- TARJETA IZQUIERDA (Configuración) --- */
135
  .config-card {
136
  background: rgba(255, 255, 255, 0.5) !important;
137
  border: 1px solid rgba(255, 255, 255, 0.6) !important;
138
  backdrop-filter: blur(12px);
139
  border-radius: 24px;
140
- padding: 24px;
141
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.01);
142
  }
143
 
144
- /* Inputs y Sliders */
145
- textarea, input[type="text"], input[type="number"] {
146
- background-color: rgba(255, 255, 255, 0.8) !important;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
  color: #111827 !important;
148
  border: 1px solid #e5e7eb !important;
149
  border-radius: 12px !important;
150
- box-shadow: none !important;
151
  }
 
 
152
  input[type=range] { accent-color: #111827; }
153
 
154
- /* --- VENTANA DE CHAT (Derecha) --- */
155
  .chat-window {
156
- background: rgba(255, 255, 255, 0.75) !important;
157
- border: 1px solid rgba(255, 255, 255, 0.8);
158
  backdrop-filter: blur(20px);
159
  border-radius: 24px;
160
  box-shadow: 0 20px 50px rgba(0,0,0,0.05);
@@ -162,7 +179,7 @@ input[type=range] { accent-color: #111827; }
162
  padding: 0 !important;
163
  }
164
 
165
- /* Header tipo Mac */
166
  .window-header {
167
  display: flex; gap: 8px; align-items: center;
168
  padding: 18px 24px 10px 24px;
@@ -180,14 +197,14 @@ input[type=range] { accent-color: #111827; }
180
  height: 550px !important;
181
  }
182
 
183
- /* Burbujas del chat */
184
  .message.user {
185
- background-color: #111827 !important; /* Usuario: Oscuro */
186
  color: white !important;
187
  border-radius: 18px 18px 4px 18px !important;
188
  }
189
  .message.bot {
190
- background-color: #ffffff !important; /* Bot: Blanco */
191
  color: #111827 !important;
192
  border: 1px solid #e5e7eb !important;
193
  border-radius: 18px 18px 18px 4px !important;
@@ -215,7 +232,7 @@ input[type=range] { accent-color: #111827; }
215
  display: flex; align-items: center; justify-content: center;
216
  }
217
 
218
- /* Logos Trusted By */
219
  .trusted-logos {
220
  opacity: 0.5; filter: grayscale(100%);
221
  margin-top: 30px; display: flex; gap: 20px;
@@ -245,28 +262,28 @@ TRUSTED_SECTION = """
245
  </div>
246
  """
247
 
248
- # Usamos theme=gr.themes.Soft() como base para evitar conflictos oscuros
249
  with gr.Blocks(theme=gr.themes.Soft(), css=CSS, title="Custom AI") as demo:
250
 
251
  with gr.Row(elem_id="main-container"):
252
 
253
- # --- COLUMNA IZQUIERDA: Configuración ---
254
  with gr.Column(scale=4):
255
  gr.Markdown(
256
  """
257
  # Custom AI Chatbot
258
  # trained on your data
259
 
260
- Personaliza el comportamiento y el contexto de tu asistente aquí mismo.
261
  """
262
  )
263
 
264
  with gr.Group(elem_classes="config-card"):
265
- gr.Markdown("### ⚙️ Configuración")
266
 
267
  context_input = gr.Textbox(
268
- label="Contexto (Data)",
269
- placeholder="Pega aquí documentos o notas para dar contexto...",
270
  lines=5
271
  )
272
 
@@ -277,16 +294,16 @@ with gr.Blocks(theme=gr.themes.Soft(), css=CSS, title="Custom AI") as demo:
277
  )
278
 
279
  with gr.Row():
280
- temp_slider = gr.Slider(0.1, 1.5, value=0.7, step=0.1, label="Creatividad")
281
  tokens_slider = gr.Slider(64, 1024, value=256, step=64, label="Max Tokens")
282
 
283
  top_p_slider = gr.Slider(0.1, 1.0, value=0.9, step=0.05, label="Top-P")
284
 
285
  gr.HTML(TRUSTED_SECTION)
286
 
287
- # --- COLUMNA DERECHA: Chatbot ---
288
  with gr.Column(scale=6):
289
- # Contenedor estilo ventana de app
290
  with gr.Column(elem_classes="chat-window"):
291
  gr.HTML(WINDOW_HEADER)
292
 
@@ -309,7 +326,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=CSS, title="Custom AI") as demo:
309
  )
310
  send_btn = gr.Button("➝", scale=1, elem_id="send-btn")
311
 
312
- # --- EVENTOS ---
313
  inputs_list = [msg, chatbot, system_prompt_input, context_input, tokens_slider, temp_slider, top_p_slider]
314
 
315
  msg.submit(chat_logic, inputs_list, [msg, chatbot])
 
3
  from transformers import AutoModelForCausalLM, AutoTokenizer
4
 
5
  # ==========================================
6
+ # 1. MODEL CONFIGURATION
7
  # ==========================================
8
 
9
  MODEL_ID = "DavidBazaldua/llama3_finetuned_transformes"
10
+ DEVICE = "cpu" # Change to "cuda" if using GPU
11
  DTYPE = torch.float32
12
 
13
+ print(f"Loading model: {MODEL_ID}...")
14
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
15
  model = AutoModelForCausalLM.from_pretrained(
16
  MODEL_ID,
 
19
  )
20
  model.to(DEVICE)
21
  model.eval()
22
+ print("Model loaded successfully.")
23
 
24
  DEFAULT_SYSTEM_PROMPT = (
25
  "You are a helpful, precise AI assistant. "
 
28
  )
29
 
30
  # ==========================================
31
+ # 2. CHAT LOGIC
32
  # ==========================================
33
 
34
  def build_prompt(system_prompt, context, history, user_message):
 
37
  if system_prompt and system_prompt.strip():
38
  messages.append({"role": "system", "content": system_prompt})
39
 
40
+ # 2. Extra Context
41
  if context and context.strip():
42
  messages.append({
43
  "role": "system",
44
  "content": f"Use the following context if relevant:\n{context}"
45
  })
46
 
47
+ # 3. History
48
  for user, assistant in history:
49
  messages.append({"role": "user", "content": user})
50
  messages.append({"role": "assistant", "content": assistant})
51
 
52
+ # 4. Current Message
53
  messages.append({"role": "user", "content": user_message})
54
 
55
  prompt = tokenizer.apply_chat_template(
 
69
  if not system_prompt:
70
  system_prompt = DEFAULT_SYSTEM_PROMPT
71
 
 
72
  max_tokens = int(min(max_tokens, 1024))
73
 
74
  prompt = build_prompt(system_prompt, context, history, message)
 
87
 
88
  decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
89
 
 
90
  if decoded.startswith(prompt):
91
  answer = decoded[len(prompt):].strip()
92
  else:
 
93
  answer = decoded.split("assistant\n")[-1].strip()
94
 
95
  history.append((message, answer))
96
  return "", history
97
 
98
  # ==========================================
99
+ # 3. UI DESIGN (CSS + LAYOUT)
100
  # ==========================================
101
 
 
102
  CSS = """
103
  @import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;600;700&display=swap');
104
 
105
+ /* --- GLOBAL RESET & LIGHT THEME ENFORCEMENT --- */
106
  :root, .dark, body, .gradio-container {
107
  --body-text-color: #1f2937 !important;
 
108
  --background-fill-primary: transparent !important;
109
  --background-fill-secondary: transparent !important;
110
  --block-background-fill: transparent !important;
111
+ --block-border-color: transparent !important;
112
  --input-background-fill: rgba(255, 255, 255, 0.9) !important;
113
  --input-border-color: #e5e7eb !important;
114
+ --color-accent-soft: transparent !important;
115
  }
116
 
117
  body {
118
  font-family: 'Inter', sans-serif !important;
 
119
  background: radial-gradient(circle at 10% 10%, #ffeef8 0%, #edf6ff 50%, #f7f0ff 90%) !important;
120
  color: #1f2937 !important;
121
  margin: 0; padding: 0;
122
  min-height: 100vh;
123
  }
124
 
125
+ /* Typography */
126
  h1 { font-weight: 700 !important; color: #111827 !important; letter-spacing: -0.02em; }
127
  p, span, label { color: #4b5563 !important; }
128
 
129
+ /* --- LEFT PANEL: CONFIG CARD --- */
130
  .config-card {
131
  background: rgba(255, 255, 255, 0.5) !important;
132
  border: 1px solid rgba(255, 255, 255, 0.6) !important;
133
  backdrop-filter: blur(12px);
134
  border-radius: 24px;
135
+ padding: 24px !important;
136
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.01);
137
  }
138
 
139
+ /* CRITICAL FIX: Make inner blocks transparent inside the config card */
140
+ .config-card .gr-form,
141
+ .config-card .gr-box,
142
+ .config-card .gr-block,
143
+ .config-card .gr-compact {
144
+ background: transparent !important;
145
+ border: none !important;
146
+ box-shadow: none !important;
147
+ }
148
+
149
+ /* Style the Labels (Context, System Prompt, etc.) */
150
+ .config-card label span {
151
+ color: #111827 !important;
152
+ font-weight: 600 !important;
153
+ font-size: 0.85rem !important;
154
+ margin-bottom: 6px !important;
155
+ }
156
+
157
+ /* Style the Inputs (White boxes) */
158
+ .config-card textarea,
159
+ .config-card input[type="text"],
160
+ .config-card input[type="number"] {
161
+ background-color: rgba(255, 255, 255, 0.95) !important;
162
  color: #111827 !important;
163
  border: 1px solid #e5e7eb !important;
164
  border-radius: 12px !important;
165
+ box-shadow: 0 1px 2px rgba(0,0,0,0.05) !important;
166
  }
167
+
168
+ /* Sliders Style */
169
  input[type=range] { accent-color: #111827; }
170
 
171
+ /* --- RIGHT PANEL: CHAT WINDOW --- */
172
  .chat-window {
173
+ background: rgba(255, 255, 255, 0.8) !important;
174
+ border: 1px solid rgba(255, 255, 255, 0.9);
175
  backdrop-filter: blur(20px);
176
  border-radius: 24px;
177
  box-shadow: 0 20px 50px rgba(0,0,0,0.05);
 
179
  padding: 0 !important;
180
  }
181
 
182
+ /* Mac-style Header */
183
  .window-header {
184
  display: flex; gap: 8px; align-items: center;
185
  padding: 18px 24px 10px 24px;
 
197
  height: 550px !important;
198
  }
199
 
200
+ /* Chat Bubbles */
201
  .message.user {
202
+ background-color: #111827 !important;
203
  color: white !important;
204
  border-radius: 18px 18px 4px 18px !important;
205
  }
206
  .message.bot {
207
+ background-color: #ffffff !important;
208
  color: #111827 !important;
209
  border: 1px solid #e5e7eb !important;
210
  border-radius: 18px 18px 18px 4px !important;
 
232
  display: flex; align-items: center; justify-content: center;
233
  }
234
 
235
+ /* Logos */
236
  .trusted-logos {
237
  opacity: 0.5; filter: grayscale(100%);
238
  margin-top: 30px; display: flex; gap: 20px;
 
262
  </div>
263
  """
264
 
265
+ # IMPORTANT: Keep theme=gr.themes.Soft() to provide a light base
266
  with gr.Blocks(theme=gr.themes.Soft(), css=CSS, title="Custom AI") as demo:
267
 
268
  with gr.Row(elem_id="main-container"):
269
 
270
+ # --- LEFT COLUMN: Configuration ---
271
  with gr.Column(scale=4):
272
  gr.Markdown(
273
  """
274
  # Custom AI Chatbot
275
  # trained on your data
276
 
277
+ Customize the behavior and context of your assistant right here.
278
  """
279
  )
280
 
281
  with gr.Group(elem_classes="config-card"):
282
+ gr.Markdown("### ⚙️ Settings")
283
 
284
  context_input = gr.Textbox(
285
+ label="Context (Data)",
286
+ placeholder="Paste documents, notes, or data here to provide temporary knowledge...",
287
  lines=5
288
  )
289
 
 
294
  )
295
 
296
  with gr.Row():
297
+ temp_slider = gr.Slider(0.1, 1.5, value=0.7, step=0.1, label="Temperature")
298
  tokens_slider = gr.Slider(64, 1024, value=256, step=64, label="Max Tokens")
299
 
300
  top_p_slider = gr.Slider(0.1, 1.0, value=0.9, step=0.05, label="Top-P")
301
 
302
  gr.HTML(TRUSTED_SECTION)
303
 
304
+ # --- RIGHT COLUMN: Chatbot ---
305
  with gr.Column(scale=6):
306
+ # Window Container
307
  with gr.Column(elem_classes="chat-window"):
308
  gr.HTML(WINDOW_HEADER)
309
 
 
326
  )
327
  send_btn = gr.Button("➝", scale=1, elem_id="send-btn")
328
 
329
+ # --- EVENTS ---
330
  inputs_list = [msg, chatbot, system_prompt_input, context_input, tokens_slider, temp_slider, top_p_slider]
331
 
332
  msg.submit(chat_logic, inputs_list, [msg, chatbot])