yolo
Browse files
app.py
CHANGED
|
@@ -1,76 +1,143 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
|
|
|
| 3 |
import os
|
| 4 |
-
import
|
|
|
|
| 5 |
from datetime import datetime
|
|
|
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
def run_evaluation(model_name):
|
|
|
|
|
|
|
| 8 |
results = []
|
| 9 |
|
| 10 |
-
# Use the secret OpenRouter API key from the Hugging Face space
|
| 11 |
if "OPENROUTER_API_KEY" not in os.environ:
|
| 12 |
return "Error: OPENROUTER_API_KEY not found in environment variables."
|
| 13 |
|
| 14 |
try:
|
| 15 |
-
# Set up
|
| 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 |
# Run evaluation
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
if metrics:
|
| 60 |
-
|
|
|
|
|
|
|
| 61 |
else:
|
| 62 |
-
results.append("
|
| 63 |
|
| 64 |
-
except subprocess.CalledProcessError as e:
|
| 65 |
-
results.append(f"Error occurred: {str(e)}")
|
| 66 |
-
results.append(f"Command output: {e.output}")
|
| 67 |
except Exception as e:
|
| 68 |
results.append(f"An unexpected error occurred: {str(e)}")
|
| 69 |
|
| 70 |
return "\n\n".join(results)
|
| 71 |
|
| 72 |
with gr.Blocks() as demo:
|
| 73 |
-
gr.Markdown("# DuckDB SQL Evaluation App
|
| 74 |
|
| 75 |
model_name = gr.Textbox(label="Model Name (e.g., qwen/qwen-2.5-72b-instruct)")
|
| 76 |
start_btn = gr.Button("Start Evaluation")
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import spaces
|
| 3 |
+
import torch
|
| 4 |
import os
|
| 5 |
+
import sys
|
| 6 |
+
from pathlib import Path
|
| 7 |
from datetime import datetime
|
| 8 |
+
import json
|
| 9 |
|
| 10 |
+
# Add the duckdb-nsql directory to the Python path
|
| 11 |
+
sys.path.append('duckdb-nsql')
|
| 12 |
+
|
| 13 |
+
# Import necessary functions and classes from predict.py and evaluate.py
|
| 14 |
+
from eval.predict import cli as predict_cli, predict, console, get_manifest, DefaultLoader, PROMPT_FORMATTERS
|
| 15 |
+
from eval.evaluate import cli as evaluate_cli, evaluate, compute_metrics, get_to_print
|
| 16 |
+
from eval.evaluate import test_suite_evaluation, read_tables_json
|
| 17 |
+
|
| 18 |
+
zero = torch.Tensor([0]).cuda()
|
| 19 |
+
print(zero.device) # <-- 'cpu' 🤔
|
| 20 |
+
|
| 21 |
+
@spaces.GPU
|
| 22 |
def run_evaluation(model_name):
|
| 23 |
+
print(zero.device) # <-- 'cuda:0' 🤗
|
| 24 |
+
|
| 25 |
results = []
|
| 26 |
|
|
|
|
| 27 |
if "OPENROUTER_API_KEY" not in os.environ:
|
| 28 |
return "Error: OPENROUTER_API_KEY not found in environment variables."
|
| 29 |
|
| 30 |
try:
|
| 31 |
+
# Set up the arguments similar to the CLI in predict.py
|
| 32 |
+
dataset_path = "eval/data/dev.json"
|
| 33 |
+
table_meta_path = "eval/data/tables.json"
|
| 34 |
+
output_dir = "output/"
|
| 35 |
+
prompt_format = "duckdbinstgraniteshort"
|
| 36 |
+
stop_tokens = [';']
|
| 37 |
+
max_tokens = 30000
|
| 38 |
+
temperature = 0.1
|
| 39 |
+
num_beams = -1
|
| 40 |
+
manifest_client = "openrouter"
|
| 41 |
+
manifest_engine = model_name
|
| 42 |
+
manifest_connection = "http://localhost:5000"
|
| 43 |
+
overwrite_manifest = True
|
| 44 |
+
parallel = False
|
| 45 |
+
|
| 46 |
+
# Initialize necessary components
|
| 47 |
+
data_formatter = DefaultLoader()
|
| 48 |
+
prompt_formatter = PROMPT_FORMATTERS[prompt_format]()
|
| 49 |
+
|
| 50 |
+
# Load manifest
|
| 51 |
+
manifest = get_manifest(
|
| 52 |
+
manifest_client=manifest_client,
|
| 53 |
+
manifest_connection=manifest_connection,
|
| 54 |
+
manifest_engine=manifest_engine,
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
results.append(f"Using model: {manifest_engine}")
|
| 58 |
+
|
| 59 |
+
# Load data and metadata
|
| 60 |
+
results.append("Loading metadata and data...")
|
| 61 |
+
db_to_tables = data_formatter.load_table_metadata(table_meta_path)
|
| 62 |
+
data = data_formatter.load_data(dataset_path)
|
| 63 |
+
|
| 64 |
+
# Generate output filename
|
| 65 |
+
date_today = datetime.now().strftime("%y-%m-%d")
|
| 66 |
+
pred_filename = f"{prompt_format}_0docs_{manifest_engine.split('/')[-1]}_{Path(dataset_path).stem}_{date_today}.json"
|
| 67 |
+
pred_path = Path(output_dir) / pred_filename
|
| 68 |
+
results.append(f"Prediction will be saved to: {pred_path}")
|
| 69 |
+
|
| 70 |
+
# Run prediction
|
| 71 |
+
results.append("Starting prediction...")
|
| 72 |
+
predict(
|
| 73 |
+
dataset_path=dataset_path,
|
| 74 |
+
table_meta_path=table_meta_path,
|
| 75 |
+
output_dir=output_dir,
|
| 76 |
+
prompt_format=prompt_format,
|
| 77 |
+
stop_tokens=stop_tokens,
|
| 78 |
+
max_tokens=max_tokens,
|
| 79 |
+
temperature=temperature,
|
| 80 |
+
num_beams=num_beams,
|
| 81 |
+
manifest_client=manifest_client,
|
| 82 |
+
manifest_engine=manifest_engine,
|
| 83 |
+
manifest_connection=manifest_connection,
|
| 84 |
+
overwrite_manifest=overwrite_manifest,
|
| 85 |
+
parallel=parallel
|
| 86 |
+
)
|
| 87 |
+
results.append("Prediction completed.")
|
| 88 |
|
| 89 |
# Run evaluation
|
| 90 |
+
results.append("Starting evaluation...")
|
| 91 |
+
|
| 92 |
+
# Set up evaluation arguments
|
| 93 |
+
gold_path = Path(dataset_path)
|
| 94 |
+
db_dir = "eval/data/databases/"
|
| 95 |
+
tables_path = Path(table_meta_path)
|
| 96 |
+
|
| 97 |
+
kmaps = test_suite_evaluation.build_foreign_key_map_from_json(str(tables_path))
|
| 98 |
+
db_schemas = read_tables_json(str(tables_path))
|
| 99 |
+
|
| 100 |
+
gold_sqls_dict = json.load(gold_path.open("r", encoding="utf-8"))
|
| 101 |
+
pred_sqls_dict = [json.loads(l) for l in pred_path.open("r").readlines()]
|
| 102 |
+
|
| 103 |
+
gold_sqls = [p.get("query", p.get("sql", "")) for p in gold_sqls_dict]
|
| 104 |
+
setup_sqls = [p["setup_sql"] for p in gold_sqls_dict]
|
| 105 |
+
validate_sqls = [p["validation_sql"] for p in gold_sqls_dict]
|
| 106 |
+
gold_dbs = [p.get("db_id", p.get("db", "")) for p in gold_sqls_dict]
|
| 107 |
+
pred_sqls = [p["pred"] for p in pred_sqls_dict]
|
| 108 |
+
categories = [p.get("category", "") for p in gold_sqls_dict]
|
| 109 |
+
|
| 110 |
+
metrics = compute_metrics(
|
| 111 |
+
gold_sqls=gold_sqls,
|
| 112 |
+
pred_sqls=pred_sqls,
|
| 113 |
+
gold_dbs=gold_dbs,
|
| 114 |
+
setup_sqls=setup_sqls,
|
| 115 |
+
validate_sqls=validate_sqls,
|
| 116 |
+
kmaps=kmaps,
|
| 117 |
+
db_schemas=db_schemas,
|
| 118 |
+
database_dir=db_dir,
|
| 119 |
+
lowercase_schema_match=False,
|
| 120 |
+
model_name=model_name,
|
| 121 |
+
categories=categories,
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
results.append("Evaluation completed.")
|
| 125 |
+
|
| 126 |
+
# Format and add the evaluation metrics to the results
|
| 127 |
if metrics:
|
| 128 |
+
to_print = get_to_print({"all": metrics}, "all", model_name, len(gold_sqls))
|
| 129 |
+
formatted_metrics = "\n".join([f"{k}: {v}" for k, v in to_print.items() if k not in ["slice", "model"]])
|
| 130 |
+
results.append(f"Evaluation metrics:\n{formatted_metrics}")
|
| 131 |
else:
|
| 132 |
+
results.append("No evaluation metrics returned.")
|
| 133 |
|
|
|
|
|
|
|
|
|
|
| 134 |
except Exception as e:
|
| 135 |
results.append(f"An unexpected error occurred: {str(e)}")
|
| 136 |
|
| 137 |
return "\n\n".join(results)
|
| 138 |
|
| 139 |
with gr.Blocks() as demo:
|
| 140 |
+
gr.Markdown("# DuckDB SQL Evaluation App")
|
| 141 |
|
| 142 |
model_name = gr.Textbox(label="Model Name (e.g., qwen/qwen-2.5-72b-instruct)")
|
| 143 |
start_btn = gr.Button("Start Evaluation")
|