Spaces:
Runtime error
Runtime error
File size: 2,085 Bytes
c66c032 02b0952 97e6085 c66c032 97e6085 02b0952 c66c032 02b0952 97e6085 02b0952 97e6085 02b0952 116a0d1 02b0952 e9d2334 02b0952 e9d2334 c66c032 02b0952 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
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"]
@spaces.GPU
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]
@spaces.GPU
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() |