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"
|
|
@@ -86,30 +86,15 @@ params = {
|
|
| 86 |
'redirect_uri': REDIRECT_URI,
|
| 87 |
'response_mode': 'query',
|
| 88 |
'scope': 'User.Read',
|
| 89 |
-
'state': '12345'
|
| 90 |
}
|
| 91 |
|
| 92 |
# Construct the login URL
|
| 93 |
login_url = f"{AUTH_URL}?{urlencode(params)}"
|
| 94 |
|
| 95 |
-
# Gradio interface
|
| 96 |
def show_login_button():
|
| 97 |
return f'<a href="{login_url}" target="_blank">Click here to login with Microsoft</a>'
|
| 98 |
|
| 99 |
-
# Dummy function to simulate token validation (you will replace this with actual validation)
|
| 100 |
-
def is_logged_in(token):
|
| 101 |
-
# Check if the token exists (or check if it's valid)
|
| 102 |
-
return token is not None
|
| 103 |
-
|
| 104 |
-
# Gradio interface
|
| 105 |
-
def check_login(status):
|
| 106 |
-
# If logged in, show the chatbot interface, otherwise show login link
|
| 107 |
-
if status:
|
| 108 |
-
return gr.update(visible=True), gr.update(visible=True)
|
| 109 |
-
else:
|
| 110 |
-
return gr.update(visible=False), gr.update(visible=False)
|
| 111 |
-
|
| 112 |
-
# Function to exchange authorization code for access token
|
| 113 |
def exchange_code_for_token(auth_code):
|
| 114 |
data = {
|
| 115 |
'grant_type': 'authorization_code',
|
|
@@ -128,14 +113,15 @@ def exchange_code_for_token(auth_code):
|
|
| 128 |
else:
|
| 129 |
return None
|
| 130 |
|
| 131 |
-
def
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
return
|
|
|
|
| 139 |
|
| 140 |
# Function to retrieve answer using the RAG system
|
| 141 |
@spaces.GPU(duration=60)
|
|
@@ -152,7 +138,6 @@ def test_rag(query):
|
|
| 152 |
|
| 153 |
return corrected_text_books
|
| 154 |
|
| 155 |
-
# Define the Gradio interface
|
| 156 |
def chat(query, history=None):
|
| 157 |
if history is None:
|
| 158 |
history = []
|
|
@@ -161,38 +146,26 @@ def chat(query, history=None):
|
|
| 161 |
history.append((query, answer))
|
| 162 |
return history, "" # Clear input after submission
|
| 163 |
|
| 164 |
-
# Function to clear input text
|
| 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 |
-
login_link = gr.HTML(f'<a href="{login_url}" target="_blank">Click here to login with Microsoft</a>')
|
| 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 |
-
#
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
lambda token: check_login(is_logged_in(token)),
|
| 191 |
-
inputs=[],
|
| 192 |
-
outputs=[input_box, submit_btn]
|
| 193 |
)
|
| 194 |
|
| 195 |
-
# Input submission and chat handling
|
| 196 |
submit_btn.click(chat, inputs=[input_box, chat_history], outputs=[chat_history, input_box])
|
| 197 |
|
| 198 |
interface.launch()
|
|
|
|
| 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"
|
|
|
|
| 86 |
'redirect_uri': REDIRECT_URI,
|
| 87 |
'response_mode': 'query',
|
| 88 |
'scope': 'User.Read',
|
| 89 |
+
'state': '12345'
|
| 90 |
}
|
| 91 |
|
| 92 |
# Construct the login URL
|
| 93 |
login_url = f"{AUTH_URL}?{urlencode(params)}"
|
| 94 |
|
|
|
|
| 95 |
def show_login_button():
|
| 96 |
return f'<a href="{login_url}" target="_blank">Click here to login with Microsoft</a>'
|
| 97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
def exchange_code_for_token(auth_code):
|
| 99 |
data = {
|
| 100 |
'grant_type': 'authorization_code',
|
|
|
|
| 113 |
else:
|
| 114 |
return None
|
| 115 |
|
| 116 |
+
def handle_redirect(url):
|
| 117 |
+
parsed_url = urlparse(url)
|
| 118 |
+
query_params = parse_qs(parsed_url.query)
|
| 119 |
+
auth_code = query_params.get('code')
|
| 120 |
+
|
| 121 |
+
if auth_code:
|
| 122 |
+
token = exchange_code_for_token(auth_code[0])
|
| 123 |
+
return token # return the token or handle accordingly
|
| 124 |
+
return None
|
| 125 |
|
| 126 |
# Function to retrieve answer using the RAG system
|
| 127 |
@spaces.GPU(duration=60)
|
|
|
|
| 138 |
|
| 139 |
return corrected_text_books
|
| 140 |
|
|
|
|
| 141 |
def chat(query, history=None):
|
| 142 |
if history is None:
|
| 143 |
history = []
|
|
|
|
| 146 |
history.append((query, answer))
|
| 147 |
return history, "" # Clear input after submission
|
| 148 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
with gr.Blocks() as interface:
|
| 150 |
gr.Markdown("## RAG Chatbot")
|
| 151 |
gr.Markdown("Please log in to continue.")
|
| 152 |
|
| 153 |
+
login_link = gr.HTML(show_login_button())
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
# Components for chat (initially hidden)
|
| 156 |
input_box = gr.Textbox(label="Enter your question", placeholder="Type your question here...", visible=False)
|
| 157 |
submit_btn = gr.Button("Submit", visible=False)
|
| 158 |
chat_history = gr.Chatbot(label="Chat History", visible=False)
|
| 159 |
|
| 160 |
+
# Add a function to handle the redirect from Microsoft
|
| 161 |
+
redirect_url_input = gr.Textbox(label="Redirect URL", visible=False)
|
| 162 |
+
|
| 163 |
+
redirect_url_input.change(
|
| 164 |
+
handle_redirect,
|
| 165 |
+
inputs=[redirect_url_input],
|
| 166 |
+
outputs=[input_box, submit_btn] # Update visibility based on login
|
|
|
|
|
|
|
|
|
|
| 167 |
)
|
| 168 |
|
|
|
|
| 169 |
submit_btn.click(chat, inputs=[input_box, chat_history], outputs=[chat_history, input_box])
|
| 170 |
|
| 171 |
interface.launch()
|