Spaces:
Sleeping
Sleeping
ardaatahan
commited on
Upload folder using huggingface_hub
Browse files- stack_overflow_users.py +41 -30
stack_overflow_users.py
CHANGED
@@ -23,7 +23,7 @@ def filter_profile_data(data):
|
|
23 |
|
24 |
:param data: array of dicts containing raw user profile information
|
25 |
:return: array of dicts filtered user profile information
|
26 |
-
"""
|
27 |
data = data["items"]
|
28 |
data = data[: min(MAX_USERS, len(data))]
|
29 |
keys_to_keep = ["reputation", "location", "display_name", "link", "profile_image"]
|
@@ -42,7 +42,7 @@ async def fetch_stack_overflow_profiles(url):
|
|
42 |
|
43 |
:param url: URL of API to be called
|
44 |
:return: Raw user profile data if response is successful else error message
|
45 |
-
"""
|
46 |
async with aiohttp.ClientSession() as session:
|
47 |
try:
|
48 |
async with session.get(url, timeout=10) as response:
|
@@ -64,7 +64,7 @@ async def download_profile_image(url):
|
|
64 |
|
65 |
:param url: URL of profile image
|
66 |
:return: RGB decoded image if image can be downloaded else error message
|
67 |
-
"""
|
68 |
async with aiohttp.ClientSession() as session:
|
69 |
try:
|
70 |
async with session.get(url, timeout=10) as response:
|
@@ -85,10 +85,10 @@ def detect_face_in_image(image):
|
|
85 |
"""
|
86 |
Detects whether a there is a face in the input image using dlib.
|
87 |
|
88 |
-
:param image: Image to check for face
|
89 |
:return: Image with bounding box highlighting face if face exists else original image
|
90 |
:return: True if face exists in image else False
|
91 |
-
"""
|
92 |
detector = dlib.get_frontal_face_detector()
|
93 |
faces, _, _ = detector.run(image, 1, -0.5)
|
94 |
for face in faces:
|
@@ -109,11 +109,11 @@ def fetch_and_process_users():
|
|
109 |
Higher level function to fetch users and analyze their profile images.
|
110 |
|
111 |
:return: HTML content displaying fetched user information and error messages as necessary
|
112 |
-
"""
|
113 |
profiles = asyncio.run(fetch_stack_overflow_profiles(URL))
|
114 |
if type(profiles) is str:
|
115 |
return get_error_html(profiles)
|
116 |
-
|
117 |
user_content = []
|
118 |
|
119 |
for profile in profiles:
|
@@ -121,7 +121,9 @@ def fetch_and_process_users():
|
|
121 |
image = asyncio.run(download_profile_image(image_url)) if image_url else None
|
122 |
|
123 |
if image is None:
|
124 |
-
user_html = get_user_html(
|
|
|
|
|
125 |
user_content.append(user_html)
|
126 |
continue
|
127 |
|
@@ -137,10 +139,10 @@ def fetch_and_process_users():
|
|
137 |
else "No face detected in the user profile image!"
|
138 |
)
|
139 |
image = image.decode("utf-8")
|
140 |
-
|
141 |
user_html = get_user_html(image, profile, face_message, None)
|
142 |
user_content.append(user_html)
|
143 |
-
|
144 |
return "".join(user_content)
|
145 |
|
146 |
|
@@ -148,9 +150,9 @@ def get_error_html(error_message):
|
|
148 |
"""
|
149 |
Constructs an HTML template to display error message.
|
150 |
|
151 |
-
:param error_message: Message to be displayed
|
152 |
:return: HTML code with the error message
|
153 |
-
"""
|
154 |
return f"""
|
155 |
<div style='display: flex; flex-direction: column; align-items: center; margin-bottom: 5rem;'>
|
156 |
<div style='text-align: center; font-size: 16px;'>
|
@@ -166,37 +168,46 @@ def get_user_html(image, profile, face_message, error_message):
|
|
166 |
Constructs an HTML template to display user information and error message.
|
167 |
|
168 |
:param image: Base64 image to be displayed on the page
|
169 |
-
:param profile: Profile information to be displayed
|
170 |
:param face_message: Message that indicates whether face exists or not in the picture
|
171 |
:param error_message: Error message to display in case image cannot be fetched
|
172 |
:return: HTML code with the user information with a potential error message
|
173 |
-
"""
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
<img src="data:image/jpeg;base64,{image}" alt="Profile image" style=
|
178 |
-
</div>
|
179 |
-
|
|
|
|
|
|
|
|
|
|
|
180 |
<br>
|
181 |
-
</div>
|
182 |
-
|
183 |
-
|
|
|
|
|
|
|
|
|
|
|
184 |
<br>
|
185 |
-
<strong style=
|
186 |
<br>
|
187 |
-
<strong style=
|
188 |
<br>
|
189 |
-
<a href="{profile.get("link", "")}" target="_blank" style=
|
190 |
-
{f"""<br>
|
191 |
-
<span style='font-size: 16px; color: grey;'>{face_message}</span>""" if error_message is None else """"""}
|
192 |
</div>
|
193 |
</div>
|
194 |
-
|
|
|
195 |
|
196 |
|
197 |
def get_html_content():
|
198 |
"""
|
199 |
-
Constructs the whole HTML template to be displayed to the user.
|
200 |
|
201 |
:return: HTML code to display each user's information and error messages
|
202 |
"""
|
|
|
23 |
|
24 |
:param data: array of dicts containing raw user profile information
|
25 |
:return: array of dicts filtered user profile information
|
26 |
+
"""
|
27 |
data = data["items"]
|
28 |
data = data[: min(MAX_USERS, len(data))]
|
29 |
keys_to_keep = ["reputation", "location", "display_name", "link", "profile_image"]
|
|
|
42 |
|
43 |
:param url: URL of API to be called
|
44 |
:return: Raw user profile data if response is successful else error message
|
45 |
+
"""
|
46 |
async with aiohttp.ClientSession() as session:
|
47 |
try:
|
48 |
async with session.get(url, timeout=10) as response:
|
|
|
64 |
|
65 |
:param url: URL of profile image
|
66 |
:return: RGB decoded image if image can be downloaded else error message
|
67 |
+
"""
|
68 |
async with aiohttp.ClientSession() as session:
|
69 |
try:
|
70 |
async with session.get(url, timeout=10) as response:
|
|
|
85 |
"""
|
86 |
Detects whether a there is a face in the input image using dlib.
|
87 |
|
88 |
+
:param image: Image to check for face
|
89 |
:return: Image with bounding box highlighting face if face exists else original image
|
90 |
:return: True if face exists in image else False
|
91 |
+
"""
|
92 |
detector = dlib.get_frontal_face_detector()
|
93 |
faces, _, _ = detector.run(image, 1, -0.5)
|
94 |
for face in faces:
|
|
|
109 |
Higher level function to fetch users and analyze their profile images.
|
110 |
|
111 |
:return: HTML content displaying fetched user information and error messages as necessary
|
112 |
+
"""
|
113 |
profiles = asyncio.run(fetch_stack_overflow_profiles(URL))
|
114 |
if type(profiles) is str:
|
115 |
return get_error_html(profiles)
|
116 |
+
|
117 |
user_content = []
|
118 |
|
119 |
for profile in profiles:
|
|
|
121 |
image = asyncio.run(download_profile_image(image_url)) if image_url else None
|
122 |
|
123 |
if image is None:
|
124 |
+
user_html = get_user_html(
|
125 |
+
None, profile, "", "Image for this user could not be fetched!"
|
126 |
+
)
|
127 |
user_content.append(user_html)
|
128 |
continue
|
129 |
|
|
|
139 |
else "No face detected in the user profile image!"
|
140 |
)
|
141 |
image = image.decode("utf-8")
|
142 |
+
|
143 |
user_html = get_user_html(image, profile, face_message, None)
|
144 |
user_content.append(user_html)
|
145 |
+
|
146 |
return "".join(user_content)
|
147 |
|
148 |
|
|
|
150 |
"""
|
151 |
Constructs an HTML template to display error message.
|
152 |
|
153 |
+
:param error_message: Message to be displayed
|
154 |
:return: HTML code with the error message
|
155 |
+
"""
|
156 |
return f"""
|
157 |
<div style='display: flex; flex-direction: column; align-items: center; margin-bottom: 5rem;'>
|
158 |
<div style='text-align: center; font-size: 16px;'>
|
|
|
168 |
Constructs an HTML template to display user information and error message.
|
169 |
|
170 |
:param image: Base64 image to be displayed on the page
|
171 |
+
:param profile: Profile information to be displayed
|
172 |
:param face_message: Message that indicates whether face exists or not in the picture
|
173 |
:param error_message: Error message to display in case image cannot be fetched
|
174 |
:return: HTML code with the user information with a potential error message
|
175 |
+
"""
|
176 |
+
if error_message is None:
|
177 |
+
image_html = f"""
|
178 |
+
<div style="margin-bottom: 1rem;">
|
179 |
+
<img src="data:image/jpeg;base64,{image}" alt="Profile image" style="width: 100%; height: 100%; object-fit: cover;">
|
180 |
+
</div>
|
181 |
+
<span style="font-size: 16px; color: grey;">{face_message}</span>
|
182 |
+
"""
|
183 |
+
else:
|
184 |
+
image_html = f"""
|
185 |
+
<div style="text-align: center; font-size: 16px; margin-bottom: 1rem;">
|
186 |
+
<strong style="font-size: 18px;"></strong> {error_message} Try generating again.
|
187 |
<br>
|
188 |
+
</div>
|
189 |
+
"""
|
190 |
+
|
191 |
+
user_html = f"""
|
192 |
+
<div style="display: flex; flex-direction: column; align-items: center; margin-bottom: 5rem;">
|
193 |
+
{image_html}
|
194 |
+
<div style="text-align: center; font-size: 16px;">
|
195 |
+
<strong style="font-size: 18px;">Name:</strong> {profile.get("display_name", "Not available")}
|
196 |
<br>
|
197 |
+
<strong style="font-size: 18px;">Reputation:</strong> {profile.get("reputation", "Not available")}
|
198 |
<br>
|
199 |
+
<strong style="font-size: 18px;">Location:</strong> {profile.get("location", "Not available")}
|
200 |
<br>
|
201 |
+
<a href="{profile.get("link", "")}" target="_blank" style="font-size: 16px; color: blue; text-decoration: none;">View Profile</a>
|
|
|
|
|
202 |
</div>
|
203 |
</div>
|
204 |
+
"""
|
205 |
+
return user_html
|
206 |
|
207 |
|
208 |
def get_html_content():
|
209 |
"""
|
210 |
+
Constructs the whole HTML template to be displayed to the user.
|
211 |
|
212 |
:return: HTML code to display each user's information and error messages
|
213 |
"""
|