Fixing generate
Browse files
app.py
CHANGED
|
@@ -1,37 +1,77 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import time
|
| 3 |
import json
|
|
|
|
| 4 |
|
| 5 |
# Import RAG system components
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
from rag_system.
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# Initialize RAG system components
|
| 12 |
-
print("Initializing RAG system...")
|
|
|
|
| 13 |
try:
|
| 14 |
vector_store = VectorStore()
|
|
|
|
|
|
|
| 15 |
retriever = SQLRetriever(vector_store)
|
|
|
|
|
|
|
| 16 |
prompt_engine = PromptEngine()
|
|
|
|
|
|
|
| 17 |
sql_generator = SQLGenerator(retriever, prompt_engine)
|
| 18 |
-
print("
|
|
|
|
|
|
|
| 19 |
except Exception as e:
|
| 20 |
-
print(f"Error initializing RAG system: {e}")
|
|
|
|
| 21 |
sql_generator = None
|
| 22 |
|
| 23 |
def generate_sql(question, table_headers):
|
| 24 |
"""Generate SQL using the RAG system directly."""
|
| 25 |
if sql_generator is None:
|
| 26 |
-
return "β Error: RAG system not initialized"
|
| 27 |
|
| 28 |
try:
|
|
|
|
|
|
|
|
|
|
| 29 |
start_time = time.time()
|
| 30 |
|
| 31 |
# Generate SQL using RAG system
|
| 32 |
result = sql_generator.generate_sql(question, table_headers)
|
| 33 |
|
| 34 |
processing_time = time.time() - start_time
|
|
|
|
|
|
|
| 35 |
|
| 36 |
return f"""
|
| 37 |
**Generated SQL:**
|
|
@@ -45,12 +85,14 @@ def generate_sql(question, table_headers):
|
|
| 45 |
**Retrieved Examples:** {len(result['retrieved_examples'])} examples used for RAG
|
| 46 |
"""
|
| 47 |
except Exception as e:
|
| 48 |
-
|
|
|
|
|
|
|
| 49 |
|
| 50 |
def batch_generate_sql(questions_text, table_headers):
|
| 51 |
"""Generate SQL for multiple questions."""
|
| 52 |
if sql_generator is None:
|
| 53 |
-
return "β Error: RAG system not initialized"
|
| 54 |
|
| 55 |
try:
|
| 56 |
# Parse questions
|
|
@@ -81,16 +123,21 @@ def batch_generate_sql(questions_text, table_headers):
|
|
| 81 |
return output
|
| 82 |
|
| 83 |
except Exception as e:
|
| 84 |
-
return f"β Error: {str(e)}"
|
| 85 |
|
| 86 |
def check_system_health():
|
| 87 |
"""Check the health of the RAG system."""
|
| 88 |
try:
|
| 89 |
if sql_generator is None:
|
| 90 |
-
return "β System Status: RAG system not initialized"
|
| 91 |
|
| 92 |
# Get model info
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
return f"""
|
| 96 |
**System Health:**
|
|
@@ -98,20 +145,30 @@ def check_system_health():
|
|
| 98 |
- **System Loaded:** β
Yes
|
| 99 |
- **System Loading:** β No
|
| 100 |
- **Error:** None
|
|
|
|
| 101 |
- **Timestamp:** {time.strftime('%Y-%m-%d %H:%M:%S')}
|
| 102 |
|
| 103 |
**Model Info:**
|
| 104 |
{json.dumps(model_info, indent=2) if model_info else 'Not available'}
|
|
|
|
|
|
|
|
|
|
| 105 |
"""
|
| 106 |
except Exception as e:
|
| 107 |
-
return f"β Health check error: {str(e)}"
|
| 108 |
|
| 109 |
# Create Gradio interface
|
| 110 |
with gr.Blocks(title="Text-to-SQL RAG with CodeLlama", theme=gr.themes.Soft()) as demo:
|
| 111 |
-
gr.Markdown("#Text-to-SQL RAG with CodeLlama")
|
| 112 |
gr.Markdown("Generate SQL queries from natural language using **RAG (Retrieval-Augmented Generation)** and **CodeLlama** models.")
|
| 113 |
gr.Markdown("**Features:** RAG-enhanced generation, CodeLlama integration, Vector-based retrieval, Advanced prompt engineering")
|
| 114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
with gr.Tab("Single Query"):
|
| 116 |
with gr.Row():
|
| 117 |
with gr.Column(scale=1):
|
|
@@ -125,7 +182,7 @@ with gr.Blocks(title="Text-to-SQL RAG with CodeLlama", theme=gr.themes.Soft()) a
|
|
| 125 |
placeholder="e.g., id, name, salary, department",
|
| 126 |
value="id, name, salary, department"
|
| 127 |
)
|
| 128 |
-
generate_btn = gr.Button("Generate SQL", variant="primary", size="lg")
|
| 129 |
|
| 130 |
with gr.Column(scale=1):
|
| 131 |
output = gr.Markdown(label="Result")
|
|
@@ -143,14 +200,14 @@ with gr.Blocks(title="Text-to-SQL RAG with CodeLlama", theme=gr.themes.Soft()) a
|
|
| 143 |
placeholder="e.g., id, name, salary, department",
|
| 144 |
value="id, name, salary, department"
|
| 145 |
)
|
| 146 |
-
batch_btn = gr.Button("Generate Batch SQL", variant="primary", size="lg")
|
| 147 |
|
| 148 |
with gr.Column(scale=1):
|
| 149 |
batch_output = gr.Markdown(label="Batch Results")
|
| 150 |
|
| 151 |
with gr.Tab("System Health"):
|
| 152 |
with gr.Row():
|
| 153 |
-
health_btn = gr.Button("Check System Health", variant="secondary", size="lg")
|
| 154 |
health_output = gr.Markdown(label="Health Status")
|
| 155 |
|
| 156 |
# Event handlers
|
|
@@ -173,14 +230,14 @@ with gr.Blocks(title="Text-to-SQL RAG with CodeLlama", theme=gr.themes.Soft()) a
|
|
| 173 |
|
| 174 |
gr.Markdown("---")
|
| 175 |
gr.Markdown("""
|
| 176 |
-
## How It Works
|
| 177 |
|
| 178 |
1. **RAG System**: Retrieves relevant SQL examples from vector database
|
| 179 |
2. **CodeLlama**: Generates SQL using retrieved examples as context
|
| 180 |
3. **Vector Search**: Finds similar questions and their SQL solutions
|
| 181 |
4. **Enhanced Generation**: Combines retrieval + generation for better accuracy
|
| 182 |
|
| 183 |
-
## Technology Stack
|
| 184 |
|
| 185 |
- **Backend**: Direct RAG system integration
|
| 186 |
- **LLM**: CodeLlama-7B-Python-GGUF (primary)
|
|
@@ -188,7 +245,7 @@ with gr.Blocks(title="Text-to-SQL RAG with CodeLlama", theme=gr.themes.Soft()) a
|
|
| 188 |
- **Frontend**: Gradio interface
|
| 189 |
- **Hosting**: Hugging Face Spaces
|
| 190 |
|
| 191 |
-
## Performance
|
| 192 |
|
| 193 |
- **Model**: CodeLlama-7B-Python-GGUF
|
| 194 |
- **Response Time**: < 5 seconds
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import time
|
| 3 |
import json
|
| 4 |
+
import traceback
|
| 5 |
|
| 6 |
# Import RAG system components
|
| 7 |
+
print("Starting RAG system initialization...")
|
| 8 |
+
try:
|
| 9 |
+
from rag_system.vector_store import VectorStore
|
| 10 |
+
print("β VectorStore imported successfully")
|
| 11 |
+
except Exception as e:
|
| 12 |
+
print(f"β VectorStore import failed: {e}")
|
| 13 |
+
traceback.print_exc()
|
| 14 |
+
|
| 15 |
+
try:
|
| 16 |
+
from rag_system.retriever import SQLRetriever
|
| 17 |
+
print("β SQLRetriever imported successfully")
|
| 18 |
+
except Exception as e:
|
| 19 |
+
print(f"β SQLRetriever import failed: {e}")
|
| 20 |
+
traceback.print_exc()
|
| 21 |
+
|
| 22 |
+
try:
|
| 23 |
+
from rag_system.prompt_engine import PromptEngine
|
| 24 |
+
print("β PromptEngine imported successfully")
|
| 25 |
+
except Exception as e:
|
| 26 |
+
print(f"β PromptEngine import failed: {e}")
|
| 27 |
+
traceback.print_exc()
|
| 28 |
+
|
| 29 |
+
try:
|
| 30 |
+
from rag_system.sql_generator import SQLGenerator
|
| 31 |
+
print("β SQLGenerator imported successfully")
|
| 32 |
+
except Exception as e:
|
| 33 |
+
print(f"β SQLGenerator import failed: {e}")
|
| 34 |
+
traceback.print_exc()
|
| 35 |
|
| 36 |
# Initialize RAG system components
|
| 37 |
+
print("Initializing RAG system components...")
|
| 38 |
+
sql_generator = None
|
| 39 |
try:
|
| 40 |
vector_store = VectorStore()
|
| 41 |
+
print("β VectorStore initialized")
|
| 42 |
+
|
| 43 |
retriever = SQLRetriever(vector_store)
|
| 44 |
+
print("β SQLRetriever initialized")
|
| 45 |
+
|
| 46 |
prompt_engine = PromptEngine()
|
| 47 |
+
print("β PromptEngine initialized")
|
| 48 |
+
|
| 49 |
sql_generator = SQLGenerator(retriever, prompt_engine)
|
| 50 |
+
print("β SQLGenerator initialized")
|
| 51 |
+
|
| 52 |
+
print("π RAG system initialized successfully!")
|
| 53 |
except Exception as e:
|
| 54 |
+
print(f"β Error initializing RAG system: {e}")
|
| 55 |
+
traceback.print_exc()
|
| 56 |
sql_generator = None
|
| 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}")
|
| 66 |
+
|
| 67 |
start_time = time.time()
|
| 68 |
|
| 69 |
# Generate SQL using RAG system
|
| 70 |
result = sql_generator.generate_sql(question, table_headers)
|
| 71 |
|
| 72 |
processing_time = time.time() - start_time
|
| 73 |
+
print(f"SQL generation completed in {processing_time:.2f}s")
|
| 74 |
+
print(f"Result: {result}")
|
| 75 |
|
| 76 |
return f"""
|
| 77 |
**Generated SQL:**
|
|
|
|
| 85 |
**Retrieved Examples:** {len(result['retrieved_examples'])} examples used for RAG
|
| 86 |
"""
|
| 87 |
except Exception as e:
|
| 88 |
+
error_msg = f"β Error: {str(e)}\n\nFull traceback:\n{traceback.format_exc()}"
|
| 89 |
+
print(error_msg)
|
| 90 |
+
return error_msg
|
| 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
|
|
|
|
| 123 |
return output
|
| 124 |
|
| 125 |
except Exception as e:
|
| 126 |
+
return f"β Error: {str(e)}\n\nFull traceback:\n{traceback.format_exc()}"
|
| 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."
|
| 133 |
|
| 134 |
# Get model info
|
| 135 |
+
try:
|
| 136 |
+
model_info = sql_generator.get_model_info()
|
| 137 |
+
model_status = "Available"
|
| 138 |
+
except Exception as e:
|
| 139 |
+
model_info = {"error": str(e)}
|
| 140 |
+
model_status = f"Error: {e}"
|
| 141 |
|
| 142 |
return f"""
|
| 143 |
**System Health:**
|
|
|
|
| 145 |
- **System Loaded:** β
Yes
|
| 146 |
- **System Loading:** β No
|
| 147 |
- **Error:** None
|
| 148 |
+
- **Model Status:** {model_status}
|
| 149 |
- **Timestamp:** {time.strftime('%Y-%m-%d %H:%M:%S')}
|
| 150 |
|
| 151 |
**Model Info:**
|
| 152 |
{json.dumps(model_info, indent=2) if model_info else 'Not available'}
|
| 153 |
+
|
| 154 |
+
**Initialization Logs:**
|
| 155 |
+
Check the console/logs above for detailed initialization information.
|
| 156 |
"""
|
| 157 |
except Exception as e:
|
| 158 |
+
return f"β Health check error: {str(e)}\n\nFull traceback:\n{traceback.format_exc()}"
|
| 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("# π Text-to-SQL RAG with CodeLlama")
|
| 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("β οΈ **Warning:** RAG system failed to initialize. Check the logs for errors.")
|
| 169 |
+
else:
|
| 170 |
+
gr.Markdown("β
**Status:** RAG system initialized successfully!")
|
| 171 |
+
|
| 172 |
with gr.Tab("Single Query"):
|
| 173 |
with gr.Row():
|
| 174 |
with gr.Column(scale=1):
|
|
|
|
| 182 |
placeholder="e.g., id, name, salary, department",
|
| 183 |
value="id, name, salary, department"
|
| 184 |
)
|
| 185 |
+
generate_btn = gr.Button("π Generate SQL", variant="primary", size="lg")
|
| 186 |
|
| 187 |
with gr.Column(scale=1):
|
| 188 |
output = gr.Markdown(label="Result")
|
|
|
|
| 200 |
placeholder="e.g., id, name, salary, department",
|
| 201 |
value="id, name, salary, department"
|
| 202 |
)
|
| 203 |
+
batch_btn = gr.Button("π Generate Batch SQL", variant="primary", size="lg")
|
| 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("π Check System Health", variant="secondary", size="lg")
|
| 211 |
health_output = gr.Markdown(label="Health Status")
|
| 212 |
|
| 213 |
# Event handlers
|
|
|
|
| 230 |
|
| 231 |
gr.Markdown("---")
|
| 232 |
gr.Markdown("""
|
| 233 |
+
## π― How It Works
|
| 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 |
+
## π οΈ Technology Stack
|
| 241 |
|
| 242 |
- **Backend**: Direct RAG system integration
|
| 243 |
- **LLM**: CodeLlama-7B-Python-GGUF (primary)
|
|
|
|
| 245 |
- **Frontend**: Gradio interface
|
| 246 |
- **Hosting**: Hugging Face Spaces
|
| 247 |
|
| 248 |
+
## π Performance
|
| 249 |
|
| 250 |
- **Model**: CodeLlama-7B-Python-GGUF
|
| 251 |
- **Response Time**: < 5 seconds
|