Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -55,10 +55,8 @@ Avoid assumptions. Focus on summarizing what the data reveals.
|
|
| 55 |
user_prompt = f"""
|
| 56 |
SQL Query:
|
| 57 |
{sql_query}
|
| 58 |
-
|
| 59 |
Query Result:
|
| 60 |
{query_result}
|
| 61 |
-
|
| 62 |
Explanation:
|
| 63 |
"""
|
| 64 |
return system_prompt.strip(), user_prompt.strip()
|
|
@@ -75,7 +73,41 @@ def explain_sql_output(sql_query, query_result):
|
|
| 75 |
return chat_completion.choices[0].message.content.strip()
|
| 76 |
|
| 77 |
|
| 78 |
-
### ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
tab1 = gr.Interface(
|
| 80 |
fn=response,
|
| 81 |
inputs=gr.JSON(label="Input JSON (question, schema)"),
|
|
@@ -95,7 +127,18 @@ tab2 = gr.Interface(
|
|
| 95 |
description="Input a SQL query and its result. Get an AI-generated explanation."
|
| 96 |
)
|
| 97 |
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
if __name__ == '__main__':
|
| 101 |
demo.launch()
|
|
|
|
| 55 |
user_prompt = f"""
|
| 56 |
SQL Query:
|
| 57 |
{sql_query}
|
|
|
|
| 58 |
Query Result:
|
| 59 |
{query_result}
|
|
|
|
| 60 |
Explanation:
|
| 61 |
"""
|
| 62 |
return system_prompt.strip(), user_prompt.strip()
|
|
|
|
| 73 |
return chat_completion.choices[0].message.content.strip()
|
| 74 |
|
| 75 |
|
| 76 |
+
### --- TAB 3: SQL Follow-Ups --- ###
|
| 77 |
+
def followup_prompt(sql_query, query_result):
|
| 78 |
+
system_prompt = """
|
| 79 |
+
You are an assistant that suggests insightful follow-up questions based on SQL query output.
|
| 80 |
+
|
| 81 |
+
Instructions:
|
| 82 |
+
- Use the SQL query and result to infer possible next questions.
|
| 83 |
+
- Output 3 to 5 follow-up questions.
|
| 84 |
+
- Be helpful, curious, and relevant to the data.
|
| 85 |
+
- Do NOT repeat the explanation. Only return follow-up questions.
|
| 86 |
+
- Format as a list.
|
| 87 |
+
"""
|
| 88 |
+
user_prompt = f"""
|
| 89 |
+
SQL Query:
|
| 90 |
+
{sql_query}
|
| 91 |
+
Query Result:
|
| 92 |
+
{query_result}
|
| 93 |
+
|
| 94 |
+
Follow-Up Questions:
|
| 95 |
+
"""
|
| 96 |
+
return system_prompt.strip(), user_prompt.strip()
|
| 97 |
+
|
| 98 |
+
def generate_followups(sql_query, query_result):
|
| 99 |
+
system_prompt, user_prompt = followup_prompt(sql_query, query_result)
|
| 100 |
+
chat_completion = client.chat.completions.create(
|
| 101 |
+
messages=[
|
| 102 |
+
{"role": "system", "content": system_prompt},
|
| 103 |
+
{"role": "user", "content": user_prompt}
|
| 104 |
+
],
|
| 105 |
+
model="llama3-70b-8192"
|
| 106 |
+
)
|
| 107 |
+
return chat_completion.choices[0].message.content.strip()
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
### --- Gradio Interfaces --- ###
|
| 111 |
tab1 = gr.Interface(
|
| 112 |
fn=response,
|
| 113 |
inputs=gr.JSON(label="Input JSON (question, schema)"),
|
|
|
|
| 127 |
description="Input a SQL query and its result. Get an AI-generated explanation."
|
| 128 |
)
|
| 129 |
|
| 130 |
+
tab3 = gr.Interface(
|
| 131 |
+
fn=generate_followups,
|
| 132 |
+
inputs=[
|
| 133 |
+
gr.Textbox(label="SQL Query"),
|
| 134 |
+
gr.Textbox(label="SQL Output (Raw JSON or Table Result)")
|
| 135 |
+
],
|
| 136 |
+
outputs="text",
|
| 137 |
+
title="Follow-Up Generator (Groq + LLaMA3)",
|
| 138 |
+
description="Suggests follow-up questions based on a SQL query and its result."
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
demo = gr.TabbedInterface([tab1, tab2, tab3], ["SQL Generator", "Explain Output", "SQL Follow-Ups"])
|
| 142 |
|
| 143 |
if __name__ == '__main__':
|
| 144 |
demo.launch()
|