khanhamzawiser commited on
Commit
2c1e198
·
verified ·
1 Parent(s): c2c63c7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -5
app.py CHANGED
@@ -2,13 +2,16 @@ import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import psycopg2
4
  import os
5
- import re
 
 
 
 
6
 
7
  # Hugging Face Zephyr model
8
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
9
 
10
  # TimescaleDB config (via Hugging Face Space secrets)
11
- #connected
12
  DB_CONFIG = {
13
  "host": os.getenv("localhost"),
14
  "port": os.getenv("DB_PORT", 5434),
@@ -17,14 +20,19 @@ DB_CONFIG = {
17
  "password": os.getenv("password"),
18
  }
19
 
20
- # Query TimescaleDB
21
  def query_timescaledb(sql_query):
22
  try:
 
23
  with psycopg2.connect(**DB_CONFIG) as conn:
 
24
  with conn.cursor() as cur:
25
  cur.execute(sql_query)
26
- return cur.fetchall()
 
 
27
  except Exception as e:
 
28
  return f"DB Error: {e}"
29
 
30
  # Basic pattern matching to route question types
@@ -45,7 +53,6 @@ def get_sql_for_question(message):
45
  AND created_at >= NOW() - INTERVAL '1 day';
46
  """, "Here is the total number of machines that are not faulty today:"
47
 
48
-
49
  elif "total current" in message:
50
  return """
51
  SELECT created_at, total_current FROM machine_current_log
 
2
  from huggingface_hub import InferenceClient
3
  import psycopg2
4
  import os
5
+ import logging
6
+
7
+ # Set up logging
8
+ logging.basicConfig(level=logging.DEBUG)
9
+ logger = logging.getLogger(__name__)
10
 
11
  # Hugging Face Zephyr model
12
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
13
 
14
  # TimescaleDB config (via Hugging Face Space secrets)
 
15
  DB_CONFIG = {
16
  "host": os.getenv("localhost"),
17
  "port": os.getenv("DB_PORT", 5434),
 
20
  "password": os.getenv("password"),
21
  }
22
 
23
+ # Query TimescaleDB with improved error handling
24
  def query_timescaledb(sql_query):
25
  try:
26
+ logger.debug("Attempting to connect to the database...")
27
  with psycopg2.connect(**DB_CONFIG) as conn:
28
+ logger.debug("Connected to the database successfully.")
29
  with conn.cursor() as cur:
30
  cur.execute(sql_query)
31
+ result = cur.fetchall()
32
+ logger.debug("Query executed successfully.")
33
+ return result
34
  except Exception as e:
35
+ logger.error(f"DB Error: {e}")
36
  return f"DB Error: {e}"
37
 
38
  # Basic pattern matching to route question types
 
53
  AND created_at >= NOW() - INTERVAL '1 day';
54
  """, "Here is the total number of machines that are not faulty today:"
55
 
 
56
  elif "total current" in message:
57
  return """
58
  SELECT created_at, total_current FROM machine_current_log