Spaces:
Runtime error
Runtime error
import gradio as gr | |
import torch | |
from transformers import AutoModelForCausalLM, AutoTokenizer | |
import spaces | |
# Dictionary to store loaded models and tokenizers | |
loaded_models = {} | |
# List of available models (update with your preferred models) | |
models = ["hanzla/gemma-2b-datascience-instruct-v5", "hanzla/gemma-2b-datascience-instruct-v4.5"] | |
def load_model(model_name): | |
if model_name not in loaded_models: | |
print(f"Loading model: {model_name}") | |
tokenizer = AutoTokenizer.from_pretrained(model_name) | |
model = AutoModelForCausalLM.from_pretrained(model_name).to("cuda" if torch.cuda.is_available() else "cpu") | |
loaded_models[model_name] = (model, tokenizer) | |
return loaded_models[model_name] | |
def get_model_response(model_name, message): | |
model, tokenizer = load_model(model_name) | |
inputs = tokenizer(message, return_tensors="pt").to(model.device) | |
outputs = model.generate(**inputs) | |
response = tokenizer.decode(outputs[0]) | |
return response | |
def chat(message, history, model1, model2): | |
response1 = get_model_response(model1, message) | |
response2 = get_model_response(model2, message) | |
history = history or [] | |
history.append({"role": "user", "content": message}) | |
history.append({"role": "assistant", "content": f"{model1}: {response1}\n\n{model2}: {response2}"}) | |
return history | |
with gr.Blocks() as demo: | |
gr.Markdown("# Hugging Face Model Comparison Chat") | |
with gr.Row(): | |
model1_dropdown = gr.Dropdown(choices=models, label="Model 1", value=models[0]) | |
model2_dropdown = gr.Dropdown(choices=models, label="Model 2", value=models[1]) | |
chatbot = gr.Chatbot(label="Chat History", type="messages") | |
msg = gr.Textbox(label="Your message") | |
clear = gr.Button("Clear") | |
with gr.Row(): | |
upvote = gr.Button("π Upvote") | |
downvote = gr.Button("π Downvote") | |
msg.submit(chat, [msg, chatbot, model1_dropdown, model2_dropdown], chatbot) | |
clear.click(lambda: [], None, chatbot, queue=False) | |
if __name__ == "__main__": | |
demo.launch() |