Spaces:
Running
Running
Commit
·
a8f26e0
1
Parent(s):
b0b7ac2
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
import datetime
|
|
|
4 |
|
5 |
# Function to fetch data from the API
|
6 |
def fetch_tenders(date, category, type_, unit_name, unit_id, job_number, name):
|
@@ -19,15 +20,15 @@ def fetch_tenders(date, category, type_, unit_name, unit_id, job_number, name):
|
|
19 |
|
20 |
# Filter data based on inputs
|
21 |
filtered_data = [
|
22 |
-
|
23 |
-
item.get("name", "N/A"),
|
24 |
-
item.get("unit", "N/A"),
|
25 |
-
item.get("category", "N/A"),
|
26 |
-
item.get("type", "N/A"),
|
27 |
-
item.get("price", "N/A"),
|
28 |
-
date,
|
29 |
-
item.get("url", "N/A")
|
30 |
-
|
31 |
for item in data
|
32 |
if (
|
33 |
(category == "不限" or item.get("category") == category) and
|
@@ -38,12 +39,12 @@ def fetch_tenders(date, category, type_, unit_name, unit_id, job_number, name):
|
|
38 |
(not name or name in item.get("name", ""))
|
39 |
)
|
40 |
]
|
41 |
-
return filtered_data if filtered_data else [
|
42 |
|
43 |
except ValueError:
|
44 |
-
return [
|
45 |
except requests.exceptions.RequestException as e:
|
46 |
-
return [
|
47 |
|
48 |
|
49 |
# Gradio Interface
|
@@ -52,7 +53,7 @@ def create_interface():
|
|
52 |
gr.Markdown("## 政府招標查詢工具\n通過日期、採購性質、招標方式以及其他關鍵字篩選政府招標公告。")
|
53 |
|
54 |
with gr.Row():
|
55 |
-
date_input = gr.Text(label="查詢日期 (YYYY-MM-DD)", placeholder="
|
56 |
category_dropdown = gr.Dropdown(
|
57 |
choices=["不限", "工程類", "財物類", "勞務類"],
|
58 |
label="採購性質",
|
@@ -75,17 +76,28 @@ def create_interface():
|
|
75 |
submit_button = gr.Button("查詢")
|
76 |
|
77 |
# Output for displaying results
|
78 |
-
output = gr.
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
submit_button.click(
|
84 |
-
|
85 |
inputs=[date_input, category_dropdown, type_dropdown,
|
86 |
unit_name_input, unit_id_input, job_number_input, name_input],
|
87 |
-
outputs=output
|
88 |
)
|
|
|
89 |
|
90 |
demo.launch()
|
91 |
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
import datetime
|
4 |
+
import pandas as pd
|
5 |
|
6 |
# Function to fetch data from the API
|
7 |
def fetch_tenders(date, category, type_, unit_name, unit_id, job_number, name):
|
|
|
20 |
|
21 |
# Filter data based on inputs
|
22 |
filtered_data = [
|
23 |
+
{
|
24 |
+
"標案名稱": f'<a href="{item.get("url", "#")}" target="_blank">{item.get("name", "N/A")}</a>',
|
25 |
+
"機關名稱": item.get("unit", "N/A"),
|
26 |
+
"類別": item.get("category", "N/A"),
|
27 |
+
"招標方式": item.get("type", "N/A"),
|
28 |
+
"價格": item.get("price", "N/A"),
|
29 |
+
"日期": date,
|
30 |
+
"連結": item.get("url", "N/A")
|
31 |
+
}
|
32 |
for item in data
|
33 |
if (
|
34 |
(category == "不限" or item.get("category") == category) and
|
|
|
39 |
(not name or name in item.get("name", ""))
|
40 |
)
|
41 |
]
|
42 |
+
return pd.DataFrame(filtered_data) if filtered_data else pd.DataFrame([{"查無資料": ""}])
|
43 |
|
44 |
except ValueError:
|
45 |
+
return pd.DataFrame([{"Error": "日期格式錯誤,請使用 YYYY-MM-DD 格式"}])
|
46 |
except requests.exceptions.RequestException as e:
|
47 |
+
return pd.DataFrame([{"Error": f"無法取得資料: {str(e)}"}])
|
48 |
|
49 |
|
50 |
# Gradio Interface
|
|
|
53 |
gr.Markdown("## 政府招標查詢工具\n通過日期、採購性質、招標方式以及其他關鍵字篩選政府招標公告。")
|
54 |
|
55 |
with gr.Row():
|
56 |
+
date_input = gr.Text(label="查詢日期 (YYYY-MM-DD)", placeholder="可不填, 預設為今天")
|
57 |
category_dropdown = gr.Dropdown(
|
58 |
choices=["不限", "工程類", "財物類", "勞務類"],
|
59 |
label="採購性質",
|
|
|
76 |
submit_button = gr.Button("查詢")
|
77 |
|
78 |
# Output for displaying results
|
79 |
+
output = gr.HTML(label="查詢結果")
|
80 |
+
download_button = gr.Button("導出 CSV")
|
81 |
+
|
82 |
+
# Handle fetch and display
|
83 |
+
def handle_query(date, category, type_, unit_name, unit_id, job_number, name):
|
84 |
+
df = fetch_tenders(date, category, type_, unit_name, unit_id, job_number, name)
|
85 |
+
return df.to_html(escape=False, index=False), df
|
86 |
+
|
87 |
+
# Handle CSV export
|
88 |
+
def export_csv(df):
|
89 |
+
file_path = "/tmp/tender_results.csv"
|
90 |
+
df.to_csv(file_path, index=False)
|
91 |
+
return file_path
|
92 |
+
|
93 |
+
# Button actions
|
94 |
submit_button.click(
|
95 |
+
handle_query,
|
96 |
inputs=[date_input, category_dropdown, type_dropdown,
|
97 |
unit_name_input, unit_id_input, job_number_input, name_input],
|
98 |
+
outputs=[output, gr.Variable()]
|
99 |
)
|
100 |
+
download_button.click(export_csv, inputs=[gr.Variable()], outputs=gr.File())
|
101 |
|
102 |
demo.launch()
|
103 |
|