Spaces:
Sleeping
Sleeping
app.py Beta 2
Browse files
app.py
CHANGED
@@ -1,148 +1,138 @@
|
|
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
|
6 |
-
import spacy
|
7 |
-
import
|
8 |
-
import
|
9 |
-
|
10 |
-
#
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
#
|
23 |
-
#
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
"
|
34 |
-
|
35 |
-
"
|
36 |
-
|
37 |
-
)
|
38 |
-
|
39 |
-
|
40 |
-
#
|
41 |
-
|
42 |
-
|
43 |
-
# spaCy
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
#
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
#
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
""
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
return
|
77 |
-
|
78 |
-
def
|
79 |
-
|
80 |
-
|
81 |
-
""
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
return
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
"answer": "No relevant context available.",
|
140 |
-
"score": 0.0
|
141 |
-
}
|
142 |
-
|
143 |
-
# Step 5: Return Combined Response
|
144 |
-
return {
|
145 |
-
"ner": ner_response,
|
146 |
-
"qa": qa_response,
|
147 |
-
"products_matched": products
|
148 |
-
}
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
+
from pydantic import BaseModel
|
3 |
+
from typing import List, Dict, Any
|
4 |
+
from pymongo import MongoClient
|
5 |
+
from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration
|
6 |
+
import spacy
|
7 |
+
import os
|
8 |
+
import logging
|
9 |
+
|
10 |
+
# Set up logging
|
11 |
+
logging.basicConfig(level=logging.INFO)
|
12 |
+
logger = logging.getLogger(__name__)
|
13 |
+
|
14 |
+
app = FastAPI()
|
15 |
+
|
16 |
+
# MongoDB Setup
|
17 |
+
connection_string = os.getenv("MONGO_URI", "mongodb+srv://clician:[email protected]/?retryWrites=true&w=majority&appName=Hutterdev")
|
18 |
+
client = MongoClient(connection_string)
|
19 |
+
db = client["test"]
|
20 |
+
products_collection = db["products"]
|
21 |
+
|
22 |
+
# BlenderBot Setup
|
23 |
+
model_name = "SyedHutter/blenderbot_model/blenderbot_model" # Points to subdirectory
|
24 |
+
model_dir = "/home/user/app/blenderbot_model"
|
25 |
+
|
26 |
+
if not os.path.exists(model_dir):
|
27 |
+
logger.info(f"Downloading {model_name} to {model_dir}...")
|
28 |
+
tokenizer = BlenderbotTokenizer.from_pretrained(model_name)
|
29 |
+
model = BlenderbotForConditionalGeneration.from_pretrained(model_name)
|
30 |
+
os.makedirs(model_dir, exist_ok=True)
|
31 |
+
tokenizer.save_pretrained(model_dir)
|
32 |
+
model.save_pretrained(model_dir)
|
33 |
+
logger.info("Model download complete.")
|
34 |
+
else:
|
35 |
+
logger.info(f"Loading pre-existing model from {model_dir}.")
|
36 |
+
|
37 |
+
tokenizer = BlenderbotTokenizer.from_pretrained(model_dir)
|
38 |
+
model = BlenderbotForConditionalGeneration.from_pretrained(model_dir)
|
39 |
+
|
40 |
+
# Static Context
|
41 |
+
context_msg = "Hutter Products GmbH provides sustainable products like shirts and shorts..."
|
42 |
+
|
43 |
+
# spaCy Setup
|
44 |
+
spacy_model_path = "/home/user/app/en_core_web_sm-3.8.0"
|
45 |
+
nlp = spacy.load(spacy_model_path)
|
46 |
+
|
47 |
+
# Pydantic Models
|
48 |
+
class PromptRequest(BaseModel):
|
49 |
+
input_text: str
|
50 |
+
conversation_history: List[str] = []
|
51 |
+
|
52 |
+
class CombinedResponse(BaseModel):
|
53 |
+
ner: Dict[str, Any]
|
54 |
+
qa: Dict[str, Any]
|
55 |
+
products_matched: List[Dict[str, Any]]
|
56 |
+
|
57 |
+
# Helper Functions
|
58 |
+
def extract_keywords(text: str) -> List[str]:
|
59 |
+
doc = nlp(text)
|
60 |
+
keywords = [token.text for token in doc if token.pos_ in ["NOUN", "PROPN"]]
|
61 |
+
return list(set(keywords))
|
62 |
+
|
63 |
+
def detect_intent(text: str) -> str:
|
64 |
+
doc = nlp(text.lower())
|
65 |
+
if any(token.text in ["shirt", "shirts"] for token in doc):
|
66 |
+
return "recommend_shirt"
|
67 |
+
elif any(token.text in ["short", "shorts"] for token in doc):
|
68 |
+
return "recommend_shorts"
|
69 |
+
elif any(token.text in ["what", "who", "company", "do", "products"] for token in doc):
|
70 |
+
return "company_info"
|
71 |
+
return "unknown"
|
72 |
+
|
73 |
+
def search_products_by_keywords(keywords: List[str]) -> List[Dict[str, Any]]:
|
74 |
+
query = {"$or": [{"name": {"$regex": keyword, "$options": "i"}} for keyword in keywords]}
|
75 |
+
matched_products = [dict(p, id=str(p["_id"])) for p in products_collection.find(query)]
|
76 |
+
return matched_products
|
77 |
+
|
78 |
+
def get_product_context(products: List[Dict]) -> str:
|
79 |
+
if not products:
|
80 |
+
return ""
|
81 |
+
product_str = "Here are some products: "
|
82 |
+
product_str += ", ".join([f"'{p['name']}' (SKU: {p['skuNumber']}) - {p['description']}" for p in products[:2]])
|
83 |
+
return product_str
|
84 |
+
|
85 |
+
def format_response(response: str, products: List[Dict], intent: str) -> str:
|
86 |
+
if intent in ["recommend_shirt", "recommend_shorts"] and products:
|
87 |
+
product = products[0]
|
88 |
+
return f"{response} For example, check out our '{product['name']}' (SKU: {product['skuNumber']})—it’s {product['description'].lower()}!"
|
89 |
+
elif intent == "company_info":
|
90 |
+
return f"{response} At Hutter Products GmbH, we specialize in sustainable product design and production!"
|
91 |
+
return response
|
92 |
+
|
93 |
+
# Endpoints
|
94 |
+
@app.get("/")
|
95 |
+
async def root():
|
96 |
+
return {"message": "Welcome to the NER and Chat API!"}
|
97 |
+
|
98 |
+
@app.post("/process/", response_model=CombinedResponse)
|
99 |
+
async def process_prompt(request: PromptRequest):
|
100 |
+
try:
|
101 |
+
input_text = request.input_text
|
102 |
+
history = request.conversation_history[-3:] if request.conversation_history else []
|
103 |
+
|
104 |
+
intent = detect_intent(input_text)
|
105 |
+
keywords = extract_keywords(input_text)
|
106 |
+
ner_response = {"extracted_keywords": keywords}
|
107 |
+
|
108 |
+
products = search_products_by_keywords(keywords)
|
109 |
+
product_context = get_product_context(products)
|
110 |
+
|
111 |
+
history_str = " || ".join(history)
|
112 |
+
full_input = f"{history_str} || {product_context} {context_msg} || {input_text}" if history else f"{product_context} {context_msg} || {input_text}"
|
113 |
+
inputs = tokenizer(full_input, return_tensors="pt", truncation=True, max_length=512)
|
114 |
+
outputs = model.generate(**inputs, max_length=150, num_beams=5, no_repeat_ngram_size=2)
|
115 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
116 |
+
|
117 |
+
enhanced_response = format_response(response, products, intent)
|
118 |
+
qa_response = {
|
119 |
+
"question": input_text,
|
120 |
+
"answer": enhanced_response,
|
121 |
+
"score": 1.0
|
122 |
+
}
|
123 |
+
|
124 |
+
return {
|
125 |
+
"ner": ner_response,
|
126 |
+
"qa": qa_response,
|
127 |
+
"products_matched": products
|
128 |
+
}
|
129 |
+
except Exception as e:
|
130 |
+
raise HTTPException(status_code=500, detail=f"Oops, something went wrong: {str(e)}. Try again!")
|
131 |
+
|
132 |
+
@app.on_event("startup")
|
133 |
+
async def startup_event():
|
134 |
+
logger.info("API is running with BlenderBot-400M-distill, connected to MongoDB.")
|
135 |
+
|
136 |
+
@app.on_event("shutdown")
|
137 |
+
def shutdown_event():
|
138 |
+
client.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|