macayaven commited on
Commit
c712312
·
verified ·
1 Parent(s): 0171b0c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -4
app.py CHANGED
@@ -6,6 +6,7 @@ A Gradio-based CBT tool for identifying and reframing cognitive distortions
6
  import hashlib
7
  import json
8
  import os
 
9
  from datetime import datetime
10
  from typing import Optional
11
 
@@ -899,6 +900,13 @@ def create_app(language='en'):
899
  set_override_btn = gr.Button("Set Limit Override", variant="secondary")
900
  refresh_btn = gr.Button("Refresh Metrics", variant="secondary")
901
 
 
 
 
 
 
 
 
902
  def _owner_is(profile: "gr.OAuthProfile | None") -> bool:
903
  try:
904
  # Prefer explicit OWNER_USER, fallback to the Space author (useful if OWNER_USER not set)
@@ -1027,6 +1035,51 @@ def create_app(language='en'):
1027
  def admin_refresh():
1028
  return _summarize_metrics_md()
1029
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1030
  # Wire admin interactions
1031
  model_dropdown.change(lambda v: v, inputs=[model_dropdown], outputs=[model_state])
1032
  set_override_btn.click(
@@ -1035,22 +1088,40 @@ def create_app(language='en'):
1035
  outputs=[admin_state, admin_limit_info],
1036
  )
1037
  refresh_btn.click(admin_refresh, outputs=[admin_summary])
 
 
1038
 
1039
  # Gate Owner tab & admin panel visibility on load (OAuth)
1040
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1041
  app.load(
1042
- show_admin,
1043
- outputs=[owner_tab, admin_panel, locked_panel, admin_summary, admin_limit_info],
1044
  )
1045
  except Exception:
1046
  # If OAuth not available, keep admin hidden
1047
  pass
1048
 
1049
- # Enable queue for Spaces / ZeroGPU compatibility
1050
  return app.queue()
1051
 
1052
 
1053
  # Launch the app
1054
  if __name__ == "__main__":
1055
  app = create_app(language='en')
1056
- app.launch(share=True, show_error=True, show_api=False)
 
6
  import hashlib
7
  import json
8
  import os
9
+ import shutil
10
  from datetime import datetime
11
  from typing import Optional
12
 
 
900
  set_override_btn = gr.Button("Set Limit Override", variant="secondary")
901
  refresh_btn = gr.Button("Refresh Metrics", variant="secondary")
902
 
903
+ gr.Markdown("### Debug")
904
+ owner_identity_md = gr.Markdown("")
905
+ with gr.Row():
906
+ identity_btn = gr.Button("Refresh Identity", variant="secondary")
907
+ storage_btn = gr.Button("Check /data", variant="secondary")
908
+ storage_info_md = gr.Markdown("")
909
+
910
  def _owner_is(profile: "gr.OAuthProfile | None") -> bool:
911
  try:
912
  # Prefer explicit OWNER_USER, fallback to the Space author (useful if OWNER_USER not set)
 
1035
  def admin_refresh():
1036
  return _summarize_metrics_md()
1037
 
1038
+ def _profile_username(profile: "gr.OAuthProfile | None") -> str:
1039
+ try:
1040
+ for key in ("preferred_username", "username", "login", "name", "sub", "id"):
1041
+ if hasattr(profile, key):
1042
+ v = getattr(profile, key)
1043
+ if v:
1044
+ return str(v)
1045
+ elif isinstance(profile, dict) and key in profile and profile[key]:
1046
+ return str(profile[key])
1047
+ except Exception:
1048
+ pass
1049
+ return "unknown"
1050
+
1051
+ def identity_refresh(profile: "gr.OAuthProfile | None"):
1052
+ visible = _owner_is(profile)
1053
+ return (
1054
+ f"Logged in as: `{_profile_username(profile)}`\n\n"
1055
+ f"OWNER_USER: `{(os.getenv('OWNER_USER') or '').strip()}`\n"
1056
+ f"SPACE_AUTHOR_NAME: `{(os.getenv('SPACE_AUTHOR_NAME') or '').strip()}`\n"
1057
+ f"Owner match: {'yes' if visible else 'no'}"
1058
+ )
1059
+
1060
+ def storage_check():
1061
+ try:
1062
+ path = "/data"
1063
+ exists = os.path.exists(path)
1064
+ lines = [f"Path: `{path}` — {'present' if exists else 'absent'}"]
1065
+ if exists:
1066
+ total, used, free = shutil.disk_usage(path)
1067
+ gb = 1024 ** 3
1068
+ lines.append(
1069
+ f"Disk: total {total/gb:.1f} GB, used {used/gb:.1f} GB, free {free/gb:.1f} GB"
1070
+ )
1071
+ try:
1072
+ entries = sorted(os.listdir(path))[:20]
1073
+ if entries:
1074
+ lines.append("Entries: " + ", ".join(entries))
1075
+ except Exception:
1076
+ pass
1077
+ hf_home = os.getenv("HF_HOME", "(not set)")
1078
+ lines.append(f"HF_HOME: `{hf_home}`")
1079
+ return "\n".join(lines)
1080
+ except Exception as e:
1081
+ return f"/data check failed: {e}"
1082
+
1083
  # Wire admin interactions
1084
  model_dropdown.change(lambda v: v, inputs=[model_dropdown], outputs=[model_state])
1085
  set_override_btn.click(
 
1088
  outputs=[admin_state, admin_limit_info],
1089
  )
1090
  refresh_btn.click(admin_refresh, outputs=[admin_summary])
1091
+ identity_btn.click(identity_refresh, outputs=[owner_identity_md])
1092
+ storage_btn.click(storage_check, outputs=[storage_info_md])
1093
 
1094
  # Gate Owner tab & admin panel visibility on load (OAuth)
1095
  try:
1096
+ # Also populate identity + storage placeholders
1097
+ def _load(profile: "gr.OAuthProfile | None"):
1098
+ visible = _owner_is(profile)
1099
+ ident = identity_refresh(profile) if visible else ""
1100
+ return (
1101
+ gr.update(visible=visible),
1102
+ gr.update(visible=visible),
1103
+ gr.update(visible=not visible),
1104
+ _summarize_metrics_md() if visible else "",
1105
+ _limit_info_md(admin_state.value if hasattr(admin_state, "value") else None)
1106
+ if visible
1107
+ else "",
1108
+ ident,
1109
+ "",
1110
+ )
1111
+
1112
  app.load(
1113
+ _load,
1114
+ outputs=[owner_tab, admin_panel, locked_panel, admin_summary, admin_limit_info, owner_identity_md, storage_info_md],
1115
  )
1116
  except Exception:
1117
  # If OAuth not available, keep admin hidden
1118
  pass
1119
 
1120
+ # Enable queue for Spaces compatibility
1121
  return app.queue()
1122
 
1123
 
1124
  # Launch the app
1125
  if __name__ == "__main__":
1126
  app = create_app(language='en')
1127
+ app.launch(share=False, show_error=True, show_api=False)