Update app.py
Browse files
app.py
CHANGED
|
@@ -5,40 +5,49 @@ from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
| 5 |
from peft import PeftModel
|
| 6 |
|
| 7 |
"""
|
| 8 |
-
๐งฎ Root_Math fine-tuned model chat app
|
| 9 |
-
|
| 10 |
"""
|
| 11 |
|
| 12 |
-
# โ
Load Hugging Face API token
|
| 13 |
api_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 14 |
if not api_token:
|
| 15 |
raise ValueError("โ ERROR: Hugging Face API token is not set. Please set it as an environment variable.")
|
| 16 |
|
| 17 |
-
# โ
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
# โ
Load base model
|
| 22 |
-
print("๐ Loading base model...")
|
| 23 |
base_model = AutoModelForCausalLM.from_pretrained(
|
| 24 |
base_model_name,
|
| 25 |
-
torch_dtype=
|
| 26 |
device_map="auto",
|
| 27 |
-
|
| 28 |
)
|
| 29 |
|
| 30 |
-
# โ
Load
|
| 31 |
-
print("๐ Loading fine-tuned adapter...")
|
| 32 |
model = PeftModel.from_pretrained(base_model, peft_model_name, token=api_token)
|
| 33 |
|
| 34 |
# โ
Load tokenizer
|
| 35 |
print("๐ Loading tokenizer...")
|
| 36 |
tokenizer = AutoTokenizer.from_pretrained(base_model_name, token=api_token)
|
| 37 |
|
| 38 |
-
|
| 39 |
-
# โ
Define the response function
|
| 40 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 41 |
-
"""Generate
|
| 42 |
full_prompt = system_message + "\n\n"
|
| 43 |
for user_msg, bot_msg in history:
|
| 44 |
if user_msg:
|
|
@@ -52,26 +61,22 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
| 52 |
with torch.no_grad():
|
| 53 |
outputs = model.generate(
|
| 54 |
**inputs,
|
| 55 |
-
max_new_tokens=max_tokens,
|
| 56 |
-
temperature=temperature,
|
| 57 |
-
top_p=top_p,
|
| 58 |
do_sample=True
|
| 59 |
)
|
| 60 |
|
| 61 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 62 |
-
|
| 63 |
-
# Extract only the assistant's last message
|
| 64 |
if "Assistant:" in response:
|
| 65 |
response = response.split("Assistant:")[-1].strip()
|
| 66 |
|
| 67 |
-
|
| 68 |
|
| 69 |
|
| 70 |
-
# โ
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
respond(message, history, system_message, max_tokens, temperature, top_p)
|
| 74 |
-
),
|
| 75 |
additional_inputs=[
|
| 76 |
gr.Textbox(value="You are a helpful math assistant.", label="System message"),
|
| 77 |
gr.Slider(minimum=1, maximum=1024, value=256, step=1, label="Max new tokens"),
|
|
@@ -79,30 +84,9 @@ chat_ui = gr.ChatInterface(
|
|
| 79 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
| 80 |
],
|
| 81 |
title="๐งฎ Root Math Assistant",
|
| 82 |
-
description="
|
| 83 |
)
|
| 84 |
|
| 85 |
-
|
| 86 |
-
# โ
Add API endpoint `/chat` (for gradio_client access)
|
| 87 |
-
api_chat = gr.Interface(
|
| 88 |
-
fn=respond,
|
| 89 |
-
inputs=[
|
| 90 |
-
gr.Textbox(label="Message"),
|
| 91 |
-
gr.State(), # placeholder for chat history (can be None)
|
| 92 |
-
gr.Textbox(value="You are a helpful math assistant.", label="System message"),
|
| 93 |
-
gr.Slider(minimum=1, maximum=1024, value=256, step=1, label="Max new tokens"),
|
| 94 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 95 |
-
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
| 96 |
-
],
|
| 97 |
-
outputs="text",
|
| 98 |
-
api_name="/chat"
|
| 99 |
-
)
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
# โ
Combine UI + API
|
| 103 |
-
demo = gr.TabbedInterface([chat_ui, api_chat], ["Chat", "API"])
|
| 104 |
-
|
| 105 |
-
|
| 106 |
# โ
Launch app
|
| 107 |
if __name__ == "__main__":
|
| 108 |
demo.launch()
|
|
|
|
| 5 |
from peft import PeftModel
|
| 6 |
|
| 7 |
"""
|
| 8 |
+
๐งฎ Root_Math fine-tuned model chat app
|
| 9 |
+
Auto-detects GPU/CPU and loads appropriate base model.
|
| 10 |
"""
|
| 11 |
|
| 12 |
+
# โ
Load Hugging Face API token
|
| 13 |
api_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 14 |
if not api_token:
|
| 15 |
raise ValueError("โ ERROR: Hugging Face API token is not set. Please set it as an environment variable.")
|
| 16 |
|
| 17 |
+
# โ
Detect environment
|
| 18 |
+
use_cuda = torch.cuda.is_available()
|
| 19 |
+
|
| 20 |
+
if use_cuda:
|
| 21 |
+
print("๐ GPU detected โ loading 4-bit quantized model for efficiency.")
|
| 22 |
+
base_model_name = "unsloth/qwen2.5-math-7b-bnb-4bit"
|
| 23 |
+
dtype = torch.float16
|
| 24 |
+
else:
|
| 25 |
+
print("๐ป CPU detected โ loading full-precision model (no quantization).")
|
| 26 |
+
base_model_name = "unsloth/qwen2.5-math-7b"
|
| 27 |
+
dtype = torch.float32
|
| 28 |
+
|
| 29 |
+
peft_model_name = "Hrushi02/Root_Math"
|
| 30 |
|
| 31 |
# โ
Load base model
|
| 32 |
+
print(f"๐ Loading base model: {base_model_name} ...")
|
| 33 |
base_model = AutoModelForCausalLM.from_pretrained(
|
| 34 |
base_model_name,
|
| 35 |
+
torch_dtype=dtype,
|
| 36 |
device_map="auto",
|
| 37 |
+
token=api_token
|
| 38 |
)
|
| 39 |
|
| 40 |
+
# โ
Load fine-tuned adapter
|
| 41 |
+
print(f"๐ Loading fine-tuned adapter: {peft_model_name} ...")
|
| 42 |
model = PeftModel.from_pretrained(base_model, peft_model_name, token=api_token)
|
| 43 |
|
| 44 |
# โ
Load tokenizer
|
| 45 |
print("๐ Loading tokenizer...")
|
| 46 |
tokenizer = AutoTokenizer.from_pretrained(base_model_name, token=api_token)
|
| 47 |
|
| 48 |
+
# โ
Response function
|
|
|
|
| 49 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 50 |
+
"""Generate a response using Root_Math model."""
|
| 51 |
full_prompt = system_message + "\n\n"
|
| 52 |
for user_msg, bot_msg in history:
|
| 53 |
if user_msg:
|
|
|
|
| 61 |
with torch.no_grad():
|
| 62 |
outputs = model.generate(
|
| 63 |
**inputs,
|
| 64 |
+
max_new_tokens=int(max_tokens),
|
| 65 |
+
temperature=float(temperature),
|
| 66 |
+
top_p=float(top_p),
|
| 67 |
do_sample=True
|
| 68 |
)
|
| 69 |
|
| 70 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
|
|
|
| 71 |
if "Assistant:" in response:
|
| 72 |
response = response.split("Assistant:")[-1].strip()
|
| 73 |
|
| 74 |
+
yield response
|
| 75 |
|
| 76 |
|
| 77 |
+
# โ
Gradio UI
|
| 78 |
+
demo = gr.ChatInterface(
|
| 79 |
+
respond,
|
|
|
|
|
|
|
| 80 |
additional_inputs=[
|
| 81 |
gr.Textbox(value="You are a helpful math assistant.", label="System message"),
|
| 82 |
gr.Slider(minimum=1, maximum=1024, value=256, step=1, label="Max new tokens"),
|
|
|
|
| 84 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
| 85 |
],
|
| 86 |
title="๐งฎ Root Math Assistant",
|
| 87 |
+
description="Fine-tuned by Hrushi02 using Unsloth + PEFT for mathematical reasoning."
|
| 88 |
)
|
| 89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
# โ
Launch app
|
| 91 |
if __name__ == "__main__":
|
| 92 |
demo.launch()
|