EditsPaarth commited on
Commit
64f4108
·
verified ·
1 Parent(s): 631b613

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -29
app.py CHANGED
@@ -3,11 +3,9 @@ import pandas as pd
3
  import numpy as np
4
  import seaborn as sns
5
  import matplotlib.pyplot as plt
6
- import tempfile
7
- import subprocess
8
  from groq import Groq
9
 
10
- # Groq API Key setup .
11
  GROQ_API_KEY = "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"
12
  client = Groq(api_key=GROQ_API_KEY)
13
 
@@ -19,7 +17,6 @@ def chat_with_groq(prompt):
19
  model="llama3-8b-8192",
20
  stream=False
21
  )
22
- print(prompt)
23
  return chat_completion.choices[0].message.content
24
  except Exception as e:
25
  return f"Error fetching response: {e}"
@@ -50,7 +47,7 @@ def parse_file(uploaded_file):
50
  # Preprocess DataFrame to Fix Type Issues
51
  def preprocess_dataframe(df):
52
  try:
53
- # Convert problematic columns to string to avoid Arrow serialization issues
54
  for col in df.columns:
55
  if df[col].dtype.name == 'object' or df[col].dtype.name == 'category':
56
  df[col] = df[col].astype(str)
@@ -85,9 +82,6 @@ def analyze_data(data, visualization_type, class_size=10):
85
  fig, ax = plt.subplots(figsize=(8, 6))
86
  sns.heatmap(numeric_data.corr(), annot=True, ax=ax, cmap="coolwarm", fmt=".2f")
87
  st.pyplot(fig)
88
-
89
-
90
-
91
  elif visualization_type == "Line Graph" and not numeric_data.empty:
92
  st.subheader("Line Graph")
93
  x_col = st.selectbox("Select the X-axis column for the Line Graph (Non-Numeric):", numeric_data.columns)
@@ -98,10 +92,6 @@ def analyze_data(data, visualization_type, class_size=10):
98
  ax.set_xlabel(x_col)
99
  ax.set_ylabel(y_col)
100
  st.pyplot(fig)
101
-
102
-
103
-
104
-
105
  elif visualization_type == "Area Chart" and not numeric_data.empty:
106
  st.subheader("Area Chart")
107
  column = st.selectbox("Select a column for the Area Chart:", numeric_data.columns)
@@ -110,7 +100,6 @@ def analyze_data(data, visualization_type, class_size=10):
110
  ax.set_xlabel(column)
111
  ax.set_ylabel("Area")
112
  st.pyplot(fig)
113
-
114
  else:
115
  st.warning("No valid visualization option selected or data available.")
116
 
@@ -118,6 +107,7 @@ def analyze_data(data, visualization_type, class_size=10):
118
  prompt = generate_groq_prompt(data, visualization_type, class_size)
119
  return prompt
120
 
 
121
  def generate_groq_prompt(data, visualization_type, class_size):
122
  # Limit the preview to the first 10 rows for readability
123
  data_snippet = data.head(10) # Select top 10 rows for the preview
@@ -150,8 +140,6 @@ def generate_groq_prompt(data, visualization_type, class_size):
150
  return prompt
151
 
152
 
153
-
154
-
155
  # Streamlit App
156
  st.title("Data Analysis AI")
157
  st.markdown("Upload a file (CSV or Excel) to analyze it.")
@@ -175,22 +163,14 @@ if uploaded_file is not None:
175
  # User input for class size customization
176
  class_size = st.slider("Select the class size for certain plots (e.g., Histogram)", 5, 50, 10)
177
 
178
- # Perform Analysis and Visualization
179
  prompt = analyze_data(data, visualization_type, class_size)
180
-
181
- # Display the textual prompt with the indented table
182
- st.subheader("Prompt Sent to Groq")
183
- st.text_area("Preview of Prompt", value=prompt, height=300)
184
-
185
 
