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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -71
app.py CHANGED
@@ -7,7 +7,7 @@ from transformers import AutoModelForCausalLM, AutoTokenizer
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}...")
@@ -19,7 +19,7 @@ model = AutoModelForCausalLM.from_pretrained(
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. "
@@ -33,46 +33,30 @@ DEFAULT_SYSTEM_PROMPT = (
33
 
34
  def build_prompt(system_prompt, context, history, user_message):
35
  messages = []
36
- # 1. System Prompt
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(
56
- messages,
57
- tokenize=False,
58
- add_generation_prompt=True,
59
  )
60
  return prompt
61
 
62
  def chat_logic(message, history, system_prompt, context, max_tokens, temperature, top_p):
63
- if not message:
64
- return "", history
65
-
66
- if history is None:
67
- history = []
68
-
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)
75
-
76
  inputs = tokenizer(prompt, return_tensors="pt", add_special_tokens=False).to(DEVICE)
77
 
78
  with torch.no_grad():
@@ -86,7 +70,6 @@ def chat_logic(message, history, system_prompt, context, max_tokens, temperature
86
  )
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:
@@ -96,81 +79,98 @@ def chat_logic(message, history, system_prompt, context, max_tokens, temperature
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;
@@ -179,11 +179,10 @@ input[type=range] { accent-color: #111827; }
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;
186
- background: transparent;
187
  }
188
  .dot { width: 12px; height: 12px; border-radius: 50%; }
189
  .red { background-color: #ff5f56; }
@@ -197,15 +196,15 @@ input[type=range] { accent-color: #111827; }
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;
211
  box-shadow: 0 2px 5px rgba(0,0,0,0.02);
@@ -221,10 +220,10 @@ input[type=range] { accent-color: #111827; }
221
  background: #ffffff !important;
222
  padding: 12px 20px !important;
223
  border: 1px solid #e5e7eb !important;
224
- color: #111827 !important;
225
  }
226
  #send-btn {
227
- background: #111827 !important;
228
  color: white !important;
229
  border-radius: 50% !important;
230
  width: 46px; height: 46px;
@@ -234,13 +233,12 @@ input[type=range] { accent-color: #111827; }
234
 
235
  /* Logos */
236
  .trusted-logos {
237
- opacity: 0.5; filter: grayscale(100%);
238
  margin-top: 30px; display: flex; gap: 20px;
239
- font-weight: 600; font-size: 1.1rem; color: #9ca3af;
240
  }
241
  """
242
 
243
- # HTML Helpers
244
  WINDOW_HEADER = """
245
  <div class="window-header">
246
  <div class="dot red"></div>
@@ -262,7 +260,7 @@ TRUSTED_SECTION = """
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"):
@@ -283,7 +281,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=CSS, title="Custom AI") as demo:
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
 
@@ -293,6 +291,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=CSS, title="Custom AI") as demo:
293
  lines=2
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")
@@ -303,7 +302,6 @@ with gr.Blocks(theme=gr.themes.Soft(), css=CSS, title="Custom AI") as demo:
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,9 +324,8 @@ with gr.Blocks(theme=gr.themes.Soft(), css=CSS, title="Custom AI") as demo:
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])
333
  send_btn.click(chat_logic, inputs_list, [msg, chatbot])
334
 
 
7
  # ==========================================
8
 
9
  MODEL_ID = "DavidBazaldua/llama3_finetuned_transformes"
10
+ DEVICE = "cpu"
11
  DTYPE = torch.float32
12
 
13
  print(f"Loading model: {MODEL_ID}...")
 
19
  )
20
  model.to(DEVICE)
21
  model.eval()
22
+ print("Model loaded.")
23
 
24
  DEFAULT_SYSTEM_PROMPT = (
25
  "You are a helpful, precise AI assistant. "
 
33
 
34
  def build_prompt(system_prompt, context, history, user_message):
35
  messages = []
 
36
  if system_prompt and system_prompt.strip():
37
  messages.append({"role": "system", "content": system_prompt})
 
 
38
  if context and context.strip():
39
  messages.append({
40
  "role": "system",
41
  "content": f"Use the following context if relevant:\n{context}"
42
  })
 
 
43
  for user, assistant in history:
44
  messages.append({"role": "user", "content": user})
45
  messages.append({"role": "assistant", "content": assistant})
 
 
46
  messages.append({"role": "user", "content": user_message})
47
 
48
  prompt = tokenizer.apply_chat_template(
49
+ messages, tokenize=False, add_generation_prompt=True,
 
 
50
  )
51
  return prompt
52
 
53
  def chat_logic(message, history, system_prompt, context, max_tokens, temperature, top_p):
54
+ if not message: return "", history
55
+ if history is None: history = []
56
+ if not system_prompt: system_prompt = DEFAULT_SYSTEM_PROMPT
 
 
 
 
 
57
 
58
  max_tokens = int(min(max_tokens, 1024))
 
59
  prompt = build_prompt(system_prompt, context, history, message)
 
60
  inputs = tokenizer(prompt, return_tensors="pt", add_special_tokens=False).to(DEVICE)
61
 
62
  with torch.no_grad():
 
70
  )
71
 
72
  decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
 
73
  if decoded.startswith(prompt):
74
  answer = decoded[len(prompt):].strip()
75
  else:
 
79
  return "", history
80
 
81
  # ==========================================
82
+ # 3. UI DESIGN (NUCLEAR FIX FOR COLORS)
83
  # ==========================================
84
 
85
  CSS = """
86
  @import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;600;700&display=swap');
87
 
88
+ /* --- 1. OVERRIDE GRADIO VARIABLES (KILL THE BLUE) --- */
89
  :root, .dark, body, .gradio-container {
90
+ /* Main Background */
91
+ --body-background-fill: transparent !important;
92
+
93
+ /* Force Text Color */
94
+ --body-text-color: #000000 !important;
95
+
96
+ /* Turn Accent Colors (Sliders, Focus) to BLACK */
97
+ --primary-500: #000000 !important; /* Slider handle */
98
+ --primary-600: #000000 !important; /* Slider active track */
99
+ --secondary-500: #000000 !important;
100
+
101
+ /* Remove default dark backgrounds */
102
  --background-fill-primary: transparent !important;
103
  --background-fill-secondary: transparent !important;
104
  --block-background-fill: transparent !important;
105
  --block-border-color: transparent !important;
106
+ --input-background-fill: #ffffff !important;
 
 
107
  }
108
 
109
  body {
110
  font-family: 'Inter', sans-serif !important;
111
+ /* Clean Gradient */
112
  background: radial-gradient(circle at 10% 10%, #ffeef8 0%, #edf6ff 50%, #f7f0ff 90%) !important;
113
+ color: #000000 !important;
114
  margin: 0; padding: 0;
115
  min-height: 100vh;
116
  }
117
 
118
+ /* Titles */
119
+ h1 { font-weight: 700 !important; color: #000000 !important; letter-spacing: -0.02em; }
120
+ p, span, label { color: #374151 !important; }
121
 
122
+ /* --- 2. LEFT PANEL: FORCE WHITE & BLACK --- */
123
  .config-card {
124
+ background: rgba(255, 255, 255, 0.6) !important; /* Semi-transparent White */
125
+ border: 1px solid rgba(255, 255, 255, 0.8) !important;
126
  backdrop-filter: blur(12px);
127
  border-radius: 24px;
128
  padding: 24px !important;
 
129
  }
130
 
131
+ /* Force all inner containers to be transparent so they don't show grey blocks */
132
  .config-card .gr-form,
133
  .config-card .gr-box,
134
  .config-card .gr-block,
135
+ .config-card .gr-compact,
136
+ .config-card .gr-group {
137
+ background-color: transparent !important;
138
  background: transparent !important;
139
  border: none !important;
140
  box-shadow: none !important;
141
  }
142
 
143
+ /* INPUTS: Force White Background + Black Text + Black Border on Focus */
 
 
 
 
 
 
 
 
144
  .config-card textarea,
145
  .config-card input[type="text"],
146
  .config-card input[type="number"] {
147
+ background-color: #ffffff !important;
148
+ color: #000000 !important;
149
+ border: 1px solid #d1d5db !important;
150
  border-radius: 12px !important;
151
+ }
152
+ .config-card textarea:focus, .config-card input:focus {
153
+ border-color: #000000 !important; /* Black border when typing */
154
+ box-shadow: none !important;
155
  }
156
 
157
+ /* LABELS: Force Black */
158
+ .config-card label span {
159
+ color: #000000 !important;
160
+ font-weight: 700 !important;
161
+ font-size: 0.85rem !important;
162
+ background: transparent !important;
163
+ }
164
 
165
+ /* SLIDERS: Double Force Black */
166
+ input[type=range] {
167
+ filter: grayscale(100%) contrast(120%) !important; /* Turns any remaining blue to black/grey */
168
+ accent-color: #000000 !important;
169
+ }
170
+
171
+ /* --- 3. RIGHT PANEL: CHAT WINDOW --- */
172
  .chat-window {
173
+ background: rgba(255, 255, 255, 0.85) !important;
174
  border: 1px solid rgba(255, 255, 255, 0.9);
175
  backdrop-filter: blur(20px);
176
  border-radius: 24px;
 
179
  padding: 0 !important;
180
  }
181
 
182
+ /* Mac Header */
183
  .window-header {
184
  display: flex; gap: 8px; align-items: center;
185
  padding: 18px 24px 10px 24px;
 
186
  }
187
  .dot { width: 12px; height: 12px; border-radius: 50%; }
188
  .red { background-color: #ff5f56; }
 
196
  height: 550px !important;
197
  }
198
 
199
+ /* Bubbles */
200
  .message.user {
201
+ background-color: #000000 !important; /* Pure Black */
202
  color: white !important;
203
  border-radius: 18px 18px 4px 18px !important;
204
  }
205
  .message.bot {
206
  background-color: #ffffff !important;
207
+ color: #000000 !important;
208
  border: 1px solid #e5e7eb !important;
209
  border-radius: 18px 18px 18px 4px !important;
210
  box-shadow: 0 2px 5px rgba(0,0,0,0.02);
 
220
  background: #ffffff !important;
221
  padding: 12px 20px !important;
222
  border: 1px solid #e5e7eb !important;
223
+ color: #000000 !important;
224
  }
225
  #send-btn {
226
+ background: #000000 !important; /* Black Button */
227
  color: white !important;
228
  border-radius: 50% !important;
229
  width: 46px; height: 46px;
 
233
 
234
  /* Logos */
235
  .trusted-logos {
236
+ opacity: 0.6; filter: grayscale(100%);
237
  margin-top: 30px; display: flex; gap: 20px;
238
+ font-weight: 600; font-size: 1.1rem; color: #4b5563;
239
  }
240
  """
241
 
 
242
  WINDOW_HEADER = """
243
  <div class="window-header">
244
  <div class="dot red"></div>
 
260
  </div>
261
  """
262
 
263
+ # Theme Soft + Custom CSS overrides
264
  with gr.Blocks(theme=gr.themes.Soft(), css=CSS, title="Custom AI") as demo:
265
 
266
  with gr.Row(elem_id="main-container"):
 
281
 
282
  context_input = gr.Textbox(
283
  label="Context (Data)",
284
+ placeholder="Paste documents, notes, or data here...",
285
  lines=5
286
  )
287
 
 
291
  lines=2
292
  )
293
 
294
+ # Using gr.Row for sliders side-by-side
295
  with gr.Row():
296
  temp_slider = gr.Slider(0.1, 1.5, value=0.7, step=0.1, label="Temperature")
297
  tokens_slider = gr.Slider(64, 1024, value=256, step=64, label="Max Tokens")
 
302
 
303
  # --- RIGHT COLUMN: Chatbot ---
304
  with gr.Column(scale=6):
 
305
  with gr.Column(elem_classes="chat-window"):
306
  gr.HTML(WINDOW_HEADER)
307
 
 
324
  )
325
  send_btn = gr.Button("➝", scale=1, elem_id="send-btn")
326
 
327
+ # Events
328
  inputs_list = [msg, chatbot, system_prompt_input, context_input, tokens_slider, temp_slider, top_p_slider]
 
329
  msg.submit(chat_logic, inputs_list, [msg, chatbot])
330
  send_btn.click(chat_logic, inputs_list, [msg, chatbot])
331