File size: 1,403 Bytes
f8497b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Install required libraries
!pip install streamlit requests

# Streamlit app code
import streamlit as st
import requests
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

# API URL (replace with your ngrok URL)
API_URL = "https://c6d9-34-27-134-153.ngrok-free.app"  # Replace with your ngrok public URL

# Streamlit app
st.title("SQL Agent with Streamlit")

# Input for the question
question = st.text_input("Enter your question:")

if st.button("Submit"):
    if question:
        # Call the API
        response = requests.post(API_URL, json={"question": question})
        
        if response.status_code == 200:
            data = response.json()
            st.write("Generated SQL Query:")
            st.code(data["sql_query"], language="sql")
            
            st.write("Query Results:")
            result_df = pd.read_json(data["result"], orient='records')
            st.dataframe(result_df)
            
            # Visualize the data
            if 'region' in result_df.columns and 'total_sales' in result_df.columns:
                st.write("Total Sales by Region")
                fig, ax = plt.subplots()
                sns.barplot(x='region', y='total_sales', data=result_df, ax=ax)
                st.pyplot(fig)
        else:
            st.error(f"Error: {response.json().get('error')}")
    else:
        st.warning("Please enter a question.")