Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -12,47 +12,37 @@ app = FastAPI()
|
|
12 |
|
13 |
@app.get("/")
|
14 |
def root():
|
15 |
-
return {"message": "Welcome to the
|
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("
|
29 |
|
30 |
# Define fields for embedding
|
31 |
fields_for_embedding = [
|
32 |
-
"
|
33 |
-
"
|
34 |
-
"
|
35 |
-
"
|
36 |
-
"
|
37 |
-
"
|
38 |
-
"
|
39 |
-
"
|
|
|
40 |
]
|
41 |
|
42 |
# Prepare Data
|
43 |
data = []
|
44 |
for item in dataset:
|
45 |
data.append({
|
46 |
-
"
|
47 |
-
"
|
48 |
-
"
|
49 |
-
"
|
50 |
-
"
|
51 |
-
"
|
52 |
-
"
|
53 |
-
"
|
54 |
-
"
|
55 |
-
"
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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("/
|
84 |
def search_products(
|
85 |
-
query: str = Query("", title="Search Query", description="Search term for
|
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 {
|