Spaces:
Sleeping
Sleeping
fix: lazy-load inference engine inside predict_sql
Browse files
app.py
CHANGED
|
@@ -1,21 +1,9 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import spaces
|
| 3 |
-
from app.engine.inference import generate_sql
|
| 4 |
|
| 5 |
-
# 1.
|
| 6 |
-
@spaces.GPU(duration=60)
|
| 7 |
-
def predict_sql(question: str, schema_text: str = ""):
|
| 8 |
-
prompt = f"[SCHEMA] {schema_text} [QUESTION] {question} [SQL] "
|
| 9 |
-
candidates = generate_sql(prompt, n_candidates=5)
|
| 10 |
-
|
| 11 |
-
# generate_sql returns a list of candidate strings; Gradio Textbox requires a single string
|
| 12 |
-
if isinstance(candidates, list):
|
| 13 |
-
return candidates[0] if len(candidates) > 0 else "SELECT 1;"
|
| 14 |
-
return str(candidates)
|
| 15 |
-
|
| 16 |
-
# 2. UI Layout
|
| 17 |
with gr.Blocks(title="Text-to-SQL API") as demo:
|
| 18 |
-
gr.Markdown("# Text-to-SQL
|
| 19 |
|
| 20 |
with gr.Row():
|
| 21 |
question_in = gr.Textbox(
|
|
@@ -29,7 +17,17 @@ with gr.Blocks(title="Text-to-SQL API") as demo:
|
|
| 29 |
|
| 30 |
sql_out = gr.Textbox(label="Generated SQL Output")
|
| 31 |
btn = gr.Button("Generate SQL")
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
btn.click(
|
| 34 |
fn=predict_sql,
|
| 35 |
inputs=[question_in, schema_in],
|
|
@@ -37,8 +35,5 @@ with gr.Blocks(title="Text-to-SQL API") as demo:
|
|
| 37 |
api_name="generate"
|
| 38 |
)
|
| 39 |
|
| 40 |
-
if __name__ == "__main__":
|
| 41 |
-
demo.launch(
|
| 42 |
-
server_name="0.0.0.0",
|
| 43 |
-
server_port=7860
|
| 44 |
-
)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import spaces
|
|
|
|
| 3 |
|
| 4 |
+
# 1. UI Definition (instantiated first so Gradio and HF register routes)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
with gr.Blocks(title="Text-to-SQL API") as demo:
|
| 6 |
+
gr.Markdown("# Text-to-SQL Backend Microservice")
|
| 7 |
|
| 8 |
with gr.Row():
|
| 9 |
question_in = gr.Textbox(
|
|
|
|
| 17 |
|
| 18 |
sql_out = gr.Textbox(label="Generated SQL Output")
|
| 19 |
btn = gr.Button("Generate SQL")
|
| 20 |
+
|
| 21 |
+
# 2. GPU function wired inside the block context
|
| 22 |
+
@spaces.GPU(duration=60)
|
| 23 |
+
def predict_sql(question: str, schema_text: str = ""):
|
| 24 |
+
from app.engine.inference import generate_sql
|
| 25 |
+
prompt = f"[SCHEMA] {schema_text} [QUESTION] {question} [SQL] "
|
| 26 |
+
candidates = generate_sql(prompt, n_candidates=5)
|
| 27 |
+
if isinstance(candidates, list):
|
| 28 |
+
return candidates[0] if len(candidates) > 0 else "SELECT 1;"
|
| 29 |
+
return str(candidates)
|
| 30 |
+
|
| 31 |
btn.click(
|
| 32 |
fn=predict_sql,
|
| 33 |
inputs=[question_in, schema_in],
|
|
|
|
| 35 |
api_name="generate"
|
| 36 |
)
|
| 37 |
|
| 38 |
+
if __name__ == "__main__":
|
| 39 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
|
|