Spaces:
Sleeping
Sleeping
File size: 11,510 Bytes
dd7df46 51f7cd4 dd7df46 51f7cd4 dd7df46 51f7cd4 dd7df46 51f7cd4 dd7df46 51f7cd4 dd7df46 51f7cd4 dd7df46 51f7cd4 dd7df46 51f7cd4 dd7df46 51f7cd4 dd7df46 |
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 |
"""
Shared constants for the NLP resources application.
Contains lists of countries, languages, domains, tasks, and utility functions.
"""
import re
from urllib.parse import urlparse
import pandas as pd
# Countries where Spanish is spoken
COUNTRIES = [
"Spain",
"Mexico",
"Argentina",
"Colombia",
"Peru",
"Venezuela",
"Chile",
"Ecuador",
"Guatemala",
"Cuba",
"Bolivia",
"Dominican Republic",
"Honduras",
"Paraguay",
"El Salvador",
"Nicaragua",
"Costa Rica",
"Panama",
"Uruguay",
"Puerto Rico",
"Portugal",
"Brazil",
"International",
]
# Languages
LANGUAGES = [
"spanish",
"catalan",
"basque",
"galician",
"guarani",
"quechua",
"aymara",
"nauhatl",
"mapudungun",
"spain",
"latam",
"portuguese",
"all",
]
# NLP tasks
TASKS = [
"text classification",
"sentiment analysis",
"named entity recognition",
"part-of-speech tagging",
"question answering",
"text summarization",
"machine translation",
"language modeling",
"text generation",
"information extraction",
"semantic similarity",
"natural language inference",
]
# Domains for datasets
DOMAINS = [
"clinical",
"legal",
"financial",
"scientific",
"news",
"social media",
"literature",
"general",
"academic",
]
# Dataset types
DATASET_TYPES = ["pretraining", "benchmark", "supervised fine-tuning", "alignment"]
# Event types
EVENT_TYPES = ["workshop", "talk", "AMA", "round table"]
# Initiative types
INITIATIVE_TYPES = [
"project",
"event",
"research group",
"community",
"research institute",
"non-profit",
"OS company",
]
# Technical levels (for events)
TECHNICAL_LEVELS = ["1", "2", "3", "4", "5"]
def format_url_for_display(url: str) -> str:
"""
Format URL for display in tables - show only the meaningful part.
Args:
url: Full URL string
Returns:
Shortened, readable version of the URL
"""
if not url or not url.strip():
return ""
url = url.strip()
# Remove protocol
if url.startswith(("http://", "https://")):
url = url.split("://", 1)[1]
# Special handling for common domains
if "huggingface.co" in url:
# Extract the meaningful part after huggingface.co
if "/datasets/" in url:
return url.split("/datasets/")[-1]
elif "/models/" in url:
return url.split("/models/")[-1]
elif "/collections/" in url:
return url.split("/collections/")[-1]
else:
# Return everything after huggingface.co/
parts = url.split("huggingface.co/")
return parts[-1] if len(parts) > 1 else url
elif "github.com" in url:
# Extract repo name (owner/repo)
parts = url.split("github.com/")
if len(parts) > 1:
repo_path = parts[-1].split("/")
if len(repo_path) >= 2:
return f"{repo_path[0]}/{repo_path[1]}"
return url
elif "zenodo.org" in url:
# Extract record ID
if "/record/" in url:
return f"zenodo:{url.split('/record/')[-1].split('/')[0]}"
return url
elif "arxiv.org" in url:
# Extract arXiv ID
if "/abs/" in url:
return f"arXiv:{url.split('/abs/')[-1]}"
elif "/pdf/" in url:
return f"arXiv:{url.split('/pdf/')[-1].replace('.pdf', '')}"
return url
elif "youtube.com" in url or "youtu.be" in url:
# Extract video ID or title if available
if "watch?v=" in url:
video_id = url.split("watch?v=")[-1].split("&")[0]
return f"YouTube:{video_id[:8]}..."
elif "youtu.be/" in url:
video_id = url.split("youtu.be/")[-1].split("?")[0]
return f"YouTube:{video_id[:8]}..."
return url
else:
# For other URLs, try to extract domain and path
try:
parsed = urlparse(
f"https://{url}" if not url.startswith(("http://", "https://")) else url
)
domain = parsed.netloc
path = parsed.path.strip("/")
if path:
# Show domain + first part of path
path_parts = path.split("/")
if len(path_parts) > 0 and path_parts[0]:
return f"{domain}/{path_parts[0]}"
return domain
except:
# Fallback: limit length
return url[:30] + "..." if len(url) > 30 else url
def make_url_clickable(url: str, display_text: str = None) -> str:
"""
Convert URL to clickable HTML link.
Args:
url: Full URL
display_text: Text to display for the link (optional)
Returns:
HTML link string
"""
# Handle non-string types (like float NaN values)
if url is None or (isinstance(url, float) and pd.isna(url)):
return ""
url = str(url).strip()
if not url or url.lower() in ["nan", "none", ""]:
return ""
# Ensure URL has protocol
if not url.startswith(("http://", "https://")):
url = f"https://{url}"
# Use provided display text or format the URL
text = display_text if display_text else format_url_for_display(url)
return f'<a target="_blank" href="{url}" style="color: var(--link-text-color); text-decoration: underline; text-decoration-style: dotted;">{text}</a>'
def get_column_display_names():
"""
Return mapping of column names to pretty display names.
Returns:
Dictionary mapping column names to display names
"""
return {
# Common fields
"name": "Name",
"submitted_by": "Submitted By",
"date_submitted": "Date Submitted",
# Dataset fields
"github_url": "GitHub",
"huggingface_url": "HF Dataset",
"zenodo_url": "Zenodo",
"paper_url": "Paper",
"website_url": "Website",
"dataset_type": "Type",
"task": "Tasks",
"domain": "Domain",
"countries": "Countries",
"languages": "Languages",
# Model fields
"familia": "Family",
"available_sizes": "Sizes (B)",
"hf_collection_url": "HF Collection",
# Event fields
"titulo": "Title",
"ponente": "Speaker",
"bio": "Bio",
"tipo": "Type",
"tema": "Topic",
"nivel_tecnico": "Tech Level",
"fecha": "Date",
"youtube": "YouTube",
# Shared task fields
"conference_name": "Conference",
"workshop_date": "Workshop Date",
"registration_deadline": "Registration",
"data_available_date": "Data Available",
"submission_deadline": "Submission",
"more_info_url": "More Info",
# Initiative fields
"type": "Type",
}
def format_dataframe_for_display(df, url_columns=None, hide_columns=None):
"""
Format a DataFrame for better display in Gradio tables with clickable URLs.
Args:
df: Pandas DataFrame
url_columns: List of column names that contain URLs
hide_columns: List of column names to hide
Returns:
Formatted DataFrame
"""
if df.empty:
return df
# Make a copy to avoid modifying original
display_df = df.copy()
# Hide specified columns
if hide_columns:
display_df = display_df.drop(
columns=[col for col in hide_columns if col in display_df.columns]
)
# Format URL columns with clickable links
if url_columns:
for col in url_columns:
if col in display_df.columns:
display_df[col] = display_df[col].apply(
lambda x: (
make_url_clickable(x) if pd.notna(x) and str(x).strip() else ""
)
)
# Ensure first column content doesn't wrap (for name/title columns)
first_col = display_df.columns[0] if len(display_df.columns) > 0 else None
if first_col:
# Keep full text but ensure it displays in a single line (no wrapping)
# Replace line breaks and excessive whitespace to ensure single line display
display_df[first_col] = display_df[first_col].apply(
lambda x: str(x).replace("\n", " ").replace("\r", " ").strip() if x else ""
)
# Rename columns to pretty names
column_names = get_column_display_names()
display_df = display_df.rename(columns=column_names)
return display_df
def format_dataframe_for_html_display(df, url_columns=None, hide_columns=None):
"""
Format a DataFrame for HTML display with clickable links.
Args:
df: Pandas DataFrame
url_columns: List of column names that contain URLs
hide_columns: List of column names to hide
Returns:
HTML string representation of the DataFrame
"""
if df.empty:
return "<p>No data available</p>"
# Make a copy to avoid modifying original
display_df = df.copy()
# Hide specified columns
if hide_columns:
display_df = display_df.drop(
columns=[col for col in hide_columns if col in display_df.columns]
)
# Format URL columns with clickable links
if url_columns:
for col in url_columns:
if col in display_df.columns:
display_df[col] = display_df[col].apply(
lambda x: (
make_url_clickable(x) if pd.notna(x) and str(x).strip() else ""
)
)
# Ensure first column content doesn't wrap (for name/title columns)
first_col = display_df.columns[0] if len(display_df.columns) > 0 else None
if first_col:
# Keep full text but ensure it displays in a single line (no wrapping)
# Replace line breaks and excessive whitespace to ensure single line display
display_df[first_col] = display_df[first_col].apply(
lambda x: str(x).replace("\n", " ").replace("\r", " ").strip() if x else ""
)
# Rename columns to pretty names
column_names = get_column_display_names()
display_df = display_df.rename(columns=column_names)
# Convert to HTML with custom styling
html = display_df.to_html(
escape=False, # Allow HTML in cells
index=False, # Don't show row indices
classes="dataframe-table",
table_id="resources-table",
)
# Add custom CSS styling
styled_html = f"""
<style>
.dataframe-table {{
border-collapse: collapse;
margin: 25px 0;
font-size: 0.9em;
font-family: sans-serif;
min-width: 400px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.15);
width: 100%;
}}
.dataframe-table thead tr {{
background-color: #009879;
color: #ffffff;
text-align: left;
}}
.dataframe-table th,
.dataframe-table td {{
padding: 12px 15px;
border: 1px solid #dddddd;
}}
.dataframe-table tbody tr {{
border-bottom: 1px solid #dddddd;
}}
.dataframe-table tbody tr:nth-of-type(even) {{
background-color: #f3f3f3;
}}
.dataframe-table tbody tr:hover {{
background-color: #f5f5f5;
}}
.dataframe-table a {{
color: #009879;
text-decoration: none;
}}
.dataframe-table a:hover {{
text-decoration: underline;
}}
</style>
{html}
"""
return styled_html
|