Spaces:
Sleeping
Sleeping
update gradio app
Browse files
app.py
CHANGED
|
@@ -1,14 +1,101 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import spaces
|
| 3 |
import torch
|
|
|
|
|
|
|
| 4 |
|
| 5 |
zero = torch.Tensor([0]).cuda()
|
| 6 |
-
print(zero.device)
|
| 7 |
|
| 8 |
@spaces.GPU
|
| 9 |
-
def
|
| 10 |
-
print(zero.device)
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
demo = gr.Interface(fn=greet, inputs=gr.Number(), outputs=gr.Text())
|
| 14 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import subprocess
|
| 3 |
import spaces
|
| 4 |
import torch
|
| 5 |
+
import os
|
| 6 |
+
import re
|
| 7 |
|
| 8 |
zero = torch.Tensor([0]).cuda()
|
| 9 |
+
print(zero.device) # <-- 'cpu' 🤔
|
| 10 |
|
| 11 |
@spaces.GPU
|
| 12 |
+
def run_evaluation(model_name):
|
| 13 |
+
print(zero.device) # <-- 'cuda:0' 🤗
|
| 14 |
+
|
| 15 |
+
results = []
|
| 16 |
+
|
| 17 |
+
# Use the secret HF token from the Hugging Face space
|
| 18 |
+
if "HF_TOKEN" not in os.environ:
|
| 19 |
+
return "Error: HF_TOKEN not found in environment variables."
|
| 20 |
+
|
| 21 |
+
manifest_process = None
|
| 22 |
+
try:
|
| 23 |
+
# Start manifest server in background with explicit CUDA_VISIBLE_DEVICES
|
| 24 |
+
manifest_cmd = f"""
|
| 25 |
+
CUDA_VISIBLE_DEVICES=0 HF_TOKEN={os.environ['HF_TOKEN']} cd duckdb-nsql/ &&
|
| 26 |
+
python -m manifest.api.app
|
| 27 |
+
--model_type huggingface
|
| 28 |
+
--model_generation_type text-generation
|
| 29 |
+
--model_name_or_path {model_name}
|
| 30 |
+
--fp16
|
| 31 |
+
--device 0
|
| 32 |
+
"""
|
| 33 |
+
manifest_process = subprocess.Popen(manifest_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
| 34 |
+
results.append("Started manifest server in background.")
|
| 35 |
+
|
| 36 |
+
# Run inference
|
| 37 |
+
inference_cmd = f"""
|
| 38 |
+
cd duckdb-nsql/ &&
|
| 39 |
+
python eval/predict.py
|
| 40 |
+
predict
|
| 41 |
+
eval/data/dev.json
|
| 42 |
+
eval/data/tables.json
|
| 43 |
+
--output-dir output/
|
| 44 |
+
--stop-tokens ';'
|
| 45 |
+
--overwrite-manifest
|
| 46 |
+
--manifest-client huggingface
|
| 47 |
+
--manifest-connection http://localhost:5000
|
| 48 |
+
--prompt-format duckdbinstgraniteshort
|
| 49 |
+
"""
|
| 50 |
+
inference_result = subprocess.run(inference_cmd, shell=True, check=True, capture_output=True, text=True)
|
| 51 |
+
results.append("Inference completed.")
|
| 52 |
+
|
| 53 |
+
# Extract JSON file path from inference output
|
| 54 |
+
json_path_match = re.search(r'(.*\.json)', inference_result.stdout)
|
| 55 |
+
if not json_path_match:
|
| 56 |
+
raise ValueError("Could not find JSON file path in inference output")
|
| 57 |
+
json_file = os.path.basename(json_path_match.group(1))
|
| 58 |
+
results.append(f"Generated JSON file: {json_file}")
|
| 59 |
+
|
| 60 |
+
# Run evaluation
|
| 61 |
+
eval_cmd = f"""
|
| 62 |
+
cd duckdb-nsql/ &&
|
| 63 |
+
python eval/evaluate.py evaluate
|
| 64 |
+
--gold eval/data/dev.json
|
| 65 |
+
--db eval/data/databases/
|
| 66 |
+
--tables eval/data/tables.json
|
| 67 |
+
--output-dir output/
|
| 68 |
+
--pred output/{json_file}
|
| 69 |
+
"""
|
| 70 |
+
eval_result = subprocess.run(eval_cmd, shell=True, check=True, capture_output=True, text=True)
|
| 71 |
+
|
| 72 |
+
# Extract and format metrics from eval output
|
| 73 |
+
metrics = eval_result.stdout
|
| 74 |
+
if metrics:
|
| 75 |
+
results.append(f"Evaluation completed:\n{metrics}")
|
| 76 |
+
else:
|
| 77 |
+
results.append("Evaluation completed, but get metrics.")
|
| 78 |
+
|
| 79 |
+
except subprocess.CalledProcessError as e:
|
| 80 |
+
results.append(f"Error occurred: {str(e)}")
|
| 81 |
+
results.append(f"Command output: {e.output}")
|
| 82 |
+
except Exception as e:
|
| 83 |
+
results.append(f"An unexpected error occurred: {str(e)}")
|
| 84 |
+
finally:
|
| 85 |
+
# Terminate the background manifest server
|
| 86 |
+
if manifest_process:
|
| 87 |
+
manifest_process.terminate()
|
| 88 |
+
results.append("Terminated manifest server.")
|
| 89 |
+
|
| 90 |
+
return "\n\n".join(results)
|
| 91 |
+
|
| 92 |
+
with gr.Blocks() as demo:
|
| 93 |
+
gr.Markdown("# DuckDB-NSQL Evaluation App")
|
| 94 |
+
|
| 95 |
+
model_name = gr.Textbox(label="Model Name (e.g., Qwen/Qwen2.5-7B-Instruct)")
|
| 96 |
+
start_btn = gr.Button("Start Evaluation")
|
| 97 |
+
output = gr.Textbox(label="Output", lines=20)
|
| 98 |
+
|
| 99 |
+
start_btn.click(fn=run_evaluation, inputs=[model_name], outputs=output)
|
| 100 |
|
|
|
|
| 101 |
demo.launch()
|