Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,39 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import torch
|
| 3 |
-
from transformers import pipeline
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
|
|
|
|
|
|
| 7 |
|
| 8 |
def generate_sql(user_question, create_table_statements):
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
# 创建 Gradio
|
| 14 |
with gr.Blocks() as demo:
|
| 15 |
gr.Markdown("## SQL Query Generator")
|
| 16 |
user_question = gr.Textbox(label="User Question", placeholder="请输入您的问题...")
|
| 17 |
-
create_table_statements = gr.Textbox(label="DDL Statements", placeholder="请输入表的
|
| 18 |
-
|
| 19 |
-
|
| 20 |
submit_btn = gr.Button("Generate SQL")
|
| 21 |
-
submit_btn.click(generate_sql, inputs=[user_question, create_table_statements], outputs=
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
|
| 24 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
|
|
|
| 4 |
|
| 5 |
+
# 加载模型和分词器
|
| 6 |
+
model_name = "defog/sqlcoder-7b-2" # 使用更新的模型以提高性能
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto") # 使用半精度以降低内存占用
|
| 9 |
|
| 10 |
def generate_sql(user_question, create_table_statements):
|
| 11 |
+
# 准备输入
|
| 12 |
+
prompt = f"Generate a SQL query to answer this question: `{user_question}`\nDDL statements:\n{create_table_statements}\nThe following SQL query best answers the question `{user_question}`:"
|
| 13 |
+
|
| 14 |
+
# 编码输入
|
| 15 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 16 |
+
|
| 17 |
+
# 生成输出
|
| 18 |
+
with torch.no_grad():
|
| 19 |
+
outputs = model.generate(**inputs, max_length=150)
|
| 20 |
+
|
| 21 |
+
# 解码输出
|
| 22 |
+
sql_query = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 23 |
+
return sql_query
|
| 24 |
|
| 25 |
+
# 创建 Gradio 接口
|
| 26 |
with gr.Blocks() as demo:
|
| 27 |
gr.Markdown("## SQL Query Generator")
|
| 28 |
user_question = gr.Textbox(label="User Question", placeholder="请输入您的问题...")
|
| 29 |
+
create_table_statements = gr.Textbox(label="DDL Statements", placeholder="请输入表的DDL语句...")
|
| 30 |
+
sql_output = gr.Textbox(label="Generated SQL Query", interactive=False)
|
| 31 |
+
|
| 32 |
submit_btn = gr.Button("Generate SQL")
|
| 33 |
+
submit_btn.click(generate_sql, inputs=[user_question, create_table_statements], outputs=sql_output)
|
| 34 |
+
|
| 35 |
+
# 启动 Gradio 应用
|
| 36 |
+
demo.launch()
|
| 37 |
|
| 38 |
|
| 39 |
if __name__ == "__main__":
|