Heecheol's picture
feat: Add new breed choices to adoptable friends finder
9d8e547
Raw History Blame Contribute Delete
3.35 kB
import requests
import gradio as gr
import os
def fetch_animals(service_key, breed_choice, state):
url = 'http://apis.data.go.kr/1543061/abandonmentPublicSrvc/abandonmentPublic'
params = {
'serviceKey': service_key,
'kind': breed_choice,
'state': state,
'pageNo': '1',
'numOfRows': '1000',
'_type': 'json'
}
response = requests.get(url, params=params)
return response.json()
def filter_items(items):
return [item for item in items if item['sexCd'] == 'F']
def create_animal(item):
return {
# "Desertion No": item.get("desertionNo", ""),
"Thumbnail": item.get("filename", ""),
"Date Found": item["happenDt"],
"Place Found": item["happenPlace"],
"Breed": item["kindCd"],
"Color": item["colorCd"],
"Age": item["age"],
"Weight": item["weight"],
"Notice No": item["noticeNo"],
"Notice Start Date": item["noticeSdt"],
"Notice End Date": item["noticeEdt"],
"Image": item["popfile"],
"Status": item["processState"],
"Gender": item["sexCd"],
"Neutered": item["neuterYn"],
"Features": item["specialMark"],
"Shelter Name": item["careNm"],
"Shelter Phone": item["careTel"],
"Shelter Address": item["careAddr"],
"Controlling Organization": item["orgNm"],
"Responsible Person": item.get("chargeNm", ""),
"Contact Number": item["officetel"],
"Special Note": item.get("noticeComment", ""),
}
def generate_html(results):
html = "<table><tr><th>Thumbnail</th><th>Description</th></tr>"
for animal in results:
html += f"<tr><td><img src='{animal['Thumbnail']}' style='width:100px;'></td><td>"
for key, value in animal.items():
if key == "Image":
value = f"<a href='{value}' target='_blank'>{value}</a>"
if key != "Thumbnail":
html += f"<strong>{key}:</strong> {value}<br>"
html += "</td></tr>"
html += "</table>"
return html
def adoptable_friends_finder(breed_choice, state):
breed_code = breed_choice[1]
service_key = os.environ.get('abandonmentPublicSrvcKey')
response = fetch_animals(service_key, breed_code, state[1])
if 'response' not in response or 'body' not in response['response'] or 'items' not in response['response']['body']:
return "An error occurred while fetching the data. Please try again later."
items = response["response"]["body"]["items"]["item"]
filtered_items = filter_items(items)
results = [create_animal(item) for item in filtered_items]
return generate_html(results)
breed_choices = [
("비숑", "000018"),
("푸들", "000128"),
("미니푸들","000078"),
("꼬똥","000037")
]
state_choices = [
("보호중", "protect"),
("공고중", "notice")
]
input_breed_choice = gr.components.Radio(choices=breed_choices, label="Breed")
input_state_choice = gr.components.Radio(choices=state_choices, label="State")
output_data = gr.components.HTML(label="Results")
gr.Interface(
fn=adoptable_friends_finder,
inputs=[input_breed_choice, input_state_choice],
outputs=output_data,
title="AdoptableFriendsFinder",
description="Click generate button to find adoptable animals.",
).launch()