Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,49 +1,74 @@
|
|
1 |
-
# Streamlit app code
|
2 |
import streamlit as st
|
3 |
import requests
|
4 |
import pandas as pd
|
5 |
import matplotlib.pyplot as plt
|
6 |
import seaborn as sns
|
7 |
|
8 |
-
|
9 |
-
|
10 |
st.set_page_config(
|
11 |
-
page_title="SQL Agent with Streamlit",
|
12 |
-
page_icon=":bar_chart:",
|
13 |
-
layout="wide"
|
14 |
)
|
15 |
|
16 |
-
|
17 |
-
st.write("## About Me")
|
18 |
-
st.write("**Mahmoud Hassanen**")
|
19 |
-
st.write("**[LinkedIn Profile](https://www.linkedin.com/in/mahmoudhassanen99//)**")
|
20 |
-
|
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("
|
28 |
if question:
|
|
|
29 |
response = requests.post(API_URL, json={"question": question})
|
30 |
|
31 |
if response.status_code == 200:
|
32 |
data = response.json()
|
33 |
-
|
34 |
-
st.
|
35 |
-
|
36 |
-
st.
|
37 |
-
|
38 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
-
# Visualize the data
|
41 |
-
if 'region' in
|
42 |
-
st.write("Total Sales by Region")
|
43 |
fig, ax = plt.subplots()
|
44 |
-
sns.barplot(x='region', y='total_sales', data=
|
45 |
st.pyplot(fig)
|
46 |
-
|
47 |
-
st.error(f"Error: {
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import requests
|
3 |
import pandas as pd
|
4 |
import matplotlib.pyplot as plt
|
5 |
import seaborn as sns
|
6 |
|
7 |
+
# Set page configuration
|
|
|
8 |
st.set_page_config(
|
9 |
+
page_title="SQL Agent with Streamlit", # Page title
|
10 |
+
page_icon=":bar_chart:", # Favicon emoji
|
11 |
+
layout="wide" # Page layout option
|
12 |
)
|
13 |
|
14 |
+
# Add a header and page name
|
|
|
|
|
|
|
|
|
15 |
st.title("SQL Agent with Streamlit")
|
16 |
st.header("Analyze Sales Data with Natural Language Queries")
|
17 |
|
18 |
+
# Add a sidebar with your name and LinkedIn profile
|
19 |
+
with st.sidebar:
|
20 |
+
st.write("## About Me")
|
21 |
+
st.write("**Name:** Your Name") # Replace with your name
|
22 |
+
st.write("**LinkedIn:** [Your LinkedIn Profile](https://www.linkedin.com/in/your-profile/)") # Replace with your LinkedIn URL
|
23 |
+
|
24 |
+
# API URL (replace with your ngrok URL)
|
25 |
+
API_URL = "https://c6d9-34-27-134-153.ngrok-free.app/query" # Replace with your ngrok public URL
|
26 |
+
|
27 |
# Input for the question
|
28 |
question = st.text_input("Enter your question:")
|
29 |
|
30 |
+
if st.button("Generate SQL"):
|
31 |
if question:
|
32 |
+
# Call the API to generate SQL
|
33 |
response = requests.post(API_URL, json={"question": question})
|
34 |
|
35 |
if response.status_code == 200:
|
36 |
data = response.json()
|
37 |
+
generated_sql = data["sql_query"]
|
38 |
+
st.session_state.generated_sql = generated_sql # Store the generated SQL in session state
|
39 |
+
st.write("### Generated SQL Query:")
|
40 |
+
st.code(generated_sql, language="sql")
|
41 |
+
else:
|
42 |
+
st.error(f"API Error: Status Code {response.status_code}")
|
43 |
+
else:
|
44 |
+
st.warning("Please enter a question.")
|
45 |
+
|
46 |
+
# Allow the user to modify the SQL query
|
47 |
+
if "generated_sql" in st.session_state:
|
48 |
+
modified_sql = st.text_area("Modify the SQL query (if needed):", st.session_state.generated_sql, height=200)
|
49 |
+
|
50 |
+
if st.button("Execute Modified Query"):
|
51 |
+
try:
|
52 |
+
# Execute the modified SQL query
|
53 |
+
result = execute_sql(modified_sql) # Use your existing execute_sql function
|
54 |
+
st.write("### Query Results:")
|
55 |
+
st.dataframe(result)
|
56 |
|
57 |
+
# Visualize the data (if applicable)
|
58 |
+
if 'region' in result.columns and 'total_sales' in result.columns:
|
59 |
+
st.write("### Total Sales by Region")
|
60 |
fig, ax = plt.subplots()
|
61 |
+
sns.barplot(x='region', y='total_sales', data=result, ax=ax)
|
62 |
st.pyplot(fig)
|
63 |
+
except Exception as e:
|
64 |
+
st.error(f"Error executing SQL: {e}")
|
65 |
+
|
66 |
+
# Function to execute SQL and return results
|
67 |
+
def execute_sql(sql_query):
|
68 |
+
# Create a SQLAlchemy connection string
|
69 |
+
connection_string = f"mssql+pyodbc://{username}:{password}@{server}/{database}?driver={driver.replace(' ', '+')}"
|
70 |
+
engine = create_engine(connection_string)
|
71 |
+
|
72 |
+
# Execute the query and fetch results
|
73 |
+
df = pd.read_sql(sql_query, engine)
|
74 |
+
return df
|