Update app.py
Browse files
app.py
CHANGED
|
@@ -24,7 +24,6 @@ def init_db():
|
|
| 24 |
)
|
| 25 |
''')
|
| 26 |
|
| 27 |
-
# Check if table is empty; if so, populate dummy data
|
| 28 |
cursor.execute("SELECT COUNT(*) FROM project_tasks")
|
| 29 |
if cursor.fetchone()[0] == 0:
|
| 30 |
dummy_data = [
|
|
@@ -52,20 +51,13 @@ def execute_ai_query(provider, api_key, user_question):
|
|
| 52 |
if not user_question.strip():
|
| 53 |
return None, "⚠️ Please type a question or scenario."
|
| 54 |
|
| 55 |
-
#
|
| 56 |
-
system_prompt =
|
| 57 |
-
"You are an expert SQL assistant. The database table is called 'project_tasks' with columns: "
|
| 58 |
-
"[id, timestamp, phase, task_name, owner, timeline, priority]. "
|
| 59 |
-
"Return ONLY a valid, executable raw SQLite query based on the user's question. "
|
| 60 |
-
"Do not include any explanation, introductory markdown, or trailing text. "
|
| 61 |
-
"Strip away any backticks like ```sql or
|
| 62 |
``` if you use them."
|
| 63 |
-
)
|
| 64 |
|
| 65 |
try:
|
| 66 |
sql_query = ""
|
| 67 |
|
| 68 |
-
# Choice A: Groq API Execution
|
| 69 |
if provider == "Groq":
|
| 70 |
client = Groq(api_key=api_key)
|
| 71 |
response = client.chat.completions.create(
|
|
@@ -78,7 +70,6 @@ def execute_ai_query(provider, api_key, user_question):
|
|
| 78 |
)
|
| 79 |
sql_query = response.choices[0].message.content.strip()
|
| 80 |
|
| 81 |
-
# Choice B: OpenRouter API Execution
|
| 82 |
elif provider == "OpenRouter":
|
| 83 |
headers = {
|
| 84 |
"Authorization": f"Bearer {api_key}",
|
|
@@ -98,10 +89,8 @@ def execute_ai_query(provider, api_key, user_question):
|
|
| 98 |
else:
|
| 99 |
return None, f"❌ OpenRouter Error: {response.text}"
|
| 100 |
|
| 101 |
-
# Clean markdown wrappers if the model ignores system prompt rules
|
| 102 |
sql_query = sql_query.replace("```sql", "").replace("```", "").strip()
|
| 103 |
|
| 104 |
-
# Execute query securely against local SQLite instance
|
| 105 |
conn = sqlite3.connect(DB_NAME)
|
| 106 |
df = pd.read_sql_query(sql_query, conn)
|
| 107 |
conn.close()
|
|
@@ -122,10 +111,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 122 |
provider_select = gr.Dropdown(choices=["Groq", "OpenRouter"], value="Groq", label="API Gateway Provider")
|
| 123 |
token_input = gr.Textbox(label="User API Secret Key", type="password", placeholder="gsk_... or sk-or-...")
|
| 124 |
|
| 125 |
-
gr.Markdown("💡 **Sample Queries to try:**\n"
|
| 126 |
-
"- *Show me all tasks where priority is High*\n"
|
| 127 |
-
"- *List tasks owned by M. Reddy*\n"
|
| 128 |
-
"- *Count the number of tasks in each phase*")
|
| 129 |
|
| 130 |
with gr.Column(scale=2):
|
| 131 |
gr.Markdown("### 🤖 Data Query Desk")
|
|
|
|
| 24 |
)
|
| 25 |
''')
|
| 26 |
|
|
|
|
| 27 |
cursor.execute("SELECT COUNT(*) FROM project_tasks")
|
| 28 |
if cursor.fetchone()[0] == 0:
|
| 29 |
dummy_data = [
|
|
|
|
| 51 |
if not user_question.strip():
|
| 52 |
return None, "⚠️ Please type a question or scenario."
|
| 53 |
|
| 54 |
+
# Completely flattened into a single line string to eliminate formatting failures
|
| 55 |
+
system_prompt = "You are an expert SQL assistant. The database table is called 'project_tasks' with columns: [id, timestamp, phase, task_name, owner, timeline, priority]. Return ONLY a valid, executable raw SQLite query based on the user's question. Do not include any explanation, introductory markdown, or trailing text. Strip away any backticks like ```sql or
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
``` if you use them."
|
|
|
|
| 57 |
|
| 58 |
try:
|
| 59 |
sql_query = ""
|
| 60 |
|
|
|
|
| 61 |
if provider == "Groq":
|
| 62 |
client = Groq(api_key=api_key)
|
| 63 |
response = client.chat.completions.create(
|
|
|
|
| 70 |
)
|
| 71 |
sql_query = response.choices[0].message.content.strip()
|
| 72 |
|
|
|
|
| 73 |
elif provider == "OpenRouter":
|
| 74 |
headers = {
|
| 75 |
"Authorization": f"Bearer {api_key}",
|
|
|
|
| 89 |
else:
|
| 90 |
return None, f"❌ OpenRouter Error: {response.text}"
|
| 91 |
|
|
|
|
| 92 |
sql_query = sql_query.replace("```sql", "").replace("```", "").strip()
|
| 93 |
|
|
|
|
| 94 |
conn = sqlite3.connect(DB_NAME)
|
| 95 |
df = pd.read_sql_query(sql_query, conn)
|
| 96 |
conn.close()
|
|
|
|
| 111 |
provider_select = gr.Dropdown(choices=["Groq", "OpenRouter"], value="Groq", label="API Gateway Provider")
|
| 112 |
token_input = gr.Textbox(label="User API Secret Key", type="password", placeholder="gsk_... or sk-or-...")
|
| 113 |
|
| 114 |
+
gr.Markdown("💡 **Sample Queries to try:**\n- *Show me all tasks where priority is High*\n- *List tasks owned by M. Reddy*\n- *Count the number of tasks in each phase*")
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
with gr.Column(scale=2):
|
| 117 |
gr.Markdown("### 🤖 Data Query Desk")
|