Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files17th Dec Updated App and req
- app.py +134 -0
- requirements.txt +8 -0
app.py
ADDED
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
from pydantic import BaseModel
|
3 |
+
from typing import List, Dict, Any
|
4 |
+
from pymongo import MongoClient
|
5 |
+
from transformers import pipeline
|
6 |
+
import spacy
|
7 |
+
|
8 |
+
# FastAPI app setup
|
9 |
+
app = FastAPI()
|
10 |
+
|
11 |
+
# ==========================
|
12 |
+
# MongoDB Connection Setup
|
13 |
+
# ==========================
|
14 |
+
connection_string = "mongodb+srv://clician:[email protected]/?retryWrites=true&w=majority&appName=Hutterdev"
|
15 |
+
client = MongoClient(connection_string)
|
16 |
+
db = client["test"] # Replace with your database name
|
17 |
+
products_collection = db["products"] # Replace with your collection name
|
18 |
+
|
19 |
+
# ==========================
|
20 |
+
# Transformers Pipeline Setup
|
21 |
+
# ==========================
|
22 |
+
# Load the Question-Answering pipeline
|
23 |
+
qa_pipeline = pipeline("question-answering", model="bert-large-uncased-whole-word-masking-finetuned-squad")
|
24 |
+
|
25 |
+
# ==========================
|
26 |
+
# Static Context Message
|
27 |
+
# ==========================
|
28 |
+
context_msg = (
|
29 |
+
"Hutter Products GmbH provides a wide array of services to help businesses create high-quality, sustainable products. "
|
30 |
+
"Their offerings include comprehensive product design, ensuring items are both visually appealing and functional, and product consulting, "
|
31 |
+
"which provides expert advice on features, materials, and design elements. They also offer sustainability consulting to integrate eco-friendly practices, "
|
32 |
+
"such as using recycled materials and Ocean Bound Plastic. Additionally, they manage customized production to ensure products meet the highest standards "
|
33 |
+
"and offer product animation services, creating realistic rendered images and animations to enhance online engagement. These services collectively enable "
|
34 |
+
"businesses to develop products that are sustainable, market-responsive, and aligned with their brand identity."
|
35 |
+
)
|
36 |
+
|
37 |
+
# ==========================
|
38 |
+
# spaCy NER Setup
|
39 |
+
# ==========================
|
40 |
+
nlp = spacy.load("en_core_web_sm")
|
41 |
+
|
42 |
+
# ==========================
|
43 |
+
# Pydantic Models
|
44 |
+
# ==========================
|
45 |
+
class PromptRequest(BaseModel):
|
46 |
+
input_text: str
|
47 |
+
|
48 |
+
class CombinedResponse(BaseModel):
|
49 |
+
ner: Dict[str, Any]
|
50 |
+
qa: Dict[str, Any]
|
51 |
+
products_matched: List[Dict[str, Any]]
|
52 |
+
|
53 |
+
# ==========================
|
54 |
+
# Helper Functions
|
55 |
+
# ==========================
|
56 |
+
def extract_keywords(text: str) -> List[str]:
|
57 |
+
"""
|
58 |
+
Extract keywords (nouns and proper nouns) using spaCy.
|
59 |
+
"""
|
60 |
+
doc = nlp(text)
|
61 |
+
keywords = [token.text for token in doc if token.pos_ in ["NOUN", "PROPN"]]
|
62 |
+
return list(set(keywords))
|
63 |
+
|
64 |
+
def search_products_by_keywords(keywords: List[str]) -> List[Dict[str, Any]]:
|
65 |
+
"""
|
66 |
+
Search MongoDB for products that match any of the extracted keywords.
|
67 |
+
"""
|
68 |
+
regex_patterns = [{"name": {"$regex": keyword, "$options": "i"}} for keyword in keywords]
|
69 |
+
query = {"$or": regex_patterns}
|
70 |
+
|
71 |
+
matched_products = []
|
72 |
+
cursor = products_collection.find(query)
|
73 |
+
for product in cursor:
|
74 |
+
matched_products.append({
|
75 |
+
"id": str(product.get("_id", "")),
|
76 |
+
"name": product.get("name", ""),
|
77 |
+
"description": product.get("description", ""),
|
78 |
+
"skuNumber": product.get("skuNumber", ""),
|
79 |
+
"baseModel": product.get("baseModel", ""),
|
80 |
+
})
|
81 |
+
|
82 |
+
return matched_products
|
83 |
+
|
84 |
+
def get_combined_context(products: List[Dict]) -> str:
|
85 |
+
"""
|
86 |
+
Combine the static context with product descriptions fetched from MongoDB.
|
87 |
+
"""
|
88 |
+
product_descriptions = " ".join([p["description"] for p in products if "description" in p and p["description"]])
|
89 |
+
combined_context = f"{product_descriptions} {context_msg}"
|
90 |
+
return combined_context
|
91 |
+
|
92 |
+
# ==========================
|
93 |
+
# FastAPI Endpoints
|
94 |
+
# ==========================
|
95 |
+
@app.get("/")
|
96 |
+
async def root():
|
97 |
+
return {"message": "Welcome to the NER and QA API!"}
|
98 |
+
|
99 |
+
@app.post("/process/", response_model=CombinedResponse)
|
100 |
+
async def process_prompt(request: PromptRequest):
|
101 |
+
input_text = request.input_text
|
102 |
+
|
103 |
+
# Step 1: Extract keywords using spaCy NER
|
104 |
+
keywords = extract_keywords(input_text)
|
105 |
+
ner_response = {"extracted_keywords": keywords}
|
106 |
+
|
107 |
+
# Step 2: Search MongoDB for matching products
|
108 |
+
products = search_products_by_keywords(keywords)
|
109 |
+
|
110 |
+
# Step 3: Generate Combined Context
|
111 |
+
combined_context = get_combined_context(products)
|
112 |
+
|
113 |
+
# Step 4: Use Q&A Model
|
114 |
+
if combined_context.strip(): # Ensure the combined context is not empty
|
115 |
+
qa_input = {"question": input_text, "context": combined_context}
|
116 |
+
qa_output = qa_pipeline(qa_input)
|
117 |
+
qa_response = {
|
118 |
+
"question": input_text,
|
119 |
+
"answer": qa_output["answer"],
|
120 |
+
"score": qa_output["score"]
|
121 |
+
}
|
122 |
+
else:
|
123 |
+
qa_response = {
|
124 |
+
"question": input_text,
|
125 |
+
"answer": "No relevant context available.",
|
126 |
+
"score": 0.0
|
127 |
+
}
|
128 |
+
|
129 |
+
# Step 5: Return Combined Response
|
130 |
+
return {
|
131 |
+
"ner": ner_response,
|
132 |
+
"qa": qa_response,
|
133 |
+
"products_matched": products
|
134 |
+
}
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi==0.98.0
|
2 |
+
uvicorn[standard]==0.23.2
|
3 |
+
transformers==4.34.0
|
4 |
+
torch==2.0.1
|
5 |
+
pydantic==1.10.9
|
6 |
+
pymongo==4.9.2
|
7 |
+
spacy==3.8.3
|
8 |
+
numpy<2.0 # Compatibility with PyTorch and Transformers
|