sanjeevbora commited on
Commit
74cbdcf
·
verified ·
1 Parent(s): 46486f5
Files changed (1) hide show
  1. app.py +33 -20
app.py CHANGED
@@ -84,15 +84,17 @@ params = {
84
  'redirect_uri': REDIRECT_URI,
85
  'response_mode': 'query',
86
  'scope': 'User.Read',
87
- 'state': '12345'
 
88
  }
89
 
90
  # Construct the login URL
91
  login_url = f"{AUTH_URL}?{urlencode(params)}"
92
 
93
  def show_login_button():
94
- return f'<a href="{login_url}" class="GFG"> Click here to login with Microsoft </a>'
95
 
 
96
  def exchange_code_for_token(auth_code):
97
  data = {
98
  'grant_type': 'authorization_code',
@@ -101,7 +103,6 @@ def exchange_code_for_token(auth_code):
101
  'code': auth_code,
102
  'redirect_uri': REDIRECT_URI
103
  }
104
-
105
  response = requests.post(TOKEN_URL, data=data)
106
 
107
  if response.status_code == 200:
@@ -111,17 +112,19 @@ def exchange_code_for_token(auth_code):
111
  else:
112
  return None
113
 
 
114
  def handle_redirect(url):
115
  parsed_url = urlparse(url)
116
  query_params = parse_qs(parsed_url.query)
117
  auth_code = query_params.get('code')
118
-
119
  if auth_code:
120
  token = exchange_code_for_token(auth_code[0])
121
- return token # Return the token or handle accordingly
122
- return None
 
123
 
124
- # Function to retrieve answers using the RAG system
125
  @spaces.GPU(duration=60)
126
  def test_rag(query):
127
  books_retriever = books_db_client_retriever.run(query)
@@ -136,6 +139,7 @@ def test_rag(query):
136
 
137
  return corrected_text_books
138
 
 
139
  def chat(query, history=None):
140
  if history is None:
141
  history = []
@@ -144,20 +148,35 @@ def chat(query, history=None):
144
  history.append((query, answer))
145
  return history, "" # Clear input after submission
146
 
147
- # Gradio interface
 
 
 
 
 
148
  with gr.Blocks() as interface:
149
  with gr.Tab("Login"):
150
  gr.Markdown("## Login Page")
151
  login_link = gr.HTML(show_login_button())
152
 
153
  # Hidden textbox for redirect URL
154
- redirect_url_input = gr.Textbox(label="Redirect URL", visible=False)
 
 
155
 
156
- # Handle redirect
 
 
 
 
 
 
 
 
157
  redirect_url_input.change(
158
- handle_redirect,
159
  inputs=[redirect_url_input],
160
- outputs=[redirect_url_input],
161
  show_progress=True
162
  )
163
 
@@ -169,13 +188,7 @@ with gr.Blocks() as interface:
169
  submit_btn = gr.Button("Submit", visible=False)
170
  chat_history = gr.Chatbot(label="Chat History", visible=False)
171
 
172
- redirect_url_input.change(
173
- handle_redirect,
174
- inputs=[redirect_url_input],
175
- outputs=[input_box, submit_btn, chat_history], # Update visibility based on login
176
- show_progress=True
177
- )
178
-
179
  submit_btn.click(chat, inputs=[input_box, chat_history], outputs=[chat_history, input_box])
180
 
181
- interface.launch()
 
84
  'redirect_uri': REDIRECT_URI,
85
  'response_mode': 'query',
86
  'scope': 'User.Read',
87
+ 'state': '12345',
88
+ 'prompt': 'login' # This ensures the login prompt appears even if already logged in
89
  }
90
 
91
  # Construct the login URL
92
  login_url = f"{AUTH_URL}?{urlencode(params)}"
93
 
94
  def show_login_button():
95
+ return f'<a href="{login_url}" class="GFG">Click here to login with Microsoft</a>'
96
 
97
+ # Function to exchange auth code for token
98
  def exchange_code_for_token(auth_code):
99
  data = {
100
  'grant_type': 'authorization_code',
 
103
  'code': auth_code,
104
  'redirect_uri': REDIRECT_URI
105
  }
 
106
  response = requests.post(TOKEN_URL, data=data)
107
 
108
  if response.status_code == 200:
 
112
  else:
113
  return None
114
 
115
+ # Function to handle redirect URL and extract the auth code
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
+ if token:
124
+ return "Logged in", True # Successfully logged in
125
+ return "Login failed", False
126
 
127
+ # Function to retrieve answer using the RAG system
128
  @spaces.GPU(duration=60)
129
  def test_rag(query):
130
  books_retriever = books_db_client_retriever.run(query)
 
139
 
140
  return corrected_text_books
141
 
142
+ # Define the Gradio interface
143
  def chat(query, history=None):
144
  if history is None:
145
  history = []
 
148
  history.append((query, answer))
149
  return history, "" # Clear input after submission
150
 
151
+ # Function to clear input text
152
+ def clear_input():
153
+ return "", # Return empty string to clear input field
154
+
155
+
156
+ # Gradio Interface
157
  with gr.Blocks() as interface:
158
  with gr.Tab("Login"):
159
  gr.Markdown("## Login Page")
160
  login_link = gr.HTML(show_login_button())
161
 
162
  # Hidden textbox for redirect URL
163
+ redirect_url_input = gr.Textbox(label="Redirect URL", visible=True) # URL from the Microsoft redirect
164
+
165
+ status_label = gr.Label(value="Not logged in") # Label to show login status
166
 
167
+ def on_redirect(redirect_url):
168
+ # Extract and exchange token
169
+ status, logged_in = handle_redirect(redirect_url)
170
+ if logged_in:
171
+ return gr.update(visible=False), status, gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
172
+ else:
173
+ return gr.update(visible=True), status, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
174
+
175
+ # Handle redirect and switch to chatbot page upon login
176
  redirect_url_input.change(
177
+ on_redirect,
178
  inputs=[redirect_url_input],
179
+ outputs=[redirect_url_input, status_label, gr.Textbox.update(visible=True), gr.Button.update(visible=True), gr.Chatbot.update(visible=True)],
180
  show_progress=True
181
  )
182
 
 
188
  submit_btn = gr.Button("Submit", visible=False)
189
  chat_history = gr.Chatbot(label="Chat History", visible=False)
190
 
191
+ # Chat submission
 
 
 
 
 
 
192
  submit_btn.click(chat, inputs=[input_box, chat_history], outputs=[chat_history, input_box])
193
 
194
+ interface.launch()