SyedHutter commited on
Commit
212a6ed
·
verified ·
1 Parent(s): e60f32c

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -124
app.py DELETED
@@ -1,124 +0,0 @@
1
- from fastapi import FastAPI, HTTPException
2
- from pydantic import BaseModel
3
- from typing import List
4
- from transformers import pipeline
5
- import torch
6
-
7
- # Initialize the FastAPI app
8
- app = FastAPI()
9
-
10
- # Determine device (use GPU if available, otherwise CPU)
11
- device = 0 if torch.cuda.is_available() else -1
12
-
13
- # Initialize the NER pipeline
14
- ner_pipeline = pipeline(
15
- "ner",
16
- model="dbmdz/bert-large-cased-finetuned-conll03-english",
17
- aggregation_strategy="simple", # Updated to replace deprecated grouped_entities
18
- device=device
19
- )
20
-
21
- # Initialize the QA pipeline
22
- qa_pipeline = pipeline(
23
- "question-answering",
24
- model="deepset/roberta-base-squad2",
25
- device=device
26
- )
27
-
28
- # Allowed domains for filtering
29
- allowed_domains = [
30
- "clothing", "fashion", "shopping", "accessories", "sustainability", "shoes", "hats", "shirts",
31
- "dresses", "pants", "jeans", "skirts", "jackets", "coats", "t-shirts", "sweaters", "hoodies",
32
- "activewear", "formal wear", "casual wear", "sportswear", "outerwear", "swimwear", "underwear",
33
- "lingerie", "socks", "scarves", "gloves", "belts", "ties", "caps", "beanies", "boots", "sandals",
34
- "heels", "sneakers", "materials", "cotton", "polyester", "wool", "silk", "leather", "denim",
35
- "linen", "athleisure", "ethnic wear", "fashion trends", "custom clothing", "tailoring",
36
- "sustainable materials", "recycled clothing", "fashion brands", "streetwear",
37
- "footwear", "handbags", "jewelry", "watches", "eyewear", "cosmetics", "beauty products",
38
- "personal care", "fragrances", "home decor", "lifestyle", "luxury goods", "vintage clothing",
39
- "second-hand clothing", "upcycled fashion", "ethical fashion", "eco-friendly products",
40
- "fashion technology", "textile innovation", "fashion marketing", "fashion retail"
41
- ]
42
-
43
- # Pydantic models for structured response
44
- class Entity(BaseModel):
45
- word: str
46
- entity_group: str
47
- score: float
48
-
49
- class NERResponse(BaseModel):
50
- entities: List[Entity]
51
-
52
- class QAResponse(BaseModel):
53
- question: str
54
- answer: str
55
- score: float
56
-
57
- class CombinedRequest(BaseModel):
58
- text: str # The input text prompt
59
-
60
- class CombinedResponse(BaseModel):
61
- ner: NERResponse # NER output
62
- qa: QAResponse # QA output
63
-
64
- # Function to check if the input text belongs to allowed domains
65
- def is_text_in_allowed_domain(text: str, domains: List[str]) -> bool:
66
- for domain in domains:
67
- if domain in text.lower():
68
- return True
69
- return False
70
-
71
- # Combined endpoint for NER and QA with domain filtering
72
- @app.post("/process/", response_model=CombinedResponse)
73
- async def process_request(request: CombinedRequest):
74
- """
75
- Process the input text for both NER and QA, returning both responses,
76
- only if the text matches the allowed domains.
77
- """
78
- context_msg = "Hutter Products GmbH provides a wide array of services to help businesses create high-quality, sustainable products. Their offerings include comprehensive product design, ensuring items are both visually appealing and functional, and product consulting, which provides expert advice on features, materials, and design elements. They also offer sustainability consulting to integrate eco-friendly practices, such as using recycled materials and Ocean Bound Plastic. Additionally, they manage customized production to ensure products meet the highest standards and offer product animation services, creating realistic rendered images and animations to enhance online engagement. These services collectively enable businesses to develop products that are sustainable, market-responsive, and aligned with their brand identity."
79
- input_text = request.text
80
-
81
- # Check if the input text belongs to the allowed domains
82
- if not is_text_in_allowed_domain(input_text, allowed_domains):
83
- raise HTTPException(
84
- status_code=400,
85
- detail=(
86
- "The input text does not match the allowed domains. "
87
- "Please provide a query related to clothing, fashion, or accessories."
88
- )
89
- )
90
-
91
- # Perform Named Entity Recognition (NER)
92
- ner_entities = ner_pipeline(input_text)
93
-
94
- # Process the NER entities into the required format
95
- formatted_entities = [
96
- {
97
- "word": entity["word"],
98
- "entity_group": entity["entity_group"],
99
- "score": float(entity["score"]), # Convert numpy.float32 to Python float
100
- }
101
- for entity in ner_entities
102
- ]
103
- ner_response = {"entities": formatted_entities}
104
-
105
- # Perform Question Answering (QA)
106
- qa_result = qa_pipeline(question=input_text, context=context_msg)
107
- qa_result["score"] = float(qa_result["score"]) # Convert numpy.float32 to Python float
108
-
109
- qa_response = {
110
- "question": input_text,
111
- "answer": qa_result["answer"],
112
- "score": qa_result["score"]
113
- }
114
-
115
- # Return both NER and QA responses
116
- return {"ner": ner_response, "qa": qa_response}
117
-
118
- # Root endpoint
119
- @app.get("/")
120
- async def root():
121
- """
122
- Root endpoint to confirm the server is running.
123
- """
124
- return {"message": "Welcome to the filtered NER and QA API!"}