samsl commited on
Commit
aeaaf52
·
1 Parent(s): 29aa9cb

oauth and pdb struct reading bug fixes

Browse files
Files changed (1) hide show
  1. app.py +45 -26
app.py CHANGED
@@ -17,7 +17,8 @@ from biotite.sequence import io as seqio
17
  from biotite.structure import filter_amino_acids, io, spread_residue_wise, to_sequence
18
  from gradio_molecule3d import Molecule3D
19
  from huggingface_hub import HfApi
20
- from huggingface_hub.utils import RepositoryNotFoundError, GatedRepoError
 
21
  from matplotlib.cm import ScalarMappable
22
  from matplotlib.colors import Normalize
23
 
@@ -118,15 +119,17 @@ def predict_rocketshp(
118
  structure_code: str | None,
119
  structure_file: str | None,
120
  chain_id: str | None,
121
- profile: gr.OAuthProfile | None,
122
  ):
123
- print(f"sequence text: {sequence}")
124
- print(f"sequence file: {sequence_file}")
125
- print(f"structure code: {structure_code}")
126
- print(f"structure file: {structure_file}")
127
- print(f"model variant: {model_variant}")
128
-
129
- is_authorized, token = check_user_access(profile)
 
 
130
  if not is_authorized:
131
  raise gr.Error("Failed to authorize repository access.")
132
 
