Fixing-1
Browse files
app.py
CHANGED
@@ -97,7 +97,7 @@ def generate_sql(question, table_headers):
|
|
97 |
print(error_msg)
|
98 |
return error_msg
|
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 |
|
@@ -113,29 +113,32 @@ def batch_generate_sql(questions_text, table_headers):
|
|
113 |
try:
|
114 |
# Parse questions
|
115 |
questions = [q.strip() for q in questions_text.split("\n") if q.strip()]
|
|
|
116 |
|
117 |
output = f"**Batch Results:**\n"
|
118 |
-
output += f"Total Queries: {
|
119 |
successful_count = 0
|
120 |
|
121 |
for i, question in enumerate(questions):
|
|
|
|
|
122 |
try:
|
123 |
start_time = time.time()
|
124 |
result = sql_generator.generate_sql(question, table_headers)
|
125 |
processing_time = time.time() - start_time
|
126 |
|
127 |
-
output += f"
|
128 |
output += f"```sql\n{result['sql_query']}\n```\n"
|
129 |
-
output += f"Model: {result['model_used']} | Time: {processing_time:.2f}s\n"
|
130 |
|
131 |
if result['status'] == 'success':
|
132 |
successful_count += 1
|
133 |
|
134 |
except Exception as e:
|
135 |
-
output += f"
|
136 |
-
output += f"❌ Error: {str(e)}\n"
|
137 |
|
138 |
-
output += f"
|
139 |
return output
|
140 |
|
141 |
except Exception as e:
|
@@ -177,15 +180,15 @@ Check the console/logs above for detailed initialization information.
|
|
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():
|
@@ -200,7 +203,7 @@ 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 |
-
generate_btn = gr.Button("
|
204 |
|
205 |
with gr.Column(scale=1):
|
206 |
output = gr.Markdown(label="Result")
|
@@ -228,7 +231,7 @@ with gr.Blocks(title="Text-to-SQL RAG with CodeLlama", theme=gr.themes.Soft()) a
|
|
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
|
232 |
generate_btn.click(
|
233 |
fn=generate_sql,
|
234 |
inputs=[question_input, table_headers_input],
|
@@ -245,7 +248,7 @@ with gr.Blocks(title="Text-to-SQL RAG with CodeLlama", theme=gr.themes.Soft()) a
|
|
245 |
|
246 |
health_btn.click(
|
247 |
fn=check_system_health,
|
248 |
-
inputs=
|
249 |
outputs=health_output,
|
250 |
api_name="check_health"
|
251 |
)
|
|
|
97 |
print(error_msg)
|
98 |
return error_msg
|
99 |
|
100 |
+
def batch_generate_sql(questions_text, table_headers, progress=gr.Progress()):
|
101 |
"""Generate SQL for multiple questions."""
|
102 |
print(f"batch_generate_sql called with: {questions_text}, {table_headers}")
|
103 |
|
|
|
113 |
try:
|
114 |
# Parse questions
|
115 |
questions = [q.strip() for q in questions_text.split("\n") if q.strip()]
|
116 |
+
total_questions = len(questions)
|
117 |
|
118 |
output = f"**Batch Results:**\n"
|
119 |
+
output += f"Total Queries: {total_questions}\n\n"
|
120 |
successful_count = 0
|
121 |
|
122 |
for i, question in enumerate(questions):
|
123 |
+
progress(i / total_questions, desc=f"Processing query {i+1}/{total_questions}")
|
124 |
+
|
125 |
try:
|
126 |
start_time = time.time()
|
127 |
result = sql_generator.generate_sql(question, table_headers)
|
128 |
processing_time = time.time() - start_time
|
129 |
|
130 |
+
output += f"**Query {i+1}:** {question}\n"
|
131 |
output += f"```sql\n{result['sql_query']}\n```\n"
|
132 |
+
output += f"Model: {result['model_used']} | Time: {processing_time:.2f}s\n\n"
|
133 |
|
134 |
if result['status'] == 'success':
|
135 |
successful_count += 1
|
136 |
|
137 |
except Exception as e:
|
138 |
+
output += f"**Query {i+1}:** {question}\n"
|
139 |
+
output += f"❌ Error: {str(e)}\n\n"
|
140 |
|
141 |
+
output += f"**Summary:** {successful_count}/{total_questions} queries successful"
|
142 |
return output
|
143 |
|
144 |
except Exception as e:
|
|
|
180 |
|
181 |
# Create Gradio interface
|
182 |
with gr.Blocks(title="Text-to-SQL RAG with CodeLlama", theme=gr.themes.Soft()) as demo:
|
183 |
+
gr.Markdown("# Text-to-SQL RAG with CodeLlama")
|
184 |
gr.Markdown("Generate SQL queries from natural language using **RAG (Retrieval-Augmented Generation)** and **CodeLlama** models.")
|
185 |
gr.Markdown("**Features:** RAG-enhanced generation, CodeLlama integration, Vector-based retrieval, Advanced prompt engineering")
|
186 |
|
187 |
# Add initialization status
|
188 |
if sql_generator is None:
|
189 |
+
gr.Markdown("⚠️ **Warning:** RAG system failed to initialize. Check the logs for errors.")
|
190 |
else:
|
191 |
+
gr.Markdown("✅ **Status:** RAG system initialized successfully!")
|
192 |
|
193 |
with gr.Tab("Single Query"):
|
194 |
with gr.Row():
|
|
|
203 |
placeholder="e.g., id, name, salary, department",
|
204 |
value="id, name, salary, department"
|
205 |
)
|
206 |
+
generate_btn = gr.Button("Generate SQL", variant="primary", size="lg")
|
207 |
|
208 |
with gr.Column(scale=1):
|
209 |
output = gr.Markdown(label="Result")
|
|
|
231 |
health_btn = gr.Button("Check System Health", variant="secondary", size="lg")
|
232 |
health_output = gr.Markdown(label="Health Status")
|
233 |
|
234 |
+
# Event handlers - Fixed with proper function binding
|
235 |
generate_btn.click(
|
236 |
fn=generate_sql,
|
237 |
inputs=[question_input, table_headers_input],
|
|
|
248 |
|
249 |
health_btn.click(
|
250 |
fn=check_system_health,
|
251 |
+
inputs=[],
|
252 |
outputs=health_output,
|
253 |
api_name="check_health"
|
254 |
)
|