Spaces:
Sleeping
Sleeping
Commit ·
7e6036b
1
Parent(s): 8bf0063
history display
Browse files- app.py +27 -67
- storage.py +6 -25
app.py
CHANGED
|
@@ -7,9 +7,8 @@ This UI layer sets up a clean initialization screen. It uses a layout state logi
|
|
| 7 |
|
| 8 |
import gradio as gr
|
| 9 |
from core_logic import chat_function
|
| 10 |
-
from storage import save_chat, load_history, get_chat_content,
|
| 11 |
from git_agent import manage_github_repo
|
| 12 |
-
import os
|
| 13 |
|
| 14 |
print("==================================================")
|
| 15 |
print(f"DEBUG: HF_TOKEN exists? {bool(os.getenv('HF_TOKEN'))}")
|
|
@@ -22,22 +21,25 @@ with gr.Blocks() as demo:
|
|
| 22 |
chat_id_state = gr.State("")
|
| 23 |
staged_files_state = gr.State([])
|
| 24 |
|
| 25 |
-
# ====================
|
| 26 |
-
with gr.
|
| 27 |
gr.Markdown("## 🔐 CoderG Enterprise Access Gate")
|
| 28 |
gr.Markdown(
|
| 29 |
"⚠️ **Workspace Notice:** You can leave the password field blank to proceed directly to the workspace. "
|
| 30 |
"However, if a master environment secret key is required and left blank, **no past conversations or sidebar logs will be loaded**."
|
| 31 |
)
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
# ====================
|
| 40 |
-
|
|
|
|
| 41 |
|
| 42 |
# --- Left Panel: Sidebar History ---
|
| 43 |
with gr.Column(scale=1, variant="secondary"):
|
|
@@ -100,65 +102,23 @@ with gr.Blocks() as demo:
|
|
| 100 |
output_log = gr.Markdown("_Awaiting local environment staging completion..._")
|
| 101 |
|
| 102 |
# --- UI ROUTING HANDLERS ---
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
#Processes the state gate router logic step when the entry button is pushed.
|
| 106 |
-
target_password = get_secret_password()
|
| 107 |
-
clean_attempt = str(password_attempt).strip()
|
| 108 |
-
|
| 109 |
-
# 1. Reject clearly incorrect inputs if an environment variable is set
|
| 110 |
-
if target_password and clean_attempt and clean_attempt != target_password:
|
| 111 |
-
raise gr.Error("❌ Invalid security token entered. Access to environment denied.")
|
| 112 |
-
|
| 113 |
-
# 2. Run verification to unlock global storage access context natively
|
| 114 |
-
verify_and_unlock(clean_attempt)
|
| 115 |
-
|
| 116 |
-
# 3. Securely query history tracking arrays
|
| 117 |
-
loaded_samples = load_history()
|
| 118 |
-
|
| 119 |
-
return (
|
| 120 |
-
gr.update(visible=False), # Hide login layout
|
| 121 |
-
gr.update(visible=True), # Reveal main application workspace
|
| 122 |
-
gr.update(samples=loaded_samples) # Update sidebar list view dataset target
|
| 123 |
-
)
|
| 124 |
-
"""
|
| 125 |
-
|
| 126 |
-
# --- UI ROUTING HANDLERS ---
|
| 127 |
-
def evaluate_login_access(password_attempt):
|
| 128 |
-
"""Processes the state gate router logic step when the entry button is pushed."""
|
| 129 |
target_password = get_secret_password()
|
| 130 |
clean_attempt = str(password_attempt).strip() if password_attempt is not None else ""
|
| 131 |
|
| 132 |
-
#
|
| 133 |
-
# 📊 RUNTIME AGENT DEEP TELEMETRY LOGS
|
| 134 |
-
# ==================================================================
|
| 135 |
-
print("\n==================================================")
|
| 136 |
-
print("🔓 CODERG LOGIN ATTEMPT METRICS:")
|
| 137 |
-
print(f" -> Raw input object type: {type(password_attempt)}")
|
| 138 |
-
print(f" -> Length of user input in UI textbox: {len(clean_attempt)}")
|
| 139 |
-
print(f" -> Length of target password from HF Secret: {len(target_password)}")
|
| 140 |
-
print(f" -> Do they match exactly in memory? {clean_attempt == target_password}")
|
| 141 |
-
print("==================================================\n")
|
| 142 |
-
# ==================================================================
|
| 143 |
-
|
| 144 |
-
# 1. Reject clearly incorrect inputs if an environment variable is set
|
| 145 |
if target_password and clean_attempt and clean_attempt != target_password:
|
| 146 |
raise gr.Error("❌ Invalid security token entered. Access to environment denied.")
|
| 147 |
|
| 148 |
-
# 2.
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
|
| 154 |
-
#
|
| 155 |
-
|
| 156 |
-
# ====================================================================
|
| 157 |
-
return (
|
| 158 |
-
gr.update(visible=False), # Hide login layout
|
| 159 |
-
gr.update(visible=True), # Reveal main application workspace
|
| 160 |
-
gr.Dataset(samples=loaded_samples) # Explicitly re-initialize the dataset samples grid
|
| 161 |
-
)
|
| 162 |
|
| 163 |
# --- CORE WORKSPACE LOGIC ---
|
| 164 |
def handle_file_upload(uploaded_files, current_staged_files):
|
|
@@ -202,11 +162,11 @@ with gr.Blocks() as demo:
|
|
| 202 |
result = manage_github_repo(repo_name.strip(), commit_msg, files)
|
| 203 |
yield f"{result}"
|
| 204 |
|
| 205 |
-
#
|
| 206 |
login_btn.click(
|
| 207 |
-
fn=
|
| 208 |
inputs=[password_input],
|
| 209 |
-
outputs=[login_layout,
|
| 210 |
)
|
| 211 |
|
| 212 |
upload_btn.upload(
|
|
|
|
| 7 |
|
| 8 |
import gradio as gr
|
| 9 |
from core_logic import chat_function
|
| 10 |
+
from storage import save_chat, load_history, get_chat_content, verify_access_token, get_secret_password
|
| 11 |
from git_agent import manage_github_repo
|
|
|
|
| 12 |
|
| 13 |
print("==================================================")
|
| 14 |
print(f"DEBUG: HF_TOKEN exists? {bool(os.getenv('HF_TOKEN'))}")
|
|
|
|
| 21 |
chat_id_state = gr.State("")
|
| 22 |
staged_files_state = gr.State([])
|
| 23 |
|
| 24 |
+
# ==================== AUTHENTICATION OVERLAY HEADER ====================
|
| 25 |
+
with gr.Group(visible=True) as login_layout:
|
| 26 |
gr.Markdown("## 🔐 CoderG Enterprise Access Gate")
|
| 27 |
gr.Markdown(
|
| 28 |
"⚠️ **Workspace Notice:** You can leave the password field blank to proceed directly to the workspace. "
|
| 29 |
"However, if a master environment secret key is required and left blank, **no past conversations or sidebar logs will be loaded**."
|
| 30 |
)
|
| 31 |
+
with gr.Row():
|
| 32 |
+
password_input = gr.Textbox(
|
| 33 |
+
label="Security Access Password",
|
| 34 |
+
placeholder="Enter password or leave blank for unauthenticated mode...",
|
| 35 |
+
type="password",
|
| 36 |
+
scale=4
|
| 37 |
+
)
|
| 38 |
+
login_btn = gr.Button("Unlock Workspace Environment", variant="primary", scale=1)
|
| 39 |
|
| 40 |
+
# ==================== MAIN ENVIRONMENT INTERFACE ====================
|
| 41 |
+
# Notice: Row stays visible immediately so components load and paint their slots cleanly on boot!
|
| 42 |
+
with gr.Row(visible=True) as main_workspace:
|
| 43 |
|
| 44 |
# --- Left Panel: Sidebar History ---
|
| 45 |
with gr.Column(scale=1, variant="secondary"):
|
|
|
|
| 102 |
output_log = gr.Markdown("_Awaiting local environment staging completion..._")
|
| 103 |
|
| 104 |
# --- UI ROUTING HANDLERS ---
|
| 105 |
+
def handle_workspace_unlock(password_attempt):
|
| 106 |
+
"""Processes login, clears security gate block, and directly forces dataset rendering."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
target_password = get_secret_password()
|
| 108 |
clean_attempt = str(password_attempt).strip() if password_attempt is not None else ""
|
| 109 |
|
| 110 |
+
# 1. Reject invalid credentials explicitly if password environment variable is active
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
if target_password and clean_attempt and clean_attempt != target_password:
|
| 112 |
raise gr.Error("❌ Invalid security token entered. Access to environment denied.")
|
| 113 |
|
| 114 |
+
# 2. Extract history context (yields data array if valid, empty array if user opted to skip with blank)
|
| 115 |
+
loaded_samples = []
|
| 116 |
+
if not target_password or clean_attempt == target_password:
|
| 117 |
+
if clean_attempt: # If they actually filled out the correct password, give them history
|
| 118 |
+
loaded_samples = load_history()
|
| 119 |
|
| 120 |
+
# 3. Collapse the auth widget and seamlessly return the sample payload
|
| 121 |
+
return gr.update(visible=False), gr.update(samples=loaded_samples)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
# --- CORE WORKSPACE LOGIC ---
|
| 124 |
def handle_file_upload(uploaded_files, current_staged_files):
|
|
|
|
| 162 |
result = manage_github_repo(repo_name.strip(), commit_msg, files)
|
| 163 |
yield f"{result}"
|
| 164 |
|
| 165 |
+
# ==================== EVENT BINDING LIFECYCLES ====================
|
| 166 |
login_btn.click(
|
| 167 |
+
fn=handle_workspace_unlock,
|
| 168 |
inputs=[password_input],
|
| 169 |
+
outputs=[login_layout, history_list]
|
| 170 |
)
|
| 171 |
|
| 172 |
upload_btn.upload(
|
storage.py
CHANGED
|
@@ -14,9 +14,6 @@ from huggingface_hub import HfApi, hf_hub_download
|
|
| 14 |
REPO_ID = "prashantmatlani/chathistorycoderg"
|
| 15 |
HISTORY_DIR = "./chathistory"
|
| 16 |
|
| 17 |
-
# Internal Session Authentication Toggle
|
| 18 |
-
_SESSION_UNLOCKED = False
|
| 19 |
-
|
| 20 |
# Initialize the API with your token
|
| 21 |
api = HfApi(token=os.getenv("HF_TOKEN"))
|
| 22 |
|
|
@@ -25,39 +22,24 @@ def get_secret_password():
|
|
| 25 |
val = os.getenv("APP_PASSWORD")
|
| 26 |
|
| 27 |
print(f"\nlen val: {len(val)}\n")
|
| 28 |
-
|
| 29 |
return str(val).strip() if val is not None else ""
|
| 30 |
|
| 31 |
-
def
|
| 32 |
-
"""Validates input credentials
|
| 33 |
-
global _SESSION_UNLOCKED
|
| 34 |
target_password = get_secret_password()
|
| 35 |
clean_input = str(user_password_input).strip()
|
| 36 |
-
|
| 37 |
print(f"len target_password: {len(target_password)}, len clean_input: {len(clean_input)}")
|
| 38 |
|
| 39 |
-
# If no master environment password exists, unlock access automatically
|
| 40 |
if not target_password:
|
| 41 |
-
_SESSION_UNLOCKED = True
|
| 42 |
return True
|
| 43 |
-
|
| 44 |
-
if clean_input == target_password:
|
| 45 |
-
_SESSION_UNLOCKED = True
|
| 46 |
-
return True
|
| 47 |
-
|
| 48 |
-
return False
|
| 49 |
|
| 50 |
def load_history():
|
| 51 |
"""Retrieves list of chat IDs from the Hub, explicitly sorted by true modification time."""
|
| 52 |
-
global _SESSION_UNLOCKED
|
| 53 |
-
|
| 54 |
-
# Guardrail: If an environment password is required but the session remains locked, return empty list
|
| 55 |
-
if get_secret_password() and not _SESSION_UNLOCKED:
|
| 56 |
-
print("[*] Security Guardrail: Bypassing history retrieval for unauthenticated lifecycle.")
|
| 57 |
-
return []
|
| 58 |
-
|
| 59 |
try:
|
| 60 |
-
#
|
| 61 |
repo_info = api.repo_info(repo_id=REPO_ID, repo_type="dataset", files_metadata=True)
|
| 62 |
|
| 63 |
chat_files_meta = []
|
|
@@ -76,7 +58,6 @@ def load_history():
|
|
| 76 |
except Exception as metadata_error:
|
| 77 |
print(f"[!] Primary metadata pipeline exception: {metadata_error}. Launching clean fallback channel...")
|
| 78 |
|
| 79 |
-
# 2. Fallback Channel
|
| 80 |
try:
|
| 81 |
fallback_api = HfApi(token=os.getenv("HF_TOKEN"))
|
| 82 |
files = fallback_api.list_repo_files(repo_id=REPO_ID, repo_type="dataset")
|
|
|
|
| 14 |
REPO_ID = "prashantmatlani/chathistorycoderg"
|
| 15 |
HISTORY_DIR = "./chathistory"
|
| 16 |
|
|
|
|
|
|
|
|
|
|
| 17 |
# Initialize the API with your token
|
| 18 |
api = HfApi(token=os.getenv("HF_TOKEN"))
|
| 19 |
|
|
|
|
| 22 |
val = os.getenv("APP_PASSWORD")
|
| 23 |
|
| 24 |
print(f"\nlen val: {len(val)}\n")
|
| 25 |
+
|
| 26 |
return str(val).strip() if val is not None else ""
|
| 27 |
|
| 28 |
+
def verify_access_token(user_password_input):
|
| 29 |
+
"""Validates input credentials directly against the environment variable string."""
|
|
|
|
| 30 |
target_password = get_secret_password()
|
| 31 |
clean_input = str(user_password_input).strip()
|
| 32 |
+
|
| 33 |
print(f"len target_password: {len(target_password)}, len clean_input: {len(clean_input)}")
|
| 34 |
|
|
|
|
| 35 |
if not target_password:
|
|
|
|
| 36 |
return True
|
| 37 |
+
return clean_input == target_password
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
def load_history():
|
| 40 |
"""Retrieves list of chat IDs from the Hub, explicitly sorted by true modification time."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
try:
|
| 42 |
+
# Fetch rich metadata for chronological sorting
|
| 43 |
repo_info = api.repo_info(repo_id=REPO_ID, repo_type="dataset", files_metadata=True)
|
| 44 |
|
| 45 |
chat_files_meta = []
|
|
|
|
| 58 |
except Exception as metadata_error:
|
| 59 |
print(f"[!] Primary metadata pipeline exception: {metadata_error}. Launching clean fallback channel...")
|
| 60 |
|
|
|
|
| 61 |
try:
|
| 62 |
fallback_api = HfApi(token=os.getenv("HF_TOKEN"))
|
| 63 |
files = fallback_api.list_repo_files(repo_id=REPO_ID, repo_type="dataset")
|