Spaces:
Paused
Paused
auth
Browse files
app.py
CHANGED
@@ -77,10 +77,27 @@ params = {
|
|
77 |
'state': '12345' # Optional state parameter
|
78 |
}
|
79 |
|
80 |
-
#
|
81 |
login_url = f"{AUTH_URL}?{urlencode(params)}"
|
82 |
-
print("Redirect to:", login_url)
|
83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
def exchange_code_for_token(auth_code):
|
85 |
data = {
|
86 |
'grant_type': 'authorization_code',
|
@@ -91,9 +108,22 @@ def exchange_code_for_token(auth_code):
|
|
91 |
}
|
92 |
|
93 |
response = requests.post(TOKEN_URL, data=data)
|
94 |
-
token_data = response.json()
|
95 |
|
96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
|
98 |
# Function to retrieve answer using the RAG system
|
99 |
@spaces.GPU(duration=60)
|
@@ -123,17 +153,34 @@ def chat(query, history=None):
|
|
123 |
def clear_input():
|
124 |
return "", # Return empty string to clear input field
|
125 |
|
126 |
-
# Gradio interface
|
127 |
with gr.Blocks() as interface:
|
128 |
gr.Markdown("## RAG Chatbot")
|
129 |
-
gr.Markdown("
|
130 |
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
|
|
135 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
136 |
submit_btn.click(chat, inputs=[input_box, chat_history], outputs=[chat_history, input_box])
|
137 |
-
# clear_btn.click(clear_input, outputs=input_box)
|
138 |
|
139 |
interface.launch()
|
|
|
77 |
'state': '12345' # Optional state parameter
|
78 |
}
|
79 |
|
80 |
+
# Construct the login URL
|
81 |
login_url = f"{AUTH_URL}?{urlencode(params)}"
|
|
|
82 |
|
83 |
+
# Gradio interface
|
84 |
+
def show_login_button():
|
85 |
+
return f'<a href="{login_url}" target="_blank">Click here to login with Microsoft</a>'
|
86 |
+
|
87 |
+
# Dummy function to simulate token validation (you will replace this with actual validation)
|
88 |
+
def is_logged_in(token):
|
89 |
+
# Check if the token exists (or check if it's valid)
|
90 |
+
return token is not None
|
91 |
+
|
92 |
+
# Gradio interface
|
93 |
+
def check_login(status):
|
94 |
+
# If logged in, show the chatbot interface, otherwise show login link
|
95 |
+
if status:
|
96 |
+
return gr.update(visible=True), gr.update(visible=True)
|
97 |
+
else:
|
98 |
+
return gr.update(visible=False), gr.update(visible=False)
|
99 |
+
|
100 |
+
# Function to exchange authorization code for access token
|
101 |
def exchange_code_for_token(auth_code):
|
102 |
data = {
|
103 |
'grant_type': 'authorization_code',
|
|
|
108 |
}
|
109 |
|
110 |
response = requests.post(TOKEN_URL, data=data)
|
|
|
111 |
|
112 |
+
if response.status_code == 200:
|
113 |
+
token_data = response.json()
|
114 |
+
access_token = token_data.get('access_token')
|
115 |
+
return access_token
|
116 |
+
else:
|
117 |
+
return None
|
118 |
+
|
119 |
+
def login_user(auth_code):
|
120 |
+
# Exchange the authorization code for an access token
|
121 |
+
token = exchange_code_for_token(auth_code)
|
122 |
+
|
123 |
+
if token:
|
124 |
+
return token
|
125 |
+
else:
|
126 |
+
return None
|
127 |
|
128 |
# Function to retrieve answer using the RAG system
|
129 |
@spaces.GPU(duration=60)
|
|
|
153 |
def clear_input():
|
154 |
return "", # Return empty string to clear input field
|
155 |
|
|
|
156 |
with gr.Blocks() as interface:
|
157 |
gr.Markdown("## RAG Chatbot")
|
158 |
+
gr.Markdown("Please log in to continue.")
|
159 |
|
160 |
+
# Custom HTML to show login link
|
161 |
+
login_link = gr.HTML(f'<a href="{login_url}" target="_blank">Click here to login with Microsoft</a>')
|
162 |
+
|
163 |
+
# Login button to simulate the login process
|
164 |
+
login_button = gr.Button("Login")
|
165 |
|
166 |
+
# Components for chat (initially hidden)
|
167 |
+
input_box = gr.Textbox(label="Enter your question", placeholder="Type your question here...", visible=False)
|
168 |
+
submit_btn = gr.Button("Submit", visible=False)
|
169 |
+
chat_history = gr.Chatbot(label="Chat History", visible=False)
|
170 |
+
|
171 |
+
# Handle login button click
|
172 |
+
login_button.click(
|
173 |
+
login_user,
|
174 |
+
inputs=[],
|
175 |
+
outputs=[login_button], # You can also update the UI to show login status
|
176 |
+
queue=False
|
177 |
+
).then(
|
178 |
+
lambda token: check_login(is_logged_in(token)),
|
179 |
+
inputs=[],
|
180 |
+
outputs=[input_box, submit_btn]
|
181 |
+
)
|
182 |
+
|
183 |
+
# Input submission and chat handling
|
184 |
submit_btn.click(chat, inputs=[input_box, chat_history], outputs=[chat_history, input_box])
|
|
|
185 |
|
186 |
interface.launch()
|