Skip to content

Commit 6f6a04a

Browse files
committed
feat: Resolving pytest issues
1 parent 6a95fd5 commit 6f6a04a

File tree

1 file changed

+129
-115
lines changed

1 file changed

+129
-115
lines changed

src/app/frontend.py

Lines changed: 129 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -3,131 +3,145 @@
33
import requests
44
from streamlit_chat import message # pip install streamlit-chat
55

6-
# ---------------------------
7-
# Page Config
8-
# ---------------------------
9-
st.set_page_config(page_title="Daraz Insight Copilot", layout="wide")
106

11-
# ---------------------------
12-
# Custom CSS for Purple + Orange Theme
13-
# ---------------------------
14-
st.markdown(
15-
"""
16-
<style>
17-
body {
18-
background: linear-gradient(to right, #5D3FD3, #FF7F50);
19-
color: #fff;
20-
}
21-
.stButton>button {
22-
background: linear-gradient(to right, #FF7F50, #5D3FD3);
23-
color: #fff;
24-
border-radius: 10px;
25-
height: 40px;
26-
width: 100%;
27-
}
28-
.stTextInput>div>input {
29-
border-radius: 8px;
30-
padding: 10px;
31-
}
32-
h1, h2, h3 {
33-
text-align: center;
34-
color: #fff;
35-
}
36-
.chat-bubble {
37-
background: linear-gradient(to right, #5D3FD3, #FF7F50);
38-
padding: 12px;
39-
border-radius: 12px;
40-
margin-bottom: 8px;
41-
max-width: 70%;
42-
}
43-
</style>
44-
""",
45-
unsafe_allow_html=True,
46-
)
7+
def main():
8+
# ---------------------------
9+
# Page Config
10+
# ---------------------------
11+
st.set_page_config(page_title="Daraz Insight Copilot", layout="wide")
4712

48-
# ---------------------------
49-
# Sidebar Navigation
50-
# ---------------------------
51-
page = st.sidebar.radio("Navigation", ["Prediction", "Chatbot"])
13+
# ---------------------------
14+
# Custom CSS for Purple + Orange Theme
15+
# ---------------------------
16+
st.markdown(
17+
"""
18+
<style>
19+
body {
20+
background: linear-gradient(to right, #5D3FD3, #FF7F50);
21+
color: #fff;
22+
}
23+
.stButton>button {
24+
background: linear-gradient(to right, #FF7F50, #5D3FD3);
25+
color: #fff;
26+
border-radius: 10px;
27+
height: 40px;
28+
width: 100%;
29+
}
30+
.stTextInput>div>input {
31+
border-radius: 8px;
32+
padding: 10px;
33+
}
34+
h1, h2, h3 {
35+
text-align: center;
36+
color: #fff;
37+
}
38+
.chat-bubble {
39+
background: linear-gradient(to right, #5D3FD3, #FF7F50);
40+
padding: 12px;
41+
border-radius: 12px;
42+
margin-bottom: 8px;
43+
max-width: 70%;
44+
}
45+
</style>
46+
""",
47+
unsafe_allow_html=True,
48+
)
5249

53-
# ---------------------------
54-
# Prediction Page
55-
# ---------------------------
56-
if page == "Prediction":
57-
st.title("💡 Product Success Predictor")
50+
# ---------------------------
51+
# Sidebar Navigation
52+
# ---------------------------
53+
page = st.sidebar.radio("Navigation", ["Prediction", "Chatbot"])
5854

59-
with st.form("prediction_form"):
60-
Original_Price = st.number_input("Original Price", value=1650)
61-
Discount_Price = st.number_input("Discount Price", value=725)
62-
Number_of_Ratings = st.number_input("Number of Ratings", value=31)
63-
Positive_Seller_Ratings = st.number_input("Positive Seller Ratings", value=86)
64-
Ship_On_Time = st.number_input("Ship On Time", value=0)
65-
Chat_Response_Rate = st.number_input("Chat Response Rate", value=93)
66-
No_of_products_to_be_sold = st.number_input(
67-
"No. of products to be sold", value=113.79
68-
)
69-
Category = st.text_input("Category", value="Watches, Bags, Jewellery")
70-
Delivery_Type = st.text_input("Delivery Type", value="Free Delivery")
71-
Flagship_Store = st.text_input("Flagship Store", value="No")
55+
# ---------------------------
56+
# Prediction Page
57+
# ---------------------------
58+
if page == "Prediction":
59+
st.title("Product Success Predictor")
7260

73-
submitted = st.form_submit_button("Predict Success Score")
61+
with st.form("prediction_form"):
62+
Original_Price = st.number_input("Original Price", value=1650)
63+
Discount_Price = st.number_input("Discount Price", value=725)
64+
Number_of_Ratings = st.number_input("Number of Ratings", value=31)
65+
Positive_Seller_Ratings = st.number_input(
66+
"Positive Seller Ratings", value=86
67+
)
68+
Ship_On_Time = st.number_input("Ship On Time", value=0)
69+
Chat_Response_Rate = st.number_input("Chat Response Rate", value=93)
70+
No_of_products_to_be_sold = st.number_input(
71+
"No. of products to be sold", value=113.79
72+
)
73+
Category = st.text_input("Category", value="Watches, Bags, Jewellery")
74+
Delivery_Type = st.text_input("Delivery Type", value="Free Delivery")
75+
Flagship_Store = st.text_input("Flagship Store", value="No")
7476

