Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -61,11 +61,13 @@ def sql_engine(query: str) -> list:
|
|
| 61 |
try:
|
| 62 |
with engine.connect() as con:
|
| 63 |
rows = con.execute(text(query))
|
| 64 |
-
|
|
|
|
|
|
|
| 65 |
except Exception as e:
|
| 66 |
-
return
|
| 67 |
|
| 68 |
-
# Table descriptions
|
| 69 |
inspector = inspect(engine)
|
| 70 |
updated_description = "Ask natural language questions about the student marks database.\n"
|
| 71 |
for table in ["students", "marks"]:
|
|
@@ -87,17 +89,15 @@ agent = CodeAgent(
|
|
| 87 |
)
|
| 88 |
|
| 89 |
# --- Gradio functions ---
|
| 90 |
-
def ask_agent(question: str)
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
if
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
# Sample prompts
|
| 101 |
sample_prompts = [
|
| 102 |
"Which student has the highest marks in Math?",
|
| 103 |
"List all students with their subjects and grades.",
|
|
@@ -108,7 +108,7 @@ sample_prompts = [
|
|
| 108 |
def set_prompt(prompt):
|
| 109 |
return prompt
|
| 110 |
|
| 111 |
-
# Display database tables
|
| 112 |
def get_tables():
|
| 113 |
with engine.connect() as con:
|
| 114 |
students_df = pd.read_sql_table("students", con)
|
|
@@ -119,18 +119,19 @@ students_df, marks_df = get_tables()
|
|
| 119 |
|
| 120 |
# --- Gradio App ---
|
| 121 |
with gr.Blocks() as demo:
|
| 122 |
-
gr.Markdown("##
|
|
|
|
| 123 |
with gr.Row():
|
| 124 |
-
gr.Column():
|
| 125 |
gr.Markdown("### Students Table")
|
| 126 |
gr.Dataframe(students_df, interactive=False)
|
| 127 |
-
gr.Column():
|
| 128 |
gr.Markdown("### Marks Table")
|
| 129 |
gr.Dataframe(marks_df, interactive=False)
|
| 130 |
|
| 131 |
gr.Markdown("### Ask the AI Agent")
|
| 132 |
question = gr.Textbox(label="Ask a question")
|
| 133 |
-
output = gr.
|
| 134 |
ask_btn = gr.Button("Ask")
|
| 135 |
ask_btn.click(ask_agent, inputs=question, outputs=output)
|
| 136 |
|
|
|
|
| 61 |
try:
|
| 62 |
with engine.connect() as con:
|
| 63 |
rows = con.execute(text(query))
|
| 64 |
+
columns = rows.keys()
|
| 65 |
+
data = [tuple(row) for row in rows]
|
| 66 |
+
return {"columns": columns, "data": data} if data else {"columns": [], "data": []}
|
| 67 |
except Exception as e:
|
| 68 |
+
return {"columns": ["Error"], "data": [[str(e)]]}
|
| 69 |
|
| 70 |
+
# --- Table descriptions ---
|
| 71 |
inspector = inspect(engine)
|
| 72 |
updated_description = "Ask natural language questions about the student marks database.\n"
|
| 73 |
for table in ["students", "marks"]:
|
|
|
|
| 89 |
)
|
| 90 |
|
| 91 |
# --- Gradio functions ---
|
| 92 |
+
def ask_agent(question: str):
|
| 93 |
+
result = agent.run(question)
|
| 94 |
+
if isinstance(result, dict) and "columns" in result and "data" in result:
|
| 95 |
+
if not result["data"]:
|
| 96 |
+
return pd.DataFrame(columns=result["columns"])
|
| 97 |
+
return pd.DataFrame(result["data"], columns=result["columns"])
|
| 98 |
+
return pd.DataFrame([[str(result)]], columns=["Result"])
|
| 99 |
+
|
| 100 |
+
# --- Sample prompts ---
|
|
|
|
|
|
|
| 101 |
sample_prompts = [
|
| 102 |
"Which student has the highest marks in Math?",
|
| 103 |
"List all students with their subjects and grades.",
|
|
|
|
| 108 |
def set_prompt(prompt):
|
| 109 |
return prompt
|
| 110 |
|
| 111 |
+
# --- Display database tables ---
|
| 112 |
def get_tables():
|
| 113 |
with engine.connect() as con:
|
| 114 |
students_df = pd.read_sql_table("students", con)
|
|
|
|
| 119 |
|
| 120 |
# --- Gradio App ---
|
| 121 |
with gr.Blocks() as demo:
|
| 122 |
+
gr.Markdown("## 🎓 Student Marks Database Explorer")
|
| 123 |
+
|
| 124 |
with gr.Row():
|
| 125 |
+
with gr.Column():
|
| 126 |
gr.Markdown("### Students Table")
|
| 127 |
gr.Dataframe(students_df, interactive=False)
|
| 128 |
+
with gr.Column():
|
| 129 |
gr.Markdown("### Marks Table")
|
| 130 |
gr.Dataframe(marks_df, interactive=False)
|
| 131 |
|
| 132 |
gr.Markdown("### Ask the AI Agent")
|
| 133 |
question = gr.Textbox(label="Ask a question")
|
| 134 |
+
output = gr.Dataframe(headers=None) # Result displayed as DataFrame
|
| 135 |
ask_btn = gr.Button("Ask")
|
| 136 |
ask_btn.click(ask_agent, inputs=question, outputs=output)
|
| 137 |
|