Update app.py
Browse files
app.py
CHANGED
|
@@ -1,80 +1,97 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import torch
|
| 3 |
import gradio as gr
|
| 4 |
-
from
|
|
|
|
| 5 |
|
| 6 |
"""
|
| 7 |
-
|
| 8 |
-
Supports private/public repo and GPU/CPU auto-detection.
|
| 9 |
"""
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 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 |
-
# โ
Correct model repo name (case-sensitive)
|
| 17 |
-
model_name = "Hrushi02/Root_Math" # double-check on HF website
|
| 18 |
-
|
| 19 |
-
# โ
Device and dtype
|
| 20 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 21 |
-
dtype = torch.float16 if device == "cuda" else torch.float32
|
| 22 |
-
print(f"โก Loading model on {device.upper()} with dtype={dtype}")
|
| 23 |
-
|
| 24 |
-
# โ
Load the model (directly, full fine-tuned)
|
| 25 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 26 |
-
model_name,
|
| 27 |
-
torch_dtype=dtype,
|
| 28 |
-
device_map="auto",
|
| 29 |
-
token=api_token # required for private repo
|
| 30 |
-
)
|
| 31 |
|
| 32 |
-
# โ
Load tokenizer
|
| 33 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name, token=api_token)
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
if user_msg:
|
| 42 |
-
full_prompt += f"User: {user_msg}\n"
|
| 43 |
-
if bot_msg:
|
| 44 |
-
full_prompt += f"Assistant: {bot_msg}\n"
|
| 45 |
-
full_prompt += f"User: {message}\nAssistant:"
|
| 46 |
|
| 47 |
-
|
| 48 |
|
| 49 |
-
|
| 50 |
-
outputs = model.generate(
|
| 51 |
-
**inputs,
|
| 52 |
-
max_new_tokens=int(max_tokens),
|
| 53 |
-
temperature=float(temperature),
|
| 54 |
-
top_p=float(top_p),
|
| 55 |
-
do_sample=True
|
| 56 |
-
)
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
|
|
|
| 63 |
|
| 64 |
|
| 65 |
-
|
|
|
|
|
|
|
| 66 |
demo = gr.ChatInterface(
|
| 67 |
respond,
|
| 68 |
additional_inputs=[
|
| 69 |
-
gr.Textbox(value="You are a
|
| 70 |
-
gr.Slider(minimum=1, maximum=
|
| 71 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 72 |
-
gr.Slider(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
],
|
| 74 |
-
title="๐งฎ Root Math Assistant",
|
| 75 |
-
description="Fine-tuned by Hrushi02 for mathematical reasoning."
|
| 76 |
)
|
| 77 |
|
| 78 |
-
|
| 79 |
if __name__ == "__main__":
|
| 80 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
client = InferenceClient(model="HuggingFaceH4/zephyr-7b-beta", token=os.getenv("HUGGINGFACEHUB_API_TOKEN"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
def respond(
|
| 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 |
+
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 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 with authentication
|
| 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 # โ
Correct
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
# Load fine-tuned model
|
| 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)
|