75-
if submitted:
76-
payload = {
77-
"Original_Price": Original_Price,
78-
"Discount_Price": Discount_Price,
79-
"Number_of_Ratings": Number_of_Ratings,
80-
"Positive_Seller_Ratings": Positive_Seller_Ratings,
81-
"Ship_On_Time": Ship_On_Time,
82-
"Chat_Response_Rate": Chat_Response_Rate,
83-
"No_of_products_to_be_sold": No_of_products_to_be_sold,
84-
"Category": Category,
85-
"Delivery_Type": Delivery_Type,
86-
"Flagship_Store": Flagship_Store,
87-
}
88-
try:
89-
response = requests.post("http://localhost:8000/predict", json=payload)
90-
if response.status_code == 200:
91-
st.success(
92-
f"Predicted Success Score: {response.json()['predicted_success_score']:.2f}"
77+
submitted = st.form_submit_button("Predict Success Score")
78+
79+
if submitted:
80+
payload = {
81+
"Original_Price": Original_Price,
82+
"Discount_Price": Discount_Price,
83+
"Number_of_Ratings": Number_of_Ratings,
84+
"Positive_Seller_Ratings": Positive_Seller_Ratings,
85+
"Ship_On_Time": Ship_On_Time,
86+
"Chat_Response_Rate": Chat_Response_Rate,
87+
"No_of_products_to_be_sold": No_of_products_to_be_sold,
88+
"Category": Category,
89+
"Delivery_Type": Delivery_Type,
90+
"Flagship_Store": Flagship_Store,
91+
}
92+
try:
93+
response = requests.post(
94+
"http://localhost:8000/predict", json=payload
9395
)
94-
else:
95-
st.error(f"Error: {response.json()['detail']}")
96-
except Exception as e:
97-
st.error(f"API Error: {e}")
96+
if response.status_code == 200:
97+
st.success(
98+
f"Predicted Success Score: {response.json()['predicted_success_score']:.2f}"
99+
)
100+
else:
101+
st.error(f"Error: {response.json()['detail']}")
102+
except Exception as e:
103+
st.error(f"API Error: {e}")
98104

99-
# ---------------------------
100-
# Chatbot Page
101-
# ---------------------------
102-
elif page == "Chatbot":
103-
st.title("🤖 Daraz RAG Chatbot")
104-
if "messages" not in st.session_state:
105-
st.session_state.messages = []
105+
# ---------------------------
106+
# Chatbot Page
107+
# ---------------------------
108+
elif page == "Chatbot":
109+
st.title("Daraz RAG Chatbot")
110+
if "messages" not in st.session_state:
111+
st.session_state.messages = []
106112

107-
with st.form("chat_form", clear_on_submit=True):
108-
user_input = st.text_input("Ask a question about product reviews:")
109-
submitted = st.form_submit_button("Send")
110-
if submitted and user_input:
111-
st.session_state.messages.append({"role": "user", "content": user_input})
112-
try:
113-
response = requests.post(
114-
"http://localhost:8000/ask", json={"question": user_input}
113+
with st.form("chat_form", clear_on_submit=True):
114+
user_input = st.text_input("Ask a question about product reviews:")
115+
submitted = st.form_submit_button("Send")
116+
if submitted and user_input:
117+
st.session_state.messages.append(
118+
{"role": "user", "content": user_input}
115119
)
116-
if response.status_code == 200:
117-
answer = response.json()["answer"]
118-
st.session_state.messages.append({"role": "bot", "content": answer})
119-
else:
120+
try:
121+
response = requests.post(
122+
"http://localhost:8000/ask", json={"question": user_input}
123+
)
124+
if response.status_code == 200:
125+
answer = response.json()["answer"]
126+
st.session_state.messages.append(
127+
{"role": "bot", "content": answer}
128+
)
129+
else:
130+
st.session_state.messages.append(
131+
{"role": "bot", "content": "Error fetching answer."}
132+
)
133+
except Exception as e:
120134
st.session_state.messages.append(
121-
{"role": "bot", "content": "Error fetching answer."}
135+
{"role": "bot", "content": f"API Error: {e}"}
122136
)
123-
except Exception as e:
124-
st.session_state.messages.append(
125-
{"role": "bot", "content": f"API Error: {e}"}
126-
)
127137

128-
# Display messages
129-
for msg in st.session_state.messages:
130-
if msg["role"] == "user":
131-
message(msg["content"], is_user=True, key=msg["content"] + "_user")
132-
else:
133-
message(msg["content"], key=msg["content"] + "_bot")
138+
# Display messages
139+
for msg in st.session_state.messages:
140+
if msg["role"] == "user":
141+
message(msg["content"], is_user=True, key=msg["content"] + "_user")
142+
else:
143+
message(msg["content"], key=msg["content"] + "_bot")
144+
145+
146+
if __name__ == "__main__":
147+
main()

0 commit comments

Comments
 (0)