Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -62,7 +62,7 @@ def init_model(state):
|
|
| 62 |
"""Callback function for the Load button"""
|
| 63 |
try:
|
| 64 |
if state.get("llm") is not None:
|
| 65 |
-
return "
|
| 66 |
|
| 67 |
log("Received load request...")
|
| 68 |
# Download and load
|
|
@@ -72,17 +72,17 @@ def init_model(state):
|
|
| 72 |
state["llm"] = llm
|
| 73 |
state["gguf_path"] = gguf_path
|
| 74 |
|
| 75 |
-
return "
|
| 76 |
except Exception as exc:
|
| 77 |
tb = traceback.format_exc()
|
| 78 |
log(f"Initialization Error: {exc}\n{tb}")
|
| 79 |
-
return f"
|
| 80 |
|
| 81 |
def generate_response(prompt: str, max_tokens: int, state):
|
| 82 |
"""Callback function for the Generate button"""
|
| 83 |
try:
|
| 84 |
if not prompt or prompt.strip() == "":
|
| 85 |
-
return "
|
| 86 |
|
| 87 |
# Lazy loading: attempt to auto-load if Generate is clicked without explicit initialization
|
| 88 |
if state.get("llm") is None:
|
|
@@ -92,7 +92,7 @@ def generate_response(prompt: str, max_tokens: int, state):
|
|
| 92 |
state["llm"] = llm
|
| 93 |
state["gguf_path"] = gguf_path
|
| 94 |
except Exception as e:
|
| 95 |
-
return f"
|
| 96 |
|
| 97 |
llm = state.get("llm")
|
| 98 |
|
|
@@ -115,15 +115,15 @@ def generate_response(prompt: str, max_tokens: int, state):
|
|
| 115 |
|
| 116 |
text = output['choices'][0]['text']
|
| 117 |
log("Generation complete.")
|
| 118 |
-
return text, "
|
| 119 |
except Exception as exc:
|
| 120 |
tb = traceback.format_exc()
|
| 121 |
log(f"Generation Error: {exc}\n{tb}")
|
| 122 |
-
return f"Runtime Error: {exc}", f"
|
| 123 |
|
| 124 |
def soft_clear(current_state):
|
| 125 |
"""Clear button: only clears text, keeps the model loaded"""
|
| 126 |
-
status = "
|
| 127 |
return "", status, current_state
|
| 128 |
|
| 129 |
# ---------------- Gradio UI Construction ----------------
|
|
@@ -141,7 +141,7 @@ with gr.Blocks(title="Llama 3.2 Lab2 Project") as demo:
|
|
| 141 |
# Header
|
| 142 |
with gr.Row():
|
| 143 |
with gr.Column(scale=1):
|
| 144 |
-
gr.Markdown("#
|
| 145 |
gr.Markdown(
|
| 146 |
f"""
|
| 147 |
**ID2223 Lab 2 Project** | Fine-tuned on **FineTome-100k**.
|
|
@@ -149,7 +149,7 @@ with gr.Blocks(title="Llama 3.2 Lab2 Project") as demo:
|
|
| 149 |
"""
|
| 150 |
)
|
| 151 |
with gr.Column(scale=0, min_width=150):
|
| 152 |
-
status_label = gr.Label(value="
|
| 153 |
|
| 154 |
# Main layout
|
| 155 |
with gr.Row():
|
|
@@ -163,7 +163,7 @@ with gr.Blocks(title="Llama 3.2 Lab2 Project") as demo:
|
|
| 163 |
elem_id="prompt-input"
|
| 164 |
)
|
| 165 |
|
| 166 |
-
with gr.Accordion("
|
| 167 |
max_tokens = gr.Slider(
|
| 168 |
minimum=16,
|
| 169 |
maximum=1024,
|
|
@@ -174,10 +174,10 @@ with gr.Blocks(title="Llama 3.2 Lab2 Project") as demo:
|
|
| 174 |
)
|
| 175 |
|
| 176 |
with gr.Row():
|
| 177 |
-
init_btn = gr.Button("
|
| 178 |
-
gen_btn = gr.Button("
|
| 179 |
|
| 180 |
-
clear_btn = gr.Button("
|
| 181 |
|
| 182 |
# Right: Output Display
|
| 183 |
with gr.Column(scale=6):
|
|
@@ -189,7 +189,7 @@ with gr.Blocks(title="Llama 3.2 Lab2 Project") as demo:
|
|
| 189 |
# Footer
|
| 190 |
with gr.Row():
|
| 191 |
gr.Markdown(
|
| 192 |
-
"
|
| 193 |
elem_classes=["footer-text"]
|
| 194 |
)
|
| 195 |
|
|
|
|
| 62 |
"""Callback function for the Load button"""
|
| 63 |
try:
|
| 64 |
if state.get("llm") is not None:
|
| 65 |
+
return " System Ready (Model Loaded)", state
|
| 66 |
|
| 67 |
log("Received load request...")
|
| 68 |
# Download and load
|
|
|
|
| 72 |
state["llm"] = llm
|
| 73 |
state["gguf_path"] = gguf_path
|
| 74 |
|
| 75 |
+
return " System Ready", state
|
| 76 |
except Exception as exc:
|
| 77 |
tb = traceback.format_exc()
|
| 78 |
log(f"Initialization Error: {exc}\n{tb}")
|
| 79 |
+
return f" Initialization Failed: {exc}", state
|
| 80 |
|
| 81 |
def generate_response(prompt: str, max_tokens: int, state):
|
| 82 |
"""Callback function for the Generate button"""
|
| 83 |
try:
|
| 84 |
if not prompt or prompt.strip() == "":
|
| 85 |
+
return " Please enter an instruction.", " Idle", state
|
| 86 |
|
| 87 |
# Lazy loading: attempt to auto-load if Generate is clicked without explicit initialization
|
| 88 |
if state.get("llm") is None:
|
|
|
|
| 92 |
state["llm"] = llm
|
| 93 |
state["gguf_path"] = gguf_path
|
| 94 |
except Exception as e:
|
| 95 |
+
return f" Model Load Failed: {e}", f" Error", state
|
| 96 |
|
| 97 |
llm = state.get("llm")
|
| 98 |
|
|
|
|
| 115 |
|
| 116 |
text = output['choices'][0]['text']
|
| 117 |
log("Generation complete.")
|
| 118 |
+
return text, " Generation Complete", state
|
| 119 |
except Exception as exc:
|
| 120 |
tb = traceback.format_exc()
|
| 121 |
log(f"Generation Error: {exc}\n{tb}")
|
| 122 |
+
return f"Runtime Error: {exc}", f" Exception", state
|
| 123 |
|
| 124 |
def soft_clear(current_state):
|
| 125 |
"""Clear button: only clears text, keeps the model loaded"""
|
| 126 |
+
status = " System Ready" if current_state.get("llm") else " Not Initialized"
|
| 127 |
return "", status, current_state
|
| 128 |
|
| 129 |
# ---------------- Gradio UI Construction ----------------
|
|
|
|
| 141 |
# Header
|
| 142 |
with gr.Row():
|
| 143 |
with gr.Column(scale=1):
|
| 144 |
+
gr.Markdown("# Llama 3.2 (1B) Fine-Tuned Chatbot")
|
| 145 |
gr.Markdown(
|
| 146 |
f"""
|
| 147 |
**ID2223 Lab 2 Project** | Fine-tuned on **FineTome-100k**.
|
|
|
|
| 149 |
"""
|
| 150 |
)
|
| 151 |
with gr.Column(scale=0, min_width=150):
|
| 152 |
+
status_label = gr.Label(value=" Not Initialized", label="System Status", show_label=False)
|
| 153 |
|
| 154 |
# Main layout
|
| 155 |
with gr.Row():
|
|
|
|
| 163 |
elem_id="prompt-input"
|
| 164 |
)
|
| 165 |
|
| 166 |
+
with gr.Accordion(" Advanced Parameters", open=False):
|
| 167 |
max_tokens = gr.Slider(
|
| 168 |
minimum=16,
|
| 169 |
maximum=1024,
|
|
|
|
| 174 |
)
|
| 175 |
|
| 176 |
with gr.Row():
|
| 177 |
+
init_btn = gr.Button(" 1. Load Model", variant="secondary")
|
| 178 |
+
gen_btn = gr.Button(" 2. Generate Response", variant="primary")
|
| 179 |
|
| 180 |
+
clear_btn = gr.Button(" Clear Chat", variant="stop")
|
| 181 |
|
| 182 |
# Right: Output Display
|
| 183 |
with gr.Column(scale=6):
|
|
|
|
| 189 |
# Footer
|
| 190 |
with gr.Row():
|
| 191 |
gr.Markdown(
|
| 192 |
+
" *Note: Inference runs on a free CPU, so speed may be slow. The model (approx. 2GB) must be downloaded on first run, please be patient.*",
|
| 193 |
elem_classes=["footer-text"]
|
| 194 |
)
|
| 195 |
|