Spaces:
Sleeping
Sleeping
Add demo mode support - enable API without Groq client
Browse files
main.py
CHANGED
|
@@ -65,6 +65,10 @@ class QueryResponse(BaseModel):
|
|
| 65 |
def generate_sql_with_groq(question: str) -> tuple:
|
| 66 |
"""Generate SQL using Groq AI"""
|
| 67 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
# Sample database schema
|
| 69 |
schema = """
|
| 70 |
Database Schema:
|
|
@@ -104,6 +108,9 @@ SQL Query:"""
|
|
| 104 |
def explain_sql_with_groq(sql: str, question: str) -> str:
|
| 105 |
"""Generate explanation for SQL query"""
|
| 106 |
try:
|
|
|
|
|
|
|
|
|
|
| 107 |
prompt = f"""Explain this SQL query in simple terms:
|
| 108 |
|
| 109 |
Original Question: {question}
|
|
@@ -160,14 +167,6 @@ async def process_query(request: QueryRequest):
|
|
| 160 |
session_id=session_id
|
| 161 |
)
|
| 162 |
|
| 163 |
-
# Check if Groq client initialized successfully
|
| 164 |
-
if not groq_client:
|
| 165 |
-
return QueryResponse(
|
| 166 |
-
success=False,
|
| 167 |
-
error="Groq client initialization failed. Running in limited mode.",
|
| 168 |
-
session_id=session_id
|
| 169 |
-
)try:
|
| 170 |
-
# Generate SQL using Groq
|
| 171 |
sql, error = generate_sql_with_groq(request.question)
|
| 172 |
|
| 173 |
if error:
|
|
|
|
| 65 |
def generate_sql_with_groq(question: str) -> tuple:
|
| 66 |
"""Generate SQL using Groq AI"""
|
| 67 |
try:
|
| 68 |
+
# Return demo SQL if Groq client is not available
|
| 69 |
+
if not groq_client:
|
| 70 |
+
demo_sql = "SELECT c.name, SUM(o.total) as order_total FROM customers c JOIN orders o ON c.id = o.customer_id GROUP BY c.name ORDER BY order_total DESC"
|
| 71 |
+
return demo_sql, None
|
| 72 |
# Sample database schema
|
| 73 |
schema = """
|
| 74 |
Database Schema:
|
|
|
|
| 108 |
def explain_sql_with_groq(sql: str, question: str) -> str:
|
| 109 |
"""Generate explanation for SQL query"""
|
| 110 |
try:
|
| 111 |
+
# Return demo explanation if Groq client is not available
|
| 112 |
+
if not groq_client:
|
| 113 |
+
return "This query retrieves data from the database. Full AI explanation unavailable in demo mode."
|
| 114 |
prompt = f"""Explain this SQL query in simple terms:
|
| 115 |
|
| 116 |
Original Question: {question}
|
|
|
|
| 167 |
session_id=session_id
|
| 168 |
)
|
| 169 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
sql, error = generate_sql_with_groq(request.question)
|
| 171 |
|
| 172 |
if error:
|