Spaces:
Runtime error
Runtime error
# # import gradio as gr | |
# # from huggingface_hub import InferenceClient | |
# # """ | |
# # 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 | |
# # """ | |
# # client = InferenceClient("HuggingFaceH4/zephyr-7b-beta") | |
# # def respond( | |
# # message, | |
# # history: list[tuple[str, str]], | |
# # system_message, | |
# # max_tokens, | |
# # temperature, | |
# # top_p, | |
# # ): | |
# # messages = [{"role": "system", "content": system_message}] | |
# # for val in history: | |
# # if val[0]: | |
# # messages.append({"role": "user", "content": val[0]}) | |
# # if val[1]: | |
# # messages.append({"role": "assistant", "content": val[1]}) | |
# # messages.append({"role": "user", "content": message}) | |
# # response = "" | |
# # for message in client.chat_completion( | |
# # messages, | |
# # max_tokens=max_tokens, | |
# # stream=True, | |
# # temperature=temperature, | |
# # top_p=top_p, | |
# # ): | |
# # token = message.choices[0].delta.content | |
# # response += token | |
# # yield response | |
# # """ | |
# # For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface | |
# # """ | |
# # demo = gr.ChatInterface( | |
# # respond, | |
# # additional_inputs=[ | |
# # gr.Textbox(value="You are a friendly Chatbot.", label="System message"), | |
# # gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"), | |
# # gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"), | |
# # gr.Slider( | |
# # minimum=0.1, | |
# # maximum=1.0, | |
# # value=0.95, | |
# # step=0.05, | |
# # label="Top-p (nucleus sampling)", | |
# # ), | |
# # ], | |
# # ) | |
# # if __name__ == "__main__": | |
# # demo.launch() | |
# import torch | |
# import gradio as gr | |
# from transformers import AutoModelForCausalLM, AutoTokenizer | |
# import os | |
# # Define model names | |
# MODEL_1_PATH = "./adapter_model.safetensors" # Local path inside Space | |
# MODEL_2_NAME = "sarvamai/sarvam-1" # The base model on Hugging Face Hub | |
# # Load the tokenizer (same for both models) | |
# TOKENIZER_NAME = "sarvamai/sarvam-1" | |
# tokenizer = AutoTokenizer.from_pretrained(TOKENIZER_NAME) | |
# def fix_checkpoint(model_path): | |
# """Fixes the model checkpoint by adjusting mismatched weight dimensions.""" | |
# checkpoint_file = os.path.join(model_path, "pytorch_model.bin") | |
# fixed_checkpoint_file = os.path.join(model_path, "pytorch_model_fixed.bin") | |
# if not os.path.exists(checkpoint_file): | |
# raise FileNotFoundError(f"Checkpoint file not found at: {checkpoint_file}") | |
# print("Loading checkpoint for fixing...") | |
# checkpoint = torch.load(checkpoint_file, map_location="cpu") | |
# # Adjust weights (truncate the last token if mismatch) | |
# if "base_model.model.lm_head.base_layer.weight" in checkpoint: | |
# checkpoint["base_model.model.lm_head.base_layer.weight"] = checkpoint["base_model.model.lm_head.base_layer.weight"][:-1] | |
# if "base_model.model.lm_head.lora_B.default.weight" in checkpoint: | |
# checkpoint["base_model.model.lm_head.lora_B.default.weight"] = checkpoint["base_model.model.lm_head.lora_B.default.weight"][:-1] | |
# # Save the fixed checkpoint | |
# print("Saving fixed checkpoint...") | |
# torch.save(checkpoint, fixed_checkpoint_file) | |
# return fixed_checkpoint_file # Return the new file path | |
# # Function to load a model | |
# def load_model(model_choice): | |
# if model_choice == "Hugging face dataset": | |
# model = AutoModelForCausalLM.from_pretrained("./", torch_dtype=torch.float16, device_map="auto") | |
# model.load_adapter(MODEL_1_PATH, "safe_tensors") # Load safetensors adapter | |
# else: | |
# model = AutoModelForCausalLM.from_pretrained(MODEL_2_NAME) | |
# model.eval() | |
# return model | |
# # Load default model on startup | |
# current_model = load_model("Hugging face dataset") | |
# # Chatbot response function | |
# def respond(message, history, model_choice, max_tokens, temperature, top_p): | |
# global current_model | |
# # Switch model if user selects a different one | |
# if (model_choice == "Hugging face dataset" and current_model is not None and current_model.config.name_or_path != MODEL_1_PATH) or \ | |
# (model_choice == "Proprietary dataset1" and current_model is not None and current_model.config.name_or_path != MODEL_2_NAME): | |
# current_model = load_model(model_choice) | |
# # Convert chat history to format | |
# messages = [{"role": "system", "content": "You are a friendly AI assistant."}] | |
# for val in history: | |
# if val[0]: | |
# messages.append({"role": "user", "content": val[0]}) | |
# if val[1]: | |
# messages.append({"role": "assistant", "content": val[1]}) | |
# messages.append({"role": "user", "content": message}) | |
# # Tokenize and generate response | |
# inputs = tokenizer.apply_chat_template(messages, tokenize=False) | |
# input_tokens = tokenizer(inputs, return_tensors="pt").to("cuda" if torch.cuda.is_available() else "cpu") | |
# output_tokens = current_model.generate( | |
# **input_tokens, | |
# max_new_tokens=max_tokens, | |
# temperature=temperature, | |
# top_p=top_p, | |
# pad_token_id=tokenizer.pad_token_id, | |
# eos_token_id=tokenizer.eos_token_id, | |
# ) | |
# response = tokenizer.decode(output_tokens[0], skip_special_tokens=True) | |
# return response | |
# # Define Gradio Chat Interface | |
# demo = gr.ChatInterface( | |
# fn=respond, | |
# additional_inputs=[ | |
# gr.Dropdown(choices=["Hugging face dataset", "Proprietary dataset1"], value="Fine-Tuned Model", label="Select Model"), | |
# gr.Slider(minimum=1, maximum=1024, value=256, step=1, label="Max Tokens"), | |
# gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature"), | |
# gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"), | |
# ], | |
# ) | |
# if __name__ == "__main__": | |
# demo.launch() | |
import torch | |
import os | |
from transformers import AutoModelForCausalLM, AutoTokenizer | |
# Define model and tokenizer paths | |
MODEL_1_PATH = "Priyanka6/fine-tuning-inference" | |
TOKENIZER_NAME = "sarvam/sarvam-1" # Keep this unchanged if tokenizer hasn't changed | |
def trim_adapter_weights(model_path): | |
""" | |
Trims the last token from the adapter's lm_head.lora_B.default.weight | |
if there is a mismatch with the base model. | |
""" | |
adapter_file = os.path.join(model_path, "adapter_model.safetensors") | |
if not os.path.exists(adapter_file): | |
raise FileNotFoundError(f"Adapter file not found: {adapter_file}") | |
checkpoint = torch.load(adapter_file, map_location="cpu") | |
key_to_trim = "lm_head.lora_B.default.weight" | |
if key_to_trim in checkpoint: | |
original_size = checkpoint[key_to_trim].shape[0] | |
expected_size = original_size - 1 # Removing last token | |
print(f"Trimming {key_to_trim}: {original_size} -> {expected_size}") | |
checkpoint[key_to_trim] = checkpoint[key_to_trim][:-1] # Trim the last row | |
# Save the modified adapter | |
trimmed_adapter_path = os.path.join(model_path, "adapter_model_trimmed.safetensors") | |
torch.save(checkpoint, trimmed_adapter_path) | |
return trimmed_adapter_path | |
return adapter_file | |
# Before loading the adapter, trim it if necessary | |
trimmed_adapter_path = trim_adapter_weights(MODEL_1_PATH) | |
# Load the tokenizer | |
tokenizer = AutoTokenizer.from_pretrained(TOKENIZER_NAME) | |
# Load the model | |
model = AutoModelForCausalLM.from_pretrained( | |
MODEL_1_PATH, torch_dtype=torch.float16, device_map="auto" | |
) | |
# Load the trimmed adapter | |
model.load_adapter(trimmed_adapter_path, "safe_tensors") | |
# Chat function | |
def chat(query): | |
inputs = tokenizer(query, return_tensors="pt").to("cuda") | |
with torch.no_grad(): | |
output = model.generate(**inputs, max_new_tokens=100) | |
return tokenizer.decode(output[0], skip_special_tokens=True) | |
# Test the chatbot | |
if __name__ == "__main__": | |
while True: | |
query = input("User: ") | |
if query.lower() in ["exit", "quit"]: | |
break | |
response = chat(query) | |
print(f"Bot: {response}") |