adamboom111 commited on
Commit
446f0d8
·
verified ·
1 Parent(s): 18824a6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -6
app.py CHANGED
@@ -5,31 +5,29 @@ from groq import Groq
5
 
6
  load_dotenv()
7
  api = os.getenv("GROQ_API_KEY")
 
8
 
 
9
  def create_prompt(user_query, table_metadata):
10
  system_prompt = """
11
  You are a SQL query generator for a single relational table.
12
  You must strictly follow the metadata and never guess or invent column names.
13
-
14
  Instructions:
15
  - Use only the table and columns listed in the metadata.
16
  - Never generate queries with columns not present in the metadata.
17
  - If a column like 'gender' is not present, do not mention it.
18
  - Do not hallucinate values or table names. Use provided structure only.
19
  - Output valid SQL (DuckDB-compatible), single line, no comments or explanations.
20
-
21
  Input:
22
  User Query: {user_query}
23
  Table Metadata:
24
  {table_metadata}
25
-
26
  Output:
27
  A single valid SQL SELECT statement using only metadata-provided columns.
28
  """
29
  return system_prompt.strip(), f"User Query: {user_query}\nTable Metadata: {table_metadata}"
30
 
31
  def generate_output(system_prompt, user_prompt):
32
- client = Groq(api_key=api)
33
  chat_completion = client.chat.completions.create(
34
  messages=[
35
  {"role": "system", "content": system_prompt},
@@ -46,7 +44,39 @@ def response(payload):
46
  system_prompt, user_prompt = create_prompt(user_query, table_metadata)
47
  return generate_output(system_prompt, user_prompt)
48
 
49
- demo = gr.Interface(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  fn=response,
51
  inputs=gr.JSON(label="Input JSON (question, schema)"),
52
  outputs="text",
@@ -54,4 +84,18 @@ demo = gr.Interface(
54
  description="Input: question & table metadata. Output: SQL using dynamic schema."
55
  )
56
 
57
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  load_dotenv()
7
  api = os.getenv("GROQ_API_KEY")
8
+ client = Groq(api_key=api)
9
 
10
+ ### --- TAB 1: SQL Generator --- ###
11
  def create_prompt(user_query, table_metadata):
12
  system_prompt = """
13
  You are a SQL query generator for a single relational table.
14
  You must strictly follow the metadata and never guess or invent column names.
 
15
  Instructions:
16
  - Use only the table and columns listed in the metadata.
17
  - Never generate queries with columns not present in the metadata.
18
  - If a column like 'gender' is not present, do not mention it.
19
  - Do not hallucinate values or table names. Use provided structure only.
20
  - Output valid SQL (DuckDB-compatible), single line, no comments or explanations.
 
21
  Input:
22
  User Query: {user_query}
23
  Table Metadata:
24
  {table_metadata}
 
25
  Output:
26
  A single valid SQL SELECT statement using only metadata-provided columns.
27
  """
28
  return system_prompt.strip(), f"User Query: {user_query}\nTable Metadata: {table_metadata}"
29
 
30
  def generate_output(system_prompt, user_prompt):
 
31
  chat_completion = client.chat.completions.create(
32
  messages=[
33
  {"role": "system", "content": system_prompt},
 
44
  system_prompt, user_prompt = create_prompt(user_query, table_metadata)
45
  return generate_output(system_prompt, user_prompt)
46
 
47
+
48
+ ### --- TAB 2: SQL Output Explanation --- ###
49
+ def explain_output_prompt(sql_query, query_result):
50
+ system_prompt = """
51
+ You are an assistant that explains the meaning of SQL query results in plain language.
52
+ You should take into account the SQL query used and the resulting output.
53
+ Avoid assumptions. Focus on summarizing what the data reveals.
54
+ """
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()
65
+
66
+ def explain_sql_output(sql_query, query_result):
67
+ system_prompt, user_prompt = explain_output_prompt(sql_query, query_result)
68
+ chat_completion = client.chat.completions.create(
69
+ messages=[
70
+ {"role": "system", "content": system_prompt},
71
+ {"role": "user", "content": user_prompt}
72
+ ],
73
+ model="llama3-70b-8192"
74
+ )
75
+ return chat_completion.choices[0].message.content.strip()
76
+
77
+
78
+ ### --- Gradio Interface --- ###
79
+ tab1 = gr.Interface(
80
  fn=response,
81
  inputs=gr.JSON(label="Input JSON (question, schema)"),
82
  outputs="text",
 
84
  description="Input: question & table metadata. Output: SQL using dynamic schema."
85
  )
86
 
87
+ tab2 = gr.Interface(
88
+ fn=explain_sql_output,
89
+ inputs=[
90
+ gr.Textbox(label="SQL Query"),
91
+ gr.Textbox(label="SQL Output (Raw JSON or Table Result)")
92
+ ],
93
+ outputs="text",
94
+ title="Explain SQL Result (Groq + LLaMA3)",
95
+ description="Input a SQL query and its result. Get an AI-generated explanation."
96
+ )
97
+
98
+ demo = gr.TabbedInterface([tab1, tab2], ["SQL Generator", "Explain Output"])
99
+
100
+ if __name__ == '__main__':
101
+ demo.launch()