Spaces:
Paused
Paused
auth
Browse files
app.py
CHANGED
|
@@ -67,20 +67,22 @@ REDIRECT_URI = 'https://sanjeevbora-chatbot.hf.space/'
|
|
| 67 |
AUTH_URL = f"https://login.microsoftonline.com/2b093ced-2571-463f-bc3e-b4f8bcb427ee/oauth2/v2.0/authorize"
|
| 68 |
TOKEN_URL = f"https://login.microsoftonline.com/2b093ced-2571-463f-bc3e-b4f8bcb427ee/oauth2/v2.0/token"
|
| 69 |
|
| 70 |
-
#
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
| 72 |
params = {
|
| 73 |
'client_id': CLIENT_ID,
|
| 74 |
'response_type': 'code',
|
| 75 |
'redirect_uri': REDIRECT_URI,
|
| 76 |
'response_mode': 'query',
|
| 77 |
'scope': 'User.Read',
|
| 78 |
-
'state': '12345' # Optional state parameter
|
| 79 |
}
|
| 80 |
-
|
| 81 |
-
return login_url
|
| 82 |
|
| 83 |
-
#
|
| 84 |
def exchange_code_for_token(auth_code):
|
| 85 |
data = {
|
| 86 |
'grant_type': 'authorization_code',
|
|
@@ -93,50 +95,90 @@ def exchange_code_for_token(auth_code):
|
|
| 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 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
-
return
|
| 110 |
-
|
| 111 |
-
# Function
|
| 112 |
-
def
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
return history, "" # Clear input after submission
|
| 119 |
-
|
| 120 |
-
# Gradio interface
|
| 121 |
-
with gr.Blocks() as interface:
|
| 122 |
-
gr.Markdown("## RAG Chatbot")
|
| 123 |
-
gr.Markdown("Ask a question and get answers based on retrieved documents.")
|
| 124 |
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
|
| 129 |
-
|
| 130 |
-
auth_btn = gr.Button("Login with Microsoft")
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
|
| 136 |
-
#
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
-
interface.launch()
|
|
|
|
| 67 |
AUTH_URL = f"https://login.microsoftonline.com/2b093ced-2571-463f-bc3e-b4f8bcb427ee/oauth2/v2.0/authorize"
|
| 68 |
TOKEN_URL = f"https://login.microsoftonline.com/2b093ced-2571-463f-bc3e-b4f8bcb427ee/oauth2/v2.0/token"
|
| 69 |
|
| 70 |
+
# Global variable to store the access token
|
| 71 |
+
access_token = None
|
| 72 |
+
|
| 73 |
+
# OAuth Authorization URL with parameters
|
| 74 |
+
def get_auth_url():
|
| 75 |
params = {
|
| 76 |
'client_id': CLIENT_ID,
|
| 77 |
'response_type': 'code',
|
| 78 |
'redirect_uri': REDIRECT_URI,
|
| 79 |
'response_mode': 'query',
|
| 80 |
'scope': 'User.Read',
|
| 81 |
+
'state': '12345' # Optional state parameter
|
| 82 |
}
|
| 83 |
+
return f"{AUTH_URL}?{urlencode(params)}"
|
|
|
|
| 84 |
|
| 85 |
+
# Exchange authorization code for an access token
|
| 86 |
def exchange_code_for_token(auth_code):
|
| 87 |
data = {
|
| 88 |
'grant_type': 'authorization_code',
|
|
|
|
| 95 |
token_data = response.json()
|
| 96 |
return token_data.get('access_token')
|
| 97 |
|
| 98 |
+
# Function to fetch user profile from Microsoft Graph
|
| 99 |
+
def get_user_profile(token):
|
| 100 |
+
headers = {
|
| 101 |
+
'Authorization': f'Bearer {token}'
|
| 102 |
+
}
|
| 103 |
+
response = requests.get(GRAPH_API_URL, headers=headers)
|
| 104 |
+
return response.json()
|
| 105 |
+
|
| 106 |
+
# Function to check if the user is authenticated
|
| 107 |
+
def is_authenticated():
|
| 108 |
+
return access_token is not None
|
| 109 |
+
|
| 110 |
# Function to retrieve answer using the RAG system
|
| 111 |
@spaces.GPU(duration=60)
|
| 112 |
+
# Gradio app with OAuth integration
|
| 113 |
+
def chat_interface():
|
| 114 |
+
global access_token
|
| 115 |
+
|
| 116 |
+
# If the user is not authenticated, redirect to Microsoft login
|
| 117 |
+
if not is_authenticated():
|
| 118 |
+
auth_url = get_auth_url()
|
| 119 |
+
return gr.Markdown(f"Please [log in]({auth_url}) to use the chatbot.")
|
| 120 |
+
|
| 121 |
+
def test_rag(query):
|
| 122 |
+
books_retriever = books_db_client_retriever.run(query)
|
| 123 |
+
|
| 124 |
+
# Extract the relevant answer using regex
|
| 125 |
+
corrected_text_match = re.search(r"Helpful Answer:(.*)", books_retriever, re.DOTALL)
|
| 126 |
+
|
| 127 |
+
if corrected_text_match:
|
| 128 |
+
corrected_text_books = corrected_text_match.group(1).strip()
|
| 129 |
+
else:
|
| 130 |
+
corrected_text_books = "No helpful answer found."
|
| 131 |
+
|
| 132 |
+
return corrected_text_books
|
| 133 |
|
| 134 |
+
# Gradio chatbot interface
|
| 135 |
+
def chat(query, history=None):
|
| 136 |
+
if history is None:
|
| 137 |
+
history = []
|
| 138 |
+
if query:
|
| 139 |
+
# Chatbot logic here
|
| 140 |
+
answer = "This is a dummy answer."
|
| 141 |
+
history.append((query, answer))
|
| 142 |
+
return history, "" # Clear input after submission
|
| 143 |
+
|
| 144 |
+
with gr.Blocks() as interface:
|
| 145 |
+
gr.Markdown("## RAG Chatbot")
|
| 146 |
+
gr.Markdown("Ask a question and get answers based on retrieved documents.")
|
| 147 |
+
|
| 148 |
+
input_box = gr.Textbox(label="Enter your question", placeholder="Type your question here...")
|
| 149 |
+
submit_btn = gr.Button("Submit")
|
| 150 |
+
chat_history = gr.Chatbot(label="Chat History")
|
| 151 |
+
|
| 152 |
+
submit_btn.click(chat, inputs=[input_box, chat_history], outputs=[chat_history, input_box])
|
| 153 |
|
| 154 |
+
return interface
|
| 155 |
+
|
| 156 |
+
# Function to handle OAuth callback and extract authorization code
|
| 157 |
+
def handle_auth_callback(query_params):
|
| 158 |
+
global access_token
|
| 159 |
+
|
| 160 |
+
# Parse the query parameters to extract the authorization code
|
| 161 |
+
parsed_url = urlparse(query_params)
|
| 162 |
+
query_dict = parse_qs(parsed_url.query)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
+
if 'code' in query_dict:
|
| 165 |
+
auth_code = query_dict['code'][0]
|
| 166 |
+
access_token = exchange_code_for_token(auth_code)
|
| 167 |
|
| 168 |
+
return "Authentication successful. You can now use the chatbot."
|
|
|
|
| 169 |
|
| 170 |
+
# Gradio app launch
|
| 171 |
+
with gr.Blocks() as app:
|
| 172 |
+
gr.Markdown("## OAuth2.0 Chatbot")
|
| 173 |
|
| 174 |
+
# Check for the authentication callback
|
| 175 |
+
app_url = spaces.get_url()
|
| 176 |
+
if app_url.startswith(REDIRECT_URI):
|
| 177 |
+
query_params = app_url.split('?')[-1]
|
| 178 |
+
handle_auth_callback(query_params)
|
| 179 |
+
|
| 180 |
+
# Display the chat interface or authentication prompt
|
| 181 |
+
chat_interface()
|
| 182 |
+
|
| 183 |
+
app.launch()
|
| 184 |
|
|
|