Update src/streamlit_app.py
Browse files- 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
|
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 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
178 |
else:
|
179 |
-
st.error("
|
180 |
else:
|
181 |
-
st.error("
|
|
|
|
|
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:")
|