Spaces:
Sleeping
Sleeping
use semantic search API as primary source for ai_short_description
Browse files
app.py
CHANGED
|
@@ -14,10 +14,10 @@ def build_agent_prompt(space_info: dict) -> str:
|
|
| 14 |
description = space_info.get("ai_short_description") or (space_info.get("cardData") or {}).get("short_description")
|
| 15 |
base_url = f"https://{subdomain}.hf.space"
|
| 16 |
|
| 17 |
-
|
| 18 |
return (
|
| 19 |
-
f'
|
| 20 |
-
f'
|
| 21 |
f'Call endpoint: POST {base_url}/gradio_api/call/{{endpoint}} {{"data": [...]}} — '
|
| 22 |
f'Poll result: GET {base_url}/gradio_api/call/{{endpoint}}/{{event_id}} — '
|
| 23 |
f'auth: Bearer $HF_TOKEN'
|
|
@@ -42,6 +42,16 @@ async def proxy_space(namespace: str, repo: str, request: Request):
|
|
| 42 |
if space_info.get("sdk") != "gradio":
|
| 43 |
return HTMLResponse(f"<html><body>Only Gradio spaces are supported. This space uses: {space_info.get('sdk')}</body></html>", status_code=400)
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
# Fetch the actual space page HTML
|
| 46 |
page_resp = await client.get(f"https://huggingface.co/spaces/{space_id}", headers=headers)
|
| 47 |
html = page_resp.text
|
|
@@ -61,6 +71,25 @@ async def proxy_space(namespace: str, repo: str, request: Request):
|
|
| 61 |
return HTMLResponse(html)
|
| 62 |
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
@app.get("/", response_class=HTMLResponse)
|
| 65 |
async def home():
|
| 66 |
return HTMLResponse("""
|
|
|
|
| 14 |
description = space_info.get("ai_short_description") or (space_info.get("cardData") or {}).get("short_description")
|
| 15 |
base_url = f"https://{subdomain}.hf.space"
|
| 16 |
|
| 17 |
+
prefix = f"To use this application ({space_id}: {description}) — " if description else ""
|
| 18 |
return (
|
| 19 |
+
f'{prefix}'
|
| 20 |
+
f'API schema: GET {base_url}/gradio_api/info — '
|
| 21 |
f'Call endpoint: POST {base_url}/gradio_api/call/{{endpoint}} {{"data": [...]}} — '
|
| 22 |
f'Poll result: GET {base_url}/gradio_api/call/{{endpoint}}/{{event_id}} — '
|
| 23 |
f'auth: Bearer $HF_TOKEN'
|
|
|
|
| 42 |
if space_info.get("sdk") != "gradio":
|
| 43 |
return HTMLResponse(f"<html><body>Only Gradio spaces are supported. This space uses: {space_info.get('sdk')}</body></html>", status_code=400)
|
| 44 |
|
| 45 |
+
# Fetch ai_short_description from semantic search
|
| 46 |
+
search_resp = await client.get(
|
| 47 |
+
f"https://huggingface.co/api/spaces/semantic-search?q={space_id}&sdk=gradio",
|
| 48 |
+
headers=headers,
|
| 49 |
+
)
|
| 50 |
+
results = search_resp.json() if search_resp.status_code == 200 else []
|
| 51 |
+
match = next((r for r in results if r.get("id") == space_id), None)
|
| 52 |
+
if match:
|
| 53 |
+
space_info["ai_short_description"] = match.get("ai_short_description")
|
| 54 |
+
|
| 55 |
# Fetch the actual space page HTML
|
| 56 |
page_resp = await client.get(f"https://huggingface.co/spaces/{space_id}", headers=headers)
|
| 57 |
html = page_resp.text
|
|
|
|
| 71 |
return HTMLResponse(html)
|
| 72 |
|
| 73 |
|
| 74 |
+
@app.get("/agent-instructions/{namespace}/{repo}")
|
| 75 |
+
async def agent_instructions(namespace: str, repo: str):
|
| 76 |
+
space_id = f"{namespace}/{repo}"
|
| 77 |
+
headers = {}
|
| 78 |
+
if HF_TOKEN:
|
| 79 |
+
headers["Authorization"] = f"Bearer {HF_TOKEN}"
|
| 80 |
+
|
| 81 |
+
async with httpx.AsyncClient(follow_redirects=True) as client:
|
| 82 |
+
api_resp = await client.get(f"https://huggingface.co/api/spaces/{space_id}", headers=headers)
|
| 83 |
+
if api_resp.status_code != 200:
|
| 84 |
+
return {"error": f"Space not found: {space_id}"}
|
| 85 |
+
|
| 86 |
+
space_info = api_resp.json()
|
| 87 |
+
if space_info.get("sdk") != "gradio":
|
| 88 |
+
return {"error": f"Only Gradio spaces supported. This uses: {space_info.get('sdk')}"}
|
| 89 |
+
|
| 90 |
+
return {"instructions": build_agent_prompt(space_info)}
|
| 91 |
+
|
| 92 |
+
|
| 93 |
@app.get("/", response_class=HTMLResponse)
|
| 94 |
async def home():
|
| 95 |
return HTMLResponse("""
|