Fixing button
Browse files
app.py
CHANGED
|
@@ -57,9 +57,17 @@ except Exception as e:
|
|
| 57 |
|
| 58 |
def generate_sql(question, table_headers):
|
| 59 |
"""Generate SQL using the RAG system directly."""
|
|
|
|
|
|
|
| 60 |
if sql_generator is None:
|
| 61 |
return "β Error: RAG system not initialized. Check the logs for initialization errors."
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
try:
|
| 64 |
print(f"Generating SQL for: {question}")
|
| 65 |
print(f"Table headers: {table_headers}")
|
|
@@ -91,9 +99,17 @@ def generate_sql(question, table_headers):
|
|
| 91 |
|
| 92 |
def batch_generate_sql(questions_text, table_headers):
|
| 93 |
"""Generate SQL for multiple questions."""
|
|
|
|
|
|
|
| 94 |
if sql_generator is None:
|
| 95 |
return "β Error: RAG system not initialized. Check the logs for initialization errors."
|
| 96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
try:
|
| 98 |
# Parse questions
|
| 99 |
questions = [q.strip() for q in questions_text.split("\n") if q.strip()]
|
|
@@ -127,6 +143,8 @@ def batch_generate_sql(questions_text, table_headers):
|
|
| 127 |
|
| 128 |
def check_system_health():
|
| 129 |
"""Check the health of the RAG system."""
|
|
|
|
|
|
|
| 130 |
try:
|
| 131 |
if sql_generator is None:
|
| 132 |
return "β System Status: RAG system not initialized\n\nCheck the logs above for initialization errors."
|
|
@@ -159,15 +177,15 @@ Check the console/logs above for detailed initialization information.
|
|
| 159 |
|
| 160 |
# Create Gradio interface
|
| 161 |
with gr.Blocks(title="Text-to-SQL RAG with CodeLlama", theme=gr.themes.Soft()) as demo:
|
| 162 |
-
gr.Markdown("#
|
| 163 |
gr.Markdown("Generate SQL queries from natural language using **RAG (Retrieval-Augmented Generation)** and **CodeLlama** models.")
|
| 164 |
gr.Markdown("**Features:** RAG-enhanced generation, CodeLlama integration, Vector-based retrieval, Advanced prompt engineering")
|
| 165 |
|
| 166 |
# Add initialization status
|
| 167 |
if sql_generator is None:
|
| 168 |
-
gr.Markdown("
|
| 169 |
else:
|
| 170 |
-
gr.Markdown("
|
| 171 |
|
| 172 |
with gr.Tab("Single Query"):
|
| 173 |
with gr.Row():
|
|
@@ -200,44 +218,48 @@ with gr.Blocks(title="Text-to-SQL RAG with CodeLlama", theme=gr.themes.Soft()) a
|
|
| 200 |
placeholder="e.g., id, name, salary, department",
|
| 201 |
value="id, name, salary, department"
|
| 202 |
)
|
| 203 |
-
batch_btn = gr.Button("
|
| 204 |
|
| 205 |
with gr.Column(scale=1):
|
| 206 |
batch_output = gr.Markdown(label="Batch Results")
|
| 207 |
|
| 208 |
with gr.Tab("System Health"):
|
| 209 |
with gr.Row():
|
| 210 |
-
health_btn = gr.Button("
|
| 211 |
health_output = gr.Markdown(label="Health Status")
|
| 212 |
|
| 213 |
-
# Event handlers
|
| 214 |
generate_btn.click(
|
| 215 |
-
generate_sql,
|
| 216 |
inputs=[question_input, table_headers_input],
|
| 217 |
-
outputs=output
|
|
|
|
| 218 |
)
|
| 219 |
|
| 220 |
batch_btn.click(
|
| 221 |
-
batch_generate_sql,
|
| 222 |
inputs=[batch_questions, batch_headers],
|
| 223 |
-
outputs=batch_output
|
|
|
|
| 224 |
)
|
| 225 |
|
| 226 |
health_btn.click(
|
| 227 |
-
check_system_health,
|
| 228 |
-
|
|
|
|
|
|
|
| 229 |
)
|
| 230 |
|
| 231 |
gr.Markdown("---")
|
| 232 |
gr.Markdown("""
|
| 233 |
-
##
|
| 234 |
|
| 235 |
1. **RAG System**: Retrieves relevant SQL examples from vector database
|
| 236 |
2. **CodeLlama**: Generates SQL using retrieved examples as context
|
| 237 |
3. **Vector Search**: Finds similar questions and their SQL solutions
|
| 238 |
4. **Enhanced Generation**: Combines retrieval + generation for better accuracy
|
| 239 |
|
| 240 |
-
##
|
| 241 |
|
| 242 |
- **Backend**: Direct RAG system integration
|
| 243 |
- **LLM**: CodeLlama-7B-Python-GGUF (primary)
|
|
@@ -245,7 +267,7 @@ with gr.Blocks(title="Text-to-SQL RAG with CodeLlama", theme=gr.themes.Soft()) a
|
|
| 245 |
- **Frontend**: Gradio interface
|
| 246 |
- **Hosting**: Hugging Face Spaces
|
| 247 |
|
| 248 |
-
##
|
| 249 |
|
| 250 |
- **Model**: CodeLlama-7B-Python-GGUF
|
| 251 |
- **Response Time**: < 5 seconds
|
|
@@ -255,4 +277,4 @@ with gr.Blocks(title="Text-to-SQL RAG with CodeLlama", theme=gr.themes.Soft()) a
|
|
| 255 |
|
| 256 |
# Launch the interface
|
| 257 |
if __name__ == "__main__":
|
| 258 |
-
demo.launch()
|
|
|
|
| 57 |
|
| 58 |
def generate_sql(question, table_headers):
|
| 59 |
"""Generate SQL using the RAG system directly."""
|
| 60 |
+
print(f"generate_sql called with: {question}, {table_headers}")
|
| 61 |
+
|
| 62 |
if sql_generator is None:
|
| 63 |
return "β Error: RAG system not initialized. Check the logs for initialization errors."
|
| 64 |
|
| 65 |
+
if not question or not question.strip():
|
| 66 |
+
return "β Error: Please enter a question."
|
| 67 |
+
|
| 68 |
+
if not table_headers or not table_headers.strip():
|
| 69 |
+
return "β Error: Please enter table headers."
|
| 70 |
+
|
| 71 |
try:
|
| 72 |
print(f"Generating SQL for: {question}")
|
| 73 |
print(f"Table headers: {table_headers}")
|
|
|
|
| 99 |
|
| 100 |
def batch_generate_sql(questions_text, table_headers):
|
| 101 |
"""Generate SQL for multiple questions."""
|
| 102 |
+
print(f"batch_generate_sql called with: {questions_text}, {table_headers}")
|
| 103 |
+
|
| 104 |
if sql_generator is None:
|
| 105 |
return "β Error: RAG system not initialized. Check the logs for initialization errors."
|
| 106 |
|
| 107 |
+
if not questions_text or not questions_text.strip():
|
| 108 |
+
return "β Error: Please enter questions."
|
| 109 |
+
|
| 110 |
+
if not table_headers or not table_headers.strip():
|
| 111 |
+
return "β Error: Please enter table headers."
|
| 112 |
+
|
| 113 |
try:
|
| 114 |
# Parse questions
|
| 115 |
questions = [q.strip() for q in questions_text.split("\n") if q.strip()]
|
|
|
|
| 143 |
|
| 144 |
def check_system_health():
|
| 145 |
"""Check the health of the RAG system."""
|
| 146 |
+
print("check_system_health called")
|
| 147 |
+
|
| 148 |
try:
|
| 149 |
if sql_generator is None:
|
| 150 |
return "β System Status: RAG system not initialized\n\nCheck the logs above for initialization errors."
|
|
|
|
| 177 |
|
| 178 |
# Create Gradio interface
|
| 179 |
with gr.Blocks(title="Text-to-SQL RAG with CodeLlama", theme=gr.themes.Soft()) as demo:
|
| 180 |
+
gr.Markdown("#Text-to-SQL RAG with CodeLlama")
|
| 181 |
gr.Markdown("Generate SQL queries from natural language using **RAG (Retrieval-Augmented Generation)** and **CodeLlama** models.")
|
| 182 |
gr.Markdown("**Features:** RAG-enhanced generation, CodeLlama integration, Vector-based retrieval, Advanced prompt engineering")
|
| 183 |
|
| 184 |
# Add initialization status
|
| 185 |
if sql_generator is None:
|
| 186 |
+
gr.Markdown("**Warning:** RAG system failed to initialize. Check the logs for errors.")
|
| 187 |
else:
|
| 188 |
+
gr.Markdown("**Status:** RAG system initialized successfully!")
|
| 189 |
|
| 190 |
with gr.Tab("Single Query"):
|
| 191 |
with gr.Row():
|
|
|
|
| 218 |
placeholder="e.g., id, name, salary, department",
|
| 219 |
value="id, name, salary, department"
|
| 220 |
)
|
| 221 |
+
batch_btn = gr.Button("Generate Batch SQL", variant="primary", size="lg")
|
| 222 |
|
| 223 |
with gr.Column(scale=1):
|
| 224 |
batch_output = gr.Markdown(label="Batch Results")
|
| 225 |
|
| 226 |
with gr.Tab("System Health"):
|
| 227 |
with gr.Row():
|
| 228 |
+
health_btn = gr.Button("Check System Health", variant="secondary", size="lg")
|
| 229 |
health_output = gr.Markdown(label="Health Status")
|
| 230 |
|
| 231 |
+
# Event handlers - Fixed with explicit function calls
|
| 232 |
generate_btn.click(
|
| 233 |
+
fn=generate_sql,
|
| 234 |
inputs=[question_input, table_headers_input],
|
| 235 |
+
outputs=output,
|
| 236 |
+
api_name="generate_sql"
|
| 237 |
)
|
| 238 |
|
| 239 |
batch_btn.click(
|
| 240 |
+
fn=batch_generate_sql,
|
| 241 |
inputs=[batch_questions, batch_headers],
|
| 242 |
+
outputs=batch_output,
|
| 243 |
+
api_name="batch_generate_sql"
|
| 244 |
)
|
| 245 |
|
| 246 |
health_btn.click(
|
| 247 |
+
fn=check_system_health,
|
| 248 |
+
inputs=None,
|
| 249 |
+
outputs=health_output,
|
| 250 |
+
api_name="check_health"
|
| 251 |
)
|
| 252 |
|
| 253 |
gr.Markdown("---")
|
| 254 |
gr.Markdown("""
|
| 255 |
+
## How It Works
|
| 256 |
|
| 257 |
1. **RAG System**: Retrieves relevant SQL examples from vector database
|
| 258 |
2. **CodeLlama**: Generates SQL using retrieved examples as context
|
| 259 |
3. **Vector Search**: Finds similar questions and their SQL solutions
|
| 260 |
4. **Enhanced Generation**: Combines retrieval + generation for better accuracy
|
| 261 |
|
| 262 |
+
## Technology Stack
|
| 263 |
|
| 264 |
- **Backend**: Direct RAG system integration
|
| 265 |
- **LLM**: CodeLlama-7B-Python-GGUF (primary)
|
|
|
|
| 267 |
- **Frontend**: Gradio interface
|
| 268 |
- **Hosting**: Hugging Face Spaces
|
| 269 |
|
| 270 |
+
## Performance
|
| 271 |
|
| 272 |
- **Model**: CodeLlama-7B-Python-GGUF
|
| 273 |
- **Response Time**: < 5 seconds
|
|
|
|
| 277 |
|
| 278 |
# Launch the interface
|
| 279 |
if __name__ == "__main__":
|
| 280 |
+
demo.launch()
|