Spaces:
Sleeping
Sleeping
Update app.py
Browse files12345678912 unmodified lines2223242526272829303131 unmodified lines636465666768697071154 unmodified lines22622722822923023123223323423523623723823924024124224324424524624766 unmodified lines314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455import osimport warningsimport timefrom typing import Dict, Tuple, Listfrom dataclasses import dataclassfrom pathlib import Pathimport numpy as npimport pandas as pd12 unmodified linesapi_key: strmodel_name: str = "gemini-1.5-flash"batch_size: int = 5retry_attempts: int = 5min_wait: int = 4max_wait: int = 60score_scale: Tuple[int, int] = (0, 100)class EvaluationPrompts:@staticmethod31 unmodified linesВыведите оценки в точном формате:Креативность: [число]Разнообразие: [число]Релевантность: [число]Затем подробно объясните каждую оценку, используя примеры из ответа. Если какая-то оценка ниже 50, дайте конкретные рекомендации по улучшению."""@staticmethoddef get_third_check(original_prompt: str, response: str) -> str:154 unmodified lines'individual_similarities': stability_coefficients}def evaluate_dataset(self, df, prompt_col='rus_prompt'):"""Evaluate stability for multiple answer columns"""results = {}# Find columns ending with '_answers'answer_columns = [col for col in df.columns if col.endswith('_answers')]for column in answer_columns:model_name = column.replace('_answers', '')results[model_name] = self.calculate_similarity(df[prompt_col].tolist(),df[column].tolist())return resultsclass BenchmarkEvaluator:def __init__(self, gemini_api_key):66 unmodified linesreturn benchmark_dfdef evaluate_single_response(gemini_api_key, prompt, response, model_name="Test Model"):"""Evaluate a single response for the UI"""# Create a temporary dataframedf = pd.DataFrame({'rus_prompt': [prompt],f'{model_name}_answers': [response]})evaluator = BenchmarkEvaluator(gemini_api_key)try:result = evaluator.evaluate_model(df, model_name)# Format the result for displaying in UIoutput = {'Creativity Score': f"{result['creative_details']['creativity']:.2f}",'Diversity Score': f"{result['creative_details']['diversity']:.2f}",'Relevance Score': f"{result['creative_details']['relevance']:.2f}",'Average Creative Score': f"{result['creativity_score']:.2f}",'Stability Score': f"{result['stability_score']:.2f}",'Combined Score': f"{result['combined_score']:.2f}"}return outputexcept Exception as e:return {'Error': str(e)}def evaluate_batch(api_key, file, prompt_column, models_text):"""Process batch evaluation from the UI"""try:# Load the CSV filefile_path = file.namedf = pd.read_csv(file_path)# Process model names if providedmodels = Noneif models_text.strip():models = [m.strip() for m in models_text.split(',')]# Run the evaluationevaluator = BenchmarkEvaluator(api_key)results = evaluator.evaluate_all_models(df, models, prompt_column)return resultsexcept Exception as e:return pd.DataFrame({'Error': [str(e)]})def create_gradio_interface():"""Create Gradio interface for evaluation app"""with gr.Blocks(title="Model Response Evaluator") as app:gr.Markdown("# Model Response Evaluator")gr.Markdown("Evaluate model responses for creativity, diversity, relevance, and stability.")with gr.Tab("Single Response Evaluation"):with gr.Row():gemini_api_key = gr.Textbox(label="Gemini API Key", type="password")with gr.Row():with gr.Column():prompt = gr.Textbox(label="Original Prompt", lines=3)response = gr.Textbox(label="Model Response", lines=6)model_name = gr.Textbox(label="Model Name", value="Test Model")evaluate_btn = gr.Button("Evaluate Response")with gr.Column():output = gr.JSON(label="Evaluation Results")evaluate_btn.click(evaluate_single_response,inputs=[gemini_api_key, prompt, response, model_name],outputs=output)with gr.Tab("Batch Evaluation"):with gr.Row():gemini_api_key_batch = gr.Textbox(label="Gemini API Key", type="password")with gr.Row():csv_file = gr.File(label="Upload CSV with responses")prompt_col = gr.Textbox(label="Prompt Column Name", value="rus_prompt")models_input = gr.Textbox(label="Model names (comma-separated, leave blank for auto-detection)")evaluate_batch_btn = gr.Button("Run Benchmark")benchmark_output = gr.DataFrame(label="Benchmark Results")evaluate_batch_btn.click(evaluate_batch,inputs=[gemini_api_key_batch, csv_file, prompt_col, models_input],outputs=benchmark_output)# Add a new tab for configuration settingswith gr.Tab("Configuration"):gr.Markdown("## Advanced Configuration")gr.Markdown("Adjust evaluation parameters to customize the benchmarking process.")with gr.Row():batch_size = gr.Slider(minimum=1, maximum=20, value=5, step=1, label="Batch Size")retry_attempts = gr.Slider(minimum=1, maximum=10, value=5, step=1, label="Retry Attempts")with gr.Row():min_wait = gr.Slider(minimum=1, maximum=30, value=4, step=1, label="Minimum Wait Time (seconds)")max_wait = gr.Slider(minimum=10, maximum=300, value=60, step=10, label="Maximum Wait Time (seconds)")with gr.Row():gemini_model = gr.Dropdown(choices=["gemini-1.5-flash", "gemini-1.5-pro", "gemini-1.5-ultra"],value="gemini-1.5-flash",label="Gemini Model")gr.Markdown("Note: Changes to configuration settings will apply to new evaluations.")def update_config(batch_size, retry_attempts, min_wait, max_wait, gemini_model):# This function doesn't actually do anything in the demo but would update global configreturn f"Configuration updated: batch_size={batch_size}, retry_attempts={retry_attempts}, min_wait={min_wait}, max_wait={max_wait}, model={gemini_model}"update_config_btn = gr.Button("Update Configuration")config_status = gr.Textbox(label="Status", interactive=False)update_config_btn.click(update_config,inputs=[batch_size, retry_attempts, min_wait, max_wait, gemini_model],outputs=config_status)return appdef main():"""Main function to run the application"""app = create_gradio_interface()app.launch(share=True)123456712 unmodified lines20212223242531 unmodified lines57585960616263154 unmodified lines21821922022122222366 unmodified lines290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342import warningsimport timefrom typing import Dict, Tuple, Listfrom dataclasses import dataclassimport numpy as npimport pandas as pd12 unmodified linesapi_key: strmodel_name: str = "gemini-1.5-flash"batch_size: int = 5class EvaluationPrompts:@staticmethod31 unmodified linesВыведите оценки в точном формате:Креативность: [число]Разнообразие: [число]Релевантность: [число]"""@staticmethoddef get_third_check(original_prompt: str, response: str) -> str:154 unmodified lines'individual_similarities': stability_coefficients}class BenchmarkEvaluator:def __init__(self, gemini_api_key):66 unmodified linesreturn benchmark_dfdef create_gradio_interface():with gr.Blocks(title="Model Response Evaluator") as app:gr.Markdown("# Model Response Evaluator")gr.Markdown("Upload a CSV file with prompts and model responses to evaluate and benchmark models.")with gr.Row():gemini_api_key = gr.Textbox(label="Gemini API Key", type="password")with gr.Row():csv_file = gr.File(label="Upload CSV with responses")prompt_col = gr.Textbox(label="Prompt Column Name", value="rus_prompt")models_input = gr.Textbox(label="Model names (comma-separated, leave blank for auto-detection)")evaluate_btn = gr.Button("Run Benchmark")with gr.Row():benchmark_output = gr.DataFrame(label="Benchmark Results")def evaluate_batch(api_key, file, prompt_column, models_text):try:# Load the CSV filefile_path = file.namedf = pd.read_csv(file_path)# Process model names if providedmodels = Noneif models_text.strip():models = [m.strip() for m in models_text.split(',')]# Run the evaluationevaluator = BenchmarkEvaluator(api_key)results = evaluator.evaluate_all_models(df, models, prompt_column)return resultsexcept Exception as e:return pd.DataFrame({'Error': [str(e)]})evaluate_btn.click(evaluate_batch,inputs=[gemini_api_key, csv_file, prompt_col, models_input],outputs=benchmark_output)return appdef main():app = create_gradio_interface()app.launch(share=True)
app.py+35-148