Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,7 @@ import pandas as pd
|
|
4 |
import matplotlib.pyplot as plt
|
5 |
import seaborn as sns
|
6 |
|
7 |
-
API_URL = "https://
|
8 |
|
9 |
st.set_page_config(
|
10 |
page_title="SQL Agent with Streamlit",
|
@@ -15,34 +15,52 @@ st.set_page_config(
|
|
15 |
with st.sidebar:
|
16 |
st.write("## About Me")
|
17 |
st.write("**Mahmoud Hassanen**")
|
18 |
-
st.write("**[LinkedIn Profile](https://www.linkedin.com/in/mahmoudhassanen99
|
19 |
-
|
|
|
20 |
st.title("SQL Agent with Streamlit")
|
21 |
st.header("Analyze Sales Data with Natural Language Queries")
|
22 |
|
23 |
# Input for the question
|
24 |
question = st.text_input("Enter your question:")
|
25 |
|
26 |
-
if st.button("
|
27 |
if question:
|
|
|
28 |
response = requests.post(API_URL, json={"question": question})
|
29 |
|
30 |
if response.status_code == 200:
|
31 |
data = response.json()
|
32 |
-
|
33 |
-
st.
|
34 |
-
|
35 |
-
st.
|
36 |
-
result_df = pd.read_json(data["result"], orient='records')
|
37 |
-
st.dataframe(result_df)
|
38 |
-
|
39 |
-
# Visualize the data
|
40 |
-
if 'region' in result_df.columns and 'total_sales' in result_df.columns:
|
41 |
-
st.write("Total Sales by Region")
|
42 |
-
fig, ax = plt.subplots()
|
43 |
-
sns.barplot(x='region', y='total_sales', data=result_df, ax=ax)
|
44 |
-
st.pyplot(fig)
|
45 |
else:
|
46 |
-
st.error(f"Error: {response.
|
47 |
else:
|
48 |
-
st.warning("Please enter a question.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
import matplotlib.pyplot as plt
|
5 |
import seaborn as sns
|
6 |
|
7 |
+
API_URL = "https://7eba-34-127-95-74.ngrok-free.app/query"
|
8 |
|
9 |
st.set_page_config(
|
10 |
page_title="SQL Agent with Streamlit",
|
|
|
15 |
with st.sidebar:
|
16 |
st.write("## About Me")
|
17 |
st.write("**Mahmoud Hassanen**")
|
18 |
+
st.write("**[LinkedIn Profile](https://www.linkedin.com/in/mahmoudhassanen99/)**")
|
19 |
+
|
20 |
+
# Main content
|
21 |
st.title("SQL Agent with Streamlit")
|
22 |
st.header("Analyze Sales Data with Natural Language Queries")
|
23 |
|
24 |
# Input for the question
|
25 |
question = st.text_input("Enter your question:")
|
26 |
|
27 |
+
if st.button("Generate SQL"):
|
28 |
if question:
|
29 |
+
# API to generate SQL
|
30 |
response = requests.post(API_URL, json={"question": question})
|
31 |
|
32 |
if response.status_code == 200:
|
33 |
data = response.json()
|
34 |
+
generated_sql = data["sql_query"]
|
35 |
+
st.session_state.generated_sql = generated_sql # Store the generated SQL in session state
|
36 |
+
st.write("### Generated SQL Query:")
|
37 |
+
st.code(generated_sql, language="sql")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
else:
|
39 |
+
st.error(f"API Error: Status Code {response.status_code}")
|
40 |
else:
|
41 |
+
st.warning("Please enter a question.")
|
42 |
+
|
43 |
+
# Allow the user to modify the SQL query
|
44 |
+
if "generated_sql" in st.session_state:
|
45 |
+
modified_sql = st.text_area("Modify the SQL query (if needed):", st.session_state.generated_sql, height=200)
|
46 |
+
|
47 |
+
if st.button("Execute Modified Query"):
|
48 |
+
try:
|
49 |
+
# Execute the modified SQL query
|
50 |
+
response = requests.post(API_URL, json={"sql_query": modified_sql}) # Send the modified SQL to the API
|
51 |
+
if response.status_code == 200:
|
52 |
+
data = response.json()
|
53 |
+
result_df = pd.read_json(data["result"], orient='records')
|
54 |
+
st.write("### Query Results:")
|
55 |
+
st.dataframe(result_df)
|
56 |
+
|
57 |
+
# Visualize the data (if applicable)
|
58 |
+
if 'region' in result_df.columns and 'total_sales' in result_df.columns:
|
59 |
+
st.write("### Total Sales by Region")
|
60 |
+
fig, ax = plt.subplots()
|
61 |
+
sns.barplot(x='region', y='total_sales', data=result_df, ax=ax)
|
62 |
+
st.pyplot(fig)
|
63 |
+
else:
|
64 |
+
st.error(f"Error executing SQL: {response.json().get('error')}")
|
65 |
+
except Exception as e:
|
66 |
+
st.error(f"Error executing SQL: {e}")
|