ndahlbom commited on
Commit
91f318f
·
verified ·
1 Parent(s): 09643ab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -61
app.py CHANGED
@@ -13,36 +13,60 @@ MODEL_REPO = "Jeppcode/ScalableLab2"
13
  GGUF_FILENAME = "model-q4_k_m.gguf"
14
 
15
  print(f"Downloading GGUF model {MODEL_REPO}/{GGUF_FILENAME} ...")
16
- model_path = hf_hub_download(
17
- repo_id=MODEL_REPO,
18
- filename=GGUF_FILENAME,
19
- )
20
-
21
- print("Initializing llama.cpp LLM ...")
22
- llm = Llama(
23
- model_path=model_path,
24
- n_ctx=2048,
25
- n_threads=2,
26
- n_batch=64,
27
- use_mmap=True,
28
- use_mlock=False,
29
- )
 
 
 
 
 
 
 
 
30
 
31
  # --- 3. Image Handling (The Fix) ---
32
- def encode_image(image_path):
 
 
 
33
  """
34
- Reads the local image and converts it to a base64 string.
35
- This ensures it loads in the CSS without 404 errors.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  """
37
- if not os.path.exists(image_path):
38
- return "" # Fail silently if file missing
39
- with open(image_path, "rb") as image_file:
40
- encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
41
- return f"data:image/jpeg;base64,{encoded_string}"
42
 
43
- # Exact filename from your screenshot
44
- IMG_FILENAME = "Cute-Christmas-Background-edit-online-1.jpg"
45
- bg_image_data = encode_image(IMG_FILENAME)
46
 
47
  # --- 4. Prompts & Logic ---
