Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files51 unmodified lines5253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651664 unmodified lines17117217317417517617717817951 unmodified lines</div>""", unsafe_allow_html=True)# Load dataset@st.cache_datadef load_data():return pd.read_csv("construction_materials_dataset_3000.csv")df = load_data()# Filter sectionst.subheader("Filters", anchor="filters")with st.container():st.markdown('<div class="filter-container">', unsafe_allow_html=True)col1, col2, col3 = st.columns(3)with col1:product_categories = sorted(df['product_category'].dropna().unique())selected_categories = st.multiselect("Product Category", product_categories, default=product_categories)with col2:grades = sorted(df['grade'].dropna().unique())selected_grades = st.multiselect("Grade", grades, default=grades)with col3:ratings = sorted(df['ratings'].dropna().astype(str).unique())selected_ratings = st.multiselect("Ratings", ratings, default=ratings)st.markdown('</div>', unsafe_allow_html=True)# Apply filtersfiltered_df = df[(df['product_category'].isin(selected_categories)) &(df['grade'].isin(selected_grades)) &(df['ratings'].astype(str).isin(selected_ratings))]# Aggregate by supplierseller_data = filtered_df.groupby("supplier_name").agg({"bidding_amount": "sum"}).reset_index()# Top 5 sellerstop_sellers = seller_data.sort_values("bidding_amount", ascending=False).head(5)# Overview sectionst.subheader("Overview", anchor="overview")st.write("**Top 5 Suppliers by Total Bidding Amount**")fig_bar = px.bar(top_sellers,x="supplier_name",y="bidding_amount",labels={"supplier_name": "Supplier", "bidding_amount": "Bidding Amount"},title="Top 5 Suppliers",color="supplier_name")fig_bar.update_layout(showlegend=False)st.plotly_chart(fig_bar, use_container_width=True)st.write("**Bidding Distribution (Top 5)**")fig_pie = px.pie(top_sellers,names="supplier_name",values="bidding_amount",title="Bidding Amount by Supplier")st.plotly_chart(fig_pie, use_container_width=True)total_bidding = seller_data["bidding_amount"].sum()st.write(f"**Total Bidding Amount (All Suppliers):** ${total_bidding:,.2f}")# Convert complex types to simple Python typesdef convert_to_serializable(obj):if isinstance(obj, np.integer):return int(obj)elif isinstance(obj, np.floating):return float(obj)elif isinstance(obj, np.ndarray):return obj.tolist()elif isinstance(obj, (pd.Series, pd.DataFrame)):return obj.to_dict()elif isinstance(obj, dict):return {k: convert_to_serializable(v) for k, v in obj.items()}elif isinstance(obj, list):return [convert_to_serializable(i) for i in obj]return obj# LLM Sectionst.subheader("Ask Mistral About the Data", anchor="insights")user_query = st.text_input("Enter your question:", "Summarize why these are the top 5 suppliers.")# Load Mistral model from Hugging Face@st.cache_resourcedef load_mistral_pipeline():model_id = "mistralai/Mistral-7B-Instruct-v0.1"tokenizer = AutoTokenizer.from_pretrained(model_id)model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto", torch_dtype="auto")pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, max_new_tokens=512)return pipeif user_query:with st.spinner("Generating response..."):pipe = load_mistral_pipeline()# Prepare prompttop_sellers_json = json.dumps(convert_to_serializable(top_sellers), indent=2)filters_applied = {"product_category": selected_categories,"grade": selected_grades,"ratings": selected_ratings}prompt = f"""You are the helpful assistant. Based on the dataset below and filters, answer the following user question.Top 5 sellers:{top_sellers_json}4 unmodified linesQuestion:{user_query}"""response = pipe(prompt)[0]['generated_text']# Display only the assistant's answer (trim prompt if echoed)st.markdown("**Mistral LLM Response:**")st.write(response.split("Question:")[-1].strip())else:st.info("Enter a question to ask Mistral about the bidding data.")51 unmodified lines5253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701714 unmodified lines17617717817918018118218318418518651 unmodified lines</div>""", unsafe_allow_html=True)# File uploader for CSVst.subheader("Upload Dataset")uploaded_file = st.file_uploader("Choose a CSV file", type="csv")if uploaded_file is not None:# Load dataset from uploaded file@st.cache_datadef load_data(uploaded_file):return pd.read_csv(uploaded_file)df = load_data(uploaded_file)# Filter sectionst.subheader("Filters", anchor="filters")with st.container():st.markdown('<div class="filter-container">', unsafe_allow_html=True)col1, col2, col3 = st.columns(3)with col1:product_categories = sorted(df['product_category'].dropna().unique())selected_categories = st.multiselect("Product Category", product_categories, default=product_categories)with col2:grades = sorted(df['grade'].dropna().unique())selected_grades = st.multiselect("Grade", grades, default=grades)with col3:ratings = sorted(df['ratings'].dropna().astype(str).unique())selected_ratings = st.multiselect("Ratings", ratings, default=ratings)st.markdown('</div>', unsafe_allow_html=True)# Apply filtersfiltered_df = df[(df['product_category'].isin(selected_categories)) &(df['grade'].isin(selected_grades)) &(df['ratings'].astype(str).isin(selected_ratings))]# Aggregate by supplierseller_data = filtered_df.groupby("supplier_name").agg({"bidding_amount": "sum"}).reset_index()# Top 5 sellerstop_sellers = seller_data.sort_values("bidding_amount", ascending=False).head(5)# Overview sectionst.subheader("Overview", anchor="overview")st.write("**Top 5 Suppliers by Total Bidding Amount**")fig_bar = px.bar(top_sellers,x="supplier_name",y="bidding_amount",labels={"supplier_name": "Supplier", "bidding_amount": "Bidding Amount"},title="Top 5 Suppliers",color="supplier_name")fig_bar.update_layout(showlegend=False)st.plotly_chart(fig_bar, use_container_width=True)st.write("**Bidding Distribution (Top 5)**")fig_pie = px.pie(top_sellers,names="supplier_name",values="bidding_amount",title="Bidding Amount by Supplier")st.plotly_chart(fig_pie, use_container_width=True)total_bidding = seller_data["bidding_amount"].sum()st.write(f"**Total Bidding Amount (All Suppliers):** ${total_bidding:,.2f}")# Convert complex types to simple Python typesdef convert_to_serializable(obj):if isinstance(obj, np.integer):return int(obj)elif isinstance(obj, np.floating):return float(obj)elif isinstance(obj, np.ndarray):return obj.tolist()elif isinstance(obj, (pd.Series, pd.DataFrame)):return obj.to_dict()elif isinstance(obj, dict):return {k: convert_to_serializable(v) for k, v in obj.items()}elif isinstance(obj, list):return [convert_to_serializable(i) for i in obj]return obj# LLM Sectionst.subheader("Ask Mistral About the Data", anchor="insights")user_query = st.text_input("Enter your question:", "Summarize why these are the top 5 suppliers.")# Load Mistral model from Hugging Face@st.cache_resourcedef load_mistral_pipeline():model_id = "mistralai/Mistral-7B-Instruct-v0.1"tokenizer = AutoTokenizer.from_pretrained(model_id)model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto", torch_dtype="auto")pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, max_new_tokens=512)return pipeif user_query:with st.spinner("Generating response..."):pipe = load_mistral_pipeline()# Prepare prompttop_sellers_json = json.dumps(convert_to_serializable(top_sellers), indent=2)filters_applied = {"product_category": selected_categories,"grade": selected_grades,"ratings": selected_ratings}prompt = f"""You are the helpful assistant. Based on the dataset below and filters, answer the following user question.Top 5 sellers:{top_sellers_json}4 unmodified linesQuestion:{user_query}"""response = pipe(prompt)[0]['generated_text']# Display only the assistant's answer (trim prompt if echoed)st.markdown("**Mistral LLM Response:**")st.write(response.split("Question:")[-1].strip())else:st.info("Enter a question to ask Mistral about the bidding data.")else:st.warning("Please upload a CSV file to proceed.")
src/streamlit_app.py+121-114