@@ -141,7 +144,7 @@ def predict_rocketshp(
141
  gr.Warning("Sequence file provided, ignoring text box.")
142
 
143
  sequence = str(seqio.load_sequence(sequence_file))
144
- print(sequence)
145
 
146
  elif sequence == "":
147
  raise gr.Error("Sequence input is required for the selected model.")
@@ -160,12 +163,15 @@ def predict_rocketshp(
160
  "pdb",
161
  target_path=structure_tmp_dir.name,
162
  )
163
- print(structure_tmp_dir)
164
- print(structure_file)
165
  elif structure_code != "":
166
  gr.Warning(f"PDB file provided, ignoring PDB code {structure_code}.")
167
 
168
- structure = io.load_structure(structure_file)
 
 
 
169
 
170
  if isinstance(structure, bs.AtomArrayStack):
171
  gr.Info(
@@ -201,8 +207,8 @@ def predict_rocketshp(
201
  f"No amino acid residues found in chain {chain_id} of the provided structure."
202
  )
203
 
204
- print(len(structure))
205
- print(structure[:3])
206
 
207
  struct_features = load_structure(structure, device=device)
208
  sequence = str(to_sequence(structure)[0][0])
@@ -210,6 +216,14 @@ def predict_rocketshp(
210
 
211
  # Make predictions
212
  with torch.no_grad():
 
 
 
 
 
 
 
 
213
  try:
214
  dynamics_pred = model(
215
  {
@@ -268,10 +282,10 @@ def cluster_network(G: nx.Graph, k: int = 5):
268
  Cluster the network using Girvan-Newman algorithm.
269
  """
270
 
271
- print(f"Nodes: {G.number_of_nodes()}")
272
- print(f"Edges: {G.number_of_edges()}")
273
- print(f"Number of connected components: {nx.number_connected_components(G)}")
274
- print(f"Connected: {nx.is_connected(G)}")
275
 
276
  comp = nx.community.girvan_newman(G)
277
  # limited = itertools.takewhile(lambda c: len(c) <= k, comp)
@@ -409,35 +423,40 @@ def visualize_network(
409
  return fig, bc_highlight, comm_highlight, out_cluster_file_name
410
 
411
 
412
- def check_user_access(profile: gr.OAuthProfile | None):
413
  """Check if user is logged in and has access to private repo"""
414
- if profile is None:
415
  return False, "Please log in to use this Space"
416
 
417
  try:
418
  # Try to access a file from your private repo
419
- api = HfApi(token=profile.oauth_info.access_token)
420
 
421
  # Test access by trying to get repo info
422
  api.repo_info(
423
  repo_id="EvolutionaryScale/esm3-sm-open-v1",
424
  repo_type="model", # or "dataset" or "space"
425
- token=profile.oauth_info.access_token,
426
  )
427
 
428
- return True, profile.oauth_info.access_token
 
 
 
 
429
 
430
  except GatedRepoError:
431
  return (
432
  False,
 
433
  "You need to request access to the private repository at https://huggingface.co/username/private-repo-name",
434
  )
435
 
436
  except RepositoryNotFoundError:
437
- return False, "You don't have access to the required repository"
438
 
439
  except Exception as e:
440
- return False, f"Error checking access: {str(e)}"
441
 
442
 
443
  reps = [
 
17
  from biotite.structure import filter_amino_acids, io, spread_residue_wise, to_sequence
18
  from gradio_molecule3d import Molecule3D
19
  from huggingface_hub import HfApi
20
+ from huggingface_hub.utils import GatedRepoError, RepositoryNotFoundError
21
+ from loguru import logger
22
  from matplotlib.cm import ScalarMappable
23
  from matplotlib.colors import Normalize
24
 
 
119
  structure_code: str | None,
120
  structure_file: str | None,
121
  chain_id: str | None,
122
+ token: gr.OAuthToken | None,
123
  ):
124
+ logger.info(f"sequence text: {sequence}")
125
+ logger.info(f"sequence file: {sequence_file}")
126
+ logger.info(f"structure code: {structure_code}")
127
+ logger.info(f"structure file: {structure_file}")
128
+ logger.info(f"model variant: {model_variant}")
129
+
130
+ is_authorized, token, message = check_user_access(token)
131
+ logger.info(is_authorized)
132
+ logger.info(message)
133
  if not is_authorized:
134
  raise gr.Error("Failed to authorize repository access.")
135
 
 
144
  gr.Warning("Sequence file provided, ignoring text box.")
145
 
146
  sequence = str(seqio.load_sequence(sequence_file))
147
+ logger.info(sequence)
148
 
149
  elif sequence == "":
150
  raise gr.Error("Sequence input is required for the selected model.")
 
163
  "pdb",
164
  target_path=structure_tmp_dir.name,
165
  )
166
+ logger.info(structure_tmp_dir)
167
+ logger.info(structure_file)
168
  elif structure_code != "":
169
  gr.Warning(f"PDB file provided, ignoring PDB code {structure_code}.")
170
 
171
+ try:
172
+ structure = io.load_structure(structure_file)
173
+ except ValueError:
174
+ raise gr.Error(f"Invalid PDB Code {structure_code}")
175
 
176
  if isinstance(structure, bs.AtomArrayStack):
177
  gr.Info(
 
207
  f"No amino acid residues found in chain {chain_id} of the provided structure."
208
  )
209
 
210
+ logger.info(len(structure))
211
+ logger.info(structure[:3])
212
 
213
  struct_features = load_structure(structure, device=device)
214
  sequence = str(to_sequence(structure)[0][0])
 
216
 
217
  # Make predictions
218
  with torch.no_grad():
219
+ logger.info(f"Sequence length: {len(sequence)}")
220
+ logger.info(
221
+ f"Structure features shape: {struct_features.shape if struct_features is not None else 'N/A'}"
222
+ )
223
+ logger.info(
224
+ f"Sequence features shape: {seq_features.shape if seq_features is not None else 'N/A'}"
225
+ )
226
+
227
  try:
228
  dynamics_pred = model(
229
  {
 
282
  Cluster the network using Girvan-Newman algorithm.
283
  """
284
 
285
+ logger.info(f"Nodes: {G.number_of_nodes()}")
286
+ logger.info(f"Edges: {G.number_of_edges()}")
287
+ logger.info(f"Number of connected components: {nx.number_connected_components(G)}")
288
+ logger.info(f"Connected: {nx.is_connected(G)}")
289
 
290
  comp = nx.community.girvan_newman(G)
291
  # limited = itertools.takewhile(lambda c: len(c) <= k, comp)
 
423
  return fig, bc_highlight, comm_highlight, out_cluster_file_name
424
 
425
 
426
+ def check_user_access(token: gr.OAuthToken | None):
427
  """Check if user is logged in and has access to private repo"""
428
+ if token is None:
429
  return False, "Please log in to use this Space"
430
 
431
  try:
432
  # Try to access a file from your private repo
433
+ api = HfApi(token=token)
434
 
435
  # Test access by trying to get repo info
436
  api.repo_info(
437
  repo_id="EvolutionaryScale/esm3-sm-open-v1",
438
  repo_type="model", # or "dataset" or "space"
439
+ token=token,
440
  )
441
 
442
+ return (
443
+ True,
444
+ token,
445
+ "Access granted to private repository.",
446
+ )
447
 
448
  except GatedRepoError:
449
  return (
450
  False,
451
+ None,
452
  "You need to request access to the private repository at https://huggingface.co/username/private-repo-name",
453
  )
454
 
455
  except RepositoryNotFoundError:
456
+ return False, None, "You don't have access to the required repository"
457
 
458
  except Exception as e:
459
+ return False, None, f"Error checking access: {str(e)}"
460
 
461
 
462
  reps = [