my files
Browse files
apps.py
ADDED
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import plotly.express as px
|
4 |
+
import base64
|
5 |
+
import ollama
|
6 |
+
import os
|
7 |
+
# Load the uploaded customer data file
|
8 |
+
uploaded_file = st.sidebar.file_uploader("Upload Customer Data (Excel):", type=["xlsx"])
|
9 |
+
|
10 |
+
if uploaded_file:
|
11 |
+
customer_data = pd.read_excel(uploaded_file)
|
12 |
+
st.sidebar.success("Customer data loaded successfully!")
|
13 |
+
|
14 |
+
# Display dataset overview
|
15 |
+
st.subheader("Customer Data Overview")
|
16 |
+
st.dataframe(customer_data)
|
17 |
+
|
18 |
+
# Sidebar filters
|
19 |
+
st.sidebar.header("Customer Data Filters")
|
20 |
+
region_filter = st.sidebar.multiselect("Filter by Region:", customer_data["Region"].unique(), default=customer_data["Region"].unique())
|
21 |
+
category_filter = st.sidebar.multiselect("Filter by Product Category:", customer_data["Product_Category"].unique(), default=customer_data["Product_Category"].unique())
|
22 |
+
|
23 |
+
filtered_data = customer_data[(customer_data["Region"].isin(region_filter)) &
|
24 |
+
(customer_data["Product_Category"].isin(category_filter))]
|
25 |
+
|
26 |
+
# Generate data summary
|
27 |
+
def generate_data_summary(data):
|
28 |
+
sales_summary = data.groupby("Product_Category")["Sales_Amount"].sum().to_dict()
|
29 |
+
region_summary = data.groupby("Region")["Sales_Amount"].sum().to_dict()
|
30 |
+
churn_distribution = data["Churn_Risk"].value_counts().to_dict()
|
31 |
+
satisfaction_avg = data.groupby("Region")["Satisfaction_Score"].mean().to_dict()
|
32 |
+
|
33 |
+
summary = {
|
34 |
+
"Total Sales by Product Category": sales_summary,
|
35 |
+
"Total Sales by Region": region_summary,
|
36 |
+
"Churn Risk Distribution": churn_distribution,
|
37 |
+
"Average Satisfaction Score by Region": satisfaction_avg,
|
38 |
+
"Total Number of Records": data
|
39 |
+
}
|
40 |
+
return summary
|
41 |
+
|
42 |
+
data_summary = generate_data_summary(filtered_data)
|
43 |
+
|
44 |
+
# Sales Analysis
|
45 |
+
st.subheader("Sales Analysis")
|
46 |
+
sales_by_category = filtered_data.groupby("Product_Category")["Sales_Amount"].sum().reset_index()
|
47 |
+
fig_sales_category = px.bar(sales_by_category, x="Product_Category", y="Sales_Amount", title="Sales by Product Category")
|
48 |
+
st.plotly_chart(fig_sales_category)
|
49 |
+
|
50 |
+
sales_by_region = filtered_data.groupby("Region")["Sales_Amount"].sum().reset_index()
|
51 |
+
fig_sales_region = px.bar(sales_by_region, x="Region", y="Sales_Amount", title="Sales by Region")
|
52 |
+
st.plotly_chart(fig_sales_region)
|
53 |
+
|
54 |
+
# AI-Powered Chatbot Section
|
55 |
+
st.subheader("AI-Powered Chatbot")
|
56 |
+
if "chat_history" not in st.session_state:
|
57 |
+
st.session_state["chat_history"] = []
|
58 |
+
|
59 |
+
user_question = st.text_input("Ask a question:")
|
60 |
+
|
61 |
+
# Button to process the question
|
62 |
+
if st.button("Ask AI"):
|
63 |
+
if user_question:
|
64 |
+
with st.spinner("Generating response, please wait..."):
|
65 |
+
try:
|
66 |
+
# Include data summary in the context
|
67 |
+
summary_context ="\n".join([f"{key}: {value}" for key, value in data_summary.items()])
|
68 |
+
response = ollama.chat(model="llama3.2-vision",
|
69 |
+
messages=[
|
70 |
+
{"role": "system", "content": "You are an AI assistant. Use the data context provided to answer questions."},
|
71 |
+
{"role": "user", "content": f"Data Context:\n{summary_context}"},
|
72 |
+
{"role": "user", "content": f"Question: {user_question}"}])
|
73 |
+
|
74 |
+
|
75 |
+
# Append user question and AI response to the chat history
|
76 |
+
st.session_state["chat_history"].append({
|
77 |
+
"question": user_question,
|
78 |
+
"answer": response["message"]["content"]
|
79 |
+
})
|
80 |
+
except Exception as e:
|
81 |
+
st.error(f"An error occurred: {e}")
|
82 |
+
else:
|
83 |
+
st.warning("Please enter a question before clicking the button.")
|
84 |
+
|
85 |
+
# Display chat history
|
86 |
+
st.subheader("Chat History")
|
87 |
+
for chat in st.session_state["chat_history"]:
|
88 |
+
st.markdown(f"**You:** {chat['question']}")
|
89 |
+
st.markdown(f"**vivekda05** {chat['answer']}")
|
90 |
+
|
91 |
+
|
92 |
+
|
93 |
+
|
94 |
+
|
95 |
+
script_dir = os.path.dirname(os.path.abspath(__file__))
|
96 |
+
app_path = os.path.join(script_dir, "aptemp.py")
|
97 |
+
|
98 |
+
# Write the embedded app code to app.py
|
99 |
+
with open(app_path, "w") as app_file:
|
100 |
+
app_file.write(app_path)
|
101 |
+
|
102 |
+
# Command to run the Streamlit application
|
103 |
+
command = f"streamlit run apps1.py"
|
104 |
+
|
105 |
+
# Execute the command
|
106 |
+
os.system(command)
|
apps1.py
ADDED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import plotly.express as px
|
4 |
+
import base64
|
5 |
+
import ollama
|
6 |
+
|
7 |
+
# Load the uploaded customer data file
|
8 |
+
uploaded_file = st.sidebar.file_uploader("Upload Customer Data (Excel):", type=["xlsx"])
|
9 |
+
|
10 |
+
if uploaded_file:
|
11 |
+
customer_data = pd.read_excel(uploaded_file)
|
12 |
+
st.sidebar.success("Customer data loaded successfully!")
|
13 |
+
|
14 |
+
# Display dataset overview
|
15 |
+
st.subheader("Customer Data Overview")
|
16 |
+
st.dataframe(customer_data)
|
17 |
+
|
18 |
+
# Sidebar filters
|
19 |
+
st.sidebar.header("Customer Data Filters")
|
20 |
+
region_filter = st.sidebar.multiselect("Filter by Region:", customer_data["Region"].unique(), default=customer_data["Region"].unique())
|
21 |
+
category_filter = st.sidebar.multiselect("Filter by Product Category:", customer_data["Product_Category"].unique(), default=customer_data["Product_Category"].unique())
|
22 |
+
|
23 |
+
filtered_data = customer_data[(customer_data["Region"].isin(region_filter)) &
|
24 |
+
(customer_data["Product_Category"].isin(category_filter))]
|
25 |
+
|
26 |
+
# Generate data summary
|
27 |
+
def generate_data_summary(data):
|
28 |
+
sales_summary = data.groupby("Product_Category")["Sales_Amount"].sum().to_dict()
|
29 |
+
region_summary = data.groupby("Region")["Sales_Amount"].sum().to_dict()
|
30 |
+
churn_distribution = data["Churn_Risk"].value_counts().to_dict()
|
31 |
+
satisfaction_avg = data.groupby("Region")["Satisfaction_Score"].mean().to_dict()
|
32 |
+
|
33 |
+
summary = {
|
34 |
+
"Total Sales by Product Category": sales_summary,
|
35 |
+
"Total Sales by Region": region_summary,
|
36 |
+
"Churn Risk Distribution": churn_distribution,
|
37 |
+
"Average Satisfaction Score by Region": satisfaction_avg,
|
38 |
+
"Total Number of Records": data
|
39 |
+
}
|
40 |
+
return summary
|
41 |
+
|
42 |
+
data_summary = generate_data_summary(filtered_data)
|
43 |
+
|
44 |
+
# Sales Analysis
|
45 |
+
st.subheader("Sales Analysis")
|
46 |
+
sales_by_category = filtered_data.groupby("Product_Category")["Sales_Amount"].sum().reset_index()
|
47 |
+
fig_sales_category = px.bar(sales_by_category, x="Product_Category", y="Sales_Amount", title="Sales by Product Category")
|
48 |
+
st.plotly_chart(fig_sales_category)
|
49 |
+
|
50 |
+
sales_by_region = filtered_data.groupby("Region")["Sales_Amount"].sum().reset_index()
|
51 |
+
fig_sales_region = px.bar(sales_by_region, x="Region", y="Sales_Amount", title="Sales by Region")
|
52 |
+
st.plotly_chart(fig_sales_region)
|
53 |
+
|
54 |
+
# AI-Powered Chatbot Section
|
55 |
+
st.subheader("AI-Powered Chatbot")
|
56 |
+
if "chat_history" not in st.session_state:
|
57 |
+
st.session_state["chat_history"] = []
|
58 |
+
|
59 |
+
user_question = st.text_input("Ask a question:")
|
60 |
+
|
61 |
+
# Button to process the question
|
62 |
+
if st.button("Ask AI"):
|
63 |
+
if user_question:
|
64 |
+
with st.spinner("Generating response, please wait..."):
|
65 |
+
try:
|
66 |
+
# Include data summary in the context
|
67 |
+
summary_context ="\n".join([f"{key}: {value}" for key, value in data_summary.items()])
|
68 |
+
response = ollama.chat(model="llama3.2-vision",
|
69 |
+
messages=[
|
70 |
+
{"role": "system", "content": "You are an AI assistant. Use the data context provided to answer questions."},
|
71 |
+
{"role": "user", "content": f"Data Context:\n{summary_context}"},
|
72 |
+
{"role": "user", "content": f"Question: {user_question}"}])
|
73 |
+
|
74 |
+
|
75 |
+
# Append user question and AI response to the chat history
|
76 |
+
st.session_state["chat_history"].append({
|
77 |
+
"question": user_question,
|
78 |
+
"answer": response["message"]["content"]
|
79 |
+
})
|
80 |
+
except Exception as e:
|
81 |
+
st.error(f"An error occurred: {e}")
|
82 |
+
else:
|
83 |
+
st.warning("Please enter a question before clicking the button.")
|
84 |
+
|
85 |
+
# Display chat history
|
86 |
+
st.subheader("Chat History")
|
87 |
+
for chat in st.session_state["chat_history"]:
|
88 |
+
st.markdown(f"**You:** {chat['question']}")
|
89 |
+
st.markdown(f"**vivekda05** {chat['answer']}")
|
aptemp.py
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
C:\Users\Vivek\PycharmProjects\PythonProject1\aptemp.py
|
requirements.txt
ADDED
Binary file (1.97 kB). View file
|
|