Al1Abdullah commited on
Commit
de187e0
·
verified ·
1 Parent(s): 89c070a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -3
app.py CHANGED
@@ -19,6 +19,10 @@ default_db_config = {
19
  'port': int(os.getenv('DB_PORT', 4000)) # Default to TiDB port
20
  }
21
 
 
 
 
 
22
  # Groq API configuration with error handling
23
  try:
24
  groq_client = Groq(api_key=os.getenv('GROQ_API_KEY'))
@@ -33,16 +37,21 @@ current_summary = {}
33
 
34
  def get_db_connection(db_name=None):
35
  """Establish a database connection using session or default config."""
 
36
  config = session.get('db_config', default_db_config).copy()
37
- if not config['host'] or not config['user'] or not config['password']:
38
- return None, "No valid MySQL credentials provided. Use the 'Configure MySQL Connection' modal to set up your own database, or contact the app owner to ensure default credentials are set."
 
 
 
 
39
  if db_name:
40
  config['database'] = db_name
41
  try:
42
  conn = mysql.connector.connect(**config)
43
  return conn, None
44
  except Error as e:
45
- return None, f"Database connection failed: {str(e)}. Ensure the MySQL server is running, accessible, and credentials are correct."
46
 
47
  def parse_sql_file(file_content):
48
  """Parse SQL file to extract database name and clean statements."""
@@ -329,5 +338,11 @@ def configure_db():
329
  conn.close()
330
  return render_template('index.html', error=None, schema=current_schema, summary=current_summary, success="Custom MySQL connection configured successfully. You can now upload .sql files and query your database.")
331
 
 
 
 
 
 
 
332
  if __name__ == '__main__':
333
  app.run(host='0.0.0.0', port=int(os.getenv('PORT', 7860)), debug=False)
 
19
  'port': int(os.getenv('DB_PORT', 4000)) # Default to TiDB port
20
  }
21
 
22
+ # Validate default config at startup
23
+ if not all([default_db_config['host'], default_db_config['user'], default_db_config['password']]):
24
+ print("Warning: Incomplete default MySQL credentials in Secrets. Default connection may fail.")
25
+
26
  # Groq API configuration with error handling
27
  try:
28
  groq_client = Groq(api_key=os.getenv('GROQ_API_KEY'))
 
37
 
38
  def get_db_connection(db_name=None):
39
  """Establish a database connection using session or default config."""
40
+ # Try session config first, but only if complete
41
  config = session.get('db_config', default_db_config).copy()
42
+ if not all([config.get('host'), config.get('user'), config.get('password')]):
43
+ # Fallback to default_db_config if session config is incomplete
44
+ config = default_db_config.copy()
45
+ if not all([config.get('host'), config.get('user'), config.get('password')]):
46
+ return None, "No valid MySQL credentials provided. Ensure Secrets (DB_HOST, DB_USER, DB_PASSWORD, DB_PORT) are set correctly in Hugging Face Space settings, or use the 'Configure MySQL Connection' modal."
47
+
48
  if db_name:
49
  config['database'] = db_name
50
  try:
51
  conn = mysql.connector.connect(**config)
52
  return conn, None
53
  except Error as e:
54
+ return None, f"Database connection failed: {str(e)}. Verify credentials, ensure the MySQL/TiDB server is running, and check network settings (e.g., IP whitelist, TLS)."
55
 
56
  def parse_sql_file(file_content):
57
  """Parse SQL file to extract database name and clean statements."""
 
338
  conn.close()
339
  return render_template('index.html', error=None, schema=current_schema, summary=current_summary, success="Custom MySQL connection configured successfully. You can now upload .sql files and query your database.")
340
 
341
+ @app.route('/reset_db', methods=['POST'])
342
+ def reset_db():
343
+ """Reset to default TiDB Serverless connection."""
344
+ session.pop('db_config', None)
345
+ return render_template('index.html', error=None, schema=current_schema, summary=current_summary, success="Reverted to default TiDB Serverless connection. You can now upload .sql files using the default database.")
346
+
347
  if __name__ == '__main__':
348
  app.run(host='0.0.0.0', port=int(os.getenv('PORT', 7860)), debug=False)