adamboom111 commited on
Commit
1f25563
·
verified ·
1 Parent(s): 9749f42

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -32
app.py CHANGED
@@ -1,41 +1,57 @@
 
 
1
  import gradio as gr
2
- from transformers import T5Tokenizer, T5ForConditionalGeneration
3
-
4
- # Load FLAN-T5-small
5
- model_name = "google/flan-t5-small"
6
- tokenizer = T5Tokenizer.from_pretrained(model_name)
7
- model = T5ForConditionalGeneration.from_pretrained(model_name)
8
-
9
- def generate_sql(payload):
10
- question = payload.get("question", "")
11
- schema = payload.get("schema", "")
12
- sample_rows = payload.get("sample_rows", [])
13
-
14
- # Convert sample rows to a flat string
15
- rows_text = " ".join([str(row) for row in sample_rows]) if sample_rows else ""
16
-
17
- # Construct the prompt
18
- prompt = (
19
- f"You are a SQL expert.\n"
20
- f"Schema: {schema}\n"
21
- f"Sample Rows: {rows_text}\n"
22
- f"Question: {question}\n"
23
- f"Generate SQL:"
 
 
 
 
 
 
 
 
 
 
 
 
24
  )
 
 
25
 
26
- input_ids = tokenizer(prompt, return_tensors="pt").input_ids
27
- outputs = model.generate(input_ids, max_length=256, temperature=0.7)
28
- sql = tokenizer.decode(outputs[0], skip_special_tokens=True)
29
-
30
- return sql
 
31
 
32
  demo = gr.Interface(
33
- fn=generate_sql,
34
- inputs=gr.JSON(label="JSON (question, schema, sample_rows)"),
35
  outputs="text",
36
- title="FLAN-T5 Text-to-SQL",
37
- description="Using FLAN-T5 to generate SQL from natural language and tabular schema.",
38
- allow_flagging="never"
39
  )
40
 
41
  demo.launch()
 
1
+ from dotenv import load_dotenv
2
+ import os
3
  import gradio as gr
4
+ 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 specialized in generating SQL queries for a single table at a time.
12
+ Your task is to accurately convert natural language queries into SQL statements based on the user's intent and the provided table metadata.
13
+
14
+ Rules:
15
+ - Single Table Only: Use only the table in the metadata.
16
+ - Metadata-Based Validation: Use only columns in the metadata.
17
+ - User Intent: Support filters, grouping, sorting, etc.
18
+ - SQL Syntax: Use standard SQL (DuckDB compatible).
19
+ - Output only valid SQL. No extra commentary.
20
+
21
+ Input:
22
+ User Query: {user_query}
23
+ Table Metadata: {table_metadata}
24
+
25
+ Output:
26
+ SQL Query (on a single line, nothing else).
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
+ client = Groq(api_key=api)
32
+ chat_completion = client.chat.completions.create(
33
+ messages=[
34
+ {"role": "system", "content": system_prompt},
35
+ {"role": "user", "content": user_prompt}
36
+ ],
37
+ model="llama3-70b-8192"
38
  )
39
+ response = chat_completion.choices[0].message.content.strip()
40
+ return response if response.lower().startswith("select") else "Can't perform the task at the moment."
41
 
42
+ # NEW: accepts user_query and dynamic table_metadata string
43
+ def response(payload):
44
+ user_query = payload.get("question", "")
45
+ table_metadata = payload.get("schema", "")
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",
53
+ title="SQL Generator (Groq + LLaMA3)",
54
+ description="Input: question & table metadata. Output: SQL using dynamic schema."
 
55
  )
56
 
57
  demo.launch()