Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,43 @@
|
|
| 1 |
import torch
|
| 2 |
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
tokenizer = T5Tokenizer.from_pretrained('t5-small')
|
| 6 |
-
|
| 7 |
-
# Load the model
|
| 8 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 9 |
model = T5ForConditionalGeneration.from_pretrained('cssupport/t5-small-awesome-text-to-sql')
|
| 10 |
model = model.to(device)
|
| 11 |
model.eval()
|
| 12 |
|
|
|
|
| 13 |
def generate_sql(input_prompt):
|
| 14 |
-
# Tokenize
|
| 15 |
inputs = tokenizer(input_prompt, padding=True, truncation=True, return_tensors="pt").to(device)
|
| 16 |
|
| 17 |
-
#
|
| 18 |
with torch.no_grad():
|
| 19 |
outputs = model.generate(**inputs, max_length=512)
|
| 20 |
|
| 21 |
-
#
|
| 22 |
generated_sql = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 23 |
|
| 24 |
return generated_sql
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
-
|
|
|
|
| 31 |
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
|
|
|
| 1 |
import torch
|
| 2 |
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
| 3 |
+
import gradio as gr
|
| 4 |
|
| 5 |
+
# Inicialize o tokenizer e o modelo
|
| 6 |
tokenizer = T5Tokenizer.from_pretrained('t5-small')
|
|
|
|
|
|
|
| 7 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 8 |
model = T5ForConditionalGeneration.from_pretrained('cssupport/t5-small-awesome-text-to-sql')
|
| 9 |
model = model.to(device)
|
| 10 |
model.eval()
|
| 11 |
|
| 12 |
+
# Função para gerar SQL
|
| 13 |
def generate_sql(input_prompt):
|
| 14 |
+
# Tokenize a entrada
|
| 15 |
inputs = tokenizer(input_prompt, padding=True, truncation=True, return_tensors="pt").to(device)
|
| 16 |
|
| 17 |
+
# Gere a saída
|
| 18 |
with torch.no_grad():
|
| 19 |
outputs = model.generate(**inputs, max_length=512)
|
| 20 |
|
| 21 |
+
# Decodifique a saída para texto (SQL)
|
| 22 |
generated_sql = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 23 |
|
| 24 |
return generated_sql
|
| 25 |
|
| 26 |
+
# Interface Gradio
|
| 27 |
+
def gerar_sql_interface(input_prompt):
|
| 28 |
+
# Adiciona o prefixo "tables:" e "query for:" automaticamente
|
| 29 |
+
full_prompt = f"tables:\n{input_prompt}\nquery for: {input_prompt}"
|
| 30 |
+
sql_query = generate_sql(full_prompt)
|
| 31 |
+
return sql_query
|
| 32 |
|
| 33 |
+
# Cria a interface
|
| 34 |
+
interface = gr.Interface(
|
| 35 |
+
fn=gerar_sql_interface,
|
| 36 |
+
inputs="text",
|
| 37 |
+
outputs="text",
|
| 38 |
+
title="Gerador de SQL",
|
| 39 |
+
description="Digite uma consulta em linguagem natural e gere a consulta SQL correspondente."
|
| 40 |
+
)
|
| 41 |
|
| 42 |
+
# Inicia a interface
|
| 43 |
+
interface.launch()
|