-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
469 lines (385 loc) · 15.7 KB
/
Copy pathapp.py
File metadata and controls
469 lines (385 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
import ast
from langchain.llms import OpenAI
import pandas as pd
import os
import json
from langchain.agents import create_csv_agent
from pydantic import BaseModel, Field
from langchain.llms import OpenAI
import streamlit as st
from streamlit import session_state
import pandas as pd
from langchain.document_loaders import CSVLoader
from langchain.indexes import VectorstoreIndexCreator
from langchain.document_loaders import DataFrameLoader
from langchain.chains import RetrievalQA
from langchain.chat_models.openai import ChatOpenAI
import emoji
import chromadb
import ast
import random
import regex as re
from tempfile import NamedTemporaryFile
def apply_custom_css():
st.markdown(
"""
<style>
.hacker-text {
background-color: black;
color: lime;
font-family: 'Courier New', monospace;
padding: 10px;
border-radius: 5px;
white-space: pre-wrap;
}
</style>
""",
unsafe_allow_html=True,
)
def apply_custom_css2():
st.markdown(
"""
<style>
.slack-container {
display: flex;
flex-direction: column;
gap: 10px;
padding: 10px;
background-color: #f8f8f8;
font-family: 'Lato', sans-serif;
border-radius: 5px;
}
.slack-question {
font-weight: bold;
color: #2c2d30;
}
.slack-answer {
background-color: #e4f0f5;
color: #2c2d30;
padding: 10px;
border-radius: 5px;
}
</style>
""",
unsafe_allow_html=True,
)
def file_selector(folder_path="."):
filenames = os.listdir(folder_path)
selected_filename = st.selectbox("Select a file", filenames)
return os.path.join(folder_path, selected_filename)
def CVStoVectorStoreIndex(path):
csv_args = {"delimiter": ",", "quotechar": '"'}
loader = CSVLoader(file_path=path, csv_args=csv_args)
# loader = DataFrameLoader(df,page_content_column=df.columns[0])
index_creator = VectorstoreIndexCreator()
docsearch = index_creator.from_loaders([loader])
return docsearch
def RefineChain(vector):
summuray_chain = RetrievalQA.from_chain_type(
llm=OpenAI(),
chain_type="stuff",
retriever=vector.vectorstore.as_retriever(),
input_key="question",
)
sum_query = f""" You are a data analyst and I am giving a file, please tell me the characteristicts of the file, including but not limitied to:
columns, data types, the subject or domain of the data within the file ie an industry (healthcare, finance,marketing)
Please output a paragraph summurizing you findings
"""
response = summuray_chain({"question": sum_query})
return response
def GetGeneratedQuestions(
vector,
role,
industry,
customer_role,
assignment_type,
chain_response,
problem_statement,
):
chain = RetrievalQA.from_chain_type(
llm=OpenAI(),
chain_type="stuff",
retriever=vector.vectorstore.as_retriever(),
input_key="question",
)
query = f""" You are top performing
{role} working in an analytics department for a {customer_role}
within a {industry} company.
Your assignment type = {assignment_type}, and your next action is solve for {problem_statement}
HERE IS context for you about the underlying data: {chain_response}
You MUST generate 10 Top Questions that are answerable ONLY THOUGH python CALCULATIONS AND AGGREGATIONS:
REMEMBER FOCUS ON THE ENTIRE DATA SET not singular rows
ALWAYS RETURN A PYTHON ARRAY LIKE THIS:
["Question 1", "Question 2", "Question 3", "Question 4", "Question 5"]
YOUR RESPONSE:
"""
response = chain({"question": query})
return response
def add_randomness(score):
# Introduce randomness within the range of +/-
randomness = random.uniform(-1, 4)
return max(0, min(2, score + randomness))
def evaluate_end_of_life_disposal(solution_statement, openai_api_key):
# Example implementation using OpenAI
openai_response = OpenAI(api_key=openai_api_key).generate(prompts=[
f"Evaluate the end-of-life disposal in the following solution: {solution_statement}"])
# Extract a relevance score from the OpenAI response (replace this with actual extraction logic)
relevance_score = 0.95 # Replace with the actual score extraction logic
# Assuming higher relevance indicates a better solution
return relevance_score
def evaluate_market_potential(solution_statement, openai_api_key):
openai_response = OpenAI(api_key=openai_api_key).generate(prompts=[
f"Evaluate the market potential in the following solution: {solution_statement}"])
relevance_score = 0.95 # Replace with the actual score extraction logic
return relevance_score
def evaluate_dangerous_operation(solution_statement, openai_api_key):
openai_response = OpenAI(api_key=openai_api_key).generate(prompts=[
f"Evaluate the potential dangers in the following solution: {solution_statement}"])
relevance_score = 0.95 # Replace with the actual score extraction logic
return relevance_score
def evaluate_innovativeness(solution_statement, openai_api_key):
openai_response = OpenAI(api_key=openai_api_key).generate(prompts=[
f"Evaluate the innovativeness in the following solution: {solution_statement}"])
relevance_score = 0.95 # Replace with the actual score extraction logic
return relevance_score
def evaluate_solution(problem_statement, solution_statement, parameters):
openai_api_key = os.environ.get("OPENAI_API_KEY")
openai_instance = OpenAI(api_key=openai_api_key)
# Idea Filters (subtracts 4 points from total)
idea_filters = {
"Evaluate end-of-life disposal": evaluate_end_of_life_disposal(solution_statement, openai_api_key),
# Implement other idea filters similarly
}
# Subtract points for each failed idea filter
idea_filter_score = max(0, 4 - sum(idea_filters.values()))
# Idea Validators (worth 7 points)
idea_validators = {
"Market Potential": add_randomness(evaluate_market_potential(solution_statement, openai_api_key=openai_api_key)),
# Implement other idea validators similarly
}
# Sum the scores from idea validators
idea_validator_score = sum(idea_validators.values())
# Human Factors (worth 2 points)
human_factors = {
"Is it dangerous, this new operation?": add_randomness(evaluate_dangerous_operation(solution_statement, openai_api_key=openai_api_key)),
# Implement other human factors similarly
}
# Sum the scores from human factors
human_factor_score = sum(human_factors.values())
# Moonshot Factors (worth 1 point)
moonshot_factors = {
"How innovative is this solution?": add_randomness(evaluate_innovativeness(solution_statement, openai_api_key=openai_api_key)),
# Implement other moonshot factors similarly
}
# Sum the scores from moonshot factors
moonshot_factor_score = sum(moonshot_factors.values())
# Calculate overall ranking by summing all scores and rounding to one digit after the decimal point
overall_ranking = round(idea_filter_score + idea_validator_score +
human_factor_score + moonshot_factor_score, 1)
return overall_ranking
def main():
# Initialize session state if not already initialized
if not session_state.get("init"):
session_state.init = True
session_state.role = "📊 Data Analyst"
session_state.assignment_type = "🔍 Exploratory Data Analysis"
# Set page configuration
st.set_page_config(
page_title="EcoRankAI ♻️",
page_icon=":bar_chart:",
layout="wide",
initial_sidebar_state="expanded",
)
# Header
st.title("EcoRankAI ♻️")
st.sidebar.header("Select Parameters:")
parameters = st.sidebar.multiselect(
"Choose parameters for evaluation",
["Feasibility", "Technological Innovation", "Circular Economy", "Ambition",
"Relevance", "Security", "Market Maturity", "Market Potential"],
default=["Feasibility", "Technological Innovation", "Circular Economy"]
)
st.sidebar.subheader("Problem Statement:")
problem_statement = st.sidebar.text_area("Enter the problem statement")
st.sidebar.subheader("Solution Statement:")
solution_statement = st.sidebar.text_area("Enter the solution statement")
if st.sidebar.button("Evaluate Solution"):
st.sidebar.spinner("Evaluating Solution...")
overall_ranking = evaluate_solution(
problem_statement, solution_statement, parameters)
st.subheader(f"Overall Ranking: {overall_ranking}/10")
css = """
<style>
[data-testid="stSidebar"]{
min-width: 200;
max-width: 800px;
}
</style>
"""
st.markdown(css, unsafe_allow_html=True)
role = st.sidebar.selectbox(
"Your Role",
[
"🎯 Marketing Analyst",
"⚙️ Operations Analyst",
"💰 Sales Analyst",
"💼 Financial Analyst",
"📊 Data Analyst",
"📈 Business Analyst",
],
on_change=session_state.clear,
)
with open("data/assignmenttypes.json", "r") as file:
# Load the JSON content into a Python dictionary
assignment_types = json.load(file)
# Load JSON data from the neighboring directory
with open("data/metricevaluation.json", "r") as file:
instruction_data = json.load(file)
# Update session state if role changes
if role != session_state.role:
session_state.role = role
session_state.assignment_type = assignment_types[role][0]
industry = st.sidebar.selectbox(
"Industry",
[
"🏦 Banking & Finance",
"🏥 Healthcare",
"💻 Technology",
"🛍️ Retail",
"🏭 Manufacturing",
"📚 Education",
"🚀 Aerospace & Defense",
"🔋 Energy",
"🛢️ Oil & Gas",
"🧪 Pharmaceuticals",
"🌾 Agriculture",
"🏨 Hospitality",
"🚢 Shipping & Logistics",
"🏡 Real Estate",
"📺 Media & Entertainment",
"🔌 Utilities",
"🚧 Construction",
"📡 Telecommunications",
"🚗 Automotive",
"🍽️ Food & Beverage",
"👔 Fashion & Apparel",
"🌐 E-commerce",
"👩🔬 Research & Development",
],
key="industry",
)
customer_role = st.sidebar.selectbox(
"Your Customer",
[
"📈 Data Analytics Manager",
"🔬 Research Manager",
"🖥️ IT Manager",
"🔒 Security Manager",
"🛠️ Maintenance Manager",
"📦 Supply Chain Manager",
"👨🔬 Quality Manager",
"🚆 Transportation Manager",
"🎯 Marketing Manager",
"💰 Sales Manager",
"👩⚖️ Legal Counsel",
"💼 Controller",
"👥 HR Manager",
"👩💼 CEO",
"📈 CFO",
"🔒 CISO",
"💻 CIO",
"📊 CDO",
"🔧 COO",
"🌐 CTO",
"📢 CMO",
"👨🔬 CRO",
],
)
assignment_type = st.sidebar.selectbox(
"Type of Assignment",
assignment_types[role],
index=assignment_types[role].index(session_state.assignment_type),
on_change=session_state.clear,
)
# Update session state if assignment type changes
if assignment_type != session_state.assignment_type:
session_state.assignment_type = assignment_type
api_key = st.sidebar.text_input("Enter your GPT API key", type="password")
os.environ["OPENAI_API_KEY"] = api_key.lstrip('"').rstrip('"')
model_version = st.selectbox(
"Choose the Model Version", ["gpt-3.5-turbo", "Davincci"]
)
temperature = st.sidebar.slider(
"Choose the style you want the AI to write in: 0 is most rational, 1 is least",
0.0,
1.0,
step=0.1,
)
st.subheader(
f" Automating your {session_state.assignment_type} work for your {customer_role}"
)
# st.markdown(
# "<h4 style='font-weight: bold;'>In a few sentences,describe in detail what you want to accomplish with this analysis</h4>",
# unsafe_allow_html=True,
# )
problem_statement = st.multiselect(
"Here are some initial asks, please select those that apply",
[
"summury statistics",
"general trends",
"data annomolies",
"data aggregations",
],
key="problem_statement",
)
# Get the variables from the role-specific dat
uploaded_file = st.sidebar.file_uploader(
"Upload Excel or CSV", type=["csv"])
if uploaded_file is not None:
if st.button("Click to Analyze Your Data!"):
with NamedTemporaryFile(dir=".", suffix=".csv") as f:
f.write(uploaded_file.getbuffer())
docsearch = CVStoVectorStoreIndex(f.name)
first_response = RefineChain(docsearch)
st.write(first_response["result"])
response = GetGeneratedQuestions(
docsearch,
role=role,
industry=industry,
customer_role=customer_role,
chain_response=first_response,
assignment_type=assignment_type,
problem_statement=problem_statement,
)
list_from_string = ast.literal_eval(response["result"])
st.subheader(
"Question and Answers have been generated by EcoRankAI♻️")
agent = create_csv_agent(
OpenAI(temperature=temperature), f.name)
gpt4_agent = create_csv_agent(
OpenAI(temperature=temperature,
model_name="gpt-3.5-turbo"), f.name
)
if api_key:
if model_version == "gpt-3.5-turbo":
# apply_custom_css()
apply_custom_css2()
try:
for i, k in enumerate(list_from_string):
st.markdown(
f"<div class='slack-container'><div class='slack-question'>Question {i}: {k}</div><div class='slack-answer'>{gpt4_agent.run(k)}</div></div>",
unsafe_allow_html=True,
)
except TypeError as e:
print(e)
else:
apply_custom_css2()
try:
for i, k in enumerate(list_from_string):
st.markdown(
f"<div class='slack-container'><div class='slack-question'>Question {i}: {k}</div><div class='slack-answer'>{agent.run(k)}</div></div>",
unsafe_allow_html=True,
)
except TypeError as e:
print(e)
main()