Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -721,11 +721,10 @@ demo = gr.TabbedInterface(
|
|
| 721 |
title="π₯ Hybrid Search RAGtim Bot - Vector + BM25 Fusion"
|
| 722 |
)
|
| 723 |
|
| 724 |
-
#
|
| 725 |
-
def
|
| 726 |
-
"""Handle
|
| 727 |
try:
|
| 728 |
-
# Get query parameters
|
| 729 |
query = request.query_params.get('query', '')
|
| 730 |
top_k = int(request.query_params.get('top_k', 5))
|
| 731 |
search_type = request.query_params.get('search_type', 'hybrid')
|
|
@@ -739,15 +738,60 @@ def api_search(request: gr.Request):
|
|
| 739 |
except Exception as e:
|
| 740 |
return {"error": str(e)}
|
| 741 |
|
| 742 |
-
def
|
| 743 |
-
"""Handle
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 744 |
try:
|
| 745 |
return get_stats_api()
|
| 746 |
except Exception as e:
|
| 747 |
return {"error": str(e)}
|
| 748 |
|
| 749 |
-
#
|
| 750 |
-
demo.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 751 |
|
| 752 |
if __name__ == "__main__":
|
| 753 |
print("π Launching Hybrid Search RAGtim Bot...")
|
|
@@ -756,60 +800,6 @@ if __name__ == "__main__":
|
|
| 756 |
print(f"π§ Vector embeddings: {len(bot.embeddings)} documents")
|
| 757 |
print("π₯ Hybrid search ready: Semantic + Keyword fusion!")
|
| 758 |
|
| 759 |
-
# Create a custom app with API routes
|
| 760 |
-
import uvicorn
|
| 761 |
-
from fastapi import FastAPI, Request
|
| 762 |
-
from fastapi.responses import JSONResponse
|
| 763 |
-
|
| 764 |
-
app = FastAPI()
|
| 765 |
-
|
| 766 |
-
@app.get("/api/search")
|
| 767 |
-
async def search_endpoint(request: Request):
|
| 768 |
-
try:
|
| 769 |
-
query = request.query_params.get('query', '')
|
| 770 |
-
top_k = int(request.query_params.get('top_k', 5))
|
| 771 |
-
search_type = request.query_params.get('search_type', 'hybrid')
|
| 772 |
-
vector_weight = float(request.query_params.get('vector_weight', 0.6))
|
| 773 |
-
bm25_weight = float(request.query_params.get('bm25_weight', 0.4))
|
| 774 |
-
|
| 775 |
-
if not query:
|
| 776 |
-
return JSONResponse({"error": "Query parameter is required"}, status_code=400)
|
| 777 |
-
|
| 778 |
-
result = search_api(query, top_k, search_type, vector_weight, bm25_weight)
|
| 779 |
-
return JSONResponse(result)
|
| 780 |
-
except Exception as e:
|
| 781 |
-
return JSONResponse({"error": str(e)}, status_code=500)
|
| 782 |
-
|
| 783 |
-
@app.post("/api/search")
|
| 784 |
-
async def search_endpoint_post(request: Request):
|
| 785 |
-
try:
|
| 786 |
-
body = await request.json()
|
| 787 |
-
query = body.get('query', '')
|
| 788 |
-
top_k = body.get('top_k', 5)
|
| 789 |
-
search_type = body.get('search_type', 'hybrid')
|
| 790 |
-
vector_weight = body.get('vector_weight', 0.6)
|
| 791 |
-
bm25_weight = body.get('bm25_weight', 0.4)
|
| 792 |
-
|
| 793 |
-
if not query:
|
| 794 |
-
return JSONResponse({"error": "Query is required"}, status_code=400)
|
| 795 |
-
|
| 796 |
-
result = search_api(query, top_k, search_type, vector_weight, bm25_weight)
|
| 797 |
-
return JSONResponse(result)
|
| 798 |
-
except Exception as e:
|
| 799 |
-
return JSONResponse({"error": str(e)}, status_code=500)
|
| 800 |
-
|
| 801 |
-
@app.get("/api/stats")
|
| 802 |
-
async def stats_endpoint():
|
| 803 |
-
try:
|
| 804 |
-
result = get_stats_api()
|
| 805 |
-
return JSONResponse(result)
|
| 806 |
-
except Exception as e:
|
| 807 |
-
return JSONResponse({"error": str(e)}, status_code=500)
|
| 808 |
-
|
| 809 |
-
# Mount Gradio app
|
| 810 |
-
app = gr.mount_gradio_app(app, demo, path="/")
|
| 811 |
-
|
| 812 |
-
# For Hugging Face Spaces, just launch the demo
|
| 813 |
demo.launch(
|
| 814 |
server_name="0.0.0.0",
|
| 815 |
server_port=7860,
|
|
|
|
| 721 |
title="π₯ Hybrid Search RAGtim Bot - Vector + BM25 Fusion"
|
| 722 |
)
|
| 723 |
|
| 724 |
+
# Create API endpoints using Gradio's built-in API functionality
|
| 725 |
+
def api_search_get(request: gr.Request):
|
| 726 |
+
"""Handle GET requests to /api/search"""
|
| 727 |
try:
|
|
|
|
| 728 |
query = request.query_params.get('query', '')
|
| 729 |
top_k = int(request.query_params.get('top_k', 5))
|
| 730 |
search_type = request.query_params.get('search_type', 'hybrid')
|
|
|
|
| 738 |
except Exception as e:
|
| 739 |
return {"error": str(e)}
|
| 740 |
|
| 741 |
+
def api_search_post(query: str, top_k: int = 5, search_type: str = "hybrid", vector_weight: float = 0.6, bm25_weight: float = 0.4):
|
| 742 |
+
"""Handle POST requests to /api/search"""
|
| 743 |
+
try:
|
| 744 |
+
if not query:
|
| 745 |
+
return {"error": "Query is required"}
|
| 746 |
+
|
| 747 |
+
return search_api(query, top_k, search_type, vector_weight, bm25_weight)
|
| 748 |
+
except Exception as e:
|
| 749 |
+
return {"error": str(e)}
|
| 750 |
+
|
| 751 |
+
def api_stats():
|
| 752 |
+
"""Handle requests to /api/stats"""
|
| 753 |
try:
|
| 754 |
return get_stats_api()
|
| 755 |
except Exception as e:
|
| 756 |
return {"error": str(e)}
|
| 757 |
|
| 758 |
+
# Add API endpoints to the demo
|
| 759 |
+
demo.api_name = "hybrid_search_api"
|
| 760 |
+
|
| 761 |
+
# Add the API functions as named endpoints
|
| 762 |
+
demo.fn_index_to_fn_name = {
|
| 763 |
+
len(demo.fns): "search_get",
|
| 764 |
+
len(demo.fns) + 1: "search_post",
|
| 765 |
+
len(demo.fns) + 2: "stats"
|
| 766 |
+
}
|
| 767 |
+
|
| 768 |
+
# Add API functions to the demo
|
| 769 |
+
demo.fns.append(gr.Interface(
|
| 770 |
+
fn=api_search_get,
|
| 771 |
+
inputs=gr.Request(),
|
| 772 |
+
outputs=gr.JSON(),
|
| 773 |
+
api_name="search_get"
|
| 774 |
+
))
|
| 775 |
+
|
| 776 |
+
demo.fns.append(gr.Interface(
|
| 777 |
+
fn=api_search_post,
|
| 778 |
+
inputs=[
|
| 779 |
+
gr.Textbox(label="query"),
|
| 780 |
+
gr.Number(label="top_k", value=5),
|
| 781 |
+
gr.Textbox(label="search_type", value="hybrid"),
|
| 782 |
+
gr.Number(label="vector_weight", value=0.6),
|
| 783 |
+
gr.Number(label="bm25_weight", value=0.4)
|
| 784 |
+
],
|
| 785 |
+
outputs=gr.JSON(),
|
| 786 |
+
api_name="search_post"
|
| 787 |
+
))
|
| 788 |
+
|
| 789 |
+
demo.fns.append(gr.Interface(
|
| 790 |
+
fn=api_stats,
|
| 791 |
+
inputs=[],
|
| 792 |
+
outputs=gr.JSON(),
|
| 793 |
+
api_name="stats"
|
| 794 |
+
))
|
| 795 |
|
| 796 |
if __name__ == "__main__":
|
| 797 |
print("π Launching Hybrid Search RAGtim Bot...")
|
|
|
|
| 800 |
print(f"π§ Vector embeddings: {len(bot.embeddings)} documents")
|
| 801 |
print("π₯ Hybrid search ready: Semantic + Keyword fusion!")
|
| 802 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 803 |
demo.launch(
|
| 804 |
server_name="0.0.0.0",
|
| 805 |
server_port=7860,
|