barathvasan-dev commited on
Commit
35c6090
Β·
1 Parent(s): acf4948

Improve: Add configuration checks, environment validation, and better error messages for missing HF_TOKEN and DATABASE_URL

Browse files
Files changed (3) hide show
  1. .env.example +23 -0
  2. app.py +44 -1
  3. database.py +31 -6
.env.example ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # =========================================================
2
+ # ENVIRONMENT CONFIGURATION
3
+ # =========================================================
4
+
5
+ # Hugging Face API Token (required for Mistral LLM features)
6
+ # Get it from: https://huggingface.co/settings/tokens
7
+ HF_TOKEN=your_hf_token_here
8
+
9
+ # PostgreSQL Database URL (required for database features)
10
+ # Format: postgresql://username:password@host:port/database
11
+ # Example: postgresql://user:pass@localhost:5432/vehicle_logs
12
+ DATABASE_URL=postgresql://username:password@host:port/database_name
13
+
14
+ # =========================================================
15
+ # HOW TO SET UP IN HUGGING FACE SPACES
16
+ # =========================================================
17
+ #
18
+ # 1. Go to your Space Settings
19
+ # 2. Click "Repository secrets"
20
+ # 3. Add HF_TOKEN and DATABASE_URL as secrets
21
+ # 4. Restart the Space
22
+ #
23
+ # =========================================================
app.py CHANGED
@@ -16,7 +16,11 @@ from database import (
16
  get_vehicles_by_state,
17
  get_hourly_traffic,
18
  get_top_plates,
19
- get_suspicious_vehicles
 
 
 
 
20
  )
21
 
22
  # =========================================================
@@ -279,6 +283,45 @@ AI-powered Vehicle Detection + NLP-to-SQL Intelligence Platform
279
  else:
280
  gr.Warning(msg)
281
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
282
  # =====================================================
283
  # TAB 1 - DETECTION
284
  # =====================================================
 
16
  get_vehicles_by_state,
17
  get_hourly_traffic,
18
  get_top_plates,
19
+ get_suspicious_vehicles,
20
+ HF_TOKEN,
21
+ DATABASE_URL,
22
+ client,
23
+ engine
24
  )
25
 
26
  # =========================================================
 
283
  else:
284
  gr.Warning(msg)
285
 
286
+ # =====================================================
287
+ # CONFIGURATION STATUS
288
+ # =====================================================
289
+
290
+ gr.Markdown("### πŸ”§ Configuration Status")
291
+
292
+ config_status = []
293
+
294
+ if HF_TOKEN:
295
+ gr.Success("βœ… Mistral LLM Configured - NLP features enabled")
296
+ else:
297
+ gr.Warning("❌ HF_TOKEN missing - NLP/AI features disabled. Add HF_TOKEN to Space Secrets")
298
+ config_status.append("HF_TOKEN")
299
+
300
+ if DATABASE_URL:
301
+ gr.Success("βœ… Database Configured - Query features enabled")
302
+ else:
303
+ gr.Warning("❌ DATABASE_URL missing - Database features disabled. Add DATABASE_URL to Space Secrets")
304
+ config_status.append("DATABASE_URL")
305
+
306
+ if config_status:
307
+ with gr.Group():
308
+ gr.Markdown(f"""
309
+ ### βš™οΈ Setup Instructions
310
+
311
+ Your Space is **missing these environment variables**:
312
+ - **{', '.join(config_status)}**
313
+
314
+ **To fix this:**
315
+ 1. Go to Space Settings β†’ Repository secrets
316
+ 2. Add the following variables:
317
+ - `HF_TOKEN`: Get from https://huggingface.co/settings/tokens
318
+ - `DATABASE_URL`: Your PostgreSQL connection string
319
+ 3. Restart the Space
320
+
321
+ Without these, NLP queries and database operations won't work.
322
+ """)
323
+
324
+
325
  # =====================================================
326
  # TAB 1 - DETECTION
327
  # =====================================================
database.py CHANGED
@@ -22,15 +22,28 @@ HF_TOKEN = os.getenv("HF_TOKEN")
22
  DATABASE_URL = os.getenv("DATABASE_URL")
23
 
24
  # Initialize Mistral client
25
- client = InferenceClient(
26
- model="mistralai/Mistral-7B-Instruct-v0.2",
27
- token=HF_TOKEN
28
- )
 
 
 
 
 
 
 
 
 
29
 
30
  # Initialize database engine
 
31
  try:
32
- engine = create_engine(DATABASE_URL)
33
- print("βœ… Database connection initialized")
 
 
 
34
  except Exception as e:
35
  print(f"⚠️ Database connection warning: {e}")
36
  engine = None
@@ -659,6 +672,10 @@ Generate PostgreSQL SQL query for:
659
 
660
  try:
661
 
 
 
 
 
662
  response = client.chat_completion(
663
  messages=[
664
  {
@@ -729,6 +746,14 @@ def run_query(user_query):
729
  print(sql)
730
  print("==============================")
731
 
 
 
 
 
 
 
 
 
732
  with engine.connect() as conn:
733
 
734
  result = conn.execute(text(sql))
 
22
  DATABASE_URL = os.getenv("DATABASE_URL")
23
 
24
  # Initialize Mistral client
25
+ client = None
26
+ try:
27
+ if HF_TOKEN:
28
+ client = InferenceClient(
29
+ model="mistralai/Mistral-7B-Instruct-v0.2",
30
+ token=HF_TOKEN
31
+ )
32
+ print("βœ… Mistral client initialized")
33
+ else:
34
+ print("⚠️ HF_TOKEN not set - LLM features disabled")
35
+ except Exception as e:
36
+ print(f"⚠️ Mistral client error: {e}")
37
+ client = None
38
 
39
  # Initialize database engine
40
+ engine = None
41
  try:
42
+ if DATABASE_URL:
43
+ engine = create_engine(DATABASE_URL)
44
+ print("βœ… Database connection initialized")
45
+ else:
46
+ print("⚠️ DATABASE_URL not set - Database features disabled")
47
  except Exception as e:
48
  print(f"⚠️ Database connection warning: {e}")
49
  engine = None
 
672
 
673
  try:
674
 
675
+ if client is None:
676
+ print("❌ Mistral client not initialized - HF_TOKEN missing")
677
+ raise Exception("LLM service unavailable - HF_TOKEN not configured")
678
+
679
  response = client.chat_completion(
680
  messages=[
681
  {
 
746
  print(sql)
747
  print("==============================")
748
 
749
+ if engine is None:
750
+ return {
751
+ "query": user_query,
752
+ "error": "❌ Database not configured - DATABASE_URL missing",
753
+ "sql": sql,
754
+ "result": []
755
+ }
756
+
757
  with engine.connect() as conn:
758
 
759
  result = conn.execute(text(sql))