Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +65 -30
src/streamlit_app.py
CHANGED
|
@@ -60,9 +60,29 @@ if uploaded_file is not None:
|
|
| 60 |
# Load dataset from uploaded file
|
| 61 |
@st.cache_data
|
| 62 |
def load_data(uploaded_file):
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
df = load_data(uploaded_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
# Filter section
|
| 68 |
st.subheader("Filters", anchor="filters")
|
|
@@ -79,7 +99,7 @@ if uploaded_file is not None:
|
|
| 79 |
selected_grades = st.multiselect("Grade", grades, default=grades)
|
| 80 |
|
| 81 |
with col3:
|
| 82 |
-
ratings = sorted(df['ratings'].dropna().
|
| 83 |
selected_ratings = st.multiselect("Ratings", ratings, default=ratings)
|
| 84 |
|
| 85 |
st.markdown('</div>', unsafe_allow_html=True)
|
|
@@ -88,9 +108,13 @@ if uploaded_file is not None:
|
|
| 88 |
filtered_df = df[
|
| 89 |
(df['product_category'].isin(selected_categories)) &
|
| 90 |
(df['grade'].isin(selected_grades)) &
|
| 91 |
-
(df['ratings'].
|
| 92 |
]
|
| 93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
# Aggregate by supplier
|
| 95 |
seller_data = filtered_df.groupby("supplier_name").agg({
|
| 96 |
"bidding_amount": "sum"
|
|
@@ -101,26 +125,29 @@ if uploaded_file is not None:
|
|
| 101 |
|
| 102 |
# Overview section
|
| 103 |
st.subheader("Overview", anchor="overview")
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
total_bidding = seller_data["bidding_amount"].sum()
|
| 126 |
st.write(f"**Total Bidding Amount (All Suppliers):** ${total_bidding:,.2f}")
|
|
@@ -148,15 +175,22 @@ if uploaded_file is not None:
|
|
| 148 |
# Load Mistral model from Hugging Face
|
| 149 |
@st.cache_resource
|
| 150 |
def load_mistral_pipeline():
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
if user_query:
|
| 158 |
with st.spinner("Generating response..."):
|
| 159 |
pipe = load_mistral_pipeline()
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
# Prepare prompt
|
| 162 |
top_sellers_json = json.dumps(convert_to_serializable(top_sellers), indent=2)
|
|
@@ -177,9 +211,10 @@ Question:
|
|
| 177 |
{user_query}
|
| 178 |
"""
|
| 179 |
response = pipe(prompt)[0]['generated_text']
|
| 180 |
-
#
|
|
|
|
| 181 |
st.markdown("**Mistral LLM Response:**")
|
| 182 |
-
st.write(
|
| 183 |
else:
|
| 184 |
st.info("Enter a question to ask Mistral about the bidding data.")
|
| 185 |
else:
|
|
|
|
| 60 |
# Load dataset from uploaded file
|
| 61 |
@st.cache_data
|
| 62 |
def load_data(uploaded_file):
|
| 63 |
+
try:
|
| 64 |
+
return pd.read_csv(uploaded_file)
|
| 65 |
+
except Exception as e:
|
| 66 |
+
st.error(f"Error reading CSV file: {str(e)}")
|
| 67 |
+
return None
|
| 68 |
|
| 69 |
df = load_data(uploaded_file)
|
| 70 |
+
if df is None:
|
| 71 |
+
st.stop()
|
| 72 |
+
|
| 73 |
+
# Display data preview
|
| 74 |
+
st.write("**Uploaded Data Preview**")
|
| 75 |
+
st.dataframe(df.head())
|
| 76 |
+
|
| 77 |
+
# Check for required columns
|
| 78 |
+
required_columns = ['product_category', 'grade', 'ratings', 'supplier_name', 'bidding_amount']
|
| 79 |
+
missing_columns = [col for col in required_columns if col not in df.columns]
|
| 80 |
+
if missing_columns:
|
| 81 |
+
st.error(f"Missing required columns in CSV: {', '.join(missing_columns)}")
|
| 82 |
+
st.stop()
|
| 83 |
+
|
| 84 |
+
# Convert ratings to string for consistency
|
| 85 |
+
df['ratings'] = df['ratings'].astype(str)
|
| 86 |
|
| 87 |
# Filter section
|
| 88 |
st.subheader("Filters", anchor="filters")
|
|
|
|
| 99 |
selected_grades = st.multiselect("Grade", grades, default=grades)
|
| 100 |
|
| 101 |
with col3:
|
| 102 |
+
ratings = sorted(df['ratings'].dropna().unique())
|
| 103 |
selected_ratings = st.multiselect("Ratings", ratings, default=ratings)
|
| 104 |
|
| 105 |
st.markdown('</div>', unsafe_allow_html=True)
|
|
|
|
| 108 |
filtered_df = df[
|
| 109 |
(df['product_category'].isin(selected_categories)) &
|
| 110 |
(df['grade'].isin(selected_grades)) &
|
| 111 |
+
(df['ratings'].isin(selected_ratings))
|
| 112 |
]
|
| 113 |
|
| 114 |
+
if filtered_df.empty:
|
| 115 |
+
st.warning("No data matches the selected filters. Please adjust your filter selections.")
|
| 116 |
+
st.stop()
|
| 117 |
+
|
| 118 |
# Aggregate by supplier
|
| 119 |
seller_data = filtered_df.groupby("supplier_name").agg({
|
| 120 |
"bidding_amount": "sum"
|
|
|
|
| 125 |
|
| 126 |
# Overview section
|
| 127 |
st.subheader("Overview", anchor="overview")
|
| 128 |
+
if not top_sellers.empty:
|
| 129 |
+
st.write("**Top 5 Suppliers by Total Bidding Amount**")
|
| 130 |
+
fig_bar = px.bar(
|
| 131 |
+
top_sellers,
|
| 132 |
+
x="supplier_name",
|
| 133 |
+
y="bidding_amount",
|
| 134 |
+
labels={"supplier_name": "Supplier", "bidding_amount": "Bidding Amount"},
|
| 135 |
+
title="Top 5 Suppliers",
|
| 136 |
+
color="supplier_name"
|
| 137 |
+
)
|
| 138 |
+
fig_bar.update_layout(showlegend=False)
|
| 139 |
+
st.plotly_chart(fig_bar, use_container_width=True)
|
| 140 |
+
|
| 141 |
+
st.write("**Bidding Distribution (Top 5)**")
|
| 142 |
+
fig_pie = px.pie(
|
| 143 |
+
top_sellers,
|
| 144 |
+
names="supplier_name",
|
| 145 |
+
values="bidding_amount",
|
| 146 |
+
title="Bidding Amount by Supplier"
|
| 147 |
+
)
|
| 148 |
+
st.plotly_chart(fig_pie, use_container_width=True)
|
| 149 |
+
else:
|
| 150 |
+
st.warning("No supplier data available after filtering.")
|
| 151 |
|
| 152 |
total_bidding = seller_data["bidding_amount"].sum()
|
| 153 |
st.write(f"**Total Bidding Amount (All Suppliers):** ${total_bidding:,.2f}")
|
|
|
|
| 175 |
# Load Mistral model from Hugging Face
|
| 176 |
@st.cache_resource
|
| 177 |
def load_mistral_pipeline():
|
| 178 |
+
try:
|
| 179 |
+
model_id = "mistralai/Mistral-7B-Instruct-v0.1"
|
| 180 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 181 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto", torch_dtype="auto")
|
| 182 |
+
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, max_new_tokens=512)
|
| 183 |
+
return pipe
|
| 184 |
+
except Exception as e:
|
| 185 |
+
st.error(f"Error loading Mistral model: {str(e)}")
|
| 186 |
+
return None
|
| 187 |
|
| 188 |
if user_query:
|
| 189 |
with st.spinner("Generating response..."):
|
| 190 |
pipe = load_mistral_pipeline()
|
| 191 |
+
if pipe is None:
|
| 192 |
+
st.error("Cannot generate response due to model loading failure.")
|
| 193 |
+
st.stop()
|
| 194 |
|
| 195 |
# Prepare prompt
|
| 196 |
top_sellers_json = json.dumps(convert_to_serializable(top_sellers), indent=2)
|
|
|
|
| 211 |
{user_query}
|
| 212 |
"""
|
| 213 |
response = pipe(prompt)[0]['generated_text']
|
| 214 |
+
# Extract the answer
|
| 215 |
+
answer = response[len(prompt):].strip() if response.startswith(prompt) else response.strip()
|
| 216 |
st.markdown("**Mistral LLM Response:**")
|
| 217 |
+
st.write(answer)
|
| 218 |
else:
|
| 219 |
st.info("Enter a question to ask Mistral about the bidding data.")
|
| 220 |
else:
|