Spaces:
Build error
Build error
gusreinaos
commited on
Commit
Β·
f3adf75
1
Parent(s):
45506bc
Add UI with default model for now
Browse files- README.md +31 -5
- app.py +202 -0
- requirements.txt +2 -0
README.md
CHANGED
|
@@ -1,12 +1,38 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
colorFrom: blue
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
|
|
|
| 10 |
---
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Fine-Tuned Llama 3.2 Chatbot
|
| 3 |
+
emoji: π¦
|
| 4 |
colorFrom: blue
|
| 5 |
+
colorTo: purple
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 4.44.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
+
license: apache-2.0
|
| 11 |
---
|
| 12 |
|
| 13 |
+
# Fine-Tuned Llama 3.2 3B Chatbot
|
| 14 |
+
|
| 15 |
+
This Space hosts a chatbot powered by a fine-tuned Llama 3.2 3B model.
|
| 16 |
+
|
| 17 |
+
## Model Details
|
| 18 |
+
|
| 19 |
+
- **Base Model:** Llama 3.2 3B Instruct
|
| 20 |
+
- **Fine-tuning Method:** LoRA (Low-Rank Adaptation)
|
| 21 |
+
- **Dataset:** FineTome-100k instruction dataset
|
| 22 |
+
- **Format:** GGUF (q4_k_m quantization)
|
| 23 |
+
- **Inference:** CPU-based using llama.cpp
|
| 24 |
+
|
| 25 |
+
## Training
|
| 26 |
+
|
| 27 |
+
The model was fine-tuned using:
|
| 28 |
+
- Parameter Efficient Fine-Tuning (PEFT) with LoRA
|
| 29 |
+
- 4-bit quantization during training
|
| 30 |
+
- Trained on 100,000 high-quality instruction-response pairs
|
| 31 |
+
|
| 32 |
+
## Usage
|
| 33 |
+
|
| 34 |
+
Simply type your message in the chat box and the model will respond!
|
| 35 |
+
|
| 36 |
+
## Course
|
| 37 |
+
|
| 38 |
+
This project was completed as part of the ID2223 Scalable Machine Learning course at KTH.
|
app.py
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from llama_cpp import Llama
|
| 3 |
+
from huggingface_hub import hf_hub_download
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Download a pre-made GGUF model from HuggingFace
|
| 7 |
+
MODEL_NAME = "TheBloke/Llama-2-7B-Chat-GGUF"
|
| 8 |
+
MODEL_FILE = "llama-2-7b-chat.Q4_K_M.gguf"
|
| 9 |
+
|
| 10 |
+
print("π₯ Downloading model from HuggingFace...")
|
| 11 |
+
model_path = hf_hub_download(
|
| 12 |
+
repo_id=MODEL_NAME,
|
| 13 |
+
filename=MODEL_FILE,
|
| 14 |
+
local_dir="./models"
|
| 15 |
+
)
|
| 16 |
+
print(f"β
Model downloaded to: {model_path}")
|
| 17 |
+
|
| 18 |
+
print("π Loading model...")
|
| 19 |
+
llm = Llama(
|
| 20 |
+
model_path=model_path,
|
| 21 |
+
n_ctx=2048,
|
| 22 |
+
n_threads=4,
|
| 23 |
+
n_gpu_layers=0,
|
| 24 |
+
verbose=False
|
| 25 |
+
)
|
| 26 |
+
print("β
Model loaded!")
|
| 27 |
+
|
| 28 |
+
def chat(message, history):
|
| 29 |
+
prompt = "<|begin_of_text|>"
|
| 30 |
+
|
| 31 |
+
for user_msg, bot_msg in history:
|
| 32 |
+
prompt += f"<|start_header_id|>user<|end_header_id|>\n\n{user_msg}<|eot_id|>"
|
| 33 |
+
prompt += f"<|start_header_id|>assistant<|end_header_id|>\n\n{bot_msg}<|eot_id|>"
|
| 34 |
+
|
| 35 |
+
prompt += f"<|start_header_id|>user<|end_header_id|>\n\n{message}<|eot_id|>"
|
| 36 |
+
prompt += "<|start_header_id|>assistant<|end_header_id|>\n\n"
|
| 37 |
+
|
| 38 |
+
response = llm(
|
| 39 |
+
prompt,
|
| 40 |
+
max_tokens=512,
|
| 41 |
+
temperature=0.7,
|
| 42 |
+
top_p=0.9,
|
| 43 |
+
stop=["<|eot_id|>", "<|start_header_id|>"],
|
| 44 |
+
echo=False
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
return response['choices'][0]['text'].strip()
|
| 48 |
+
|
| 49 |
+
# Ultra-modern CSS
|
| 50 |
+
custom_css = """
|
| 51 |
+
@import url('https://fonts.googleapis.com/css2?family=Space+Grotesk:wght@400;600;700&display=swap');
|
| 52 |
+
|
| 53 |
+
* {
|
| 54 |
+
font-family: 'Space Grotesk', sans-serif !important;
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
.gradio-container {
|
| 58 |
+
background: linear-gradient(135deg, #1e3a8a 0%, #7c3aed 50%, #db2777 100%) !important;
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
#chatbot {
|
| 62 |
+
height: 650px !important;
|
| 63 |
+
border-radius: 24px !important;
|
| 64 |
+
border: 2px solid rgba(255,255,255,0.1) !important;
|
| 65 |
+
box-shadow: 0 25px 50px -12px rgba(0,0,0,0.5) !important;
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
.message {
|
| 69 |
+
padding: 18px 24px !important;
|
| 70 |
+
border-radius: 20px !important;
|
| 71 |
+
font-size: 15px !important;
|
| 72 |
+
margin: 8px 0 !important;
|
| 73 |
+
backdrop-filter: blur(10px) !important;
|
| 74 |
+
box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.37) !important;
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
.user {
|
| 78 |
+
background: linear-gradient(135deg, rgba(147, 51, 234, 0.9) 0%, rgba(219, 39, 119, 0.9) 100%) !important;
|
| 79 |
+
color: white !important;
|
| 80 |
+
border: 1px solid rgba(255,255,255,0.2) !important;
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
.bot {
|
| 84 |
+
background: linear-gradient(135deg, rgba(59, 130, 246, 0.9) 0%, rgba(147, 51, 234, 0.9) 100%) !important;
|
| 85 |
+
color: white !important;
|
| 86 |
+
border: 1px solid rgba(255,255,255,0.2) !important;
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
button {
|
| 90 |
+
border-radius: 16px !important;
|
| 91 |
+
font-weight: 600 !important;
|
| 92 |
+
transition: all 0.3s ease !important;
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
button:hover {
|
| 96 |
+
transform: translateY(-2px) !important;
|
| 97 |
+
box-shadow: 0 12px 24px rgba(0,0,0,0.3) !important;
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
.primary {
|
| 101 |
+
background: linear-gradient(135deg, #9333ea 0%, #db2777 100%) !important;
|
| 102 |
+
border: none !important;
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
input, textarea {
|
| 106 |
+
border-radius: 16px !important;
|
| 107 |
+
border: 2px solid rgba(255,255,255,0.2) !important;
|
| 108 |
+
background: rgba(255,255,255,0.1) !important;
|
| 109 |
+
backdrop-filter: blur(10px) !important;
|
| 110 |
+
color: white !important;
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
input::placeholder, textarea::placeholder {
|
| 114 |
+
color: rgba(255,255,255,0.6) !important;
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
.prose {
|
| 118 |
+
color: white !important;
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
.prose h1 {
|
| 122 |
+
background: linear-gradient(135deg, #fbbf24 0%, #f59e0b 100%);
|
| 123 |
+
-webkit-background-clip: text;
|
| 124 |
+
-webkit-text-fill-color: transparent;
|
| 125 |
+
font-weight: 700 !important;
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
footer {
|
| 129 |
+
display: none !important;
|
| 130 |
+
}
|
| 131 |
+
"""
|
| 132 |
+
|
| 133 |
+
with gr.Blocks(
|
| 134 |
+
theme=gr.themes.Glass(
|
| 135 |
+
primary_hue="purple",
|
| 136 |
+
secondary_hue="pink",
|
| 137 |
+
),
|
| 138 |
+
css=custom_css,
|
| 139 |
+
title="π¦ Llama 3.2 AI"
|
| 140 |
+
) as demo:
|
| 141 |
+
|
| 142 |
+
gr.Markdown(
|
| 143 |
+
"""
|
| 144 |
+
# π¦ Llama Chat AI Assistant (TEST)
|
| 145 |
+
### β‘ Testing deployment with pre-trained model
|
| 146 |
+
"""
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
chatbot = gr.Chatbot(
|
| 150 |
+
elem_id="chatbot",
|
| 151 |
+
bubble_full_width=False,
|
| 152 |
+
avatar_images=(
|
| 153 |
+
"https://em-content.zobj.net/thumbs/120/apple/354/sparkles_2728.png",
|
| 154 |
+
"https://em-content.zobj.net/thumbs/120/apple/354/llama_1f999.png"
|
| 155 |
+
),
|
| 156 |
+
height=650,
|
| 157 |
+
show_copy_button=True,
|
| 158 |
+
likeable=True
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
+
with gr.Row():
|
| 162 |
+
msg = gr.Textbox(
|
| 163 |
+
placeholder="β¨ Ask me anything...",
|
| 164 |
+
show_label=False,
|
| 165 |
+
scale=8,
|
| 166 |
+
container=False
|
| 167 |
+
)
|
| 168 |
+
submit = gr.Button("Send π", scale=1, variant="primary", size="lg")
|
| 169 |
+
|
| 170 |
+
gr.Examples(
|
| 171 |
+
examples=[
|
| 172 |
+
"π What is the capital of France?",
|
| 173 |
+
"π§ Explain quantum computing simply",
|
| 174 |
+
"π» Write fibonacci in Python",
|
| 175 |
+
"π΄ Tips for better sleep?",
|
| 176 |
+
"π’ Continue: 2, 4, 6, 8...",
|
| 177 |
+
"π Write a haiku about AI",
|
| 178 |
+
],
|
| 179 |
+
inputs=msg,
|
| 180 |
+
label="π‘ Quick Start:"
|
| 181 |
+
)
|
| 182 |
+
|
| 183 |
+
with gr.Accordion("βΉοΈ Model Information", open=False):
|
| 184 |
+
gr.Markdown(
|
| 185 |
+
"""
|
| 186 |
+
**Testing Model:**
|
| 187 |
+
- π€ Model: Llama 2 7B Chat (Pre-trained)
|
| 188 |
+
- βοΈ Format: GGUF (q4_k_m quantization)
|
| 189 |
+
- π Note: This is a test deployment. Will be replaced with fine-tuned model.
|
| 190 |
+
"""
|
| 191 |
+
)
|
| 192 |
+
|
| 193 |
+
clear = gr.ClearButton([msg, chatbot], value="ποΈ New Chat")
|
| 194 |
+
|
| 195 |
+
submit.click(chat, [msg, chatbot], [chatbot])
|
| 196 |
+
submit.click(lambda: "", None, msg)
|
| 197 |
+
msg.submit(chat, [msg, chatbot], [chatbot])
|
| 198 |
+
msg.submit(lambda: "", None, msg)
|
| 199 |
+
|
| 200 |
+
if __name__ == "__main__":
|
| 201 |
+
demo.queue()
|
| 202 |
+
demo.launch(share=False)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==4.44.0
|
| 2 |
+
llama-cpp-python==0.2.90
|