48
  STYLE_SYSTEM_PROMPTS = {
@@ -76,11 +100,16 @@ def build_prompt(message, history, style):
76
  return "".join(prompt_parts)
77
 
78
  def chat_fn(message, history, max_new_tokens, style):
 
79
  temperature = 0.7
80
  top_p = 0.9
81
  repetition_penalty = 1.1
82
  prompt = build_prompt(message, history, style)
83
 
 
 
 
 
84
  output = llm(
85
  prompt,
86
  max_tokens=int(max_new_tokens),
@@ -92,66 +121,60 @@ def chat_fn(message, history, max_new_tokens, style):
92
  return output["choices"][0]["text"].strip()
93
 
94
  # --- 5. Theme & CSS ---
 
 
95
  winter_theme = gr.themes.Soft(
96
  primary_hue="blue",
97
  neutral_hue="slate",
98
  ).set(
99
  body_background_fill="transparent",
100
- block_background_fill="rgba(10, 20, 40, 0.7)",
101
  border_color_primary="#FFFFFF",
102
  button_primary_background_fill="#FFFFFF",
103
  button_primary_text_color="#000000",
104
  )
105
 
106
- # We inject the base64 image data directly into the URL()
107
- custom_css = f"""
108
- /* Apply Background to the main app container */
109
- .gradio-container {{
110
- background-image: url('{bg_image_data}') !important;
111
- background-size: cover !important;
112
- background-position: center center !important;
113
- background-attachment: fixed !important;
114
- background-repeat: no-repeat !important;
115
- }}
116
-
117
- /* Ensure the main content area is transparent so bg shows */
118
- .gradio-container > .main {{
119
  background: transparent !important;
120
- }}
121
 
122
- /* Dark Glassy Look for Chat and Settings */
123
- .group, .form, .bubble-wrap {{
124
- background: rgba(15, 23, 42, 0.75) !important;
125
  border: 2px solid #FFFFFF !important;
126
  border-radius: 12px !important;
127
- backdrop-filter: blur(4px);
128
  box-shadow: 0 4px 15px rgba(0,0,0,0.5);
129
- }}
130
 
131
- /* Chat Bubbles */
132
- .user-message {{
133
  background-color: rgba(255, 255, 255, 0.2) !important;
134
  border: 1px solid #FFFFFF !important;
135
  color: #FFFFFF !important;
136
- }}
137
- .bot-message {{
138
  background-color: rgba(0, 0, 0, 0.6) !important;
139
  border: 1px solid #A0A0A0 !important;
140
  color: #E0E0E0 !important;
141
- }}
142
 
143
- /* Inputs and Text */
144
- textarea, input {{
145
  background-color: rgba(0, 0, 0, 0.5) !important;
146
  border: 1px solid #FFFFFF !important;
147
  color: white !important;
148
- }}
149
- label, span, p, .prose {{
150
  color: #FFFFFF !important;
151
  text-shadow: 1px 1px 2px black;
152
- }}
153
 
154
- footer {{visibility: hidden}}
 
155
  """
156
 
157
  max_new_tokens_slider = gr.Slider(
@@ -170,12 +193,9 @@ demo = gr.ChatInterface(
170
  description="Stay frosty. Chat with the fine-tuned model.",
171
  additional_inputs=[max_new_tokens_slider, style_radio],
172
  additional_inputs_accordion="Settings",
 
 
173
  )
174
 
175
- # Apply configuration
176
- demo.theme = winter_theme
177
- demo.css = custom_css
178
-
179
  if __name__ == "__main__":
180
- # allowed_paths=["."] is a backup, but base64 should solve it.
181
  demo.launch(allowed_paths=["."])
 
13
  GGUF_FILENAME = "model-q4_k_m.gguf"
14
 
15
  print(f"Downloading GGUF model {MODEL_REPO}/{GGUF_FILENAME} ...")
16
+ try:
17
+ model_path = hf_hub_download(
18
+ repo_id=MODEL_REPO,
19
+ filename=GGUF_FILENAME,
20
+ )
21
+ except Exception as e:
22
+ print(f"Error downloading model: {e}")
23
+ # Fallback for debugging if download fails
24
+ model_path = ""
25
+
26
+ if model_path:
27
+ print("Initializing llama.cpp LLM ...")
28
+ llm = Llama(
29
+ model_path=model_path,
30
+ n_ctx=2048,
31
+ n_threads=2,
32
+ n_batch=64,
33
+ use_mmap=True,
34
+ use_mlock=False,
35
+ )
36
+ else:
37
+ print("WARNING: Model path invalid. App will start but chat will fail.")
38
 
39
  # --- 3. Image Handling (The Fix) ---
40
+ def get_background_image_css():
41
+ """
42
+ Tries to load local image. If missing, uses a URL fallback.
43
+ Returns the CSS string for the background.
44
  """
45
+ local_img = "Cute-Christmas-Background-edit-online-1.jpg"
46
+
47
+ # 1. Try Local Base64
48
+ if os.path.exists(local_img):
49
+ print(f"Found local image: {local_img}. Encoding...")
50
+ with open(local_img, "rb") as f:
51
+ encoded = base64.b64encode(f.read()).decode('utf-8')
52
+ img_url = f"data:image/jpeg;base64,{encoded}"
53
+ # 2. Fallback to URL if local fails
54
+ else:
55
+ print(f"⚠️ Could not find {local_img}. Using fallback URL.")
56
+ img_url = "https://images.unsplash.com/photo-1544976735-a10c71a39644?q=80&w=2560&auto=format&fit=crop"
57
+
58
+ return f"""
59
+ body, .gradio-container, .gradio-app {{
60
+ background-image: url('{img_url}') !important;
61
+ background-size: cover !important;
62
+ background-position: center center !important;
63
+ background-attachment: fixed !important;
64
+ background-repeat: no-repeat !important;
65
+ background-color: #0b121e !important; /* Fallback color */
66
+ }}
67
  """
 
 
 
 
 
68
 
69
+ bg_css_rule = get_background_image_css()
 
 
70
 
71
  # --- 4. Prompts & Logic ---
72
  STYLE_SYSTEM_PROMPTS = {
 
100
  return "".join(prompt_parts)
101
 
102
  def chat_fn(message, history, max_new_tokens, style):
103
+ # Safe defaults
104
  temperature = 0.7
105
  top_p = 0.9
106
  repetition_penalty = 1.1
107
  prompt = build_prompt(message, history, style)
108
 
109
+ # Check if LLM loaded correctly
110
+ if 'llm' not in globals():
111
+ return "Error: Model not loaded."
112
+
113
  output = llm(
114
  prompt,
115
  max_tokens=int(max_new_tokens),
 
121
  return output["choices"][0]["text"].strip()
122
 
123
  # --- 5. Theme & CSS ---
124
+
125
+ # A robust theme base
126
  winter_theme = gr.themes.Soft(
127
  primary_hue="blue",
128
  neutral_hue="slate",
129
  ).set(
130
  body_background_fill="transparent",
131
+ block_background_fill="rgba(15, 23, 42, 0.7)",
132
  border_color_primary="#FFFFFF",
133
  button_primary_background_fill="#FFFFFF",
134
  button_primary_text_color="#000000",
135
  )
136
 
137
+ # Combined CSS
138
+ custom_css = bg_css_rule + """
139
+ /* Make the App Wrapper Transparent */
140
+ .gradio-container {
 
 
 
 
 
 
 
 
 
141
  background: transparent !important;
142
+ }
143
 
144
+ /* Chat & Settings Blocks - Frosty Glass with White Borders */
145
+ .group, .form, .bubble-wrap, .chatbot {
146
+ background-color: rgba(15, 23, 42, 0.8) !important;
147
  border: 2px solid #FFFFFF !important;
148
  border-radius: 12px !important;
149
+ backdrop-filter: blur(5px);
150
  box-shadow: 0 4px 15px rgba(0,0,0,0.5);
151
+ }
152
 
153
+ /* Specific Chat Bubble Coloring */
154
+ .user-message {
155
  background-color: rgba(255, 255, 255, 0.2) !important;
156
  border: 1px solid #FFFFFF !important;
157
  color: #FFFFFF !important;
158
+ }
159
+ .bot-message {
160
  background-color: rgba(0, 0, 0, 0.6) !important;
161
  border: 1px solid #A0A0A0 !important;
162
  color: #E0E0E0 !important;
163
+ }
164
 
165
+ /* Text Colors */
166
+ textarea, input {
167
  background-color: rgba(0, 0, 0, 0.5) !important;
168
  border: 1px solid #FFFFFF !important;
169
  color: white !important;
170
+ }
171
+ label, span, p, .prose {
172
  color: #FFFFFF !important;
173
  text-shadow: 1px 1px 2px black;
174
+ }
175
 
176
+ /* Hide Footer */
177
+ footer {visibility: hidden}
178
  """
179
 
180
  max_new_tokens_slider = gr.Slider(
 
193
  description="Stay frosty. Chat with the fine-tuned model.",
194
  additional_inputs=[max_new_tokens_slider, style_radio],
195
  additional_inputs_accordion="Settings",
196
+ theme=winter_theme,
197
+ css=custom_css
198
  )
199
 
 
 
 
 
200
  if __name__ == "__main__":
 
201
  demo.launch(allowed_paths=["."])