Oleksii Obolonskyi commited on
Commit
df8084d
·
1 Parent(s): 4832c6f

Fix GitHub ticket auth and popup state

Browse files
Files changed (1) hide show
  1. app.py +40 -11
app.py CHANGED
@@ -860,12 +860,12 @@ def llm_chat(prompt: str, timeout: Tuple[int, int] = (10, 600)) -> Tuple[str, Op
860
 
861
  def github_create_issue(title: str, body: str, labels: Optional[List[str]] = None) -> Tuple[Optional[int], Optional[str]]:
862
  global _GITHUB_TOKEN_LOGGED
863
- token = os.getenv("GITHUB_TOKEN") or ""
864
  if not _GITHUB_TOKEN_LOGGED:
865
- print("GitHub token present:", bool(token))
866
  _GITHUB_TOKEN_LOGGED = True
867
  if not token:
868
- return None, "GITHUB_TOKEN is not set. Add it as a Hugging Face Secret."
869
  if not (title or "").strip() or not (body or "").strip():
870
  return None, "GitHub issue title/body must be non-empty."
871
  url = f"https://api.github.com/repos/{REPO_OWNER}/{REPO_NAME}/issues"
@@ -873,7 +873,20 @@ def github_create_issue(title: str, body: str, labels: Optional[List[str]] = Non
873
  "Authorization": f"Bearer {token}",
874
  "Accept": "application/vnd.github+json",
875
  "Content-Type": "application/json",
 
876
  }
 
 
 
 
 
 
 
 
 
 
 
 
877
  payload = {"title": title, "body": body}
878
  if labels:
879
  payload["labels"] = labels
@@ -1019,12 +1032,16 @@ with st.sidebar:
1019
  st.write("")
1020
  st.subheader("Support")
1021
  st.caption("If an answer is not found in the dataset, you can create a support ticket (GitHub issue).")
1022
- st.session_state.setdefault("open_ticket_ui", False)
1023
- if st.button("Open ticket form", use_container_width=True, disabled=st.session_state["is_thinking"]):
 
 
 
 
1024
  st.session_state["open_ticket_ui"] = True
1025
  if read_only_mode:
1026
  st.warning("Embeddings unavailable (HF Hub temporary error). App is in read-only mode.")
1027
- if st.button("Retry loading embeddings", use_container_width=True):
1028
  load_embedder.clear()
1029
  st.session_state.pop("embedder_error", None)
1030
  st.rerun()
@@ -1070,7 +1087,12 @@ with st.sidebar:
1070
  st.write("")
1071
  st.session_state.setdefault("show_sources", False)
1072
  st.markdown('<div class="stacked-control sources-btn">', unsafe_allow_html=True)
1073
- if st.button("Sources (click to expand the list)", use_container_width=True, disabled=st.session_state["is_thinking"]):
 
 
 
 
 
1074
  st.session_state["show_sources"] = not st.session_state["show_sources"]
1075
  st.markdown("</div>", unsafe_allow_html=True)
1076
  if st.session_state["show_sources"]:
@@ -1091,8 +1113,8 @@ if "chat" not in st.session_state:
1091
  st.session_state["chat"] = []
1092
  if "pending_question" not in st.session_state:
1093
  st.session_state["pending_question"] = ""
1094
- if "ticket_prefill" not in st.session_state:
1095
- st.session_state["ticket_prefill"] = None
1096
  if "enhancing_key" not in st.session_state:
1097
  st.session_state["enhancing_key"] = None
1098
  if "active_action" not in st.session_state:
@@ -1365,6 +1387,7 @@ with aq1:
1365
  with aq2:
1366
  regen_btn = st.button(
1367
  "Regenerate article questions",
 
1368
  use_container_width=True,
1369
  disabled=st.session_state["is_thinking"],
1370
  )
@@ -1422,8 +1445,12 @@ def ticket_form(prefill: Optional[Dict]):
1422
  desc_default = f"Question:\n{q_pref}\n\nWhat I expected:\n\nWhat happened:\n"
1423
  details = st.text_area("Description (details)", value=(desc_default if q_pref else ""))
1424
  col1, col2 = st.columns(2)
1425
- submit = col1.button("Submit ticket", use_container_width=True)
1426
- cancel = col2.button("Cancel", use_container_width=True)
 
 
 
 
1427
  if cancel:
1428
  st.session_state["open_ticket_ui"] = False
1429
  return
@@ -1447,6 +1474,8 @@ def ticket_form(prefill: Optional[Dict]):
1447
  else:
1448
  st.success(f"Ticket created: Issue #{num}")
1449
  st.session_state["open_ticket_ui"] = False
 
 
1450
 
1451
  if st.session_state.get("open_ticket_ui"):
1452
  prefill = st.session_state.get("ticket_prefill") or {}
 
860
 
861
  def github_create_issue(title: str, body: str, labels: Optional[List[str]] = None) -> Tuple[Optional[int], Optional[str]]:
862
  global _GITHUB_TOKEN_LOGGED
863
+ token = (os.getenv("GITHUB_TOKEN") or "").strip()
864
  if not _GITHUB_TOKEN_LOGGED:
865
+ print("GitHub token present:", bool(token), "len:", len(token))
866
  _GITHUB_TOKEN_LOGGED = True
867
  if not token:
868
+ return None, "GITHUB_TOKEN is missing. Add it as a Hugging Face Secret and restart the Space."
869
  if not (title or "").strip() or not (body or "").strip():
870
  return None, "GitHub issue title/body must be non-empty."
871
  url = f"https://api.github.com/repos/{REPO_OWNER}/{REPO_NAME}/issues"
 
873
  "Authorization": f"Bearer {token}",
874
  "Accept": "application/vnd.github+json",
875
  "Content-Type": "application/json",
876
+ "X-GitHub-Api-Version": "2022-11-28",
877
  }
