Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,6 +10,7 @@ import sys
|
|
| 10 |
from datetime import datetime
|
| 11 |
import time
|
| 12 |
import logging
|
|
|
|
| 13 |
|
| 14 |
# Set up logging
|
| 15 |
logging.basicConfig(
|
|
@@ -34,6 +35,7 @@ DB_CONFIG = {
|
|
| 34 |
# Global variables for model and tokenizer
|
| 35 |
GLOBAL_MODEL = None
|
| 36 |
GLOBAL_TOKENIZER = None
|
|
|
|
| 37 |
|
| 38 |
def initialize_model():
|
| 39 |
"""Initialize model and tokenizer globally"""
|
|
@@ -56,6 +58,7 @@ def initialize_model():
|
|
| 56 |
|
| 57 |
def test_db_connection():
|
| 58 |
"""Test database connection with timeout"""
|
|
|
|
| 59 |
try:
|
| 60 |
logging.info("Testing database connection...")
|
| 61 |
connection = mysql.connector.connect(
|
|
@@ -69,9 +72,11 @@ def test_db_connection():
|
|
| 69 |
db_name = cursor.fetchone()[0]
|
| 70 |
cursor.close()
|
| 71 |
connection.close()
|
|
|
|
| 72 |
logging.info(f"Successfully connected to MySQL Server version {db_info} - Database: {db_name}")
|
| 73 |
return True, f"Successfully connected to MySQL Server version {db_info}\nDatabase: {db_name}"
|
| 74 |
except Error as e:
|
|
|
|
| 75 |
logging.error(f"Error connecting to MySQL database: {e}")
|
| 76 |
return False, f"Error connecting to MySQL database: {e}"
|
| 77 |
return False, "Unable to establish database connection"
|
|
@@ -187,19 +192,25 @@ def format_result(query_result):
|
|
| 187 |
|
| 188 |
return "\n".join(results)
|
| 189 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
def main():
|
| 191 |
"""Main function with Streamlit UI components"""
|
| 192 |
st.title("Natural Language to SQL Query")
|
| 193 |
st.write("Ask questions about pizza sales data in plain English.")
|
| 194 |
|
| 195 |
-
#
|
| 196 |
-
|
| 197 |
-
st.write(db_message)
|
| 198 |
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
st.
|
| 202 |
-
|
|
|
|
| 203 |
|
| 204 |
# Initialize model
|
| 205 |
initialize_model()
|
|
|
|
| 10 |
from datetime import datetime
|
| 11 |
import time
|
| 12 |
import logging
|
| 13 |
+
import threading
|
| 14 |
|
| 15 |
# Set up logging
|
| 16 |
logging.basicConfig(
|
|
|
|
| 35 |
# Global variables for model and tokenizer
|
| 36 |
GLOBAL_MODEL = None
|
| 37 |
GLOBAL_TOKENIZER = None
|
| 38 |
+
db_connection_status = False
|
| 39 |
|
| 40 |
def initialize_model():
|
| 41 |
"""Initialize model and tokenizer globally"""
|
|
|
|
| 58 |
|
| 59 |
def test_db_connection():
|
| 60 |
"""Test database connection with timeout"""
|
| 61 |
+
global db_connection_status
|
| 62 |
try:
|
| 63 |
logging.info("Testing database connection...")
|
| 64 |
connection = mysql.connector.connect(
|
|
|
|
| 72 |
db_name = cursor.fetchone()[0]
|
| 73 |
cursor.close()
|
| 74 |
connection.close()
|
| 75 |
+
db_connection_status = True
|
| 76 |
logging.info(f"Successfully connected to MySQL Server version {db_info} - Database: {db_name}")
|
| 77 |
return True, f"Successfully connected to MySQL Server version {db_info}\nDatabase: {db_name}"
|
| 78 |
except Error as e:
|
| 79 |
+
db_connection_status = False
|
| 80 |
logging.error(f"Error connecting to MySQL database: {e}")
|
| 81 |
return False, f"Error connecting to MySQL database: {e}"
|
| 82 |
return False, "Unable to establish database connection"
|
|
|
|
| 192 |
|
| 193 |
return "\n".join(results)
|
| 194 |
|
| 195 |
+
def check_live_connection():
|
| 196 |
+
"""Check the database connection status periodically."""
|
| 197 |
+
while True:
|
| 198 |
+
test_db_connection()
|
| 199 |
+
time.sleep(10) # Check every 10 seconds
|
| 200 |
+
|
| 201 |
def main():
|
| 202 |
"""Main function with Streamlit UI components"""
|
| 203 |
st.title("Natural Language to SQL Query")
|
| 204 |
st.write("Ask questions about pizza sales data in plain English.")
|
| 205 |
|
| 206 |
+
# Start the live connection check in a separate thread
|
| 207 |
+
threading.Thread(target=check_live_connection, daemon=True).start()
|
|
|
|
| 208 |
|
| 209 |
+
# Test and display database connection status
|
| 210 |
+
if db_connection_status:
|
| 211 |
+
st.success("Database connection is live.")
|
| 212 |
+
else:
|
| 213 |
+
st.error("Database connection is not live.")
|
| 214 |
|
| 215 |
# Initialize model
|
| 216 |
initialize_model()
|