alihmaou commited on
Commit
77bf197
·
verified ·
1 Parent(s): 98b35c9

Deploy tools/paris_trees_sql.py via Meta-MCP

Browse files
Files changed (1) hide show
  1. tools/paris_trees_sql.py +76 -0
tools/paris_trees_sql.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import json
3
+ from PIL import Image
4
+ import numpy as np
5
+
6
+ # --- User Defined Logic ---
7
+ import duckdb
8
+ import pandas as pd
9
+ import requests
10
+ import tempfile
11
+ import os
12
+
13
+ def paris_trees_sql(sql_query: str) -> str:
14
+ """
15
+ Execute a SQL query on the Paris trees dataset using DuckDB.
16
+
17
+ Downloads the official Paris open data file (les-arbres) as a Parquet file,
18
+ loads it into an in-memory DuckDB database as table 'paris_trees', and
19
+ executes the provided SQL query against it. Results are limited to 1000 rows.
20
+
21
+ Args:
22
+ sql_query (str): A valid SQL query to run against the paris_trees table.
23
+
24
+ Returns:
25
+ str: A formatted table string with column headers and up to 1000 rows of results.
26
+ If an error occurs, returns a descriptive error message.
27
+ """
28
+ url = "https://opendata.paris.fr/api/explore/v2.1/catalog/datasets/les-arbres/exports/parquet?lang=fr&timezone=Europe%2FBerlin"
29
+
30
+ try:
31
+ # Download the Parquet file
32
+ response = requests.get(url, timeout=60)
33
+ response.raise_for_status()
34
+
35
+ # Save to a temporary file
36
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".parquet") as tmp:
37
+ tmp.write(response.content)
38
+ tmp_path = tmp.name
39
+
40
+ # Connect to DuckDB in-memory and load the data
41
+ conn = duckdb.connect(":memory:")
42
+ conn.execute(f"CREATE TABLE paris_trees AS SELECT * FROM read_parquet('{tmp_path}')")
43
+
44
+ # Execute user query with a limit
45
+ limited_query = f"{sql_query.rstrip(';')} LIMIT 1000"
46
+ result_df = conn.execute(limited_query).df()
47
+
48
+ # Format output as a table string
49
+ if result_df.empty:
50
+ return "Query executed successfully but returned no results."
51
+
52
+ output = result_df.to_string(index=False)
53
+ return output
54
+
55
+ except requests.exceptions.RequestException as e:
56
+ return f"Error downloading dataset: {e}"
57
+ except duckdb.CatalogException as e:
58
+ return f"SQL error (possibly invalid table/column names): {e}"
59
+ except duckdb.ParserException as e:
60
+ return f"SQL syntax error: {e}"
61
+ except Exception as e:
62
+ return f"Unexpected error: {e}"
63
+ finally:
64
+ # Clean up temporary file
65
+ if 'tmp_path' in locals() and os.path.exists(tmp_path):
66
+ os.remove(tmp_path)
67
+
68
+ # --- Interface Factory ---
69
+ def create_interface():
70
+ return gr.Interface(
71
+ fn=paris_trees_sql,
72
+ inputs=[gr.Textbox(label=k) for k in ['sql_query']],
73
+ outputs=gr.Textbox(label="Query results formatted as a table (max 1000 rows) or an error message"),
74
+ title="paris_trees_sql",
75
+ description="Auto-generated tool: paris_trees_sql"
76
+ )