Spaces:
Runtime error
Runtime error
Add Gradio interface for comparing two models
Browse files- README.md +14 -0
- app.py +78 -0
- requirements.txt +2 -0
README.md
CHANGED
@@ -12,3 +12,17 @@ short_description: compare unsloth/DeepSeek-R1-Distill-Llama-8B-unsloth-bnb-4bi
|
|
12 |
---
|
13 |
|
14 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
---
|
13 |
|
14 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
15 |
+
|
16 |
+
# Model Comparison Space
|
17 |
+
|
18 |
+
This Space allows you to compare the responses of two models:
|
19 |
+
- **Original Model**: `unsloth/DeepSeek-R1-Distill-Llama-8B-unsloth-bnb-4bit`
|
20 |
+
- **Fine-Tuned Model**: `kas1/DeepSeek-R1-Distill-Llama-8B-unsloth-bnb-4bit-John1`
|
21 |
+
|
22 |
+
## Features
|
23 |
+
- Enter a single prompt and compare responses side by side.
|
24 |
+
- Upload a JSON file with multiple questions and compare batch results.
|
25 |
+
|
26 |
+
## How to Use
|
27 |
+
1. Go to the "Single Prompt" tab and enter a question.
|
28 |
+
2. Alternatively, upload a JSON file in the "Batch Testing" tab.
|
app.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
|
4 |
+
# Load the original model
|
5 |
+
original_model = AutoModelForCausalLM.from_pretrained("unsloth/DeepSeek-R1-Distill-Llama-8B-unsloth-bnb-4bit")
|
6 |
+
original_tokenizer = AutoTokenizer.from_pretrained("unsloth/DeepSeek-R1-Distill-Llama-8B-unsloth-bnb-4bit")
|
7 |
+
|
8 |
+
# Load the fine-tuned model
|
9 |
+
fine_tuned_model = AutoModelForCausalLM.from_pretrained("kas1/DeepSeek-R1-Distill-Llama-8B-unsloth-bnb-4bit-John1")
|
10 |
+
fine_tuned_tokenizer = AutoTokenizer.from_pretrained("kas1/DeepSeek-R1-Distill-Llama-8B-unsloth-bnb-4bit-John1")
|
11 |
+
|
12 |
+
# Function to generate responses from both models
|
13 |
+
def compare_models(prompt):
|
14 |
+
# Generate response from the original model
|
15 |
+
original_inputs = original_tokenizer(prompt, return_tensors="pt")
|
16 |
+
original_outputs = original_model.generate(**original_inputs)
|
17 |
+
original_response = original_tokenizer.decode(original_outputs[0], skip_special_tokens=True)
|
18 |
+
|
19 |
+
# Generate response from the fine-tuned model
|
20 |
+
fine_tuned_inputs = fine_tuned_tokenizer(prompt, return_tensors="pt")
|
21 |
+
fine_tuned_outputs = fine_tuned_model.generate(**fine_tuned_inputs)
|
22 |
+
fine_tuned_response = fine_tuned_tokenizer.decode(fine_tuned_outputs[0], skip_special_tokens=True)
|
23 |
+
|
24 |
+
return original_response, fine_tuned_response
|
25 |
+
|
26 |
+
# Function to handle batch testing with a JSON file
|
27 |
+
def batch_test(json_file):
|
28 |
+
import json
|
29 |
+
results = []
|
30 |
+
data = json.load(json_file)
|
31 |
+
|
32 |
+
for item in data:
|
33 |
+
question = item.get("question", "")
|
34 |
+
expected_answer = item.get("answer", "")
|
35 |
+
|
36 |
+
# Generate responses from both models
|
37 |
+
original_response, fine_tuned_response = compare_models(question)
|
38 |
+
|
39 |
+
results.append({
|
40 |
+
"question": question,
|
41 |
+
"expected_answer": expected_answer,
|
42 |
+
"original_model_response": original_response,
|
43 |
+
"fine_tuned_model_response": fine_tuned_response
|
44 |
+
})
|
45 |
+
|
46 |
+
return results
|
47 |
+
|
48 |
+
# Define the Gradio interface
|
49 |
+
with gr.Blocks() as demo:
|
50 |
+
gr.Markdown("# Compare Two Models Side by Side")
|
51 |
+
|
52 |
+
# Single prompt comparison
|
53 |
+
with gr.Tab("Single Prompt"):
|
54 |
+
prompt_input = gr.Textbox(label="Enter a Prompt/Question")
|
55 |
+
compare_button = gr.Button("Compare Responses")
|
56 |
+
original_output = gr.Textbox(label="Original Model Response")
|
57 |
+
fine_tuned_output = gr.Textbox(label="Fine-Tuned Model Response")
|
58 |
+
|
59 |
+
compare_button.click(
|
60 |
+
compare_models,
|
61 |
+
inputs=prompt_input,
|
62 |
+
outputs=[original_output, fine_tuned_output]
|
63 |
+
)
|
64 |
+
|
65 |
+
# Batch testing with a JSON file
|
66 |
+
with gr.Tab("Batch Testing"):
|
67 |
+
json_file_input = gr.File(label="Upload JSON File with Questions")
|
68 |
+
batch_results = gr.JSON(label="Comparison Results")
|
69 |
+
batch_button = gr.Button("Run Batch Test")
|
70 |
+
|
71 |
+
batch_button.click(
|
72 |
+
batch_test,
|
73 |
+
inputs=json_file_input,
|
74 |
+
outputs=batch_results
|
75 |
+
)
|
76 |
+
|
77 |
+
# Launch the app
|
78 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
gradio
|