import gradio as gr
from controller import Controller
controller = Controller()
# Custom CSS for modern styling
custom_css = """
.main-container {
background: linear-gradient(135deg, #0f1419 0%, #1a1f2e 100%);
min-height: 100vh;
}
.header-section {
background: linear-gradient(135deg, #1e2a3a 0%, #2d3e50 100%);
padding: 2rem;
border-radius: 16px;
margin-bottom: 2rem;
border: 1px solid rgba(52, 152, 219, 0.2);
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
}
.stats-card {
background: rgba(30, 42, 58, 0.8);
border: 1px solid rgba(52, 152, 219, 0.3);
border-radius: 12px;
padding: 1.5rem;
backdrop-filter: blur(10px);
transition: all 0.3s ease;
}
.stats-card:hover {
border-color: rgba(52, 152, 219, 0.6);
box-shadow: 0 4px 20px rgba(52, 152, 219, 0.2);
}
.query-section {
background: rgba(30, 42, 58, 0.6);
border-radius: 12px;
padding: 1.5rem;
border: 1px solid rgba(52, 152, 219, 0.2);
}
.tips-section {
background: linear-gradient(135deg, rgba(46, 204, 113, 0.1) 0%, rgba(26, 188, 156, 0.1) 100%);
border: 1px solid rgba(46, 204, 113, 0.3);
border-radius: 12px;
padding: 1.5rem;
}
.example-queries {
background: rgba(155, 89, 182, 0.1);
border: 1px solid rgba(155, 89, 182, 0.3);
border-radius: 8px;
padding: 1rem;
margin-top: 1rem;
}
.feature-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 1rem;
margin-top: 1rem;
}
.feature-item {
background: rgba(52, 73, 94, 0.5);
padding: 1rem;
border-radius: 8px;
border: 1px solid rgba(52, 152, 219, 0.2);
text-align: center;
}
/* Button styling */
.primary-btn {
background: linear-gradient(135deg, #3498db 0%, #2980b9 100%);
border: none;
border-radius: 8px;
padding: 0.75rem 1.5rem;
color: white;
font-weight: 600;
transition: all 0.3s ease;
}
.primary-btn:hover {
background: linear-gradient(135deg, #2980b9 0%, #1f5582 100%);
transform: translateY(-1px);
box-shadow: 0 4px 12px rgba(52, 152, 219, 0.3);
}
/* File upload styling */
.file-upload-area {
border: 2px dashed rgba(52, 152, 219, 0.5);
border-radius: 12px;
background: rgba(30, 42, 58, 0.3);
transition: all 0.3s ease;
}
.file-upload-area:hover {
border-color: rgba(52, 152, 219, 0.8);
background: rgba(30, 42, 58, 0.5);
}
"""
def create_example_button(question_text):
return question_text
with gr.Blocks(title="AI CSV → SQL Agent", css=custom_css, theme=gr.themes.Soft()) as demo:
# Header
with gr.Row():
gr.HTML("""
""")
# Main content area
with gr.Row():
with gr.Column(scale=3):
# File upload section
with gr.Group():
gr.HTML("📁 Data Upload
")
file_input = gr.File(
label="Choose your CSV file",
file_types=[".csv"],
elem_classes=["file-upload-area"]
)
load_status = gr.Textbox(
label="Upload Status",
interactive=False,
show_label=False
)
# Dataset info section
with gr.Group():
gr.HTML("📋 Dataset Information
")
dataset_info = gr.HTML("Upload a CSV to see dataset information
")
# Query section
with gr.Group():
gr.HTML("❓ Ask Your Question
")
q = gr.Textbox(
label="",
placeholder="e.g., What is the average age of employees in the Marketing department?",
lines=2,
show_label=False
)
with gr.Row():
ask_btn = gr.Button("🚀 Ask Question", variant="primary", size="lg")
clear_btn = gr.Button("🗑️ Clear", variant="secondary", size="lg")
with gr.Column(scale=2):
# Tips and examples section
with gr.Group():
gr.HTML("""
💡 Tips for Better Results
- Use specific column names when known
- Ask for aggregations (sum, average, count, etc.)
- Try filtering queries (where, having)
- Use comparison operators (>, <, =, between)
- Request top N results for rankings
""")
# Example queries
with gr.Group():
gr.HTML("🎯 Example Queries
")
example_queries = [
"Show me the top 5 highest values",
"What's the average by category?",
"Count records by status",
"Find records where amount > 1000",
"Show latest 10 entries"
]
for i, query in enumerate(example_queries):
example_btn = gr.Button(
f"📝 {query}",
variant="secondary",
size="sm",
elem_id=f"example_{i}"
)
example_btn.click(
lambda x, q=query: q,
outputs=q
)
# Query history section
with gr.Group():
gr.HTML("📚 Recent Queries
")
query_history = gr.HTML("Your recent queries will appear here
")
# Results section
with gr.Row():
with gr.Column():
gr.HTML("📊 Results
")
result_html = gr.HTML(
"""
🎯 Upload a CSV and ask a question to see results here
"""
)
# SQL Query Display
with gr.Row():
with gr.Column():
gr.HTML("🔍 Generated SQL Query
")
sql_display = gr.Code(
label="",
language="sql",
show_label=False,
interactive=False
)
# Footer
gr.HTML("""
Built with ❤️ using Gradio • Powered by AI •
Secure & Private Processing
""")
# Event handlers
def enhanced_load_csv(file):
status = controller.load_csv(file)
info = controller.get_dataset_info()
return status, info
def enhanced_question_handler(question):
if not question.strip():
return "Please enter a question", "", ""
result_html_content, sql_query = controller.handle_question_with_sql(question)
return result_html_content, sql_query, controller.add_to_history(question)
def clear_inputs():
return "", ""
file_input.upload(
enhanced_load_csv,
inputs=file_input,
outputs=[load_status, dataset_info]
)
ask_btn.click(
enhanced_question_handler,
inputs=q,
outputs=[result_html, sql_display, query_history]
)
q.submit(
enhanced_question_handler,
inputs=q,
outputs=[result_html, sql_display, query_history]
)
clear_btn.click(
clear_inputs,
outputs=[q, sql_display]
)
if __name__ == "__main__":
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=False,
show_error=True,
quiet=False
)