Update app.py
Browse files
app.py
CHANGED
|
@@ -46,35 +46,88 @@ if OPENROUTER_API_KEY:
|
|
| 46 |
|
| 47 |
# --- UPDATED AUTHENTICATION FOR WEB ---
|
| 48 |
@st.cache_resource(show_spinner=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
def authenticate_service():
|
| 50 |
creds = None
|
| 51 |
|
| 52 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
if os.path.exists(TOKEN_FILE):
|
| 54 |
creds = Credentials.from_authorized_user_file(TOKEN_FILE, ALL_SCOPES)
|
| 55 |
-
|
| 56 |
if not creds or not creds.valid:
|
| 57 |
if creds and creds.expired and creds.refresh_token:
|
| 58 |
creds.refresh(Request())
|
| 59 |
else:
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
return None
|
| 63 |
-
|
| 64 |
-
# Load the credentials from the secret string
|
| 65 |
-
client_config = json.loads(GCP_CREDENTIALS_STR)
|
| 66 |
|
| 67 |
-
#
|
| 68 |
-
|
| 69 |
-
# For a quick fix, keep this, but you may need to use 'Redirect URI'
|
| 70 |
-
# for a truly public app.
|
| 71 |
-
flow = InstalledAppFlow.from_client_config(client_config, ALL_SCOPES)
|
| 72 |
-
creds = flow.run_local_server(port=0)
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
return creds
|
| 77 |
|
|
|
|
|
|
|
|
|
|
| 78 |
def normalize_event_json(event_data):
|
| 79 |
"""Ensures JSON from AI or Google uses the same keys."""
|
| 80 |
return {
|
|
|
|
| 46 |
|
| 47 |
# --- UPDATED AUTHENTICATION FOR WEB ---
|
| 48 |
@st.cache_resource(show_spinner=False)
|
| 49 |
+
# def authenticate_service():
|
| 50 |
+
# creds = None
|
| 51 |
+
|
| 52 |
+
# # Check if we have a saved token
|
| 53 |
+
# if os.path.exists(TOKEN_FILE):
|
| 54 |
+
# creds = Credentials.from_authorized_user_file(TOKEN_FILE, ALL_SCOPES)
|
| 55 |
+
|
| 56 |
+
# if not creds or not creds.valid:
|
| 57 |
+
# if creds and creds.expired and creds.refresh_token:
|
| 58 |
+
# creds.refresh(Request())
|
| 59 |
+
# else:
|
| 60 |
+
# if not GCP_CREDENTIALS_STR:
|
| 61 |
+
# st.error("Missing GCP_CREDENTIALS in Space Secrets!")
|
| 62 |
+
# return None
|
| 63 |
+
|
| 64 |
+
# # Load the credentials from the secret string
|
| 65 |
+
# client_config = json.loads(GCP_CREDENTIALS_STR)
|
| 66 |
+
|
| 67 |
+
# # Note: run_local_server only works if you are running this locally.
|
| 68 |
+
# # On Hugging Face, this will try to open a browser on their server.
|
| 69 |
+
# # For a quick fix, keep this, but you may need to use 'Redirect URI'
|
| 70 |
+
# # for a truly public app.
|
| 71 |
+
# flow = InstalledAppFlow.from_client_config(client_config, ALL_SCOPES)
|
| 72 |
+
# creds = flow.run_local_server(port=0)
|
| 73 |
+
|
| 74 |
+
# with open(TOKEN_FILE, "w") as token:
|
| 75 |
+
# token.write(creds.to_json())
|
| 76 |
+
# return creds
|
| 77 |
def authenticate_service():
|
| 78 |
creds = None
|
| 79 |
|
| 80 |
+
# 1. Try to load from session state (so they don't have to log in every click)
|
| 81 |
+
if 'google_creds' in st.session_state:
|
| 82 |
+
return st.session_state.google_creds
|
| 83 |
+
|
| 84 |
+
# 2. Check for token.json (saved on server, might reset)
|
| 85 |
if os.path.exists(TOKEN_FILE):
|
| 86 |
creds = Credentials.from_authorized_user_file(TOKEN_FILE, ALL_SCOPES)
|
| 87 |
+
|
| 88 |
if not creds or not creds.valid:
|
| 89 |
if creds and creds.expired and creds.refresh_token:
|
| 90 |
creds.refresh(Request())
|
| 91 |
else:
|
| 92 |
+
# 3. USE THE REDIRECT FLOW
|
| 93 |
+
client_config = json.loads(os.environ.get("GCP_CREDENTIALS"))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
+
# Use YOUR actual Hugging Face URL
|
| 96 |
+
redirect_uri = "https://alakbarli-assistant.hf.space/"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
+
flow = InstalledAppFlow.from_client_config(
|
| 99 |
+
client_config,
|
| 100 |
+
scopes=ALL_SCOPES,
|
| 101 |
+
redirect_uri=redirect_uri
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
# Check if we are returning from the Google Login redirect
|
| 105 |
+
# Streamlit 1.30+ uses st.query_params
|
| 106 |
+
query_params = st.query_params
|
| 107 |
+
if "code" in query_params:
|
| 108 |
+
# Google gave us a code! Exchange it for a token.
|
| 109 |
+
flow.fetch_token(code=query_params["code"])
|
| 110 |
+
creds = flow.credentials
|
| 111 |
+
st.session_state.google_creds = creds
|
| 112 |
+
# Clear the URL parameters
|
| 113 |
+
st.query_params.clear()
|
| 114 |
+
|
| 115 |
+
with open(TOKEN_FILE, "w") as token:
|
| 116 |
+
token.write(creds.to_json())
|
| 117 |
+
st.rerun() # Refresh the app to show the logged-in state
|
| 118 |
+
else:
|
| 119 |
+
# No code? Show the login button
|
| 120 |
+
auth_url, _ = flow.authorization_url(prompt='consent', access_type='offline')
|
| 121 |
+
st.info("⚠️ Access Restricted: Please sign in to your Google Account.")
|
| 122 |
+
st.link_button("🔑 Login with Google", auth_url)
|
| 123 |
+
return None
|
| 124 |
+
|
| 125 |
+
st.session_state.google_creds = creds
|
| 126 |
return creds
|
| 127 |
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
|
| 131 |
def normalize_event_json(event_data):
|
| 132 |
"""Ensures JSON from AI or Google uses the same keys."""
|
| 133 |
return {
|