File size: 1,376 Bytes
fc5b211
 
88914b2
6189794
88914b2
fc5b211
 
 
 
 
6189794
88914b2
fc5b211
88914b2
fc5b211
 
88914b2
fc5b211
 
 
88914b2
fc5b211
 
 
6189794
88914b2
 
 
 
 
 
fc5b211
88914b2
 
 
 
 
 
 
 
fc5b211
88914b2
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import torch
from transformers import T5Tokenizer, T5ForConditionalGeneration
import gradio as gr

# Inicialize o tokenizer e o modelo
tokenizer = T5Tokenizer.from_pretrained('t5-small')
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = T5ForConditionalGeneration.from_pretrained('cssupport/t5-small-awesome-text-to-sql')
model = model.to(device)
model.eval()

# Função para gerar SQL
def generate_sql(input_prompt):
    # Tokenize a entrada
    inputs = tokenizer(input_prompt, padding=True, truncation=True, return_tensors="pt").to(device)
    
    # Gere a saída
    with torch.no_grad():
        outputs = model.generate(**inputs, max_length=512)
    
    # Decodifique a saída para texto (SQL)
    generated_sql = tokenizer.decode(outputs[0], skip_special_tokens=True)
    
    return generated_sql

# Interface Gradio
def gerar_sql_interface(input_prompt):
    # Adiciona o prefixo "tables:" e "query for:" automaticamente
    full_prompt = f"tables:\n{input_prompt}\nquery for: {input_prompt}"
    sql_query = generate_sql(full_prompt)
    return sql_query

# Cria a interface
interface = gr.Interface(
    fn=gerar_sql_interface,
    inputs="text",
    outputs="text",
    title="Gerador de SQL",
    description="Digite uma consulta em linguagem natural e gere a consulta SQL correspondente."
)

# Inicia a interface
interface.launch()