Spaces:
Sleeping
Sleeping
Create app.py
Browse files创建在线测试自然语言转换为sql
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from pandasql import sqldf
|
| 4 |
+
|
| 5 |
+
# 模拟数据库表
|
| 6 |
+
products = pd.DataFrame({
|
| 7 |
+
"product_id": [1, 2, 3, 4, 5],
|
| 8 |
+
"name": ["iPhone 14", "Galaxy S22", "Sony WH-1000XM5", "MacBook Pro", "Echo Dot"],
|
| 9 |
+
"category": ["Electronics", "Electronics", "Audio", "Computers", "Smart Home"],
|
| 10 |
+
"price": [799.99, 699.99, 399.99, 1999.99, 49.99],
|
| 11 |
+
"stock": [50, 40, 30, 20, 70]
|
| 12 |
+
})
|
| 13 |
+
|
| 14 |
+
orders = pd.DataFrame({
|
| 15 |
+
"order_id": [1, 2, 3],
|
| 16 |
+
"order_number": ["ORD001", "ORD002", "ORD003"],
|
| 17 |
+
"customer_id": [1, 2, 3],
|
| 18 |
+
"total_amount": [1599.98, 699.99, 399.99],
|
| 19 |
+
"status": ["PAID", "PAID", "PENDING"]
|
| 20 |
+
})
|
| 21 |
+
|
| 22 |
+
customers = pd.DataFrame({
|
| 23 |
+
"customer_id": [1, 2, 3],
|
| 24 |
+
"name": ["Alice", "Bob", "Charlie"],
|
| 25 |
+
"email": ["alice@example.com", "bob@example.com", "charlie@example.com"],
|
| 26 |
+
"phone": ["1234567890", "2345678901", "3456789012"]
|
| 27 |
+
})
|
| 28 |
+
|
| 29 |
+
order_items = pd.DataFrame({
|
| 30 |
+
"order_item_id": [1, 2, 3],
|
| 31 |
+
"order_id": [1, 2, 3],
|
| 32 |
+
"product_id": [1, 2, 3],
|
| 33 |
+
"quantity": [2, 1, 1],
|
| 34 |
+
"subtotal": [1599.98, 699.99, 399.99]
|
| 35 |
+
})
|
| 36 |
+
|
| 37 |
+
# 定义查询函数
|
| 38 |
+
def query_to_sql(sql_query):
|
| 39 |
+
try:
|
| 40 |
+
result = sqldf(sql_query, locals())
|
| 41 |
+
return result.to_string(index=False)
|
| 42 |
+
except Exception as e:
|
| 43 |
+
return f"Error: {str(e)}"
|
| 44 |
+
|
| 45 |
+
# 创建 Gradio 界面
|
| 46 |
+
interface = gr.Interface(
|
| 47 |
+
fn=query_to_sql,
|
| 48 |
+
inputs="text",
|
| 49 |
+
outputs="text",
|
| 50 |
+
title="SQL Query Simulator",
|
| 51 |
+
description="输入 SQL 查询语句,模拟查询结果。"
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
# 启动应用
|
| 55 |
+
interface.launch()
|
| 56 |
+
|