Jeppcode commited on
Commit
7101a3f
Β·
verified Β·
1 Parent(s): f74183e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -52
app.py CHANGED
@@ -3,8 +3,8 @@ import subprocess
3
  from huggingface_hub import hf_hub_download
4
 
5
  # 1. Install llama-cpp-python in runtime (not via requirements.txt)
6
- # Important: remove `llama-cpp-python` from requirements.txt
7
- # or Spaces will try to build from source and get stuck.
8
  subprocess.run("pip install -q 'llama_cpp_python==0.3.15'", shell=True, check=False)
9
  from llama_cpp import Llama
10
 
@@ -52,7 +52,7 @@ def _extract_text_from_content(content):
52
  In Gradio 6 ChatInterface, history uses the messages format.
53
  content can be:
54
  - a string
55
- - a list of blocks: [{"type": "text", "text": "..."} , ...]
56
  We convert it into a simple string.
57
  """
58
  if isinstance(content, list):
@@ -89,7 +89,7 @@ def build_prompt(message, history, style):
89
  prompt_parts.append(f"System: {system_prompt}\n")
90
  prompt_parts.append("Conversation:\n")
91
 
92
- # history is a list of dicts: {"role": "...", "content": ...}
93
  for msg in history or []:
94
  role = msg.get("role")
95
  content = _extract_text_from_content(msg.get("content", ""))
@@ -187,10 +187,9 @@ style_radio = gr.Radio(
187
  label="Answer style",
188
  )
189
 
190
- # 5. Christmas theme CSS
191
- # Assumes the image "Cute-Christmas-Background-edit-online-1.jpg"
192
- # is in the Space repository root, so we can reference it as file=...
193
- christmas_css = """
194
  body {
195
  background: radial-gradient(circle at top, #1b1c2b 0, #050611 55%, #000000 100%);
196
  color: #fdf6e3;
@@ -368,53 +367,43 @@ input[type="range"] {
368
  background: rgba(255, 255, 255, 0.5);
369
  border-radius: 999px;
370
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
371
  """
372
 
373
 
374
- # 6. Build the UI with Blocks so we can add a hero section
375
- with gr.Blocks(css=christmas_css) as demo:
376
- gr.HTML(
377
- """
378
- <div class="hero">
379
- <div class="hero-inner">
380
- <div class="hero-badge"></div>
381
- <div class="hero-text">
382
- <h1>Scalable Lab 2 Christmas Chat</h1>
383
- <p>
384
- Talk to our fine tuned Llama based model, wrapped as a compact GGUF
385
- and running on CPU. Use the controls below to tune response length,
386
- randomness and style like a Christmas DJ for language models.
387
- </p>
388
- </div>
389
- </div>
390
- <div class="hero-keyline"></div>
391
- </div>
392
- """
393
- )
394
-
395
- gr.Markdown(
396
- "πŸŽ„ **Tip:** Try switching between short answers and step by step reasoning, "
397
- "and play with temperature and top p to see how the model behaves."
398
- )
399
-
400
- gr.ChatInterface(
401
- fn=chat_fn,
402
- title="Lab 2 – Fine tuned GGUF model",
403
- description=(
404
- "Chat with our fine tuned Llama based model, converted to GGUF and "
405
- "loaded via llama.cpp from `Jeppcode/ScalableLab2`.\n\n"
406
- "Use the controls in the accordion below like a DJ board to tweak "
407
- "response length, randomness and style."
408
- ),
409
- additional_inputs=[
410
- max_new_tokens_slider,
411
- temperature_slider,
412
- top_p_slider,
413
- repetition_penalty_slider,
414
- style_radio,
415
- ],
416
- additional_inputs_accordion="Generation controls",
417
- )
418
 
419
  if __name__ == "__main__":
420
  demo.launch()
 
3
  from huggingface_hub import hf_hub_download
4
 
5
  # 1. Install llama-cpp-python in runtime (not via requirements.txt)
6
+ # Important: remove `llama-cpp-python` from requirements.txt,
7
+ # otherwise Spaces may try to build from source and get stuck.
8
  subprocess.run("pip install -q 'llama_cpp_python==0.3.15'", shell=True, check=False)
9
  from llama_cpp import Llama
10
 
 
52
  In Gradio 6 ChatInterface, history uses the messages format.
53
  content can be:
54
  - a string
55
+ - a list of blocks: [{'type': 'text', 'text': '...'} , ...]
56
  We convert it into a simple string.
57
  """
58
  if isinstance(content, list):
 
89
  prompt_parts.append(f"System: {system_prompt}\n")
90
  prompt_parts.append("Conversation:\n")
91
 
92
+ # history is a list of dicts: {'role': 'user'/'assistant'/'system', 'content': ...}
93
  for msg in history or []:
94
  role = msg.get("role")
95
  content = _extract_text_from_content(msg.get("content", ""))
 
187
  label="Answer style",
188
  )
189
 
190
+ # 5. Christmas theme: inject CSS + hero directly into description
191
+ christmas_style_and_hero = """
192
+ <style>
 
193
  body {
194
  background: radial-gradient(circle at top, #1b1c2b 0, #050611 55%, #000000 100%);
195
  color: #fdf6e3;
 
367
  background: rgba(255, 255, 255, 0.5);
368
  border-radius: 999px;
369
  }
370
+ </style>
371
+
372
+ <div class="hero">
373
+ <div class="hero-inner">
374
+ <div class="hero-badge"></div>
375
+ <div class="hero-text">
376
+ <h1>Scalable Lab 2 Christmas Chat</h1>
377
+ <p>
378
+ Talk to our fine tuned Llama based model, wrapped as a compact GGUF
379
+ and running on CPU. Use the controls in the accordion below to tune
380
+ response length, randomness and style like a Christmas DJ for language models.
381
+ </p>
382
+ </div>
383
+ </div>
384
+ <div class="hero-keyline"></div>
385
+ </div>
386
+
387
+ <p>
388
+ πŸŽ„ <strong>Tip:</strong> Try switching between short answers and step by step reasoning,
389
+ and play with temperature and top p to see how the model behaves.
390
+ </p>
391
  """
392
 
393
 
394
+ demo = gr.ChatInterface(
395
+ fn=chat_fn,
396
+ title="Lab 2 – Fine-tuned GGUF model",
397
+ description=christmas_style_and_hero,
398
+ additional_inputs=[
399
+ max_new_tokens_slider,
400
+ temperature_slider,
401
+ top_p_slider,
402
+ repetition_penalty_slider,
403
+ style_radio,
404
+ ],
405
+ additional_inputs_accordion="Generation controls",
406
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
407
 
408
  if __name__ == "__main__":
409
  demo.launch()