Update app.py
Browse files
app.py
CHANGED
|
@@ -1,97 +1,83 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
"""
|
| 6 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 7 |
-
"""
|
| 8 |
-
|
| 9 |
import os
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
message,
|
| 15 |
-
history: list[tuple[str, str]],
|
| 16 |
-
system_message,
|
| 17 |
-
max_tokens,
|
| 18 |
-
temperature,
|
| 19 |
-
top_p,
|
| 20 |
-
):
|
| 21 |
-
messages = [{"role": "system", "content": system_message}]
|
| 22 |
-
|
| 23 |
-
for val in history:
|
| 24 |
-
if val[0]:
|
| 25 |
-
messages.append({"role": "user", "content": val[0]})
|
| 26 |
-
if val[1]:
|
| 27 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 28 |
-
|
| 29 |
-
messages.append({"role": "user", "content": message})
|
| 30 |
-
|
| 31 |
-
response = ""
|
| 32 |
-
|
| 33 |
-
for message in client.chat_completion(
|
| 34 |
-
messages,
|
| 35 |
-
max_tokens=max_tokens,
|
| 36 |
-
stream=True,
|
| 37 |
-
temperature=temperature,
|
| 38 |
-
top_p=top_p,
|
| 39 |
-
):
|
| 40 |
-
token = message.choices[0].delta.content
|
| 41 |
-
|
| 42 |
-
response += token
|
| 43 |
-
yield response
|
| 44 |
-
|
| 45 |
|
| 46 |
"""
|
| 47 |
-
|
| 48 |
"""
|
| 49 |
-
demo = gr.ChatInterface(
|
| 50 |
-
respond,
|
| 51 |
-
additional_inputs=[
|
| 52 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 53 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 54 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 55 |
-
gr.Slider(
|
| 56 |
-
minimum=0.1,
|
| 57 |
-
maximum=1.0,
|
| 58 |
-
value=0.95,
|
| 59 |
-
step=0.05,
|
| 60 |
-
label="Top-p (nucleus sampling)",
|
| 61 |
-
),
|
| 62 |
-
],
|
| 63 |
-
)
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
if __name__ == "__main__":
|
| 67 |
-
demo.launch()
|
| 68 |
-
|
| 69 |
-
import os
|
| 70 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 71 |
-
from peft import PeftModel
|
| 72 |
-
import torch
|
| 73 |
|
| 74 |
-
# Load Hugging Face API token securely
|
| 75 |
api_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 76 |
-
|
| 77 |
if not api_token:
|
| 78 |
raise ValueError("โ ERROR: Hugging Face API token is not set. Please set it as an environment variable.")
|
| 79 |
|
| 80 |
-
# Define model names
|
| 81 |
base_model_name = "unsloth/qwen2.5-math-7b-bnb-4bit"
|
| 82 |
-
peft_model_name = "Hrushi02/Root_Math"
|
| 83 |
|
| 84 |
-
# Load base model
|
|
|
|
| 85 |
base_model = AutoModelForCausalLM.from_pretrained(
|
| 86 |
base_model_name,
|
| 87 |
torch_dtype=torch.float16,
|
| 88 |
device_map="auto",
|
| 89 |
-
use_auth_token=api_token
|
| 90 |
)
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
| 94 |
model = PeftModel.from_pretrained(base_model, peft_model_name, token=api_token)
|
| 95 |
|
| 96 |
-
# Load tokenizer
|
|
|
|
| 97 |
tokenizer = AutoTokenizer.from_pretrained(base_model_name, token=api_token)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import torch
|
| 3 |
+
import gradio as gr
|
| 4 |
+
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
|
| 12 |
api_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
|
|
|
| 13 |
if not api_token:
|
| 14 |
raise ValueError("โ ERROR: Hugging Face API token is not set. Please set it as an environment variable.")
|
| 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...")
|
| 22 |
base_model = AutoModelForCausalLM.from_pretrained(
|
| 23 |
base_model_name,
|
| 24 |
torch_dtype=torch.float16,
|
| 25 |
device_map="auto",
|
| 26 |
+
use_auth_token=api_token
|
| 27 |
)
|
| 28 |
|
| 29 |
+
# โ
Load your fine-tuned PEFT adapter
|
| 30 |
+
print("๐ Loading fine-tuned adapter...")
|
| 31 |
model = PeftModel.from_pretrained(base_model, peft_model_name, token=api_token)
|
| 32 |
|
| 33 |
+
# โ
Load tokenizer
|
| 34 |
+
print("๐ Loading tokenizer...")
|
| 35 |
tokenizer = AutoTokenizer.from_pretrained(base_model_name, token=api_token)
|
| 36 |
+
|
| 37 |
+
# โ
Define chat response function
|
| 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"
|
| 41 |
+
for user_msg, bot_msg in history:
|
| 42 |
+
if user_msg:
|
| 43 |
+
full_prompt += f"User: {user_msg}\n"
|
| 44 |
+
if bot_msg:
|
| 45 |
+
full_prompt += f"Assistant: {bot_msg}\n"
|
| 46 |
+
full_prompt += f"User: {message}\nAssistant:"
|
| 47 |
+
|
| 48 |
+
inputs = tokenizer(full_prompt, return_tensors="pt").to(model.device)
|
| 49 |
+
|
| 50 |
+
with torch.no_grad():
|
| 51 |
+
outputs = model.generate(
|
| 52 |
+
**inputs,
|
| 53 |
+
max_new_tokens=max_tokens,
|
| 54 |
+
temperature=temperature,
|
| 55 |
+
top_p=top_p,
|
| 56 |
+
do_sample=True
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 60 |
+
# extract only the assistant's last message
|
| 61 |
+
if "Assistant:" in response:
|
| 62 |
+
response = response.split("Assistant:")[-1].strip()
|
| 63 |
+
|
| 64 |
+
yield response
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
# โ
Create Gradio interface
|
| 68 |
+
demo = gr.ChatInterface(
|
| 69 |
+
respond,
|
| 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"),
|
| 73 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 74 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
| 75 |
+
],
|
| 76 |
+
title="๐งฎ Root Math Assistant",
|
| 77 |
+
description="A fine-tuned math reasoning model by Hrushi02 using Unsloth + PEFT."
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
# โ
Launch app
|
| 82 |
+
if __name__ == "__main__":
|
| 83 |
+
demo.launch()
|