Spaces:
Running
Running
# 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.") |