Update app.py
Browse files
app.py
CHANGED
@@ -49,10 +49,12 @@ def match_code_blocks(llm_response: str) -> str:
|
|
49 |
return ""
|
50 |
|
51 |
def chat_with_llm(e2b_code_interpreter: Sandbox, user_message: str, dataset_path: str) -> Tuple[Optional[List[Any]], str]:
|
52 |
-
#
|
53 |
-
system_prompt = f"""You're a Python data scientist and data visualization expert. You are given a dataset at path '{dataset_path}' and
|
54 |
You need to analyze the dataset and answer the user's query with a response and you run Python code to solve them.
|
55 |
-
IMPORTANT:
|
|
|
|
|
56 |
|
57 |
messages = [
|
58 |
{"role": "system", "content": system_prompt},
|
@@ -86,13 +88,12 @@ def upload_dataset(code_interpreter: Sandbox, uploaded_file) -> str:
|
|
86 |
st.error(f"Error during file upload: {error}")
|
87 |
raise error
|
88 |
|
89 |
-
|
90 |
def main():
|
91 |
"""Main Streamlit application."""
|
92 |
st.set_page_config(page_title="π AI Data Visualization Agent", page_icon="π", layout="wide")
|
93 |
|
94 |
st.title("π AI Data Visualization Agent")
|
95 |
-
st.write("Upload your dataset and ask questions about it!")
|
96 |
|
97 |
# Initialize session state variables
|
98 |
if 'together_api_key' not in st.session_state:
|
@@ -127,15 +128,20 @@ def main():
|
|
127 |
st.session_state.model_name = model_options[st.session_state.model_name]
|
128 |
|
129 |
# Main content layout
|
130 |
-
col1, col2 = st.columns([1, 2])
|
131 |
|
132 |
with col1:
|
133 |
st.header("π Upload Dataset")
|
134 |
-
|
|
|
135 |
|
136 |
if uploaded_file is not None:
|
137 |
-
#
|
138 |
-
|
|
|
|
|
|
|
|
|
139 |
st.write("### Dataset Preview")
|
140 |
show_full = st.checkbox("Show full dataset")
|
141 |
if show_full:
|
@@ -172,17 +178,14 @@ def main():
|
|
172 |
if code_results:
|
173 |
st.header("π Analysis Results")
|
174 |
for result in code_results:
|
175 |
-
if hasattr(result, 'png') and result.png:
|
176 |
-
# Decode the base64-encoded PNG data
|
177 |
png_data = base64.b64decode(result.png)
|
178 |
-
|
179 |
-
# Convert PNG data to an image and display it
|
180 |
image = Image.open(BytesIO(png_data))
|
181 |
-
st.image(image, caption="Generated Visualization",
|
182 |
-
elif hasattr(result, 'figure'):
|
183 |
-
fig = result.figure
|
184 |
-
st.pyplot(fig)
|
185 |
-
elif hasattr(result, 'show'):
|
186 |
st.plotly_chart(result)
|
187 |
elif isinstance(result, (pd.DataFrame, pd.Series)):
|
188 |
st.dataframe(result)
|
|
|
49 |
return ""
|
50 |
|
51 |
def chat_with_llm(e2b_code_interpreter: Sandbox, user_message: str, dataset_path: str) -> Tuple[Optional[List[Any]], str]:
|
52 |
+
# Updated system prompt with Excel support
|
53 |
+
system_prompt = f"""You're a Python data scientist and data visualization expert. You are given a dataset at path '{dataset_path}' (could be CSV or Excel) and the user's query.
|
54 |
You need to analyze the dataset and answer the user's query with a response and you run Python code to solve them.
|
55 |
+
IMPORTANT:
|
56 |
+
- Use pd.read_csv() for .csv files and pd.read_excel() for .xlsx/.xls files
|
57 |
+
- Always use the dataset path variable '{dataset_path}' in your code when reading the file"""
|
58 |
|
59 |
messages = [
|
60 |
{"role": "system", "content": system_prompt},
|
|
|
88 |
st.error(f"Error during file upload: {error}")
|
89 |
raise error
|
90 |
|
|
|
91 |
def main():
|
92 |
"""Main Streamlit application."""
|
93 |
st.set_page_config(page_title="π AI Data Visualization Agent", page_icon="π", layout="wide")
|
94 |
|
95 |
st.title("π AI Data Visualization Agent")
|
96 |
+
st.write("Upload your dataset (CSV or Excel) and ask questions about it!")
|
97 |
|
98 |
# Initialize session state variables
|
99 |
if 'together_api_key' not in st.session_state:
|
|
|
128 |
st.session_state.model_name = model_options[st.session_state.model_name]
|
129 |
|
130 |
# Main content layout
|
131 |
+
col1, col2 = st.columns([1, 2])
|
132 |
|
133 |
with col1:
|
134 |
st.header("π Upload Dataset")
|
135 |
+
# Updated file uploader to accept both CSV and Excel
|
136 |
+
uploaded_file = st.file_uploader("Choose a CSV or Excel file", type=["csv", "xlsx", "xls"], key="file_uploader")
|
137 |
|
138 |
if uploaded_file is not None:
|
139 |
+
# Handle both CSV and Excel files
|
140 |
+
if uploaded_file.name.endswith(('.xlsx', '.xls')):
|
141 |
+
df = pd.read_excel(uploaded_file)
|
142 |
+
else:
|
143 |
+
df = pd.read_csv(uploaded_file)
|
144 |
+
|
145 |
st.write("### Dataset Preview")
|
146 |
show_full = st.checkbox("Show full dataset")
|
147 |
if show_full:
|
|
|
178 |
if code_results:
|
179 |
st.header("π Analysis Results")
|
180 |
for result in code_results:
|
181 |
+
if hasattr(result, 'png') and result.png:
|
|
|
182 |
png_data = base64.b64decode(result.png)
|
|
|
|
|
183 |
image = Image.open(BytesIO(png_data))
|
184 |
+
st.image(image, caption="Generated Visualization", use_column_width=True)
|
185 |
+
elif hasattr(result, 'figure'):
|
186 |
+
fig = result.figure
|
187 |
+
st.pyplot(fig)
|
188 |
+
elif hasattr(result, 'show'):
|
189 |
st.plotly_chart(result)
|
190 |
elif isinstance(result, (pd.DataFrame, pd.Series)):
|
191 |
st.dataframe(result)
|