Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Install required libraries
|
2 |
+
!pip install streamlit requests
|
3 |
+
|
4 |
+
# Streamlit app code
|
5 |
+
import streamlit as st
|
6 |
+
import requests
|
7 |
+
import pandas as pd
|
8 |
+
import matplotlib.pyplot as plt
|
9 |
+
import seaborn as sns
|
10 |
+
|
11 |
+
# API URL (replace with your ngrok URL)
|
12 |
+
API_URL = "https://c6d9-34-27-134-153.ngrok-free.app" # Replace with your ngrok public URL
|
13 |
+
|
14 |
+
# Streamlit app
|
15 |
+
st.title("SQL Agent with Streamlit")
|
16 |
+
|
17 |
+
# Input for the question
|
18 |
+
question = st.text_input("Enter your question:")
|
19 |
+
|
20 |
+
if st.button("Submit"):
|
21 |
+
if question:
|
22 |
+
# Call the API
|
23 |
+
response = requests.post(API_URL, json={"question": question})
|
24 |
+
|
25 |
+
if response.status_code == 200:
|
26 |
+
data = response.json()
|
27 |
+
st.write("Generated SQL Query:")
|
28 |
+
st.code(data["sql_query"], language="sql")
|
29 |
+
|
30 |
+
st.write("Query Results:")
|
31 |
+
result_df = pd.read_json(data["result"], orient='records')
|
32 |
+
st.dataframe(result_df)
|
33 |
+
|
34 |
+
# Visualize the data
|
35 |
+
if 'region' in result_df.columns and 'total_sales' in result_df.columns:
|
36 |
+
st.write("Total Sales by Region")
|
37 |
+
fig, ax = plt.subplots()
|
38 |
+
sns.barplot(x='region', y='total_sales', data=result_df, ax=ax)
|
39 |
+
st.pyplot(fig)
|
40 |
+
else:
|
41 |
+
st.error(f"Error: {response.json().get('error')}")
|
42 |
+
else:
|
43 |
+
st.warning("Please enter a question.")
|