validate username
Browse files- submit.py +3 -1
- validation.py +56 -0
submit.py
CHANGED
|
@@ -8,7 +8,7 @@ from datetime import datetime
|
|
| 8 |
import uuid
|
| 9 |
|
| 10 |
from constants import API, SUBMISSIONS_REPO
|
| 11 |
-
from validation import validate_csv_file
|
| 12 |
|
| 13 |
|
| 14 |
def make_submission(
|
|
@@ -19,6 +19,8 @@ def make_submission(
|
|
| 19 |
if user_state is None:
|
| 20 |
raise gr.Error("You must submit your username to submit a file.")
|
| 21 |
|
|
|
|
|
|
|
| 22 |
if submitted_file is None:
|
| 23 |
raise gr.Error("Please upload a CSV file before submitting.")
|
| 24 |
|
|
|
|
| 8 |
import uuid
|
| 9 |
|
| 10 |
from constants import API, SUBMISSIONS_REPO
|
| 11 |
+
from validation import validate_csv_file, validate_username
|
| 12 |
|
| 13 |
|
| 14 |
def make_submission(
|
|
|
|
| 19 |
if user_state is None:
|
| 20 |
raise gr.Error("You must submit your username to submit a file.")
|
| 21 |
|
| 22 |
+
validate_username(user_state)
|
| 23 |
+
|
| 24 |
if submitted_file is None:
|
| 25 |
raise gr.Error("Please upload a CSV file before submitting.")
|
| 26 |
|
validation.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import pandas as pd
|
| 2 |
import io
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
from constants import (
|
| 5 |
REQUIRED_COLUMNS,
|
| 6 |
ASSAY_LIST,
|
|
@@ -10,6 +11,61 @@ from constants import (
|
|
| 10 |
)
|
| 11 |
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
def validate_csv_can_be_read(file_content: str) -> pd.DataFrame:
|
| 14 |
"""
|
| 15 |
Validate that the CSV file can be read and parsed.
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
import io
|
| 3 |
import gradio as gr
|
| 4 |
+
import requests
|
| 5 |
from constants import (
|
| 6 |
REQUIRED_COLUMNS,
|
| 7 |
ASSAY_LIST,
|
|
|
|
| 11 |
)
|
| 12 |
|
| 13 |
|
| 14 |
+
def validate_username(username: str) -> bool:
|
| 15 |
+
"""
|
| 16 |
+
Validate that the username corresponds to a real Hugging Face profile.
|
| 17 |
+
Just check https://huggingface.co/username exists.
|
| 18 |
+
|
| 19 |
+
Parameters
|
| 20 |
+
----------
|
| 21 |
+
username: str
|
| 22 |
+
The username to validate
|
| 23 |
+
|
| 24 |
+
Returns
|
| 25 |
+
-------
|
| 26 |
+
bool
|
| 27 |
+
True if the username is valid and profile exists, False otherwise
|
| 28 |
+
|
| 29 |
+
Raises
|
| 30 |
+
------
|
| 31 |
+
gr.Error: If username is invalid or profile doesn't exist
|
| 32 |
+
"""
|
| 33 |
+
username = username.strip()
|
| 34 |
+
|
| 35 |
+
# Check if the Hugging Face profile exists
|
| 36 |
+
profile_url = f"https://huggingface.co/{username}"
|
| 37 |
+
|
| 38 |
+
try:
|
| 39 |
+
response = requests.get(profile_url, timeout=10)
|
| 40 |
+
|
| 41 |
+
if response.status_code == 200:
|
| 42 |
+
# Additional check: make sure it's actually a user profile page
|
| 43 |
+
# and not some other page that happens to exist
|
| 44 |
+
if "profile" in response.text.lower() or "models" in response.text.lower():
|
| 45 |
+
return True
|
| 46 |
+
else:
|
| 47 |
+
raise gr.Error(
|
| 48 |
+
f"❌ '{username}' does not appear to be a valid Hugging Face user profile"
|
| 49 |
+
)
|
| 50 |
+
elif response.status_code == 404:
|
| 51 |
+
raise gr.Error(
|
| 52 |
+
f"❌ Hugging Face user '{username}' does not exist. Please check the username or create an account at https://huggingface.co"
|
| 53 |
+
)
|
| 54 |
+
else:
|
| 55 |
+
raise gr.Error(
|
| 56 |
+
f"❌ Unable to verify username '{username}'. Please try again later."
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
except requests.exceptions.Timeout:
|
| 60 |
+
raise gr.Error("❌ Timeout while checking username. Please try again.")
|
| 61 |
+
except requests.exceptions.ConnectionError:
|
| 62 |
+
raise gr.Error(
|
| 63 |
+
"❌ Unable to connect to Hugging Face. Please check your internet connection."
|
| 64 |
+
)
|
| 65 |
+
except requests.exceptions.RequestException as e:
|
| 66 |
+
raise gr.Error(f"❌ Error validating username: {str(e)}")
|
| 67 |
+
|
| 68 |
+
|
| 69 |
def validate_csv_can_be_read(file_content: str) -> pd.DataFrame:
|
| 70 |
"""
|
| 71 |
Validate that the CSV file can be read and parsed.
|