878
+ try:
879
+ auth_resp = requests.get("https://api.github.com/user", headers=headers, timeout=(10, 30))
880
+ if auth_resp.status_code != 200:
881
+ return (
882
+ None,
883
+ "GitHub auth failed "
884
+ f"(status={auth_resp.status_code}): {auth_resp.text}. "
885
+ "This usually means the token is invalid/expired, or you copied it incorrectly. "
886
+ "If the repo is under an org with SSO, authorize the token for that org.",
887
+ )
888
+ except Exception as e:
889
+ return None, f"GitHub auth check failed: {e}"
890
  payload = {"title": title, "body": body}
891
  if labels:
892
  payload["labels"] = labels
 
1032
  st.write("")
1033
  st.subheader("Support")
1034
  st.caption("If an answer is not found in the dataset, you can create a support ticket (GitHub issue).")
1035
+ if st.button(
1036
+ "Open ticket form",
1037
+ key="sidebar_open_ticket_btn",
1038
+ use_container_width=True,
1039
+ disabled=st.session_state["is_thinking"],
1040
+ ):
1041
  st.session_state["open_ticket_ui"] = True
1042
  if read_only_mode:
1043
  st.warning("Embeddings unavailable (HF Hub temporary error). App is in read-only mode.")
1044
+ if st.button("Retry loading embeddings", key="retry_embed_btn", use_container_width=True):
1045
  load_embedder.clear()
1046
  st.session_state.pop("embedder_error", None)
1047
  st.rerun()
 
1087
  st.write("")
1088
  st.session_state.setdefault("show_sources", False)
1089
  st.markdown('<div class="stacked-control sources-btn">', unsafe_allow_html=True)
1090
+ if st.button(
1091
+ "Sources (click to expand the list)",
1092
+ key="sidebar_sources_btn",
1093
+ use_container_width=True,
1094
+ disabled=st.session_state["is_thinking"],
1095
+ ):
1096
  st.session_state["show_sources"] = not st.session_state["show_sources"]
1097
  st.markdown("</div>", unsafe_allow_html=True)
1098
  if st.session_state["show_sources"]:
 
1113
  st.session_state["chat"] = []
1114
  if "pending_question" not in st.session_state:
1115
  st.session_state["pending_question"] = ""
1116
+ st.session_state.setdefault("open_ticket_ui", False)
1117
+ st.session_state.setdefault("ticket_prefill", {"question": "", "citations": []})
1118
  if "enhancing_key" not in st.session_state:
1119
  st.session_state["enhancing_key"] = None
1120
  if "active_action" not in st.session_state:
 
1387
  with aq2:
1388
  regen_btn = st.button(
1389
  "Regenerate article questions",
1390
+ key="regen_article_btn",
1391
  use_container_width=True,
1392
  disabled=st.session_state["is_thinking"],
1393
  )
 
1445
  desc_default = f"Question:\n{q_pref}\n\nWhat I expected:\n\nWhat happened:\n"
1446
  details = st.text_area("Description (details)", value=(desc_default if q_pref else ""))
1447
  col1, col2 = st.columns(2)
1448
+ submit = col1.button("Submit ticket", key="ticket_submit_btn", use_container_width=True)
1449
+ cancel = col2.button("Cancel", key="ticket_cancel_btn", use_container_width=True)
1450
+ close = st.button("Close", key="ticket_close_btn", use_container_width=True)
1451
+ if close:
1452
+ st.session_state["open_ticket_ui"] = False
1453
+ st.rerun()
1454
  if cancel:
1455
  st.session_state["open_ticket_ui"] = False
1456
  return
 
1474
  else:
1475
  st.success(f"Ticket created: Issue #{num}")
1476
  st.session_state["open_ticket_ui"] = False
1477
+ st.session_state["ticket_prefill"] = {"question": "", "citations": []}
1478
+ st.rerun()
1479
 
1480
  if st.session_state.get("open_ticket_ui"):
1481
  prefill = st.session_state.get("ticket_prefill") or {}