Shijun Ju
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
tokenizer = T5Tokenizer.from_pretrained('t5-small')
|
6 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
7 |
+
model = T5ForConditionalGeneration.from_pretrained('cssupport/t5-small-awesome-text-to-sql')
|
8 |
+
model = model.to(device)
|
9 |
+
model.eval()
|
10 |
+
|
11 |
+
def generate_sql(input_prompt):
|
12 |
+
inputs = tokenizer(input_prompt, padding=True, truncation=True, return_tensors="pt").to(device)
|
13 |
+
with torch.no_grad():
|
14 |
+
outputs = model.generate(**inputs, max_length=512)
|
15 |
+
generated_sql = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
16 |
+
return generated_sql
|
17 |
+
|
18 |
+
def gradio_interface(tables, query):
|
19 |
+
input_prompt = f"tables:\n{tables}\nquery for:{query}"
|
20 |
+
return generate_sql(input_prompt)
|
21 |
+
|
22 |
+
iface = gr.Interface(
|
23 |
+
fn=gradio_interface,
|
24 |
+
inputs=[
|
25 |
+
gr.Textbox(lines=5, label="Context Tables", placeholder="EXAMPLE: CREATE TABLE student_course_attendance (student_id VARCHAR); CREATE TABLE students (student_id VARCHAR)"),
|
26 |
+
gr.Textbox(lines=2, label="Query for", placeholder="EXAMPLE: List the id of students who never attends courses?")
|
27 |
+
],
|
28 |
+
outputs=gr.Textbox(label="Generated SQL Query", placeholder="EXAMPLE OUTPUT: The generated SQL query is: SELECT student_id FROM students WHERE NOT student_id IN (SELECT student_id FROM student_course_attendance)"),
|
29 |
+
title="Text to SQL Generator"
|
30 |
+
)
|
31 |
+
|
32 |
+
iface.launch()
|