MukeshKapoor25 commited on
Commit
4452393
·
verified ·
1 Parent(s): 45b4db7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -8
app.py CHANGED
@@ -1,9 +1,14 @@
 
 
 
1
  import spacy
2
- from typing import Dict, Any, Optional
3
 
4
  # Load spaCy language model
5
  nlp = spacy.load("en_core_web_sm")
6
 
 
 
 
7
  # Define mappings for categories and filters
8
  KEYWORD_MAPPINGS = {
9
  "categories": {
@@ -27,6 +32,13 @@ KEYWORD_MAPPINGS = {
27
  },
28
  }
29
 
 
 
 
 
 
 
 
30
  def parse_sentence_to_query_ner(sentence: str, lat: Optional[float] = None, lng: Optional[float] = None) -> Dict[str, Any]:
31
  """
32
  Parse a sentence using spaCy NER and build a search query.
@@ -73,11 +85,21 @@ def parse_sentence_to_query_ner(sentence: str, lat: Optional[float] = None, lng:
73
 
74
  return query
75
 
76
- # Example usage
77
- if __name__ == "__main__":
78
- sentence = "Top-rated salons near me"
79
- latitude = 13.0827
80
- longitude = 80.2707
 
 
 
81
 
82
- query = parse_sentence_to_query_ner(sentence, lat=latitude, lng=longitude)
83
- print(query)
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Query
2
+ from pydantic import BaseModel
3
+ from typing import Optional, Dict, Any
4
  import spacy
 
5
 
6
  # Load spaCy language model
7
  nlp = spacy.load("en_core_web_sm")
8
 
9
+ # FastAPI instance
10
+ app = FastAPI()
11
+
12
  # Define mappings for categories and filters
13
  KEYWORD_MAPPINGS = {
14
  "categories": {
 
32
  },
33
  }
34
 
35
+ # Pydantic schema for the input query
36
+ class QueryRequest(BaseModel):
37
+ sentence: str
38
+ latitude: Optional[float] = None
39
+ longitude: Optional[float] = None
40
+
41
+ # Function to parse the sentence using spaCy
42
  def parse_sentence_to_query_ner(sentence: str, lat: Optional[float] = None, lng: Optional[float] = None) -> Dict[str, Any]:
43
  """
44
  Parse a sentence using spaCy NER and build a search query.
 
85
 
86
  return query
87
 
88
+ # FastAPI route
89
+ @app.post("/parse-query/")
90
+ async def parse_query(request: QueryRequest):
91
+ """
92
+ API endpoint to parse a query and return a structured response.
93
+
94
+ Args:
95
+ request (QueryRequest): Input sentence and optional location data.
96
 
97
+ Returns:
98
+ Dict[str, Any]: Parsed search query.
99
+ """
100
+ parsed_query = parse_sentence_to_query_ner(
101
+ sentence=request.sentence,
102
+ lat=request.latitude,
103
+ lng=request.longitude
104
+ )
105
+ return {"query": parsed_query}