Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,92 +1,250 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import hf_hub_download
|
| 3 |
from llama_cpp import Llama
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
model_path = hf_hub_download(
|
| 8 |
repo_id="iamabhayaditya/EfficientMath-AI",
|
| 9 |
-
filename="Meta-Llama-3.1-8B.Q4_K_M.gguf"
|
| 10 |
)
|
| 11 |
|
| 12 |
-
|
|
|
|
| 13 |
print("Loading model into memory...")
|
|
|
|
| 14 |
llm = Llama(
|
| 15 |
model_path=model_path,
|
| 16 |
-
n_ctx=2048,
|
| 17 |
-
n_threads=4,
|
| 18 |
)
|
| 19 |
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
def solve_math_problem(question):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
try:
|
| 23 |
-
prompt = f"Below is a math word problem. Solve it step by step and provide the final answer.\n\n### Problem:\n{question}\n\n### Solution:\n"
|
| 24 |
-
|
| 25 |
stream = llm(
|
| 26 |
prompt,
|
| 27 |
max_tokens=256,
|
| 28 |
temperature=0.2,
|
| 29 |
top_p=0.9,
|
| 30 |
stream=True,
|
| 31 |
-
stop=[
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
)
|
| 33 |
-
|
| 34 |
-
generated_text = ""
|
| 35 |
for output in stream:
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
yield generated_text
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
custom_css = """
|
| 47 |
-
.gradio-container {
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
textarea {
|
| 51 |
border: 2px solid #ff7f00 !important;
|
| 52 |
background-color: #111111 !important;
|
| 53 |
color: #ffffff !important;
|
| 54 |
}
|
|
|
|
| 55 |
button.primary {
|
| 56 |
background: linear-gradient(90deg, #ff7f00, #ffaa00) !important;
|
| 57 |
border: none !important;
|
| 58 |
color: black !important;
|
| 59 |
font-weight: bold !important;
|
| 60 |
}
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
"""
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
gr.
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
with gr.Row():
|
| 69 |
with gr.Column(scale=1):
|
| 70 |
-
user_input = gr.Textbox(
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
gr.Examples(
|
| 73 |
examples=[
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
],
|
| 78 |
inputs=user_input,
|
| 79 |
-
label="Click an example below to test:"
|
| 80 |
)
|
| 81 |
-
|
| 82 |
with gr.Row():
|
| 83 |
clear_btn = gr.ClearButton([user_input])
|
| 84 |
-
submit_btn = gr.Button(
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
| 86 |
with gr.Column(scale=1):
|
| 87 |
-
model_output = gr.Textbox(
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
-
# Launch natively in Hugging Face (no debug mode, no share link needed)
|
| 92 |
app.launch()
|
|
|
|
| 1 |
+
import time
|
| 2 |
+
|
| 3 |
import gradio as gr
|
| 4 |
from huggingface_hub import hf_hub_download
|
| 5 |
from llama_cpp import Llama
|
| 6 |
|
| 7 |
+
|
| 8 |
+
# -----------------------------
|
| 9 |
+
# 1. Measure model startup time
|
| 10 |
+
# -----------------------------
|
| 11 |
+
|
| 12 |
+
startup_start = time.perf_counter()
|
| 13 |
+
|
| 14 |
+
print("Downloading/locating model from Hugging Face...")
|
| 15 |
+
|
| 16 |
model_path = hf_hub_download(
|
| 17 |
repo_id="iamabhayaditya/EfficientMath-AI",
|
| 18 |
+
filename="Meta-Llama-3.1-8B.Q4_K_M.gguf",
|
| 19 |
)
|
| 20 |
|
| 21 |
+
download_finished = time.perf_counter()
|
| 22 |
+
|
| 23 |
print("Loading model into memory...")
|
| 24 |
+
|
| 25 |
llm = Llama(
|
| 26 |
model_path=model_path,
|
| 27 |
+
n_ctx=2048,
|
| 28 |
+
n_threads=4,
|
| 29 |
)
|
| 30 |
|
| 31 |
+
model_loaded_finished = time.perf_counter()
|
| 32 |
+
|
| 33 |
+
download_time = download_finished - startup_start
|
| 34 |
+
model_load_time = model_loaded_finished - download_finished
|
| 35 |
+
total_startup_time = model_loaded_finished - startup_start
|
| 36 |
+
|
| 37 |
+
print(f"Model download time: {download_time:.2f} seconds")
|
| 38 |
+
print(f"Model loading time: {model_load_time:.2f} seconds")
|
| 39 |
+
print(f"Total startup time: {total_startup_time:.2f} seconds")
|
| 40 |
+
print("Model loaded successfully.")
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
# Used to identify the first request after the model is loaded
|
| 44 |
+
request_count = 0
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
# -----------------------------
|
| 48 |
+
# 2. Prediction function
|
| 49 |
+
# -----------------------------
|
| 50 |
+
|
| 51 |
def solve_math_problem(question):
|
| 52 |
+
global request_count
|
| 53 |
+
|
| 54 |
+
request_count += 1
|
| 55 |
+
request_number = request_count
|
| 56 |
+
|
| 57 |
+
prompt = (
|
| 58 |
+
"Below is a math word problem. Solve it step by step "
|
| 59 |
+
"and provide the final answer.\n\n"
|
| 60 |
+
f"### Problem:\n{question}\n\n"
|
| 61 |
+
"### Solution:\n"
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
request_start = time.perf_counter()
|
| 65 |
+
first_output_time = None
|
| 66 |
+
generated_text = ""
|
| 67 |
+
|
| 68 |
try:
|
|
|
|
|
|
|
| 69 |
stream = llm(
|
| 70 |
prompt,
|
| 71 |
max_tokens=256,
|
| 72 |
temperature=0.2,
|
| 73 |
top_p=0.9,
|
| 74 |
stream=True,
|
| 75 |
+
stop=[
|
| 76 |
+
"<|end_of_text|>",
|
| 77 |
+
"</s>",
|
| 78 |
+
"<|eot_id|>",
|
| 79 |
+
],
|
| 80 |
)
|
| 81 |
+
|
|
|
|
| 82 |
for output in stream:
|
| 83 |
+
new_text = output["choices"][0]["text"]
|
| 84 |
+
|
| 85 |
+
if first_output_time is None:
|
| 86 |
+
first_output_time = time.perf_counter()
|
| 87 |
+
|
| 88 |
+
generated_text += new_text
|
| 89 |
yield generated_text
|
| 90 |
+
|
| 91 |
+
request_finished = time.perf_counter()
|
| 92 |
+
|
| 93 |
+
total_latency = request_finished - request_start
|
| 94 |
+
|
| 95 |
+
if first_output_time is not None:
|
| 96 |
+
time_to_first_output = first_output_time - request_start
|
| 97 |
+
else:
|
| 98 |
+
time_to_first_output = None
|
| 99 |
+
|
| 100 |
+
# Count generated tokens after generation is complete
|
| 101 |
+
generated_tokens = len(
|
| 102 |
+
llm.tokenize(
|
| 103 |
+
generated_text.encode("utf-8"),
|
| 104 |
+
add_bos=False,
|
| 105 |
+
)
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
if total_latency > 0:
|
| 109 |
+
tokens_per_second = generated_tokens / total_latency
|
| 110 |
+
else:
|
| 111 |
+
tokens_per_second = 0
|
| 112 |
+
|
| 113 |
+
request_type = (
|
| 114 |
+
"FIRST REQUEST AFTER MODEL LOAD"
|
| 115 |
+
if request_number == 1
|
| 116 |
+
else "WARM REQUEST"
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
print("\n----- INFERENCE METRICS -----")
|
| 120 |
+
print(f"Request type: {request_type}")
|
| 121 |
+
print(f"Request number: {request_number}")
|
| 122 |
+
print(f"Total latency: {total_latency:.2f} seconds")
|
| 123 |
+
print(
|
| 124 |
+
f"Time to first output: "
|
| 125 |
+
f"{time_to_first_output:.2f} seconds"
|
| 126 |
+
if time_to_first_output is not None
|
| 127 |
+
else "Time to first output: unavailable"
|
| 128 |
+
)
|
| 129 |
+
print(f"Generated tokens: {generated_tokens}")
|
| 130 |
+
print(f"Throughput: {tokens_per_second:.2f} tokens/second")
|
| 131 |
+
print("-----------------------------\n")
|
| 132 |
+
|
| 133 |
+
except Exception as error:
|
| 134 |
+
print(f"Server error: {error}")
|
| 135 |
+
|
| 136 |
+
yield (
|
| 137 |
+
"I encountered an issue while solving the problem. "
|
| 138 |
+
"Please try again."
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
# -----------------------------
|
| 143 |
+
# 3. Gradio interface
|
| 144 |
+
# -----------------------------
|
| 145 |
+
|
| 146 |
custom_css = """
|
| 147 |
+
.gradio-container {
|
| 148 |
+
background-color: #000000 !important;
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
.markdown-text h1 {
|
| 152 |
+
color: #ff7f00 !important;
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
+
.markdown-text p {
|
| 156 |
+
color: #cccccc !important;
|
| 157 |
+
}
|
| 158 |
+
|
| 159 |
textarea {
|
| 160 |
border: 2px solid #ff7f00 !important;
|
| 161 |
background-color: #111111 !important;
|
| 162 |
color: #ffffff !important;
|
| 163 |
}
|
| 164 |
+
|
| 165 |
button.primary {
|
| 166 |
background: linear-gradient(90deg, #ff7f00, #ffaa00) !important;
|
| 167 |
border: none !important;
|
| 168 |
color: black !important;
|
| 169 |
font-weight: bold !important;
|
| 170 |
}
|
| 171 |
+
|
| 172 |
+
span.svelte-1gfkn6j,
|
| 173 |
+
.label {
|
| 174 |
+
color: #ff7f00 !important;
|
| 175 |
+
}
|
| 176 |
"""
|
| 177 |
|
| 178 |
+
|
| 179 |
+
with gr.Blocks(
|
| 180 |
+
theme=gr.themes.Monochrome(),
|
| 181 |
+
css=custom_css,
|
| 182 |
+
) as app:
|
| 183 |
+
gr.Markdown(
|
| 184 |
+
"<h1 style='text-align: center; margin-top: 20px;'>"
|
| 185 |
+
"EfficientMath-AI"
|
| 186 |
+
"</h1>"
|
| 187 |
+
)
|
| 188 |
+
|
| 189 |
+
gr.Markdown(
|
| 190 |
+
"<p style='text-align: center;'>"
|
| 191 |
+
"This is a custom fine-tuned Llama 3.1 8B model, "
|
| 192 |
+
"trained to solve grade school math word problems."
|
| 193 |
+
"</p>"
|
| 194 |
+
)
|
| 195 |
+
|
| 196 |
with gr.Row():
|
| 197 |
with gr.Column(scale=1):
|
| 198 |
+
user_input = gr.Textbox(
|
| 199 |
+
lines=5,
|
| 200 |
+
placeholder="Enter a math word problem here...",
|
| 201 |
+
label="Question",
|
| 202 |
+
)
|
| 203 |
+
|
| 204 |
gr.Examples(
|
| 205 |
examples=[
|
| 206 |
+
(
|
| 207 |
+
"A bag containing 30 apples weighs 6 kg. "
|
| 208 |
+
"How much will 1080 apples weigh?"
|
| 209 |
+
),
|
| 210 |
+
(
|
| 211 |
+
"If the cost of 18 apples is 90 rupees, "
|
| 212 |
+
"what is the cost of 24 apples?"
|
| 213 |
+
),
|
| 214 |
+
(
|
| 215 |
+
"Abhay has 16 apples, he borrowed 5 from Akash "
|
| 216 |
+
"then gave 14 to Shivam. "
|
| 217 |
+
"How many apples is Abhay left with?"
|
| 218 |
+
),
|
| 219 |
],
|
| 220 |
inputs=user_input,
|
| 221 |
+
label="Click an example below to test:",
|
| 222 |
)
|
| 223 |
+
|
| 224 |
with gr.Row():
|
| 225 |
clear_btn = gr.ClearButton([user_input])
|
| 226 |
+
submit_btn = gr.Button(
|
| 227 |
+
"Submit",
|
| 228 |
+
variant="primary",
|
| 229 |
+
)
|
| 230 |
+
|
| 231 |
with gr.Column(scale=1):
|
| 232 |
+
model_output = gr.Textbox(
|
| 233 |
+
label="Model Solution",
|
| 234 |
+
lines=5,
|
| 235 |
+
max_lines=50,
|
| 236 |
+
interactive=False,
|
| 237 |
+
)
|
| 238 |
+
|
| 239 |
+
submit_btn.click(
|
| 240 |
+
fn=solve_math_problem,
|
| 241 |
+
inputs=user_input,
|
| 242 |
+
outputs=model_output,
|
| 243 |
+
)
|
| 244 |
+
|
| 245 |
+
|
| 246 |
+
# -----------------------------
|
| 247 |
+
# 4. Launch Hugging Face Space
|
| 248 |
+
# -----------------------------
|
| 249 |
|
|
|
|
| 250 |
app.launch()
|