Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
3 |
+
from deep_translator import GoogleTranslator
|
4 |
+
import torch
|
5 |
+
# Limitar el uso de CPU para servidores lentos
|
6 |
+
torch.set_num_threads(1)
|
7 |
+
torch.set_num_interop_threads(1)
|
8 |
+
# Cargar modelo
|
9 |
+
tokenizer = T5Tokenizer.from_pretrained("cssupport/t5-small-awesome-text-to-sql")
|
10 |
+
model = T5ForConditionalGeneration.from_pretrained("cssupport/t5-small-awesome-text-to-sql")
|
11 |
+
# Esquema de base de datos
|
12 |
+
SCHEMA = """
|
13 |
+
Database schema:
|
14 |
+
Table customers(id, name, age, country)
|
15 |
+
Table orders(id, customer_id, amount)
|
16 |
+
"""
|
17 |
+
# Funci贸n principal
|
18 |
+
def generar_sql(pregunta_espanol):
|
19 |
+
try:
|
20 |
+
# Traducir pregunta a ingl茅s
|
21 |
+
pregunta_ingles = GoogleTranslator(source="es", target="en").translate(pregunta_espanol)
|
22 |
+
# Crear prompt
|
23 |
+
prompt = f"{SCHEMA}\ntranslate English to SQL: {pregunta_ingles}"
|
24 |
+
input_ids = tokenizer(prompt, return_tensors="pt").input_ids
|
25 |
+
output = model.generate(input_ids, max_length=128)
|
26 |
+
sql = tokenizer.decode(output[0], skip_special_tokens=True)
|
27 |
+
return sql
|
28 |
+
except Exception as e:
|
29 |
+
return f"Error: {str(e)}"
|
30 |
+
# Interfaz Gradio
|
31 |
+
iface = gr.Interface(
|
32 |
+
fn=generar_sql,
|
33 |
+
inputs=gr.Textbox(lines=3, label="Pregunta en espa帽ol"),
|
34 |
+
outputs=gr.Textbox(label="Consulta SQL generada"),
|
35 |
+
title="Texto a SQL (entrada en espa帽ol)",
|
36 |
+
description="Escribe una pregunta en espa帽ol sobre la base de datos y obt茅n la consulta SQL."
|
37 |
+
)
|
38 |
+
iface.launch()
|