Spaces:
Sleeping
Sleeping
findEthics commited on
Commit ·
b4e0ec0
1
Parent(s): d13810a
Update NER load fucntion
Browse files
app.py
CHANGED
|
@@ -43,14 +43,25 @@ class SearchRequest(BaseModel):
|
|
| 43 |
max_results: int = 5
|
| 44 |
|
| 45 |
qa_pipeline = None
|
|
|
|
| 46 |
|
| 47 |
def load_ner_model():
|
| 48 |
"""Load Named Entity Recognition model"""
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
# Function to load the local language model
|
| 56 |
def load_model():
|
|
@@ -114,7 +125,7 @@ def generate_response(prompt: str,search_context: str) -> str:
|
|
| 114 |
async def startup_event():
|
| 115 |
"""Load model on startup"""
|
| 116 |
load_model()
|
| 117 |
-
|
| 118 |
|
| 119 |
@app.get("/")
|
| 120 |
async def root():
|
|
@@ -140,7 +151,7 @@ async def chat(request: ChatRequest):
|
|
| 140 |
if request.use_search:
|
| 141 |
|
| 142 |
# Extract entities for focused search
|
| 143 |
-
entities =
|
| 144 |
logger.info(f"Identified entities: {entities}")
|
| 145 |
|
| 146 |
# Create search query from entities
|
|
@@ -149,11 +160,10 @@ async def chat(request: ChatRequest):
|
|
| 149 |
if ent["entity_group"] in ["PER", "ORG", "LOC", "MISC"]
|
| 150 |
]
|
| 151 |
search_query = " ".join(search_terms) if search_terms else request.prompt
|
| 152 |
-
|
| 153 |
|
| 154 |
search_results = search_web(search_query)
|
| 155 |
-
|
| 156 |
-
|
| 157 |
search_context = "\n".join([
|
| 158 |
f"- {result['title']}: {result['body'][:200]}..."
|
| 159 |
for result in search_results[:min(len(search_results), 5)]
|
|
@@ -162,7 +172,7 @@ async def chat(request: ChatRequest):
|
|
| 162 |
else:
|
| 163 |
search_context = request.prompt
|
| 164 |
|
| 165 |
-
|
| 166 |
if search_context:
|
| 167 |
# Generate response
|
| 168 |
response = generate_response(
|
|
|
|
| 43 |
max_results: int = 5
|
| 44 |
|
| 45 |
qa_pipeline = None
|
| 46 |
+
ner_pipeline = None
|
| 47 |
|
| 48 |
def load_ner_model():
|
| 49 |
"""Load Named Entity Recognition model"""
|
| 50 |
+
global ner_pipeline
|
| 51 |
+
try:
|
| 52 |
+
# Check if GPU is available
|
| 53 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 54 |
+
logger.info(f"Using device: {device}")
|
| 55 |
+
|
| 56 |
+
ner_pipeline = pipeline(
|
| 57 |
+
"ner",
|
| 58 |
+
model="dbmdz/bert-large-cased-finetuned-conll03-english",
|
| 59 |
+
device=0 if device == "cuda" else -1,
|
| 60 |
+
grouped_entities=True
|
| 61 |
+
)
|
| 62 |
+
logger.info("NER model loaded successfully!")
|
| 63 |
+
except Exception as e:
|
| 64 |
+
logger.error(f"Error loading NER model: {e}")
|
| 65 |
|
| 66 |
# Function to load the local language model
|
| 67 |
def load_model():
|
|
|
|
| 125 |
async def startup_event():
|
| 126 |
"""Load model on startup"""
|
| 127 |
load_model()
|
| 128 |
+
load_ner_model()
|
| 129 |
|
| 130 |
@app.get("/")
|
| 131 |
async def root():
|
|
|
|
| 151 |
if request.use_search:
|
| 152 |
|
| 153 |
# Extract entities for focused search
|
| 154 |
+
entities = ner_pipeline(request.prompt)
|
| 155 |
logger.info(f"Identified entities: {entities}")
|
| 156 |
|
| 157 |
# Create search query from entities
|
|
|
|
| 160 |
if ent["entity_group"] in ["PER", "ORG", "LOC", "MISC"]
|
| 161 |
]
|
| 162 |
search_query = " ".join(search_terms) if search_terms else request.prompt
|
| 163 |
+
logger.info(f"Search query: {search_query}")
|
| 164 |
|
| 165 |
search_results = search_web(search_query)
|
| 166 |
+
|
|
|
|
| 167 |
search_context = "\n".join([
|
| 168 |
f"- {result['title']}: {result['body'][:200]}..."
|
| 169 |
for result in search_results[:min(len(search_results), 5)]
|
|
|
|
| 172 |
else:
|
| 173 |
search_context = request.prompt
|
| 174 |
|
| 175 |
+
logger.info(f"Search context: {search_context}")
|
| 176 |
if search_context:
|
| 177 |
# Generate response
|
| 178 |
response = generate_response(
|