Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -407,9 +407,28 @@ def get_schema_info():
|
|
| 407 |
|
| 408 |
return md
|
| 409 |
|
| 410 |
-
# ===== FASTAPI =====
|
| 411 |
app = FastAPI(title="ConceptNet API", version="1.0")
|
| 412 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 413 |
@app.get("/api/profile/{word}")
|
| 414 |
def api_profile(word: str, lang: str = "en", limit: int = 10):
|
| 415 |
"""Get semantic profile"""
|
|
@@ -433,31 +452,13 @@ def api_languages():
|
|
| 433 |
"""List languages"""
|
| 434 |
return JSONResponse({"languages": TARGET_LANGUAGES})
|
| 435 |
|
| 436 |
-
@app.get("/")
|
| 437 |
-
def api_root():
|
| 438 |
-
"""API docs"""
|
| 439 |
-
return {
|
| 440 |
-
"name": "ConceptNet API",
|
| 441 |
-
"version": "1.0",
|
| 442 |
-
"endpoints": {
|
| 443 |
-
"/api/profile/{word}": "Semantic profile (params: lang, limit)",
|
| 444 |
-
"/api/query": "Query edges (params: start, relation, end, limit)",
|
| 445 |
-
"/api/relations": "List relations",
|
| 446 |
-
"/api/languages": "List languages"
|
| 447 |
-
},
|
| 448 |
-
"examples": {
|
| 449 |
-
"profile": "/api/profile/dog?lang=en&limit=10",
|
| 450 |
-
"query": "/api/query?start=dog&relation=IsA&limit=20"
|
| 451 |
-
}
|
| 452 |
-
}
|
| 453 |
-
|
| 454 |
# ===== GRADIO UI =====
|
| 455 |
with gr.Blocks(title="ConceptNet Explorer", theme=gr.themes.Soft()) as demo:
|
| 456 |
gr.Markdown("# π§ ConceptNet Explorer")
|
| 457 |
gr.Markdown(
|
| 458 |
f"**Multi-language semantic network** | "
|
| 459 |
f"**Languages:** {', '.join([l.upper() for l in TARGET_LANGUAGES])} | "
|
| 460 |
-
f"**API:**
|
| 461 |
)
|
| 462 |
|
| 463 |
with gr.Tabs():
|
|
@@ -504,26 +505,18 @@ with gr.Blocks(title="ConceptNet Explorer", theme=gr.themes.Soft()) as demo:
|
|
| 504 |
|
| 505 |
with gr.TabItem("π API"):
|
| 506 |
gr.Markdown("## JSON API Endpoints\n")
|
| 507 |
-
gr.Markdown("###
|
| 508 |
-
gr.Markdown("```\nGET /api/profile/{word}?lang=en&limit=10\n```")
|
| 509 |
-
gr.Markdown("
|
| 510 |
-
gr.Markdown("```\
|
| 511 |
-
|
| 512 |
-
gr.Markdown("
|
| 513 |
-
gr.Markdown("```\
|
| 514 |
-
gr.Markdown("**Example:**")
|
| 515 |
-
gr.Markdown("```\ncurl 'http://localhost:7860/api/query?start=dog&relation=IsA&limit=10'\n```\n")
|
| 516 |
-
|
| 517 |
-
gr.Markdown("### List Relations")
|
| 518 |
-
gr.Markdown("```\nGET /api/relations\n```\n")
|
| 519 |
-
|
| 520 |
-
gr.Markdown("### List Languages")
|
| 521 |
-
gr.Markdown("```\nGET /api/languages\n```")
|
| 522 |
|
| 523 |
gr.Markdown(
|
| 524 |
"---\n"
|
| 525 |
"**Performance:** Exact match on rel_id for fast queries | "
|
| 526 |
-
"**API:** Full REST API
|
| 527 |
)
|
| 528 |
|
| 529 |
semantic_btn.click(get_semantic_profile, [word_input, lang_input], semantic_output)
|
|
@@ -531,15 +524,17 @@ with gr.Blocks(title="ConceptNet Explorer", theme=gr.themes.Soft()) as demo:
|
|
| 531 |
raw_btn.click(run_raw_query, raw_sql_input, [raw_results, raw_status])
|
| 532 |
schema_btn.click(get_schema_info, None, schema_output)
|
| 533 |
|
| 534 |
-
#
|
| 535 |
app = gr.mount_gradio_app(app, demo, path="/")
|
| 536 |
|
| 537 |
if __name__ == "__main__":
|
| 538 |
log_progress("="*60, "SUCCESS")
|
| 539 |
-
log_progress("π APP READY
|
| 540 |
log_progress("="*60, "SUCCESS")
|
| 541 |
-
log_progress("UI: http://localhost:7860", "INFO")
|
| 542 |
-
log_progress("API: http://localhost:7860/api
|
|
|
|
|
|
|
| 543 |
log_progress("="*60, "SUCCESS")
|
| 544 |
|
| 545 |
import uvicorn
|
|
|
|
| 407 |
|
| 408 |
return md
|
| 409 |
|
| 410 |
+
# ===== FASTAPI - Create app FIRST =====
|
| 411 |
app = FastAPI(title="ConceptNet API", version="1.0")
|
| 412 |
|
| 413 |
+
@app.get("/api")
|
| 414 |
+
def api_docs():
|
| 415 |
+
"""API documentation - accessible at /api"""
|
| 416 |
+
return {
|
| 417 |
+
"name": "ConceptNet API",
|
| 418 |
+
"version": "1.0",
|
| 419 |
+
"endpoints": {
|
| 420 |
+
"/api/profile/{word}": "Semantic profile (params: lang, limit)",
|
| 421 |
+
"/api/query": "Query edges (params: start, relation, end, limit)",
|
| 422 |
+
"/api/relations": "List relations",
|
| 423 |
+
"/api/languages": "List languages"
|
| 424 |
+
},
|
| 425 |
+
"examples": {
|
| 426 |
+
"profile": "/api/profile/dog?lang=en&limit=10",
|
| 427 |
+
"query": "/api/query?start=dog&relation=IsA&limit=20"
|
| 428 |
+
},
|
| 429 |
+
"note": "Visit root (/) for the Gradio UI"
|
| 430 |
+
}
|
| 431 |
+
|
| 432 |
@app.get("/api/profile/{word}")
|
| 433 |
def api_profile(word: str, lang: str = "en", limit: int = 10):
|
| 434 |
"""Get semantic profile"""
|
|
|
|
| 452 |
"""List languages"""
|
| 453 |
return JSONResponse({"languages": TARGET_LANGUAGES})
|
| 454 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 455 |
# ===== GRADIO UI =====
|
| 456 |
with gr.Blocks(title="ConceptNet Explorer", theme=gr.themes.Soft()) as demo:
|
| 457 |
gr.Markdown("# π§ ConceptNet Explorer")
|
| 458 |
gr.Markdown(
|
| 459 |
f"**Multi-language semantic network** | "
|
| 460 |
f"**Languages:** {', '.join([l.upper() for l in TARGET_LANGUAGES])} | "
|
| 461 |
+
f"**API:** `/api/profile/{{word}}` `/api/query`"
|
| 462 |
)
|
| 463 |
|
| 464 |
with gr.Tabs():
|
|
|
|
| 505 |
|
| 506 |
with gr.TabItem("π API"):
|
| 507 |
gr.Markdown("## JSON API Endpoints\n")
|
| 508 |
+
gr.Markdown("### API Documentation\n```\nGET /api\n```")
|
| 509 |
+
gr.Markdown("### Get Semantic Profile\n```\nGET /api/profile/{word}?lang=en&limit=10\n```")
|
| 510 |
+
gr.Markdown("### Query Edges\n```\nGET /api/query?start=dog&relation=IsA&limit=50\n```")
|
| 511 |
+
gr.Markdown("### List Relations\n```\nGET /api/relations\n```")
|
| 512 |
+
gr.Markdown("### Examples\n")
|
| 513 |
+
gr.Markdown("```\ncurl https://your-space.hf.space/api/profile/dog?lang=en\n```")
|
| 514 |
+
gr.Markdown("```\ncurl 'https://your-space.hf.space/api/query?start=dog&relation=IsA&limit=10'\n```")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 515 |
|
| 516 |
gr.Markdown(
|
| 517 |
"---\n"
|
| 518 |
"**Performance:** Exact match on rel_id for fast queries | "
|
| 519 |
+
"**API:** Full REST API at `/api/*` endpoints"
|
| 520 |
)
|
| 521 |
|
| 522 |
semantic_btn.click(get_semantic_profile, [word_input, lang_input], semantic_output)
|
|
|
|
| 524 |
raw_btn.click(run_raw_query, raw_sql_input, [raw_results, raw_status])
|
| 525 |
schema_btn.click(get_schema_info, None, schema_output)
|
| 526 |
|
| 527 |
+
# ===== MOUNT GRADIO TO FASTAPI - Gradio at root, API at /api/* =====
|
| 528 |
app = gr.mount_gradio_app(app, demo, path="/")
|
| 529 |
|
| 530 |
if __name__ == "__main__":
|
| 531 |
log_progress("="*60, "SUCCESS")
|
| 532 |
+
log_progress("π APP READY!", "SUCCESS")
|
| 533 |
log_progress("="*60, "SUCCESS")
|
| 534 |
+
log_progress("UI: http://localhost:7860/", "INFO")
|
| 535 |
+
log_progress("API: http://localhost:7860/api", "INFO")
|
| 536 |
+
log_progress(" http://localhost:7860/api/profile/dog", "INFO")
|
| 537 |
+
log_progress(" http://localhost:7860/api/query?start=dog&relation=IsA", "INFO")
|
| 538 |
log_progress("="*60, "SUCCESS")
|
| 539 |
|
| 540 |
import uvicorn
|