Spaces:
Sleeping
Sleeping
Rename api.py to fast_api.py
Browse files- api.py β fast_api.py +31 -3
api.py β fast_api.py
RENAMED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
from transformers import pipeline
|
| 3 |
import uvicorn
|
|
|
|
| 4 |
|
| 5 |
# Load trained model
|
| 6 |
model_name = "DINGOLANI/distilbert-ner-v2"
|
|
@@ -10,7 +11,7 @@ try:
|
|
| 10 |
except Exception as e:
|
| 11 |
raise RuntimeError(f"Failed to load model: {e}")
|
| 12 |
|
| 13 |
-
#
|
| 14 |
label_map = {
|
| 15 |
"LABEL_1": "B-BRAND",
|
| 16 |
"LABEL_2": "I-BRAND",
|
|
@@ -45,6 +46,8 @@ def predict(query: str):
|
|
| 45 |
for label in result:
|
| 46 |
label["score"] = float(label["score"])
|
| 47 |
|
|
|
|
|
|
|
| 48 |
structured_output = {}
|
| 49 |
prev_label = None
|
| 50 |
prev_word = None
|
|
@@ -60,7 +63,9 @@ def predict(query: str):
|
|
| 60 |
if prev_label == entity and prev_word:
|
| 61 |
structured_output[entity][-1] += word[2:]
|
| 62 |
else:
|
| 63 |
-
structured_output.setdefault(entity, []).append(word)
|
|
|
|
|
|
|
| 64 |
|
| 65 |
prev_label = entity
|
| 66 |
prev_word = word
|
|
@@ -74,5 +79,28 @@ def predict(query: str):
|
|
| 74 |
except Exception as e:
|
| 75 |
raise HTTPException(status_code=500, detail=f"Error processing request: {e}")
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
if __name__ == "__main__":
|
| 78 |
-
|
|
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
from transformers import pipeline
|
| 3 |
import uvicorn
|
| 4 |
+
import streamlit as st
|
| 5 |
|
| 6 |
# Load trained model
|
| 7 |
model_name = "DINGOLANI/distilbert-ner-v2"
|
|
|
|
| 11 |
except Exception as e:
|
| 12 |
raise RuntimeError(f"Failed to load model: {e}")
|
| 13 |
|
| 14 |
+
# Corrected label mapping based on expected training labels
|
| 15 |
label_map = {
|
| 16 |
"LABEL_1": "B-BRAND",
|
| 17 |
"LABEL_2": "I-BRAND",
|
|
|
|
| 46 |
for label in result:
|
| 47 |
label["score"] = float(label["score"])
|
| 48 |
|
| 49 |
+
print("RAW MODEL OUTPUT:", result)
|
| 50 |
+
|
| 51 |
structured_output = {}
|
| 52 |
prev_label = None
|
| 53 |
prev_word = None
|
|
|
|
| 63 |
if prev_label == entity and prev_word:
|
| 64 |
structured_output[entity][-1] += word[2:]
|
| 65 |
else:
|
| 66 |
+
structured_output.setdefault(entity, []).append(word[2:])
|
| 67 |
+
else:
|
| 68 |
+
structured_output.setdefault(entity, []).append(word)
|
| 69 |
|
| 70 |
prev_label = entity
|
| 71 |
prev_word = word
|
|
|
|
| 79 |
except Exception as e:
|
| 80 |
raise HTTPException(status_code=500, detail=f"Error processing request: {e}")
|
| 81 |
|
| 82 |
+
# π Streamlit Frontend
|
| 83 |
+
def main():
|
| 84 |
+
st.set_page_config(page_title="Luxury Fashion NER", layout="wide")
|
| 85 |
+
|
| 86 |
+
st.title("π Luxury Fashion Entity Extractor")
|
| 87 |
+
st.write("Enter a text query and extract structured entities like **Brand, Category, Gender, and Price.**")
|
| 88 |
+
|
| 89 |
+
query = st.text_input("Enter Query:", "Gucci handbags for women under $5000")
|
| 90 |
+
|
| 91 |
+
if st.button("Analyze"):
|
| 92 |
+
response = predict(query)
|
| 93 |
+
|
| 94 |
+
col1, col2 = st.columns(2)
|
| 95 |
+
|
| 96 |
+
with col1:
|
| 97 |
+
st.subheader("π Structured Output")
|
| 98 |
+
for key, value in response["structured_output"].items():
|
| 99 |
+
st.write(f"**{key}:** {', '.join(value)}")
|
| 100 |
+
|
| 101 |
+
with col2:
|
| 102 |
+
st.subheader("π Raw Model Output")
|
| 103 |
+
st.json(response["raw_output"])
|
| 104 |
+
|
| 105 |
if __name__ == "__main__":
|
| 106 |
+
main()
|