Sazzz02 commited on
Commit
c2dcbda
·
verified ·
1 Parent(s): b138760

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -7
app.py CHANGED
@@ -18,7 +18,10 @@ warnings.filterwarnings('ignore')
18
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
19
 
20
  if not GROQ_API_KEY:
21
- raise ValueError("GROQ_API_KEY environment variable not set. Please add it to your Hugging Face Space secrets.")
 
 
 
22
 
23
  # ------------------------------
24
  # SQL Converter Using Groq API
@@ -26,9 +29,19 @@ if not GROQ_API_KEY:
26
 
27
  class EnhancedNL2SQLConverter:
28
  def __init__(self, model_name: str = "llama3-70b-8192"):
29
- self.client = Groq(api_key=GROQ_API_KEY)
30
  self.model_name = model_name
31
- print(f"Using Groq API with model: {self.model_name}")
 
 
 
 
 
 
 
 
 
 
 
32
 
33
  self.default_schema = """
34
  Table: employees
@@ -43,6 +56,10 @@ class EnhancedNL2SQLConverter:
43
 
44
  def generate_sql(self, query: str, schema: str = None) -> str:
45
  try:
 
 
 
 
46
  schema_to_use = schema or self.default_schema
47
 
48
  system_prompt = """You are an expert SQL query generator. Convert natural language questions to SQL queries based on the provided database schema.
@@ -157,10 +174,17 @@ class SQLEvaluator:
157
  return False, str(e)
158
 
159
  # ------------------------------
160
- # Initialize components
161
  # ------------------------------
162
- converter = EnhancedNL2SQLConverter()
163
- evaluator = SQLEvaluator()
 
 
 
 
 
 
 
164
 
165
  # ------------------------------
166
  # Gradio Interface Functions
@@ -172,11 +196,15 @@ def process_nl_query(nl_query: str) -> Tuple[str, str, str]:
172
  return "", "", "Please enter a natural language query."
173
 
174
  try:
 
 
 
 
175
  # Generate SQL
176
  generated_sql = converter.generate_sql(nl_query)
177
 
178
  if generated_sql.startswith("ERROR"):
179
- return generated_sql, "", "Failed to generate SQL query."
180
 
181
  # Execute SQL
182
  success, result = evaluator.execute_sql(generated_sql)
 
18
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
19
 
20
  if not GROQ_API_KEY:
21
+ print("⚠️ WARNING: GROQ_API_KEY environment variable not set!")
22
+ print("Please add your Groq API key to your Hugging Face Space secrets.")
23
+ print("For demo purposes, the app will continue but API calls will fail.")
24
+ GROQ_API_KEY = "dummy-key-for-demo"
25
 
26
  # ------------------------------
27
  # SQL Converter Using Groq API
 
29
 
30
  class EnhancedNL2SQLConverter:
31
  def __init__(self, model_name: str = "llama3-70b-8192"):
 
32
  self.model_name = model_name
33
+ self.client = None
34
+
35
+ try:
36
+ # Initialize Groq client with proper error handling
37
+ if GROQ_API_KEY and GROQ_API_KEY != "dummy-key-for-demo":
38
+ self.client = Groq(api_key=GROQ_API_KEY)
39
+ print(f"✅ Successfully initialized Groq client with model: {self.model_name}")
40
+ else:
41
+ print("⚠️ Groq client not initialized - API key missing")
42
+ except Exception as e:
43
+ print(f"❌ Error initializing Groq client: {str(e)}")
44
+ self.client = None
45
 
46
  self.default_schema = """
47
  Table: employees
 
56
 
57
  def generate_sql(self, query: str, schema: str = None) -> str:
58
  try:
59
+ # Check if client is properly initialized
60
+ if not self.client:
61
+ return "ERROR: Groq API client not initialized. Please check your API key."
62
+
63
  schema_to_use = schema or self.default_schema
64
 
65
  system_prompt = """You are an expert SQL query generator. Convert natural language questions to SQL queries based on the provided database schema.
 
174
  return False, str(e)
175
 
176
  # ------------------------------
177
+ # Initialize components with error handling
178
  # ------------------------------
179
+ try:
180
+ converter = EnhancedNL2SQLConverter()
181
+ evaluator = SQLEvaluator()
182
+ print("✅ Application components initialized successfully")
183
+ except Exception as e:
184
+ print(f"❌ Error initializing components: {str(e)}")
185
+ # Create dummy components for graceful degradation
186
+ converter = None
187
+ evaluator = SQLEvaluator() # Database component should still work
188
 
189
  # ------------------------------
190
  # Gradio Interface Functions
 
196
  return "", "", "Please enter a natural language query."
197
 
198
  try:
199
+ # Check if converter is available
200
+ if not converter:
201
+ return "", "", "❌ Error: SQL converter not initialized. Please check API configuration."
202
+
203
  # Generate SQL
204
  generated_sql = converter.generate_sql(nl_query)
205
 
206
  if generated_sql.startswith("ERROR"):
207
+ return generated_sql, "", "Failed to generate SQL query. Please check your API key."
208
 
209
  # Execute SQL
210
  success, result = evaluator.execute_sql(generated_sql)