Spaces:
Paused
Paused
| import gradio as gr | |
| import requests | |
| import webbrowser | |
| from http.server import BaseHTTPRequestHandler, HTTPServer | |
| import threading | |
| import spaces | |
| # OAuth Configuration | |
| TENANT_ID = '2b093ced-2571-463f-bc3e-b4f8bcb427ee' | |
| CLIENT_ID = '2a7c884c-942d-49e2-9e5d-7a29d8a0d3e5' | |
| CLIENT_SECRET = 'EOF8Q~kKHCRgx8tnlLM-H8e93ifetxI6x7sU6bGW' | |
| REDIRECT_URI = 'https://sanjeevbora-chatbot.hf.space/' | |
| AUTH_URL = f"https://login.microsoftonline.com/{TENANT_ID}/oauth2/v2.0/authorize" | |
| TOKEN_URL = f"https://login.microsoftonline.com/{TENANT_ID}/oauth2/v2.0/token" | |
| SCOPE = 'User.Read' | |
| access_token = None | |
| class RequestHandler(BaseHTTPRequestHandler): | |
| def do_GET(self): | |
| global access_token | |
| if self.path.startswith("/callback"): | |
| # Capture the authorization code | |
| code = self.path.split("code=")[1] | |
| token_url = f"{AUTHORITY_URL}/token" | |
| response = requests.post(token_url, data={ | |
| 'client_id': CLIENT_ID, | |
| 'client_secret': CLIENT_SECRET, | |
| 'grant_type': 'authorization_code', | |
| 'code': code, | |
| 'redirect_uri': REDIRECT_URI | |
| }) | |
| token_data = response.json() | |
| access_token = token_data.get('access_token') | |
| self.send_response(200) | |
| self.end_headers() | |
| self.wfile.write(b"Login successful! You can close this window.") | |
| return | |
| self.send_response(404) | |
| self.end_headers() | |
| def start_http_server(): | |
| server_address = ('', 8080) | |
| httpd = HTTPServer(server_address, RequestHandler) | |
| httpd.serve_forever() | |
| def login(): | |
| auth_url = f"{AUTHORITY_URL}/authorize?client_id={CLIENT_ID}&response_type=code&redirect_uri={REDIRECT_URI}&scope={SCOPE}" | |
| webbrowser.open(auth_url) | |
| def check_login(): | |
| return "You are logged in!" if access_token else "You are not logged in." | |
| def gradio_interface(): | |
| with gr.Blocks() as demo: | |
| gr.Markdown("### Welcome to the App") | |
| btn_login = gr.Button("Login with Microsoft") | |
| output = gr.Textbox(label="Status") | |
| btn_login.click(lambda: (login(), check_login()), None, output) | |
| return demo | |
| if __name__ == "__main__": | |
| # Start the HTTP server in a separate thread | |
| threading.Thread(target=start_http_server, daemon=True).start() | |
| # Launch Gradio app | |
| demo = gradio_interface() | |
| demo.launch() |