Download functions/sqlite_functions.py from nolanzandi/virtual-data-analyst: direct link, hf CLI and curl.
- Browser
- Download file 1.1 kB
-
https://huggingface.co/spaces/nolanzandi/virtual-data-analyst/resolve/refs%2Fpr%2F8/functions/sqlite_functions.py
- Command line
-
hf download hf://spaces/nolanzandi/virtual-data-analyst@refs/pr/8/functions/sqlite_functions.py
-
curl -L -o sqlite_functions.py https://huggingface.co/spaces/nolanzandi/virtual-data-analyst/resolve/refs%2Fpr%2F8/functions/sqlite_functions.py
1.1 kB
| from typing import List | |
| from haystack import component | |
| import pandas as pd | |
| import sqlite3 | |
| class SQLiteQuery: | |
| def __init__(self, sql_database: str): | |
| self.connection = sqlite3.connect(sql_database, check_same_thread=False) | |
| def run(self, queries: List[str]): | |
| print("ATTEMPTING TO RUN QUERY") | |
| results = [] | |
| for query in queries: | |
| result = pd.read_sql(query, self.connection) | |
| results.append(f"{result}") | |
| self.connection.close() | |
| return {"results": results, "queries": queries} | |
| def sqlite_query_func(queries: List[str], session_hash): | |
| sql_query = SQLiteQuery(f'data_source_{session_hash}.db') | |
| try: | |
| result = sql_query.run(queries) | |
| return {"reply": result["results"][0]} | |
| except Exception as e: | |
| reply = f"""There was an error running the SQL Query = {queries} | |
| The error is {e}, | |
| You should probably try again. | |
| """ | |
| return {"reply": reply} | |