Skip to content

Commit dc8091e

Browse files
Update app.py
Signed-off-by: PROJECT ZERO <[email protected]>
1 parent 6226dbe commit dc8091e

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

app.py

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,24 @@
7272
}
7373

7474
# Configure logging
75-
logging.basicConfig(level=logging.ERROR, format='%(asctime)s - %(levelname)s - %(message)s')
75+
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
76+
7677

7778
async def random_url(_):
78-
try:
79-
pet = random.choice(["cat", "dog"])
80-
api_url = f"https://api.the{pet}api.com/v1/images/search"
81-
async with aiohttp.ClientSession() as session:
82-
async with session.get(api_url) as resp:
83-
resp.raise_for_status()
84-
return (await resp.json())[0]["url"]
85-
except aiohttp.ClientError as e:
86-
logging.error(f"API request failed: {e}")
87-
return None
79+
retries = 3
80+
for _ in range(retries):
81+
try:
82+
pet = random.choice(["cat", "dog"])
83+
api_url = f"https://api.the{pet}api.com/v1/images/search"
84+
async with aiohttp.ClientSession() as session:
85+
async with session.get(api_url) as resp:
86+
resp.raise_for_status()
87+
return (await resp.json())[0]["url"]
88+
except aiohttp.ClientError as e:
89+
logging.error(f"API request failed: {e}")
90+
except Exception as e:
91+
logging.error(f"Unexpected error: {e}")
92+
return None
8893

8994

9095
@pn.cache
@@ -97,14 +102,18 @@ def load_processor_model(
97102

98103

99104
async def open_image_url(image_url: str) -> Image:
100-
try:
101-
async with aiohttp.ClientSession() as session:
102-
async with session.get(image_url) as resp:
103-
resp.raise_for_status()
104-
return Image.open(io.BytesIO(await resp.read()))
105-
except aiohttp.ClientError as e:
106-
logging.error(f"HTTP request failed: {e}")
107-
return None
105+
retries = 3
106+
for _ in range(retries):
107+
try:
108+
async with aiohttp.ClientSession() as session:
109+
async with session.get(image_url) as resp:
110+
resp.raise_for_status()
111+
return Image.open(io.BytesIO(await resp.read()))
112+
except aiohttp.ClientError as e:
113+
logging.error(f"HTTP request failed: {e}")
114+
except Exception as e:
115+
logging.error(f"Unexpected error: {e}")
116+
return None
108117

109118

110119
def get_similarity_scores(class_items: List[str], image: Image) -> List[float]:

0 commit comments

Comments
 (0)