Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -721,77 +721,46 @@ demo = gr.TabbedInterface(
|
|
| 721 |
title="π₯ Hybrid Search RAGtim Bot - Vector + BM25 Fusion"
|
| 722 |
)
|
| 723 |
|
| 724 |
-
# Create API
|
| 725 |
-
def
|
| 726 |
-
"""
|
| 727 |
try:
|
| 728 |
-
query
|
| 729 |
-
top_k = int(request.query_params.get('top_k', 5))
|
| 730 |
-
search_type = request.query_params.get('search_type', 'hybrid')
|
| 731 |
-
vector_weight = float(request.query_params.get('vector_weight', 0.6))
|
| 732 |
-
bm25_weight = float(request.query_params.get('bm25_weight', 0.4))
|
| 733 |
-
|
| 734 |
-
if not query:
|
| 735 |
return {"error": "Query parameter is required"}
|
| 736 |
|
| 737 |
-
return search_api(query, top_k, search_type, vector_weight, bm25_weight)
|
| 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
|
| 752 |
-
"""
|
| 753 |
try:
|
| 754 |
return get_stats_api()
|
| 755 |
except Exception as e:
|
| 756 |
return {"error": str(e)}
|
| 757 |
|
| 758 |
-
#
|
| 759 |
-
|
| 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.
|
| 782 |
-
gr.Number(label="vector_weight", value=0.6),
|
| 783 |
-
gr.Number(label="bm25_weight", value=0.4)
|
| 784 |
],
|
| 785 |
-
outputs=gr.JSON(),
|
| 786 |
-
|
| 787 |
-
|
|
|
|
| 788 |
|
| 789 |
-
|
| 790 |
-
fn=
|
| 791 |
inputs=[],
|
| 792 |
-
outputs=gr.JSON(),
|
| 793 |
-
|
| 794 |
-
|
|
|
|
| 795 |
|
| 796 |
if __name__ == "__main__":
|
| 797 |
print("π Launching Hybrid Search RAGtim Bot...")
|
|
@@ -800,9 +769,15 @@ if __name__ == "__main__":
|
|
| 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,
|
| 806 |
share=False,
|
| 807 |
show_error=True
|
| 808 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 721 |
title="π₯ Hybrid Search RAGtim Bot - Vector + BM25 Fusion"
|
| 722 |
)
|
| 723 |
|
| 724 |
+
# Create API functions for external access
|
| 725 |
+
def api_search_function(query: str, top_k: int = 5, search_type: str = "hybrid", vector_weight: float = 0.6, bm25_weight: float = 0.4):
|
| 726 |
+
"""API function for search - accessible via Gradio API"""
|
| 727 |
try:
|
| 728 |
+
if not query or not query.strip():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 729 |
return {"error": "Query parameter is required"}
|
| 730 |
|
| 731 |
+
return search_api(query.strip(), top_k, search_type, vector_weight, bm25_weight)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 732 |
except Exception as e:
|
| 733 |
return {"error": str(e)}
|
| 734 |
|
| 735 |
+
def api_stats_function():
|
| 736 |
+
"""API function for stats - accessible via Gradio API"""
|
| 737 |
try:
|
| 738 |
return get_stats_api()
|
| 739 |
except Exception as e:
|
| 740 |
return {"error": str(e)}
|
| 741 |
|
| 742 |
+
# Create separate API interfaces that can be accessed via HTTP
|
| 743 |
+
search_api_interface = gr.Interface(
|
| 744 |
+
fn=api_search_function,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 745 |
inputs=[
|
| 746 |
+
gr.Textbox(label="query", placeholder="Enter search query"),
|
| 747 |
+
gr.Number(label="top_k", value=5, minimum=1, maximum=20),
|
| 748 |
+
gr.Dropdown(label="search_type", choices=["hybrid", "vector", "bm25"], value="hybrid"),
|
| 749 |
+
gr.Number(label="vector_weight", value=0.6, minimum=0.0, maximum=1.0),
|
| 750 |
+
gr.Number(label="bm25_weight", value=0.4, minimum=0.0, maximum=1.0)
|
| 751 |
],
|
| 752 |
+
outputs=gr.JSON(label="Search Results"),
|
| 753 |
+
title="Search API",
|
| 754 |
+
description="Hybrid search API endpoint"
|
| 755 |
+
)
|
| 756 |
|
| 757 |
+
stats_api_interface = gr.Interface(
|
| 758 |
+
fn=api_stats_function,
|
| 759 |
inputs=[],
|
| 760 |
+
outputs=gr.JSON(label="Statistics"),
|
| 761 |
+
title="Stats API",
|
| 762 |
+
description="Knowledge base statistics API endpoint"
|
| 763 |
+
)
|
| 764 |
|
| 765 |
if __name__ == "__main__":
|
| 766 |
print("π Launching Hybrid Search RAGtim Bot...")
|
|
|
|
| 769 |
print(f"π§ Vector embeddings: {len(bot.embeddings)} documents")
|
| 770 |
print("π₯ Hybrid search ready: Semantic + Keyword fusion!")
|
| 771 |
|
| 772 |
+
# Launch the main demo
|
| 773 |
demo.launch(
|
| 774 |
server_name="0.0.0.0",
|
| 775 |
server_port=7860,
|
| 776 |
share=False,
|
| 777 |
show_error=True
|
| 778 |
+
)
|
| 779 |
+
|
| 780 |
+
# Note: The API interfaces are available at:
|
| 781 |
+
# - Main interface: https://your-space-url.hf.space
|
| 782 |
+
# - Search API: https://your-space-url.hf.space/api/search (via the main interface)
|
| 783 |
+
# - Stats API: https://your-space-url.hf.space/api/stats (via the main interface)
|