Ashraf-CK commited on
Commit
71a8c88
·
verified ·
1 Parent(s): 60ca9f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -60
app.py CHANGED
@@ -12,47 +12,37 @@ app = FastAPI()
12
 
13
  @app.get("/")
14
  def root():
15
- return {"message": "Welcome to the Product Search API!"}
16
- def encode_image_to_base64(image):
17
- """
18
- Converts a PIL Image or an image-like object to a Base64-encoded string.
19
- """
20
- if isinstance(image, Image.Image):
21
- buffer = BytesIO()
22
- image.save(buffer, format="PNG")
23
- return base64.b64encode(buffer.getvalue()).decode("utf-8")
24
- return None
25
- # Initialize FastAPI
26
-
27
  # Load Dataset
28
- dataset = load_dataset("ashraq/fashion-product-images-small", split="train")
29
 
30
  # Define fields for embedding
31
  fields_for_embedding = [
32
- "productDisplayName",
33
- "usage",
34
- "season",
35
- "baseColour",
36
- "articleType",
37
- "subCategory",
38
- "masterCategory",
39
- "gender",
 
40
  ]
41
 
42
  # Prepare Data
43
  data = []
44
  for item in dataset:
45
  data.append({
46
- "productDisplayName": item["productDisplayName"],
47
- "usage": item["usage"],
48
- "season": item["season"],
49
- "baseColour": item["baseColour"],
50
- "articleType": item["articleType"],
51
- "subCategory": item["subCategory"],
52
- "masterCategory": item["masterCategory"],
53
- "gender": item["gender"],
54
- "year": item["year"],
55
- "image": item["image"],
56
  })
57
 
58
  # Load Sentence Transformer Model
@@ -60,29 +50,29 @@ model = SentenceTransformer("sentence-transformers/multi-qa-MiniLM-L6-cos-v1")
60
 
61
  # Generate Embeddings
62
  def create_combined_text(item):
63
- return " ".join([str(item[field]) for field in fields_for_embedding if item[field]])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
  texts = [create_combined_text(item) for item in data]
66
  embeddings = model.encode(texts, convert_to_tensor=True)
67
 
68
- # Response Model
69
- class ProductResponse(BaseModel):
70
- productDisplayName: str
71
- usage: str
72
- season: str
73
- baseColour: str
74
- articleType: str
75
- subCategory: str
76
- masterCategory: str
77
- gender: str
78
- year: int
79
- image: str # Base64 encoded string
80
-
81
-
82
 
83
- @app.get("/products")
84
  def search_products(
85
- query: str = Query("", title="Search Query", description="Search term for products"),
86
  page: int = Query(1, ge=1, title="Page Number"),
87
  items_per_page: int = Query(10, ge=1, le=100, title="Items Per Page"),
88
  ):
@@ -105,18 +95,7 @@ def search_products(
105
  results = []
106
  for idx in paginated_indices:
107
  item = data[idx]
108
- results.append({
109
- "productDisplayName": item["productDisplayName"],
110
- "usage": item["usage"],
111
- "season": item["season"],
112
- "baseColour": item["baseColour"],
113
- "articleType": item["articleType"],
114
- "subCategory": item["subCategory"],
115
- "masterCategory": item["masterCategory"],
116
- "gender": item["gender"],
117
- "year": item["year"],
118
- "image": encode_image_to_base64(item["image"]),
119
- })
120
 
121
  # Construct the API response
122
  return {
 
12
 
13
  @app.get("/")
14
  def root():
15
+ return {"message": "Welcome to the medicine Search API!"}
 
 
 
 
 
 
 
 
 
 
 
16
  # Load Dataset
17
+ dataset = load_dataset("MohamedAshraf701/medicine-dataset", split="train")
18
 
19
  # Define fields for embedding
20
  fields_for_embedding = [
21
+ "product_name",
22
+ "sub_category",
23
+ "salt_composition",
24
+ "product_manufactured",
25
+ "medicine_desc",
26
+ "side_effects",
27
+ "drug",
28
+ "brand",
29
+ "effect"
30
  ]
31
 
32
  # Prepare Data
33
  data = []
34
  for item in dataset:
35
  data.append({
36
+ "product_name": item["product_name"],
37
+ "sub_category": item["sub_category"],
38
+ "salt_composition": item["salt_composition"],
39
+ "product_price": item["product_price"],
40
+ "product_manufactured": item["product_manufactured"],
41
+ "medicine_desc": item["medicine_desc"],
42
+ "side_effects": item["side_effects"],
43
+ "drug": item["drug"],
44
+ "brand": item["brand"],
45
+ "effect": item["effect"],
46
  })
47
 
48
  # Load Sentence Transformer Model
 
50
 
51
  # Generate Embeddings
52
  def create_combined_text(item):
53
+ """
54
+ Combines fields from an item into a single string for embedding,
55
+ converting arrays to comma-separated strings where necessary.
56
+ """
57
+ combined_text = []
58
+ for field in fields_for_embedding:
59
+ value = item.get(field)
60
+ if value:
61
+ # If the field is a list, join its elements into a single string
62
+ if isinstance(value, list):
63
+ combined_text.append(", ".join(map(str, value)))
64
+ else:
65
+ combined_text.append(str(value))
66
+ return " ".join(combined_text)
67
+
68
 
69
  texts = [create_combined_text(item) for item in data]
70
  embeddings = model.encode(texts, convert_to_tensor=True)
71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
+ @app.get("/meds")
74
  def search_products(
75
+ query: str = Query("", title="Search Query", description="Search term for medicine"),
76
  page: int = Query(1, ge=1, title="Page Number"),
77
  items_per_page: int = Query(10, ge=1, le=100, title="Items Per Page"),
78
  ):
 
95
  results = []
96
  for idx in paginated_indices:
97
  item = data[idx]
98
+ results.append(item)
 
 
 
 
 
 
 
 
 
 
 
99
 
100
  # Construct the API response
101
  return {