Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,8 @@ from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
| 5 |
from peft import PeftModel
|
| 6 |
|
| 7 |
"""
|
| 8 |
-
Root_Math fine-tuned model chat app for Hugging Face Spaces.
|
|
|
|
| 9 |
"""
|
| 10 |
|
| 11 |
# โ
Load Hugging Face API token securely
|
|
@@ -15,7 +16,7 @@ if not api_token:
|
|
| 15 |
|
| 16 |
# โ
Define model names
|
| 17 |
base_model_name = "unsloth/qwen2.5-math-7b-bnb-4bit"
|
| 18 |
-
peft_model_name = "Hrushi02/Root_Math" # <-- stays the same
|
| 19 |
|
| 20 |
# โ
Load base model
|
| 21 |
print("๐ Loading base model...")
|
|
@@ -34,7 +35,8 @@ model = PeftModel.from_pretrained(base_model, peft_model_name, token=api_token)
|
|
| 34 |
print("๐ Loading tokenizer...")
|
| 35 |
tokenizer = AutoTokenizer.from_pretrained(base_model_name, token=api_token)
|
| 36 |
|
| 37 |
-
|
|
|
|
| 38 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 39 |
"""Generate responses from your fine-tuned model."""
|
| 40 |
full_prompt = system_message + "\n\n"
|
|
@@ -57,16 +59,19 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
| 57 |
)
|
| 58 |
|
| 59 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 60 |
-
|
|
|
|
| 61 |
if "Assistant:" in response:
|
| 62 |
response = response.split("Assistant:")[-1].strip()
|
| 63 |
|
| 64 |
-
|
| 65 |
|
| 66 |
|
| 67 |
-
# โ
Create Gradio
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
| 70 |
additional_inputs=[
|
| 71 |
gr.Textbox(value="You are a helpful math assistant.", label="System message"),
|
| 72 |
gr.Slider(minimum=1, maximum=1024, value=256, step=1, label="Max new tokens"),
|
|
@@ -78,6 +83,26 @@ demo = gr.ChatInterface(
|
|
| 78 |
)
|
| 79 |
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
# โ
Launch app
|
| 82 |
if __name__ == "__main__":
|
| 83 |
demo.launch()
|
|
|
|
| 5 |
from peft import PeftModel
|
| 6 |
|
| 7 |
"""
|
| 8 |
+
๐งฎ Root_Math fine-tuned model chat app for Hugging Face Spaces.
|
| 9 |
+
Supports both Gradio UI and API access via `/chat`.
|
| 10 |
"""
|
| 11 |
|
| 12 |
# โ
Load Hugging Face API token securely
|
|
|
|
| 16 |
|
| 17 |
# โ
Define model names
|
| 18 |
base_model_name = "unsloth/qwen2.5-math-7b-bnb-4bit"
|
| 19 |
+
peft_model_name = "Hrushi02/Root_Math" # <-- model name stays the same
|
| 20 |
|
| 21 |
# โ
Load base model
|
| 22 |
print("๐ Loading base model...")
|
|
|
|
| 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 responses from your fine-tuned model."""
|
| 42 |
full_prompt = system_message + "\n\n"
|
|
|
|
| 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 |
+
return response
|
| 68 |
|
| 69 |
|
| 70 |
+
# โ
Create Gradio Chat Interface
|
| 71 |
+
chat_ui = gr.ChatInterface(
|
| 72 |
+
fn=lambda message, history, system_message, max_tokens, temperature, top_p: (
|
| 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"),
|
|
|
|
| 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()
|