Spaces:
Paused
Paused
auth
Browse files
app.py
CHANGED
|
@@ -8,7 +8,7 @@ from langchain_community.document_loaders import DirectoryLoader
|
|
| 8 |
import torch
|
| 9 |
import re
|
| 10 |
import requests
|
| 11 |
-
from urllib.parse import urlencode
|
| 12 |
import transformers
|
| 13 |
import spaces
|
| 14 |
|
|
@@ -60,13 +60,12 @@ books_db_client_retriever = RetrievalQA.from_chain_type(
|
|
| 60 |
)
|
| 61 |
|
| 62 |
# OAuth Configuration
|
| 63 |
-
TENANT_ID = '
|
| 64 |
-
CLIENT_ID = '
|
| 65 |
-
CLIENT_SECRET = '
|
| 66 |
-
REDIRECT_URI = 'https://
|
| 67 |
-
AUTH_URL = f"https://login.microsoftonline.com/
|
| 68 |
-
TOKEN_URL = f"https://login.microsoftonline.com/
|
| 69 |
-
GRAPH_API_URL = "https://graph.microsoft.com/v1.0/me"
|
| 70 |
|
| 71 |
# Function to redirect to Microsoft login
|
| 72 |
def get_login_url():
|
|
@@ -94,27 +93,6 @@ def exchange_code_for_token(auth_code):
|
|
| 94 |
token_data = response.json()
|
| 95 |
return token_data.get('access_token')
|
| 96 |
|
| 97 |
-
# Step 3: Function to get user profile
|
| 98 |
-
def get_user_profile(access_token):
|
| 99 |
-
headers = {
|
| 100 |
-
'Authorization': f'Bearer {access_token}'
|
| 101 |
-
}
|
| 102 |
-
response = requests.get(GRAPH_API_URL, headers=headers)
|
| 103 |
-
return response.json()
|
| 104 |
-
|
| 105 |
-
# Function to handle OAuth callback
|
| 106 |
-
def handle_oauth_callback(url):
|
| 107 |
-
parsed_url = urlparse(url)
|
| 108 |
-
query_params = parse_qs(parsed_url.query)
|
| 109 |
-
auth_code = query_params.get('code', [None])[0]
|
| 110 |
-
|
| 111 |
-
if auth_code:
|
| 112 |
-
access_token = exchange_code_for_token(auth_code)
|
| 113 |
-
user_profile = get_user_profile(access_token)
|
| 114 |
-
return user_profile
|
| 115 |
-
else:
|
| 116 |
-
return "Authorization failed."
|
| 117 |
-
|
| 118 |
# Function to retrieve answer using the RAG system
|
| 119 |
@spaces.GPU(duration=60)
|
| 120 |
def test_rag(query):
|
|
@@ -150,29 +128,15 @@ with gr.Blocks() as interface:
|
|
| 150 |
|
| 151 |
# Add Microsoft OAuth Login
|
| 152 |
auth_btn = gr.Button("Login with Microsoft")
|
| 153 |
-
|
| 154 |
-
# OAuth callback URL input (for demonstration, replace with actual callback handler)
|
| 155 |
-
callback_url = gr.Textbox(label="OAuth Callback URL", placeholder="Paste the callback URL here...")
|
| 156 |
-
|
| 157 |
-
# Display user profile after login
|
| 158 |
-
profile_output = gr.JSON(label="User Profile")
|
| 159 |
|
| 160 |
# Action for OAuth login
|
| 161 |
def login_action():
|
| 162 |
return gr.redirect(get_login_url())
|
| 163 |
|
| 164 |
-
# Action for handling OAuth callback and displaying the user profile
|
| 165 |
-
def handle_callback_action(url):
|
| 166 |
-
user_profile = handle_oauth_callback(url)
|
| 167 |
-
return user_profile
|
| 168 |
-
|
| 169 |
# Bind login action to button
|
| 170 |
auth_btn.click(login_action)
|
| 171 |
|
| 172 |
-
#
|
| 173 |
-
callback_url.change(handle_callback_action, inputs=[callback_url], outputs=[profile_output])
|
| 174 |
-
|
| 175 |
-
# Submit action for chat
|
| 176 |
submit_btn.click(chat, inputs=[input_box, chat_history], outputs=[chat_history, input_box])
|
| 177 |
|
| 178 |
interface.launch()
|
|
|
|
| 8 |
import torch
|
| 9 |
import re
|
| 10 |
import requests
|
| 11 |
+
from urllib.parse import urlencode
|
| 12 |
import transformers
|
| 13 |
import spaces
|
| 14 |
|
|
|
|
| 60 |
)
|
| 61 |
|
| 62 |
# OAuth Configuration
|
| 63 |
+
TENANT_ID = 'your-tenant-id'
|
| 64 |
+
CLIENT_ID = 'your-client-id'
|
| 65 |
+
CLIENT_SECRET = 'your-client-secret'
|
| 66 |
+
REDIRECT_URI = 'https://your-chatbot.hf.space/auth/callback'
|
| 67 |
+
AUTH_URL = f"https://login.microsoftonline.com/{TENANT_ID}/oauth2/v2.0/authorize"
|
| 68 |
+
TOKEN_URL = f"https://login.microsoftonline.com/{TENANT_ID}/oauth2/v2.0/token"
|
|
|
|
| 69 |
|
| 70 |
# Function to redirect to Microsoft login
|
| 71 |
def get_login_url():
|
|
|
|
| 93 |
token_data = response.json()
|
| 94 |
return token_data.get('access_token')
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
# Function to retrieve answer using the RAG system
|
| 97 |
@spaces.GPU(duration=60)
|
| 98 |
def test_rag(query):
|
|
|
|
| 128 |
|
| 129 |
# Add Microsoft OAuth Login
|
| 130 |
auth_btn = gr.Button("Login with Microsoft")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
# Action for OAuth login
|
| 133 |
def login_action():
|
| 134 |
return gr.redirect(get_login_url())
|
| 135 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
# Bind login action to button
|
| 137 |
auth_btn.click(login_action)
|
| 138 |
|
| 139 |
+
# Submit action
|
|
|
|
|
|
|
|
|
|
| 140 |
submit_btn.click(chat, inputs=[input_box, chat_history], outputs=[chat_history, input_box])
|
| 141 |
|
| 142 |
interface.launch()
|