Spaces:
Paused
Paused
auth
Browse files
app.py
CHANGED
|
@@ -22,7 +22,7 @@ import re
|
|
| 22 |
import transformers
|
| 23 |
import spaces
|
| 24 |
import requests
|
| 25 |
-
from urllib.parse import urlencode
|
| 26 |
|
| 27 |
# Initialize embeddings and ChromaDB
|
| 28 |
model_name = "sentence-transformers/all-mpnet-base-v2"
|
|
@@ -120,14 +120,18 @@ def exchange_code_for_token(auth_code):
|
|
| 120 |
}
|
| 121 |
|
| 122 |
response = requests.post(TOKEN_URL, data=data)
|
| 123 |
-
|
| 124 |
if response.status_code == 200:
|
| 125 |
token_data = response.json()
|
| 126 |
-
|
| 127 |
-
return access_token
|
| 128 |
else:
|
| 129 |
return None
|
| 130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
def login_user(auth_code):
|
| 132 |
# Exchange the authorization code for an access token
|
| 133 |
token = exchange_code_for_token(auth_code)
|
|
@@ -165,33 +169,36 @@ def chat(query, history=None):
|
|
| 165 |
def clear_input():
|
| 166 |
return "", # Return empty string to clear input field
|
| 167 |
|
|
|
|
| 168 |
with gr.Blocks() as interface:
|
| 169 |
gr.Markdown("## RAG Chatbot")
|
| 170 |
gr.Markdown("Please log in to continue.")
|
| 171 |
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
# Login button to simulate the login process
|
| 176 |
-
login_button = gr.Button("Login")
|
| 177 |
|
| 178 |
# Components for chat (initially hidden)
|
| 179 |
input_box = gr.Textbox(label="Enter your question", placeholder="Type your question here...", visible=False)
|
| 180 |
submit_btn = gr.Button("Submit", visible=False)
|
| 181 |
chat_history = gr.Chatbot(label="Chat History", visible=False)
|
| 182 |
|
| 183 |
-
# Handle login
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
# Input submission and chat handling
|
| 196 |
submit_btn.click(chat, inputs=[input_box, chat_history], outputs=[chat_history, input_box])
|
| 197 |
|
|
|
|
| 22 |
import transformers
|
| 23 |
import spaces
|
| 24 |
import requests
|
| 25 |
+
from urllib.parse import urlencode, urlparse, parse_qs
|
| 26 |
|
| 27 |
# Initialize embeddings and ChromaDB
|
| 28 |
model_name = "sentence-transformers/all-mpnet-base-v2"
|
|
|
|
| 120 |
}
|
| 121 |
|
| 122 |
response = requests.post(TOKEN_URL, data=data)
|
|
|
|
| 123 |
if response.status_code == 200:
|
| 124 |
token_data = response.json()
|
| 125 |
+
return token_data.get('access_token')
|
|
|
|
| 126 |
else:
|
| 127 |
return None
|
| 128 |
|
| 129 |
+
def handle_redirect(auth_code):
|
| 130 |
+
token = exchange_code_for_token(auth_code)
|
| 131 |
+
if token:
|
| 132 |
+
return True # Successful login
|
| 133 |
+
return False # Failed login
|
| 134 |
+
|
| 135 |
def login_user(auth_code):
|
| 136 |
# Exchange the authorization code for an access token
|
| 137 |
token = exchange_code_for_token(auth_code)
|
|
|
|
| 169 |
def clear_input():
|
| 170 |
return "", # Return empty string to clear input field
|
| 171 |
|
| 172 |
+
# Gradio interface
|
| 173 |
with gr.Blocks() as interface:
|
| 174 |
gr.Markdown("## RAG Chatbot")
|
| 175 |
gr.Markdown("Please log in to continue.")
|
| 176 |
|
| 177 |
+
login_link = gr.HTML(show_login_button())
|
| 178 |
+
login_status = gr.Textbox(label="Login Status", visible=False)
|
|
|
|
|
|
|
|
|
|
| 179 |
|
| 180 |
# Components for chat (initially hidden)
|
| 181 |
input_box = gr.Textbox(label="Enter your question", placeholder="Type your question here...", visible=False)
|
| 182 |
submit_btn = gr.Button("Submit", visible=False)
|
| 183 |
chat_history = gr.Chatbot(label="Chat History", visible=False)
|
| 184 |
|
| 185 |
+
# Handle the login link click
|
| 186 |
+
login_link.click(lambda: gr.update(visible=False), inputs=[], outputs=[login_link])
|
| 187 |
+
|
| 188 |
+
# Example redirection logic (replace with actual URL handling)
|
| 189 |
+
@gr.Interface().on_submit
|
| 190 |
+
def on_redirect(url):
|
| 191 |
+
# Extract the authorization code from the URL
|
| 192 |
+
parsed_url = urlparse(url)
|
| 193 |
+
query_params = parse_qs(parsed_url.query)
|
| 194 |
+
auth_code = query_params.get('code')
|
| 195 |
+
|
| 196 |
+
if auth_code:
|
| 197 |
+
if handle_redirect(auth_code[0]): # Pass the first code if there are multiple
|
| 198 |
+
return gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
|
| 199 |
+
else:
|
| 200 |
+
login_status.update("Login failed.")
|
| 201 |
+
|
| 202 |
# Input submission and chat handling
|
| 203 |
submit_btn.click(chat, inputs=[input_box, chat_history], outputs=[chat_history, input_box])
|
| 204 |
|