Sayandip commited on
Commit
61f2c8a
·
verified ·
1 Parent(s): 48d5e02

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -19
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
- return [tuple(row) for row in rows] or []
 
 
65
  except Exception as e:
66
- return [f"⚠️ SQL Error: {str(e)}"]
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) -> str:
91
- try:
92
- result = agent.run(question)
93
- if isinstance(result, list):
94
- df = pd.DataFrame(result)
95
- return df.to_string(index=False)
96
- return str(result)
97
- except Exception as e:
98
- return f"⚠️ Error: {str(e)}"
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("## Text-TO-SQL Agent")
 
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.Textbox(label="Agent response")
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