Mhassanen commited on
Commit
cb437b6
·
verified ·
1 Parent(s): a87f18d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -26
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
- API_URL = "https://14d0-34-27-134-153.ngrok-free.app/query" # Replace with your ngrok public URL
9
-
10
  st.set_page_config(
11
- page_title="SQL Agent with Streamlit",
12
- page_icon=":bar_chart:",
13
- layout="wide"
14
  )
15
 
16
- with st.sidebar:
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("Submit"):
28
  if question:
 
29
  response = requests.post(API_URL, json={"question": question})
30
 
31
  if response.status_code == 200:
32
  data = response.json()
33
- st.write("Generated SQL Query:")
34
- st.code(data["sql_query"], language="sql")
35
-
36
- st.write("Query Results:")
37
- result_df = pd.read_json(data["result"], orient='records')
38
- st.dataframe(result_df)
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
- # Visualize the data
41
- if 'region' in result_df.columns and 'total_sales' in result_df.columns:
42
- st.write("Total Sales by Region")
43
  fig, ax = plt.subplots()
44
- sns.barplot(x='region', y='total_sales', data=result_df, ax=ax)
45
  st.pyplot(fig)
46
- else:
47
- st.error(f"Error: {response.json().get('error')}")
48
- else:
49
- st.warning("Please enter a question.")
 
 
 
 
 
 
 
 
 
 
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