grasant commited on
Commit
66684df
·
verified ·
1 Parent(s): 5982998

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -17
app.py CHANGED
@@ -97,32 +97,62 @@ ANSWERS = [
97
  def get_username():
98
  """Get the username of the logged-in user"""
99
  try:
100
- return os.environ.get("SPACE_AUTHOR")
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  except:
102
  return None
103
 
104
  def check_login_status():
105
  """Check if the user is logged in"""
106
- username = get_username()
107
- if username:
108
- return f"Logged in as {username}"
109
- else:
110
- return "Not logged in. Please log in to submit."
 
 
 
 
 
 
 
 
111
 
112
  def run_and_submit_all():
113
  """Run the agent on all questions and submit the answers"""
114
- username = get_username()
115
- if not username:
116
- return "Please log in to submit your answers.", None
117
-
118
- # Get the Space name
119
- space_name = os.environ.get("SPACE_ID", "")
120
-
121
- # Create the code URL
122
- code_url = f"https://huggingface.co/spaces/{space_name}/tree/main"
123
-
124
- # Submit to the API
125
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
  api_url = "https://agents-course-unit4-scoring.hf.space/submit"
127
  payload = {
128
  "username": username,
@@ -157,6 +187,7 @@ def run_and_submit_all():
157
  return submission_result, df
158
  else:
159
  return f"Error: {response.status_code} - {response.text}", None
 
160
  except Exception as e:
161
  return f"Error: {str(e)}", None
162
 
 
97
  def get_username():
98
  """Get the username of the logged-in user"""
99
  try:
100
+ # Try multiple methods to get the username
101
+ username = os.environ.get("SPACE_AUTHOR")
102
+
103
+ # If that doesn't work, try using the HfApi
104
+ if not username:
105
+ try:
106
+ from huggingface_hub import HfApi
107
+ api = HfApi()
108
+ user_info = api.whoami()
109
+ username = user_info.get("name", None)
110
+ except:
111
+ pass
112
+
113
+ return username
114
  except:
115
  return None
116
 
117
  def check_login_status():
118
  """Check if the user is logged in"""
119
+ try:
120
+ # Try using HfApi directly
121
+ from huggingface_hub import HfApi
122
+ api = HfApi()
123
+ user_info = api.whoami()
124
+ username = user_info.get("name", None)
125
+
126
+ if username:
127
+ return f"Logged in as {username}"
128
+ else:
129
+ return "Not logged in. Please log in to submit."
130
+ except Exception as e:
131
+ return f"Error checking login status: {str(e)}"
132
 
133
  def run_and_submit_all():
134
  """Run the agent on all questions and submit the answers"""
 
 
 
 
 
 
 
 
 
 
 
135
  try:
136
+ # Get username directly from HfApi
137
+ from huggingface_hub import HfApi
138
+ api = HfApi()
139
+ user_info = api.whoami()
140
+ username = user_info.get("name", None)
141
+
142
+ if not username:
143
+ return "Please log in to submit your answers.", None
144
+
145
+ # Get the Space name
146
+ space_name = os.environ.get("SPACE_ID", "")
147
+ if not space_name:
148
+ # Try to get it from the URL if available
149
+ space_name = f"{username}/{os.environ.get('SPACE_TITLE', 'gaia-submission')}"
150
+
151
+
152
+ # Create the code URL
153
+ code_url = f"https://huggingface.co/spaces/{space_name}/tree/main"
154
+
155
+ # Submit to the API
156
  api_url = "https://agents-course-unit4-scoring.hf.space/submit"
157
  payload = {
158
  "username": username,
 
187
  return submission_result, df
188
  else:
189
  return f"Error: {response.status_code} - {response.text}", None
190
+
191
  except Exception as e:
192
  return f"Error: {str(e)}", None
193