Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import requests | |
| import pandas as pd | |
| import plotly.express as px | |
| from app.data_handling import save_results_to_csv | |
| import os | |
| BACKEND_URL = os.getenv("BACKEND_URL", "http://localhost:8000") # Local backend URL if running locally. HF Spaces need the correct backend URL | |
| st.title("GenAI Model Evaluator By ARAVINDSELVAN") | |
| # Input fields | |
| prompt = st.text_area("Enter your prompt:") | |
| llm_names = st.multiselect("Select LLMs to compare:", ["openai", "gemini"], default=["openai", "gemini"]) | |
| dataset_file = st.file_uploader("Upload a CSV dataset (optional)", type=["csv"]) | |
| # Button to run the benchmark | |
| if st.button("Run Benchmark"): | |
| results = [] | |
| if dataset_file: | |
| # Process dataset file | |
| df = pd.read_csv(dataset_file) | |
| if "prompt" not in df.columns: | |
| st.error("CSV file must have a 'prompt' column.") | |
| st.stop() | |
| prompts = df["prompt"].tolist() | |
| else: | |
| prompts = [prompt] | |
| for llm_name in llm_names: | |
| for p in prompts: | |
| try: | |
| response = requests.post(f"{BACKEND_URL}/generate", json={"prompt": p, "llm_name": llm_name}) | |
| response.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx) | |
| data = response.json() | |
| results.append({"llm": llm_name, "prompt": p, **data}) | |
| except requests.exceptions.RequestException as e: | |
| st.error(f"Error calling backend for {llm_name}: {e}") | |
| continue | |
| if results: | |
| # Display results | |
| results_df = pd.DataFrame(results) | |
| st.dataframe(results_df) | |
| # Visualization - Response Time | |
| fig_time = px.bar(results_df, x="llm", y="response_time", title="Response Time Comparison") | |
| st.plotly_chart(fig_time) | |
| # Visualization - Cost Comparison | |
| fig_cost = px.bar(results_df, x="llm", y="cost", title="Cost Comparison") | |
| st.plotly_chart(fig_cost) | |
| if st.button("Save Results to CSV"): | |
| save_results_to_csv(results, "benchmark_results.csv") | |
| st.success("Results saved to benchmark_results.csv") | |
| else: | |
| st.warning("No results to display.") |