-
Notifications
You must be signed in to change notification settings - Fork 0
/
first_studio.py
143 lines (108 loc) · 4.74 KB
/
first_studio.py
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
#!/usr/bin/env python
# coding: utf-8
# Import modules
import streamlit as st
import pandas as pd
import numpy as np
import requests
import json
import openai
import random
#Setting titles
st.title("Dataflow design studio")
st.markdown('<p style="font-size:25px">Statistics are just <b style="color:blue">one face</b> of reality</p>', unsafe_allow_html=True)
# Step 2: Connect to the ChatGPT API
def query_chatgpt(prompt):
api_key = st.secrets["API_KEY"]
url = 'https://api.openai.com/v1/chat/completions'
headers = {
'Authorization': f'Bearer {api_key}',
'Content-Type': 'application/json'
}
data = {
'model': 'gpt-3.5-turbo',
'messages': [{'role': 'system', 'content': 'You are a human-data interaction designer that can explain to a five year old children some insights about dataset.'},
{'role': 'user', 'content': prompt}],
'temperature': 0.1
}
response = requests.post(url, headers=headers, data=json.dumps(data))
response_json = response.json()
print(response_json)
answer = response_json['choices'][0]['message']['content']
return answer
# Step 3: Prepare the prompt
def prepare_prompt(data):
prompt = f"Tell me what are the two most pertinent columns/variables to understand the dataset and explain to me why. Create one sentence (max 20 words long) for each variable and use metaphors to explain. Space the two sentences:\n\n{data}"
return prompt
# Step 4: Send the prompt to ChatGPT
def send_prompt_to_chatgpt(prompt):
answer = query_chatgpt(prompt)
return answer
# Step 5: Display the response
def display_response(response):
words = response.split() # Split the response into individual words
colored_response = ""
# Define a list of colors to assign to each word
colors = ["red", "blue", "green", "orange", "purple"]
# Iterate over each word and apply a different color
for i, word in enumerate(words):
color = colors[i % len(colors)] # Use modulo operator to cycle through colors
colored_word = f'<span style="color: {color};">{word}</span>'
colored_response += colored_word + " "
st.markdown(f'<div style="font-size: 14px; margin-bottom: 20px; font-weight: bold;">{colored_response}</div>', unsafe_allow_html=True)
# Streamlit app code
st.subheader('Throw your messy, queasy fabric in !')
# Create three columns for the file uploaders
col1, col2, col3 = st.columns(3)
dataframe1 = None
dataframe2 = None
dataframe3 = None
# File uploader in the first column
with col1:
uploaded_file1 = st.file_uploader("Fabric 1b")
if uploaded_file1 is not None:
def load_data1():
dataframe1 = pd.read_csv(uploaded_file1, nrows=10)
return dataframe1
dataframe1 = load_data1()
first_10_rows1 = dataframe1.head(10)
# Apply random color styling to the dataframe
styled_dataframe1 = first_10_rows1.style.applymap(lambda x: 'background-color: #{:06x}'.format(random.randint(0, 256**3-1)))
st.write(styled_dataframe1) # Display the styled dataframe
# File uploader in the second column
with col2:
uploaded_file2 = st.file_uploader("Fabric 2b")
if uploaded_file2 is not None:
def load_data2():
dataframe2 = pd.read_csv(uploaded_file2, nrows=10)
return dataframe2
dataframe2 = load_data2()
first_10_rows2 = dataframe2.head(10)
# Apply random color styling to the dataframe
styled_dataframe2 = first_10_rows2.style.applymap(lambda x: 'background-color: #{:06x}'.format(random.randint(0, 256**3-1)))
st.write(styled_dataframe2) # Display the styled dataframe
# File uploader in the third column
with col3:
uploaded_file3 = st.file_uploader("Fabric 3b")
if uploaded_file3 is not None:
def load_data3():
dataframe3 = pd.read_csv(uploaded_file3, nrows=10)
return dataframe3
dataframe3 = load_data3()
first_10_rows3 = dataframe3.head(10)
# Apply random color styling to the dataframe
styled_dataframe3 = first_10_rows3.style.applymap(lambda x: 'background-color: #{:06x}'.format(random.randint(0, 256**3-1)))
st.write(styled_dataframe3) # Display the styled dataframe
if dataframe1 is not None and dataframe2 is not None and dataframe3 is not None:
prompt1 = prepare_prompt(dataframe1)
prompt2 = prepare_prompt(dataframe2)
prompt3 = prepare_prompt(dataframe3)
response1 = send_prompt_to_chatgpt(prompt1)
response2 = send_prompt_to_chatgpt(prompt2)
response3 = send_prompt_to_chatgpt(prompt3)
with col1:
display_response(response1)
with col2:
display_response(response2)
with col3:
display_response(response3)