raktimhugging commited on
Commit
6016ab1
Β·
verified Β·
1 Parent(s): 3592121

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -57
app.py CHANGED
@@ -721,77 +721,46 @@ demo = gr.TabbedInterface(
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')
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 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,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)