Spaces:
Sleeping
Sleeping
Changes in app.py
Browse files
app.py
CHANGED
|
@@ -1,102 +1,114 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import requests
|
| 3 |
-
import json
|
| 4 |
import time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
def generate_sql(question, table_headers):
|
| 7 |
-
"""Generate SQL using the RAG
|
|
|
|
|
|
|
|
|
|
| 8 |
try:
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
}
|
| 14 |
|
| 15 |
-
|
| 16 |
-
response = requests.post("http://localhost:8000/predict", json=data)
|
| 17 |
|
| 18 |
-
|
| 19 |
-
result = response.json()
|
| 20 |
-
return f"""
|
| 21 |
**Generated SQL:**
|
| 22 |
```sql
|
| 23 |
{result['sql_query']}
|
| 24 |
```
|
| 25 |
|
| 26 |
**Model Used:** {result['model_used']}
|
| 27 |
-
**Processing Time:** {
|
| 28 |
**Status:** {result['status']}
|
| 29 |
**Retrieved Examples:** {len(result['retrieved_examples'])} examples used for RAG
|
| 30 |
"""
|
| 31 |
-
else:
|
| 32 |
-
return f"β Error: {response.status_code} - {response.text}"
|
| 33 |
-
|
| 34 |
except Exception as e:
|
| 35 |
return f"β Error: {str(e)}"
|
| 36 |
|
| 37 |
def batch_generate_sql(questions_text, table_headers):
|
| 38 |
"""Generate SQL for multiple questions."""
|
|
|
|
|
|
|
|
|
|
| 39 |
try:
|
| 40 |
# Parse questions
|
| 41 |
questions = [q.strip() for q in questions_text.split("\n") if q.strip()]
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
{
|
| 47 |
-
"question": q,
|
| 48 |
-
"table_headers": [h.strip() for h in table_headers.split(",") if h.strip()]
|
| 49 |
-
}
|
| 50 |
-
for q in questions
|
| 51 |
-
]
|
| 52 |
-
}
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
if response.status_code == 200:
|
| 58 |
-
result = response.json()
|
| 59 |
-
output = f"**Batch Results:**\n"
|
| 60 |
-
output += f"Total Queries: {result['total_queries']}\n"
|
| 61 |
-
output += f"Successful: {result['successful_queries']}\n\n"
|
| 62 |
-
|
| 63 |
-
for i, res in enumerate(result['results']):
|
| 64 |
-
output += f"**Query {i+1}:** {res['question']}\n"
|
| 65 |
-
output += f"```sql\n{res['sql_query']}\n```\n"
|
| 66 |
-
output += f"Model: {res['model_used']} | Time: {res['processing_time']:.2f}s\n\n"
|
| 67 |
-
|
| 68 |
-
return output
|
| 69 |
-
else:
|
| 70 |
-
return f"β Error: {response.status_code} - {response.text}"
|
| 71 |
-
|
| 72 |
except Exception as e:
|
| 73 |
return f"β Error: {str(e)}"
|
| 74 |
|
| 75 |
def check_system_health():
|
| 76 |
"""Check the health of the RAG system."""
|
| 77 |
try:
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
| 82 |
**System Health:**
|
| 83 |
-
- **Status:**
|
| 84 |
-
- **System Loaded:**
|
| 85 |
-
- **System Loading:**
|
| 86 |
-
- **Error:**
|
| 87 |
-
- **Timestamp:** {time.strftime('%Y-%m-%d %H:%M:%S'
|
| 88 |
|
| 89 |
**Model Info:**
|
| 90 |
-
{json.dumps(
|
| 91 |
"""
|
| 92 |
-
else:
|
| 93 |
-
return f"β Health check failed: {response.status_code}"
|
| 94 |
except Exception as e:
|
| 95 |
return f"β Health check error: {str(e)}"
|
| 96 |
|
| 97 |
# Create Gradio interface
|
| 98 |
with gr.Blocks(title="Text-to-SQL RAG with CodeLlama", theme=gr.themes.Soft()) as demo:
|
| 99 |
-
gr.Markdown("#
|
| 100 |
gr.Markdown("Generate SQL queries from natural language using **RAG (Retrieval-Augmented Generation)** and **CodeLlama** models.")
|
| 101 |
gr.Markdown("**Features:** RAG-enhanced generation, CodeLlama integration, Vector-based retrieval, Advanced prompt engineering")
|
| 102 |
|
|
@@ -113,7 +125,7 @@ with gr.Blocks(title="Text-to-SQL RAG with CodeLlama", theme=gr.themes.Soft()) a
|
|
| 113 |
placeholder="e.g., id, name, salary, department",
|
| 114 |
value="id, name, salary, department"
|
| 115 |
)
|
| 116 |
-
generate_btn = gr.Button("
|
| 117 |
|
| 118 |
with gr.Column(scale=1):
|
| 119 |
output = gr.Markdown(label="Result")
|
|
@@ -131,14 +143,14 @@ with gr.Blocks(title="Text-to-SQL RAG with CodeLlama", theme=gr.themes.Soft()) a
|
|
| 131 |
placeholder="e.g., id, name, salary, department",
|
| 132 |
value="id, name, salary, department"
|
| 133 |
)
|
| 134 |
-
batch_btn = gr.Button("
|
| 135 |
|
| 136 |
with gr.Column(scale=1):
|
| 137 |
batch_output = gr.Markdown(label="Batch Results")
|
| 138 |
|
| 139 |
with gr.Tab("System Health"):
|
| 140 |
with gr.Row():
|
| 141 |
-
health_btn = gr.Button("
|
| 142 |
health_output = gr.Markdown(label="Health Status")
|
| 143 |
|
| 144 |
# Event handlers
|
|
@@ -161,22 +173,22 @@ with gr.Blocks(title="Text-to-SQL RAG with CodeLlama", theme=gr.themes.Soft()) a
|
|
| 161 |
|
| 162 |
gr.Markdown("---")
|
| 163 |
gr.Markdown("""
|
| 164 |
-
##
|
| 165 |
|
| 166 |
1. **RAG System**: Retrieves relevant SQL examples from vector database
|
| 167 |
2. **CodeLlama**: Generates SQL using retrieved examples as context
|
| 168 |
3. **Vector Search**: Finds similar questions and their SQL solutions
|
| 169 |
4. **Enhanced Generation**: Combines retrieval + generation for better accuracy
|
| 170 |
|
| 171 |
-
##
|
| 172 |
|
| 173 |
-
- **Backend**:
|
| 174 |
- **LLM**: CodeLlama-7B-Python-GGUF (primary)
|
| 175 |
- **Vector DB**: ChromaDB with sentence transformers
|
| 176 |
- **Frontend**: Gradio interface
|
| 177 |
- **Hosting**: Hugging Face Spaces
|
| 178 |
|
| 179 |
-
##
|
| 180 |
|
| 181 |
- **Model**: CodeLlama-7B-Python-GGUF
|
| 182 |
- **Response Time**: < 5 seconds
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
import time
|
| 3 |
+
import json
|
| 4 |
+
|
| 5 |
+
# Import RAG system components
|
| 6 |
+
from rag_system.vector_store import VectorStore
|
| 7 |
+
from rag_system.retriever import SQLRetriever
|
| 8 |
+
from rag_system.prompt_engine import PromptEngine
|
| 9 |
+
from rag_system.sql_generator import SQLGenerator
|
| 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("RAG system initialized successfully!")
|
| 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:**
|
| 38 |
```sql
|
| 39 |
{result['sql_query']}
|
| 40 |
```
|
| 41 |
|
| 42 |
**Model Used:** {result['model_used']}
|
| 43 |
+
**Processing Time:** {processing_time:.2f}s
|
| 44 |
**Status:** {result['status']}
|
| 45 |
**Retrieved Examples:** {len(result['retrieved_examples'])} examples used for RAG
|
| 46 |
"""
|
|
|
|
|
|
|
|
|
|
| 47 |
except Exception as e:
|
| 48 |
return f"β Error: {str(e)}"
|
| 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
|
| 57 |
questions = [q.strip() for q in questions_text.split("\n") if q.strip()]
|
| 58 |
|
| 59 |
+
output = f"**Batch Results:**\n"
|
| 60 |
+
output += f"Total Queries: {len(questions)}\n"
|
| 61 |
+
successful_count = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
for i, question in enumerate(questions):
|
| 64 |
+
try:
|
| 65 |
+
start_time = time.time()
|
| 66 |
+
result = sql_generator.generate_sql(question, table_headers)
|
| 67 |
+
processing_time = time.time() - start_time
|
| 68 |
+
|
| 69 |
+
output += f"\n**Query {i+1}:** {question}\n"
|
| 70 |
+
output += f"```sql\n{result['sql_query']}\n```\n"
|
| 71 |
+
output += f"Model: {result['model_used']} | Time: {processing_time:.2f}s\n"
|
| 72 |
+
|
| 73 |
+
if result['status'] == 'success':
|
| 74 |
+
successful_count += 1
|
| 75 |
+
|
| 76 |
+
except Exception as e:
|
| 77 |
+
output += f"\n**Query {i+1}:** {question}\n"
|
| 78 |
+
output += f"β Error: {str(e)}\n"
|
| 79 |
+
|
| 80 |
+
output += f"\n**Successful:** {successful_count}/{len(questions)}"
|
| 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 |
+
model_info = sql_generator.get_model_info()
|
| 94 |
+
|
| 95 |
+
return f"""
|
| 96 |
**System Health:**
|
| 97 |
+
- **Status:** β
Healthy
|
| 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 |
|
|
|
|
| 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 |
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 |
|
| 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)
|
| 187 |
- **Vector DB**: ChromaDB with sentence transformers
|
| 188 |
- **Frontend**: Gradio interface
|
| 189 |
- **Hosting**: Hugging Face Spaces
|
| 190 |
|
| 191 |
+
## Performance
|
| 192 |
|
| 193 |
- **Model**: CodeLlama-7B-Python-GGUF
|
| 194 |
- **Response Time**: < 5 seconds
|