File size: 1,480 Bytes
35849b2
 
5822624
bb4a570
47a2dc5
bb4a570
ee62733
bb4a570
 
ee62733
35849b2
 
 
 
 
 
55d6351
 
 
35849b2
94ca4fb
55d6351
5822624
ee62733
47a2dc5
35849b2
bb4a570
 
 
ee62733
 
 
 
5822624
ee62733
 
 
35849b2
 
5822624
ee62733
 
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
# app.py

import gradio as gr
from transformers import T5Tokenizer, T5ForConditionalGeneration
from deep_translator import GoogleTranslator
import torch

torch.set_num_threads(1)
torch.set_num_interop_threads(1)

model_name = "mrm8488/t5-base-finetuned-wikiSQL"
tokenizer = T5Tokenizer.from_pretrained(model_name)
model = T5ForConditionalGeneration.from_pretrained(model_name)

# Tu esquema personalizado
SCHEMA = """
Schema:
Table bodegas with columns: Id, Nombre, Encargado, Telefono, Email, Direccion, Horario, Regional, Latitud, Longitud.
Table maestra with columns: CodigoSap, Descripcion, Grupo, Agrupador, Marca, Parte, Operacion, Componente.
"""


def generar_sql(pregunta_espanol):
    try:
        pregunta_ingles = GoogleTranslator(source="es", target="en").translate(pregunta_espanol)
        prompt = f"{SCHEMA}\ntranslate English to SQL: {pregunta_ingles}"
        input_ids = tokenizer(prompt, return_tensors="pt").input_ids
        output = model.generate(input_ids, max_length=128)
        sql = tokenizer.decode(output[0], skip_special_tokens=True)
        return sql
    except Exception as e:
        return f"Error: {str(e)}"

iface = gr.Interface(
    fn=generar_sql,
    inputs=gr.Textbox(lines=3, label="Pregunta en español"),
    outputs=gr.Textbox(label="Consulta SQL generada"),
    title="Texto a SQL con esquema personalizado",
    description="Escribe una pregunta en español y genera SQL sobre las tablas `bodegas` y `maestra`."
)

iface.launch()