Mhassanen commited on
Commit
138e89c
·
verified ·
1 Parent(s): 16a4075

Update app.py

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