File size: 4,719 Bytes
f2a274f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import gradio as gr
import time
import json
import pandas as pd
from typing import List, Dict, Any

class BenchmarkSystem:
    def __init__(self):
        self.results = {}
        
    def run_benchmark(self, 
                     model_name: str,
                     test_cases: List[str],
                     system_prompt: str = "") -> Dict[str, Any]:
        """
        Run benchmark tests and measure performance metrics
        """
        results = {
            "model_name": model_name,
            "timestamp": time.strftime("%Y-%m-%d %H:%M:%S"),
            "total_tokens": 0,
            "total_time": 0,
            "responses": [],
            "metrics": {}
        }
        
        start_time = time.time()
        
        # Simulate processing test cases
        for test in test_cases:
            # Here you would add actual model inference
            # This is a placeholder for demonstration
            time.sleep(0.5)  # Simulate processing time
            results["responses"].append({
                "input": test,
                "output": f"Sample response for: {test}",
                "tokens": len(test.split()),
                "time": 0.5
            })
            
        results["total_time"] = time.time() - start_time
        results["total_tokens"] = sum(r["tokens"] for r in results["responses"])
        
        # Calculate aggregate metrics
        results["metrics"] = {
            "avg_response_time": results["total_time"] / len(test_cases),
            "avg_tokens_per_response": results["total_tokens"] / len(test_cases)
        }
        
        self.results[model_name] = results
        return results

def format_results(results: Dict[str, Any]) -> str:
    """Format benchmark results for display"""
    output = f"Model: {results['model_name']}\n"
    output += f"Timestamp: {results['timestamp']}\n"
    output += f"Total Time: {results['total_time']:.2f}s\n"
    output += f"Total Tokens: {results['total_tokens']}\n\n"
    
    output += "Metrics:\n"
    for metric, value in results["metrics"].items():
        output += f"- {metric}: {value:.2f}\n"
    
    return output

def save_results(results: Dict[str, Any], filename: str = "benchmark_results.json"):
    """Save benchmark results to a file"""
    with open(filename, "w") as f:
        json.dump(results, f, indent=2)
    return f"Results saved to {filename}"

def run_benchmark_interface(model_name: str, 
                          test_cases: str,
                          system_prompt: str) -> tuple[str, pd.DataFrame]:
    """
    Gradio interface function for running benchmarks
    """
    benchmark = BenchmarkSystem()
    
    # Parse test cases (assuming one per line)
    test_cases_list = [t.strip() for t in test_cases.split("\n") if t.strip()]
    
    # Run benchmark
    results = benchmark.run_benchmark(
        model_name=model_name,
        test_cases=test_cases_list,
        system_prompt=system_prompt
    )
    
    # Create DataFrame for response details
    df = pd.DataFrame([
        {
            "Input": r["input"],
            "Output": r["output"],
            "Tokens": r["tokens"],
            "Time (s)": r["time"]
        }
        for r in results["responses"]
    ])
    
    # Save results
    save_results(results)
    
    return format_results(results), df

# Create Gradio interface
with gr.Blocks(title="Model Benchmark Suite") as demo:
    gr.Markdown("# Model Benchmark Suite")
    gr.Markdown("Test and compare model performance across different scenarios")
    
    with gr.Row():
        with gr.Column():
            model_name = gr.Textbox(
                label="Model Name",
                placeholder="Enter model name or identifier"
            )
            system_prompt = gr.Textbox(
                label="System Prompt (Optional)",
                placeholder="Enter system prompt if applicable",
                lines=2
            )
            test_cases = gr.Textbox(
                label="Test Cases",
                placeholder="Enter test cases (one per line)",
                lines=5
            )
            run_button = gr.Button("Run Benchmark")
        
        with gr.Column():
            results_text = gr.Textbox(
                label="Benchmark Results",
                lines=10,
                readonly=True
            )
            results_table = gr.DataFrame(
                label="Detailed Results",
                headers=["Input", "Output", "Tokens", "Time (s)"]
            )
    
    run_button.click(
        fn=run_benchmark_interface,
        inputs=[model_name, test_cases, system_prompt],
        outputs=[results_text, results_table]
    )

if __name__ == "__main__":
    demo.launch()