Spaces:
Running
Running
add token support
Browse files
app.py
CHANGED
@@ -161,22 +161,23 @@ import json
|
|
161 |
# Download the health data
|
162 |
DATA_REPO = "{dataset_repo_id}"
|
163 |
|
164 |
-
def get_db_connection():
|
165 |
"""Get a connection to the SQLite database."""
|
166 |
# Download the health_data.db file from the dataset
|
167 |
db_path = hf_hub_download(
|
168 |
repo_id=DATA_REPO,
|
169 |
filename="health_data.db",
|
170 |
repo_type="dataset",
|
171 |
-
|
172 |
)
|
173 |
return sqlite3.connect(db_path)
|
174 |
|
175 |
-
def execute_sql_query(sql_query):
|
176 |
"""Execute any SQL query on the Apple Health SQLite database.
|
177 |
|
178 |
Args:
|
179 |
sql_query (str): The SQL query to execute
|
|
|
180 |
|
181 |
Returns:
|
182 |
str: JSON formatted results or error message
|
@@ -185,7 +186,7 @@ def execute_sql_query(sql_query):
|
|
185 |
return "Error: Empty SQL query provided"
|
186 |
|
187 |
try:
|
188 |
-
conn = get_db_connection()
|
189 |
|
190 |
# Execute the query
|
191 |
result = pd.read_sql_query(sql_query, conn)
|
@@ -206,6 +207,13 @@ with gr.Blocks(title="Apple Health MCP Server") as demo:
|
|
206 |
gr.Markdown("### Execute SQL Queries")
|
207 |
gr.Markdown("Enter any SQL query to execute against your Apple Health SQLite database.")
|
208 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
209 |
sql_input = gr.Textbox(
|
210 |
label="SQL Query",
|
211 |
placeholder="SELECT * FROM records LIMIT 10;",
|
@@ -218,7 +226,7 @@ with gr.Blocks(title="Apple Health MCP Server") as demo:
|
|
218 |
|
219 |
query_btn.click(
|
220 |
fn=execute_sql_query,
|
221 |
-
inputs=[sql_input],
|
222 |
outputs=output
|
223 |
)
|
224 |
|
@@ -234,17 +242,23 @@ with gr.Blocks(title="Apple Health MCP Server") as demo:
|
|
234 |
"apple-health": {{
|
235 |
"command": "npx",
|
236 |
"args": [
|
237 |
-
"-
|
238 |
-
"
|
239 |
-
"
|
240 |
-
"
|
241 |
-
]
|
|
|
|
|
|
|
242 |
}}
|
243 |
}}
|
244 |
}}
|
245 |
```
|
246 |
|
247 |
-
|
|
|
|
|
|
|
248 |
""")
|
249 |
|
250 |
if __name__ == "__main__":
|
|
|
161 |
# Download the health data
|
162 |
DATA_REPO = "{dataset_repo_id}"
|
163 |
|
164 |
+
def get_db_connection(token=None):
|
165 |
"""Get a connection to the SQLite database."""
|
166 |
# Download the health_data.db file from the dataset
|
167 |
db_path = hf_hub_download(
|
168 |
repo_id=DATA_REPO,
|
169 |
filename="health_data.db",
|
170 |
repo_type="dataset",
|
171 |
+
token=token
|
172 |
)
|
173 |
return sqlite3.connect(db_path)
|
174 |
|
175 |
+
def execute_sql_query(sql_query, hf_token=None):
|
176 |
"""Execute any SQL query on the Apple Health SQLite database.
|
177 |
|
178 |
Args:
|
179 |
sql_query (str): The SQL query to execute
|
180 |
+
hf_token (str): Hugging Face token for accessing private dataset
|
181 |
|
182 |
Returns:
|
183 |
str: JSON formatted results or error message
|
|
|
186 |
return "Error: Empty SQL query provided"
|
187 |
|
188 |
try:
|
189 |
+
conn = get_db_connection(token=hf_token)
|
190 |
|
191 |
# Execute the query
|
192 |
result = pd.read_sql_query(sql_query, conn)
|
|
|
207 |
gr.Markdown("### Execute SQL Queries")
|
208 |
gr.Markdown("Enter any SQL query to execute against your Apple Health SQLite database.")
|
209 |
|
210 |
+
hf_token_input = gr.Textbox(
|
211 |
+
label="Hugging Face Token",
|
212 |
+
placeholder="hf_...",
|
213 |
+
type="password",
|
214 |
+
info="Your HF token to access the private dataset. Get it from https://huggingface.co/settings/tokens"
|
215 |
+
)
|
216 |
+
|
217 |
sql_input = gr.Textbox(
|
218 |
label="SQL Query",
|
219 |
placeholder="SELECT * FROM records LIMIT 10;",
|
|
|
226 |
|
227 |
query_btn.click(
|
228 |
fn=execute_sql_query,
|
229 |
+
inputs=[sql_input, hf_token_input],
|
230 |
outputs=output
|
231 |
)
|
232 |
|
|
|
242 |
"apple-health": {{
|
243 |
"command": "npx",
|
244 |
"args": [
|
245 |
+
"mcp-remote",
|
246 |
+
"https://huggingface.co/spaces/{space_repo_id}/gradio_api/mcp/sse",
|
247 |
+
"--header",
|
248 |
+
"Authorization:${{AUTH_HEADER}}"
|
249 |
+
],
|
250 |
+
"env": {{
|
251 |
+
"AUTH_HEADER": "Bearer YOUR_HF_TOKEN_HERE"
|
252 |
+
}}
|
253 |
}}
|
254 |
}}
|
255 |
}}
|
256 |
```
|
257 |
|
258 |
+
**Setup Instructions:**
|
259 |
+
1. Replace `YOUR_HF_TOKEN_HERE` with your actual Hugging Face token
|
260 |
+
2. Add this configuration to your Claude Desktop config file
|
261 |
+
3. Claude will be able to query your Apple Health data using SQL
|
262 |
""")
|
263 |
|
264 |
if __name__ == "__main__":
|