186
- # Chat with Groq Section
187
- st.subheader("Chat with Groq")
188
- chat_input = st.text_area("Ask Groq questions about the data:")
189
- if st.button("Chat"):
190
- if chat_input:
191
- chat_response = chat_with_groq(f"Here is the data:\n{data}\n\n{chat_input}")
192
- st.write("Groq's Response:")
193
- st.write(chat_response)
194
 
195
  # Groq Code Generation Section
196
  st.subheader("Generate Python Code with Groq")
@@ -205,3 +185,4 @@ if uploaded_file is not None:
205
  st.code(response, language="python")
206
  except Exception as e:
207
  st.error(f"An error occurred: {e}")
 
 
3
  import numpy as np
4
  import seaborn as sns
5
  import matplotlib.pyplot as plt
 
 
6
  from groq import Groq
7
 
8
+ # Groq API Key setup
9
  GROQ_API_KEY = "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"
10
  client = Groq(api_key=GROQ_API_KEY)
11
 
 
17
  model="llama3-8b-8192",
18
  stream=False
19
  )
 
20
  return chat_completion.choices[0].message.content
21
  except Exception as e:
22
  return f"Error fetching response: {e}"
 
47
  # Preprocess DataFrame to Fix Type Issues
48
  def preprocess_dataframe(df):
49
  try:
50
+ # Convert problematic columns to string to avoid serialization issues
51
  for col in df.columns:
52
  if df[col].dtype.name == 'object' or df[col].dtype.name == 'category':
53
  df[col] = df[col].astype(str)
 
82
  fig, ax = plt.subplots(figsize=(8, 6))
83
  sns.heatmap(numeric_data.corr(), annot=True, ax=ax, cmap="coolwarm", fmt=".2f")
84
  st.pyplot(fig)
 
 
 
85
  elif visualization_type == "Line Graph" and not numeric_data.empty:
86
  st.subheader("Line Graph")
87
  x_col = st.selectbox("Select the X-axis column for the Line Graph (Non-Numeric):", numeric_data.columns)
 
92
  ax.set_xlabel(x_col)
93
  ax.set_ylabel(y_col)
94
  st.pyplot(fig)
 
 
 
 
95
  elif visualization_type == "Area Chart" and not numeric_data.empty:
96
  st.subheader("Area Chart")
97
  column = st.selectbox("Select a column for the Area Chart:", numeric_data.columns)
 
100
  ax.set_xlabel(column)
101
  ax.set_ylabel("Area")
102
  st.pyplot(fig)
 
103
  else:
104
  st.warning("No valid visualization option selected or data available.")
105
 
 
107
  prompt = generate_groq_prompt(data, visualization_type, class_size)
108
  return prompt
109
 
110
+ # Generate Groq Prompt with Indented Table
111
  def generate_groq_prompt(data, visualization_type, class_size):
112
  # Limit the preview to the first 10 rows for readability
113
  data_snippet = data.head(10) # Select top 10 rows for the preview
 
140
  return prompt
141
 
142
 
 
 
143
  # Streamlit App
144
  st.title("Data Analysis AI")
145
  st.markdown("Upload a file (CSV or Excel) to analyze it.")
 
163
  # User input for class size customization
164
  class_size = st.slider("Select the class size for certain plots (e.g., Histogram)", 5, 50, 10)
165
 
166
+ # Perform Analysis and Generate Prompt for Groq
167
  prompt = analyze_data(data, visualization_type, class_size)
 
 
 
 
 
168
 
169
+ # Send the prompt directly to Groq
170
+ st.subheader("Groq Processing")
171
+ response = chat_with_groq(prompt)
172
+ st.write("Groq's Response:")
173
+ st.write(response)
 
 
 
174
 
175
  # Groq Code Generation Section
176
  st.subheader("Generate Python Code with Groq")
 
185
  st.code(response, language="python")
186
  except Exception as e:
187
  st.error(f"An error occurred: {e}")
188
+