File size: 2,335 Bytes
7f57139
 
 
 
36e307d
 
 
 
 
7f57139
 
36e307d
 
 
 
 
 
589fc6b
36e307d
589fc6b
36e307d
 
 
 
 
7f57139
 
 
c9ad35d
 
 
 
 
 
7f57139
c9ad35d
 
 
 
 
7f57139
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import gradio as gr
import pandas as pd
from fetch_pets import fetch_pets
import os
import logging

# 設定日誌
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# 讀取收容所資料
try:
    logger.info(f"Current working directory: {os.getcwd()}")
    logger.info(f"Files in directory: {os.listdir()}")
    
    SHELTERS_DF = pd.read_csv('shelters.csv')
    logger.info(f"Successfully loaded shelters.csv with {len(SHELTERS_DF)} rows")
    logger.info(f"CSV columns: {SHELTERS_DF.columns.tolist()}")
    
    SHELTER_CHOICES = [(row['收容所名稱'], row['代碼']) for _, row in SHELTERS_DF.iterrows()]
    logger.info(f"Created {len(SHELTER_CHOICES)} shelter choices")
    
except Exception as e:
    logger.error(f"Error loading shelters.csv: {str(e)}")
    raise

def fetch_shelter_data(shelter_name):
    try:
        # 找到對應的收容所代碼
        shelter_code = None
        for name, code in SHELTER_CHOICES:
            if name == shelter_name:
                shelter_code = code
                break
        
        if shelter_code is None:
            return None
        
        # 抓取該收容所的狗狗資料,設定較大的頁數和每頁數量
        df = fetch_pets(shelter_code=shelter_code, animal_type="0", page_size=100, max_pages=100)
        
        # 儲存為暫時檔案
        temp_file = f"temp_{shelter_code}_pets.csv"
        df.to_csv(temp_file, index=False, encoding='utf-8-sig')
        
        return temp_file
        
    except Exception as e:
        return str(e)

# 創建 Gradio 介面
with gr.Blocks(title="台灣收容所動物資料下載器") as demo:
    gr.Markdown("""
    # 台灣收容所動物資料下載器
    選擇一個收容所,下載該收容所的動物資料。
    """)
    
    with gr.Row():
        shelter_dropdown = gr.Dropdown(
            choices=[name for name, _ in SHELTER_CHOICES],
            label="選擇收容所",
            info="選擇要下載資料的收容所"
        )
    
    with gr.Row():
        fetch_button = gr.Button("下載資料")
    
    output_file = gr.File(label="下載的資料")
    
    fetch_button.click(
        fn=fetch_shelter_data,
        inputs=shelter_dropdown,
        outputs=output_file
    )

# 啟動應用
if __name__ == "__main__":
    demo.launch()