Spaces:
Sleeping
Sleeping
Download app.py from fromnowoncoding/pet_scrapper: direct link, hf CLI and curl.
- Browser
- Download file 2.34 kB
-
https://huggingface.co/spaces/fromnowoncoding/pet_scrapper/resolve/main/app.py
- Command line
-
hf download hf://spaces/fromnowoncoding/pet_scrapper/app.py
-
curl -L -o app.py https://huggingface.co/spaces/fromnowoncoding/pet_scrapper/resolve/main/app.py
2.34 kB
| 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() | |