tdeshane commited on
Commit
fa3d6a7
1 Parent(s): e9fd72e
Files changed (1) hide show
  1. app.py +160 -0
app.py ADDED
@@ -0,0 +1,160 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import chainlit as cl
2
+ import pandas as pd
3
+ import io
4
+ import matplotlib.pyplot as plt
5
+ import base64
6
+ from io import BytesIO
7
+ from pandasai import SmartDataframe
8
+ import pandas as pd
9
+ from pandasai.llm import OpenAI
10
+ from io import StringIO
11
+ import matplotlib.pyplot as plt
12
+ import csv
13
+ from collections import defaultdict
14
+
15
+
16
+ def find_most_valuable_feature(csv_file):
17
+ print("find_most_valuable_feature")
18
+ print(csv_file)
19
+
20
+ openai.api_key = os.environ["OPENAI_API_KEY"]
21
+ smart_llm = OpenAI()
22
+
23
+
24
+
25
+
26
+ # Initialize a defaultdict to store column data
27
+ columns = defaultdict(list)
28
+
29
+ # Read the CSV file and populate the defaultdict
30
+ with open("upload.csv") as f:
31
+ reader = csv.reader(f)
32
+ headers = next(reader)
33
+
34
+ for row in reader:
35
+ for header, value in zip(headers, row):
36
+ columns[header].append(value)
37
+
38
+ # Manually create a DataFrame from the defaultdict
39
+ smart_df = pd.DataFrame({
40
+ "ID": columns["ID"],
41
+ "Date and Time": columns["Date and Time"],
42
+ "Business Unit": columns["Business Unit"],
43
+ "Usage Change": columns["Usage Change"],
44
+ "Wolftech Improvement": columns["Wolftech Improvement"],
45
+ "Likelihood to Recommend": columns["Likelihood to Recommend"],
46
+ "Effective Training": columns["Effective Training"],
47
+ "Most Valuable Feature": columns["Most Valuable Feature"]
48
+ })
49
+
50
+ smart_df = SmartDataframe(smart_df, config={"llm": smart_llm})
51
+ out = smart_df.chat('Summarize the top three "Most Valuable Feature" for people where Usage Changed was Increased?')
52
+
53
+ print(out)
54
+
55
+
56
+ df = out
57
+
58
+ # Plotting
59
+ plt.figure(figsize=(10, 6))
60
+ plt.bar(df["Most Valuable Feature"], df["Count"], color='blue')
61
+ plt.xlabel('Most Valuable Feature')
62
+ plt.ylabel('Count')
63
+ plt.title('Count of Most Valuable Features')
64
+ plt.xticks(rotation=45, ha="right") # Rotate labels for better readability
65
+ plt.tight_layout() # Adjust layout for better fit
66
+
67
+ # Save the plot to a BytesIO object
68
+ image_buffer = BytesIO()
69
+ plt.savefig(image_buffer, format='png')
70
+ image_buffer.seek(0)
71
+
72
+ return image_buffer
73
+
74
+
75
+
76
+
77
+
78
+ def process_and_analyze_data(csv_file):
79
+ # Read CSV file
80
+ csv_data = pd.read_csv(csv_file)
81
+
82
+ # Logging to check data loading
83
+ print(f"CSV Data Loaded: {csv_data.head()}")
84
+
85
+ # Count of responses in each category of 'Business Unit'
86
+ business_unit_counts = csv_data['Business Unit'].value_counts()
87
+
88
+ # Plotting the count of responses in each 'Business Unit' category
89
+ plt.figure(figsize=(10, 6))
90
+ business_unit_counts.plot(kind='bar')
91
+ plt.title('Count of Responses by Business Unit')
92
+ plt.xlabel('Business Unit')
93
+ plt.ylabel('Count')
94
+ plt.xticks(rotation=45)
95
+ plt.tight_layout()
96
+
97
+ # Save the plot to a BytesIO object
98
+ image_buffer = BytesIO()
99
+ plt.savefig(image_buffer, format='png')
100
+ image_buffer.seek(0)
101
+
102
+ return image_buffer
103
+
104
+
105
+ # Function to handle message events
106
+
107
+ @cl.on_message
108
+ async def handle_message(message: cl.Message):
109
+ # Retrieve the CSV file from the message
110
+ csv_file = next(
111
+ (
112
+ io.BytesIO(file.content)
113
+ for file in message.elements or []
114
+ if file.mime and "csv" in file.mime
115
+ ),
116
+ None,
117
+ )
118
+
119
+ # Logging to check file retrieval
120
+ print(f"CSV File: {csv_file}")
121
+
122
+ if csv_file:
123
+ try:
124
+
125
+ image_buffer = find_most_valuable_feature(csv_file)
126
+
127
+ # Get bytes data from BytesIO object and send the image data
128
+ image_data = image_buffer.getvalue()
129
+ name = "chart"
130
+ cl.user_session.set(name, image_data)
131
+ cl.user_session.set("generated_image", name)
132
+
133
+ await cl.Message(content="Based on the people who increased usage, here are the most valuable features").send()
134
+
135
+ generated_image = cl.user_session.get(name)
136
+
137
+ elements = []
138
+ actions = []
139
+
140
+ elements = [
141
+ cl.Image(
142
+ content=generated_image,
143
+ name=name,
144
+ display="inline",
145
+ size="large"
146
+ )
147
+ ]
148
+
149
+ await cl.Message(content=name, elements=elements, actions=actions).send()
150
+
151
+
152
+ except Exception as e:
153
+ await cl.Message(content=f"An error occurred: {str(e)}").send()
154
+ else:
155
+ await cl.Message(content="Please upload a CSV file.").send()
156
+
157
+
158
+ # Run the ChainLit app
159
+ if __name__ == "__main__":
160
+ cl.run()