Garvitj commited on
Commit
3b1b499
Β·
verified Β·
1 Parent(s): 4d86911

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +38 -25
src/streamlit_app.py CHANGED
@@ -12,7 +12,7 @@ import requests
12
  from sqlalchemy import create_engine, text, inspect
13
 
14
  # --- Get HF Token ---
15
- HF_TOKEN = os.environ["HF_TOKEN"] # will raise KeyError if not set
16
 
17
  # --- Helper: Call Mistral Model ---
18
  def mistral_call(schema=None, question="no questions were asked", hf_token=HF_TOKEN, model_id="mistralai/Mistral-7B-Instruct-v0.3"):
@@ -153,32 +153,45 @@ option = st.sidebar.radio("Select Feature", ["πŸ“Š Data Visualization", "🧠 SQ
153
  if option == "πŸ“Š Data Visualization":
154
  uploaded_file = st.file_uploader("Upload your CSV", type="csv")
155
  if uploaded_file:
156
- df = pd.read_csv(uploaded_file)
157
- df.columns = df.columns.str.strip()
158
- st.dataframe(df.head())
159
- with st.spinner("Getting chart suggestion..."):
160
- suggestion = get_visualization_suggestion(df)
161
- if suggestion:
162
- x_col = suggestion.get("x", "").strip()
163
- y_col = suggestion.get("y", [])
164
- y_col = [y_col] if isinstance(y_col, str) else y_col
165
- chart = suggestion.get("chart_type")
166
- if x_col in df.columns and all(y in df.columns for y in y_col):
167
- fig = None
168
- if chart == "bar":
169
- fig = px.bar(df, x=x_col, y=y_col)
170
- elif chart == "line":
171
- fig = px.line(df, x=x_col, y=y_col)
172
- elif chart == "scatter":
173
- fig = px.scatter(df, x=x_col, y=y_col)
174
- elif chart == "pie" and len(y_col) == 1:
175
- fig = px.pie(df, names=x_col, values=y_col[0])
176
- if fig:
177
- st.plotly_chart(fig)
 
 
 
 
 
 
 
 
 
 
 
178
  else:
179
- st.error("Unsupported chart type.")
180
  else:
181
- st.error("Invalid column suggestion from model.")
 
 
182
 
183
  elif option == "🧠 SQL Query Generator":
184
  user_input = st.text_area("Describe your SQL query in plain English:")
 
12
  from sqlalchemy import create_engine, text, inspect
13
 
14
  # --- Get HF Token ---
15
+ HF_TOKEN = os.environ.get("HF_TOKEN", "") # Safely get token, fallback if missing
16
 
17
  # --- Helper: Call Mistral Model ---
18
  def mistral_call(schema=None, question="no questions were asked", hf_token=HF_TOKEN, model_id="mistralai/Mistral-7B-Instruct-v0.3"):
 
153
  if option == "πŸ“Š Data Visualization":
154
  uploaded_file = st.file_uploader("Upload your CSV", type="csv")
155
  if uploaded_file:
156
+ try:
157
+ content = uploaded_file.getvalue().decode("utf-8")
158
+ df = pd.read_csv(io.StringIO(content))
159
+ df.columns = df.columns.str.strip().str.replace(" ", "_")
160
+ st.write("CSV Preview")
161
+ st.dataframe(df.head())
162
+ st.write("Shape:", df.shape)
163
+
164
+ with st.spinner("Getting chart suggestion..."):
165
+ suggestion = get_visualization_suggestion(df)
166
+
167
+ st.write("Model suggestion:")
168
+ st.code(suggestion)
169
+
170
+ if suggestion:
171
+ x_col = suggestion.get("x", "").strip()
172
+ y_col = suggestion.get("y", [])
173
+ y_col = [y_col] if isinstance(y_col, str) else y_col
174
+ chart = suggestion.get("chart_type")
175
+ if x_col in df.columns and all(y in df.columns for y in y_col):
176
+ fig = None
177
+ if chart == "bar":
178
+ fig = px.bar(df, x=x_col, y=y_col)
179
+ elif chart == "line":
180
+ fig = px.line(df, x=x_col, y=y_col)
181
+ elif chart == "scatter":
182
+ fig = px.scatter(df, x=x_col, y=y_col)
183
+ elif chart == "pie" and len(y_col) == 1:
184
+ fig = px.pie(df, names=x_col, values=y_col[0])
185
+ if fig:
186
+ st.plotly_chart(fig)
187
+ else:
188
+ st.error("Unsupported chart type.")
189
  else:
190
+ st.error("⚠️ Column suggestion doesn't match your CSV.")
191
  else:
192
+ st.error("❌ No valid visualization suggestion returned.")
193
+ except Exception as e:
194
+ st.error(f"❌ Error reading CSV: {e}")
195
 
196
  elif option == "🧠 SQL Query Generator":
197
  user_input = st.text_area("Describe your SQL query in plain English:")