Pijush2023 commited on
Commit
675a762
·
verified ·
1 Parent(s): 7d3451d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -2
app.py CHANGED
@@ -23,6 +23,51 @@ from langchain.chains.conversation.memory import ConversationBufferWindowMemory
23
  from langchain.agents import Tool, initialize_agent
24
  from huggingface_hub import login
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
 
28
 
@@ -975,5 +1020,8 @@ with gr.Blocks(theme='Pijush2023/scikit-learn-pijush') as demo:
975
  refresh_button = gr.Button("Refresh Images")
976
  refresh_button.click(fn=update_images, inputs=None, outputs=[image_output_1, image_output_2, image_output_3])
977
 
978
- demo.queue()
979
- demo.launch(share=True)
 
 
 
 
23
  from langchain.agents import Tool, initialize_agent
24
  from huggingface_hub import login
25
 
26
+ # Flask app for authentication
27
+ auth_app = Flask(__name__)
28
+ auth_app.secret_key = os.urandom(24)
29
+
30
+ client_id = os.getenv("GOOGLE_CLIENT_ID")
31
+ client_secret = os.getenv("GOOGLE_CLIENT_SECRET")
32
+ authorization_base_url = 'https://accounts.google.com/o/oauth2/auth'
33
+ token_url = 'https://accounts.google.com/o/oauth2/token'
34
+ redirect_uri = 'http://localhost:5000/callback'
35
+
36
+ scope = [
37
+ "https://www.googleapis.com/auth/userinfo.profile",
38
+ "https://www.googleapis.com/auth/userinfo.email",
39
+ "openid",
40
+ ]
41
+
42
+ @auth_app.route("/")
43
+ def home():
44
+ return '<a href="/login">Login with Google</a>'
45
+
46
+ @auth_app.route("/login")
47
+ def login():
48
+ google = OAuth2Session(client_id, scope=scope, redirect_uri=redirect_uri)
49
+ authorization_url, state = google.authorization_url(authorization_base_url, access_type="offline")
50
+ session['oauth_state'] = state
51
+ return redirect(authorization_url)
52
+
53
+ @auth_app.route("/callback")
54
+ def callback():
55
+ google = OAuth2Session(client_id, state=session['oauth_state'], redirect_uri=redirect_uri)
56
+ token = google.fetch_token(token_url, client_secret=client_secret, authorization_response=request.url)
57
+ session['oauth_token'] = token
58
+ return redirect(url_for('.profile'))
59
+
60
+ @auth_app.route("/profile")
61
+ def profile():
62
+ google = OAuth2Session(client_id, token=session['oauth_token'])
63
+ response = google.get('https://www.googleapis.com/oauth2/v1/userinfo')
64
+ user_info = response.json()
65
+ return f"Hello, {user_info['name']}! Your email is {user_info['email']}."
66
+
67
+ # Check if user is authenticated
68
+ def check_auth():
69
+ if 'oauth_token' not in session:
70
+ return redirect(url_for('login'))
71
 
72
 
73
 
 
1020
  refresh_button = gr.Button("Refresh Images")
1021
  refresh_button.click(fn=update_images, inputs=None, outputs=[image_output_1, image_output_2, image_output_3])
1022
 
1023
+ if __name__ == "__main__":
1024
+ from threading import Thread
1025
+ Thread(target=auth_app.run, kwargs={'port': 5000, 'debug': True}).start()
1026
+ demo.queue()
1027
+ demo.launch(share=True)