Spaces:
Sleeping
Sleeping
fix
Browse files
app.py
CHANGED
|
@@ -51,80 +51,8 @@ if not OPENAI_API_KEY:
|
|
| 51 |
print("⚠️ Warning: OPENAI_API_KEY not found. OpenAI features will be disabled.")
|
| 52 |
|
| 53 |
# ------------------------------------------------------------
|
| 54 |
-
# DATABASE CONNECTION (AUTO-DETECTION)
|
| 55 |
-
# ------------------------------------------------------------
|
| 56 |
-
# print("🔍 Checking SQL Server connectivity...")
|
| 57 |
-
|
| 58 |
-
# # Try both ODBC Driver 17 and 18
|
| 59 |
-
# possible_drivers = ['{ODBC Driver 18 for SQL Server}', '{ODBC Driver 17 for SQL Server}']
|
| 60 |
-
# driver = None
|
| 61 |
-
# for d in possible_drivers:
|
| 62 |
-
# try:
|
| 63 |
-
# if d.strip('{}') in pyodbc.drivers():
|
| 64 |
-
# driver = d
|
| 65 |
-
# break
|
| 66 |
-
# except Exception:
|
| 67 |
-
# pass
|
| 68 |
-
|
| 69 |
-
# if not driver:
|
| 70 |
-
# driver = '{ODBC Driver 17 for SQL Server}'
|
| 71 |
-
# print("⚠️ Defaulting to ODBC Driver 17 for SQL Server")
|
| 72 |
-
|
| 73 |
-
# # Candidate SQL Server instances
|
| 74 |
-
# test_servers = [
|
| 75 |
-
# r'localhost\SQLEXPRESS',
|
| 76 |
-
# r'localhost\MSSQLSERVER',
|
| 77 |
-
# r'localhost',
|
| 78 |
-
# r'127.0.0.1'
|
| 79 |
-
# ]
|
| 80 |
-
|
| 81 |
-
# database = 'PyDetect'
|
| 82 |
-
|
| 83 |
-
# def get_db_connection():
|
| 84 |
-
# """Try multiple connection methods until success."""
|
| 85 |
-
# for s in test_servers:
|
| 86 |
-
# try:
|
| 87 |
-
# conn = pyodbc.connect(
|
| 88 |
-
# f'DRIVER={driver};SERVER={s};DATABASE={database};Trusted_Connection=yes;',
|
| 89 |
-
# timeout=3
|
| 90 |
-
# )
|
| 91 |
-
# print(f"✅ Connected to SQL Server instance: {s}")
|
| 92 |
-
# return conn
|
| 93 |
-
# except pyodbc.OperationalError:
|
| 94 |
-
# continue
|
| 95 |
-
# raise ConnectionError("❌ Cannot connect to any SQL Server instance. Please ensure SQL Server is running.")
|
| 96 |
-
|
| 97 |
-
# def create_user_table():
|
| 98 |
-
# try:
|
| 99 |
-
# conn = get_db_connection()
|
| 100 |
-
# cursor = conn.cursor()
|
| 101 |
-
# cursor.execute('''
|
| 102 |
-
# IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='Users' AND xtype='U')
|
| 103 |
-
# CREATE TABLE Users (
|
| 104 |
-
# id INT IDENTITY(1,1) PRIMARY KEY,
|
| 105 |
-
# name NVARCHAR(120) NOT NULL,
|
| 106 |
-
# role NVARCHAR(50) NOT NULL,
|
| 107 |
-
# email NVARCHAR(120) UNIQUE NOT NULL,
|
| 108 |
-
# password NVARCHAR(255) NOT NULL
|
| 109 |
-
# )
|
| 110 |
-
# ''')
|
| 111 |
-
# conn.commit()
|
| 112 |
-
# cursor.close()
|
| 113 |
-
# conn.close()
|
| 114 |
-
# print("✅ Users table verified/created successfully.")
|
| 115 |
-
# except Exception as e:
|
| 116 |
-
# print(f"❌ Database setup failed: {str(e)}")
|
| 117 |
-
|
| 118 |
-
# create_user_table()
|
| 119 |
-
|
| 120 |
-
USE_DATABASE = os.getenv("USE_DATABASE", "1") == "1"
|
| 121 |
-
|
| 122 |
-
# DB_HOST = os.getenv("DB_HOST", r"localhost")
|
| 123 |
-
# DB_PORT = int(os.getenv("DB_PORT", "1433"))
|
| 124 |
-
# DB_NAME = os.getenv("DB_NAME", "PyDetect")
|
| 125 |
-
# DB_USER = os.getenv("DB_USER", "")
|
| 126 |
-
# DB_PASSWORD = os.getenv("DB_PASSWORD", "")
|
| 127 |
|
|
|
|
| 128 |
USE_DATABASE = os.getenv("USE_DATABASE", "1") == "1"
|
| 129 |
|
| 130 |
DB_HOST = os.getenv("DB_HOST") or os.getenv("RDS_SQL_SERVER", r"localhost")
|
|
@@ -160,6 +88,7 @@ def get_db_connection():
|
|
| 160 |
timeout=30,
|
| 161 |
as_dict=False # we access rows by index
|
| 162 |
)
|
|
|
|
| 163 |
return conn
|
| 164 |
except Exception as e:
|
| 165 |
print(f"❌ Could not connect to SQL Server at {DB_HOST}:{DB_PORT} - {e}")
|
|
@@ -487,7 +416,7 @@ def _normalize_epoch_to_seconds(value):
|
|
| 487 |
return None
|
| 488 |
v = float(value)
|
| 489 |
# Heuristic: treat large values as ms
|
| 490 |
-
if v > 1e11:
|
| 491 |
return v / 1000.0
|
| 492 |
return v
|
| 493 |
except Exception:
|
|
|
|
| 51 |
print("⚠️ Warning: OPENAI_API_KEY not found. OpenAI features will be disabled.")
|
| 52 |
|
| 53 |
# ------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
MODE = os.getenv("MODE", "local").lower()
|
| 56 |
USE_DATABASE = os.getenv("USE_DATABASE", "1") == "1"
|
| 57 |
|
| 58 |
DB_HOST = os.getenv("DB_HOST") or os.getenv("RDS_SQL_SERVER", r"localhost")
|
|
|
|
| 88 |
timeout=30,
|
| 89 |
as_dict=False # we access rows by index
|
| 90 |
)
|
| 91 |
+
print(f"✅ Connected to SQL Server {DB_HOST}:{DB_PORT}")
|
| 92 |
return conn
|
| 93 |
except Exception as e:
|
| 94 |
print(f"❌ Could not connect to SQL Server at {DB_HOST}:{DB_PORT} - {e}")
|
|
|
|
| 416 |
return None
|
| 417 |
v = float(value)
|
| 418 |
# Heuristic: treat large values as ms
|
| 419 |
+
if v > 1e11:
|
| 420 |
return v / 1000.0
|
| 421 |
return v
|
| 422 |
except Exception:
|