Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
#
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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}
|