Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,44 +1,41 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import
|
3 |
-
import
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
-
torch.set_num_interop_threads(1)
|
8 |
|
9 |
-
#
|
10 |
-
tokenizer = T5Tokenizer.from_pretrained("cssupport/t5-small-awesome-text-to-sql")
|
11 |
-
model = T5ForConditionalGeneration.from_pretrained("cssupport/t5-small-awesome-text-to-sql")
|
12 |
-
|
13 |
-
# Esquema de base de datos simplificado
|
14 |
SCHEMA = """
|
15 |
Database schema:
|
16 |
Table bodegas(Id, Nombre, Encargado, Telefono, Email, Direccion, Horario, Regional, Latitud, Longitud)
|
17 |
Table maestra(CodigoSap, Descripcion, Grupo, Agrupador, Marca, Parte, Operacion, Componente)
|
18 |
"""
|
19 |
|
20 |
-
# Función
|
21 |
def generar_sql(pregunta_espanol):
|
22 |
try:
|
23 |
-
#
|
24 |
-
|
|
|
|
|
|
|
25 |
|
26 |
-
# Generar la consulta SQL
|
27 |
-
|
28 |
-
|
29 |
-
sql = tokenizer.decode(output[0], skip_special_tokens=True)
|
30 |
|
31 |
return sql
|
32 |
except Exception as e:
|
33 |
return f"Error: {str(e)}"
|
34 |
|
35 |
-
# Interfaz Gradio
|
36 |
iface = gr.Interface(
|
37 |
fn=generar_sql,
|
38 |
inputs=gr.Textbox(lines=3, label="Pregunta en español"),
|
39 |
outputs=gr.Textbox(label="Consulta SQL generada"),
|
40 |
-
title="
|
41 |
-
description="Escribe una pregunta en español sobre la base de datos y obtén la consulta SQL."
|
42 |
)
|
43 |
|
44 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
from deep_translator import GoogleTranslator
|
4 |
|
5 |
+
# Cargar el modelo de Hugging Face para text-to-SQL
|
6 |
+
model = pipeline("text2text-generation", model="tscholak/text-to-sql")
|
|
|
7 |
|
8 |
+
# Esquema de base de datos
|
|
|
|
|
|
|
|
|
9 |
SCHEMA = """
|
10 |
Database schema:
|
11 |
Table bodegas(Id, Nombre, Encargado, Telefono, Email, Direccion, Horario, Regional, Latitud, Longitud)
|
12 |
Table maestra(CodigoSap, Descripcion, Grupo, Agrupador, Marca, Parte, Operacion, Componente)
|
13 |
"""
|
14 |
|
15 |
+
# Función para generar la consulta SQL
|
16 |
def generar_sql(pregunta_espanol):
|
17 |
try:
|
18 |
+
# Traducir pregunta a inglés
|
19 |
+
pregunta_ingles = GoogleTranslator(source="es", target="en").translate(pregunta_espanol)
|
20 |
+
|
21 |
+
# Crear el prompt para el modelo
|
22 |
+
prompt = f"{SCHEMA}\ntranslate English to SQL: {pregunta_ingles}"
|
23 |
|
24 |
+
# Generar la consulta SQL usando el modelo de Hugging Face
|
25 |
+
result = model(prompt)
|
26 |
+
sql = result[0]['generated_text']
|
|
|
27 |
|
28 |
return sql
|
29 |
except Exception as e:
|
30 |
return f"Error: {str(e)}"
|
31 |
|
32 |
+
# Interfaz Gradio para desplegar
|
33 |
iface = gr.Interface(
|
34 |
fn=generar_sql,
|
35 |
inputs=gr.Textbox(lines=3, label="Pregunta en español"),
|
36 |
outputs=gr.Textbox(label="Consulta SQL generada"),
|
37 |
+
title="Generador de SQL",
|
38 |
+
description="Escribe una pregunta en español sobre la base de datos y obtén la consulta SQL.",
|
39 |
)
|
40 |
|
41 |
iface.launch()
|