aagoluoglu commited on
Commit
35eab52
·
verified ·
1 Parent(s): fc4c647

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -38
app.py CHANGED
@@ -1,60 +1,116 @@
1
- import os
2
- import requests
3
- import json
4
- from io import BytesIO
5
 
6
- from flask import Flask, jsonify, render_template, request, send_file
7
 
8
- from modules.inference import infer_t5
9
- from modules.dataset import query_emotion
10
 
11
- # https://huggingface.co/settings/tokens
12
- # https://huggingface.co/spaces/{username}/{space}/settings
13
- API_TOKEN = os.getenv("BIG_GAN_TOKEN")
14
 
15
- app = Flask(__name__)
16
 
17
 
18
- @app.route("/")
19
- def index():
20
- return render_template("index.html")
21
 
22
 
23
- @app.route("/infer_biggan")
24
- def biggan():
25
- input = request.args.get("input")
26
 
27
- output = requests.request(
28
- "POST",
29
- "https://api-inference.huggingface.co/models/osanseviero/BigGAN-deep-128",
30
- headers={"Authorization": f"Bearer {API_TOKEN}"},
31
- data=json.dumps(input),
32
- )
33
 
34
- return send_file(BytesIO(output.content), mimetype="image/png")
35
 
36
 
37
- @app.route("/infer_t5")
38
- def t5():
39
- input = request.args.get("input")
40
 
41
- output = infer_t5(input)
42
 
43
- return jsonify({"output": output})
44
 
45
 
46
- @app.route("/query_emotion")
47
- def emotion():
48
- start = request.args.get("start")
49
- end = request.args.get("end")
50
 
51
- print(start)
52
- print(end)
53
 
54
- output = query_emotion(int(start), int(end))
55
 
56
- return jsonify({"output": output})
57
 
58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  if __name__ == "__main__":
60
- app.run(host="0.0.0.0", port=7860)
 
 
1
+ # import os
2
+ # import requests
3
+ # import json
4
+ # from io import BytesIO
5
 
6
+ # from flask import Flask, jsonify, render_template, request, send_file
7
 
8
+ # from modules.inference import infer_t5
9
+ # from modules.dataset import query_emotion
10
 
11
+ # # https://huggingface.co/settings/tokens
12
+ # # https://huggingface.co/spaces/{username}/{space}/settings
13
+ # API_TOKEN = os.getenv("BIG_GAN_TOKEN")
14
 
15
+ # app = Flask(__name__)
16
 
17
 
18
+ # @app.route("/")
19
+ # def index():
20
+ # return render_template("index.html")
21
 
22
 
23
+ # @app.route("/infer_biggan")
24
+ # def biggan():
25
+ # input = request.args.get("input")
26
 
27
+ # output = requests.request(
28
+ # "POST",
29
+ # "https://api-inference.huggingface.co/models/osanseviero/BigGAN-deep-128",
30
+ # headers={"Authorization": f"Bearer {API_TOKEN}"},
31
+ # data=json.dumps(input),
32
+ # )
33
 
34
+ # return send_file(BytesIO(output.content), mimetype="image/png")
35
 
36
 
37
+ # @app.route("/infer_t5")
38
+ # def t5():
39
+ # input = request.args.get("input")
40
 
41
+ # output = infer_t5(input)
42
 
43
+ # return jsonify({"output": output})
44
 
45
 
46
+ # @app.route("/query_emotion")
47
+ # def emotion():
48
+ # start = request.args.get("start")
49
+ # end = request.args.get("end")
50
 
51
+ # print(start)
52
+ # print(end)
53
 
54
+ # output = query_emotion(int(start), int(end))
55
 
56
+ # return jsonify({"output": output})
57
 
58
 
59
+ # if __name__ == "__main__":
60
+ # app.run(host="0.0.0.0", port=7860)
61
+
62
+ from flask import Flask, redirect, url_for, session, request, jsonify, render_template
63
+ from google.oauth2 import id_token
64
+ from google.auth.transport import requests as grequests
65
+ import os
66
+
67
+ app = Flask(__name__)
68
+ app.secret_key = os.urandom(24)
69
+
70
+ CLIENT_ID = "your_google_client_id"
71
+ CLIENT_SECRET = os.environ.get("GOOGLE_CLIENT_SECRET")
72
+
73
+ @app.route("/")
74
+ def index():
75
+ return render_template("index.html")
76
+
77
+ @app.route("/login")
78
+ def login():
79
+ return redirect("https://accounts.google.com/o/oauth2/auth?response_type=code&client_id={}&redirect_uri=http://localhost:5000/callback&scope=email%20profile&access_type=offline".format(CLIENT_ID))
80
+
81
+ @app.route("/callback")
82
+ def callback():
83
+ code = request.args.get("code")
84
+ token_response = grequests.post("https://oauth2.googleapis.com/token",
85
+ data={
86
+ "code": code,
87
+ "client_id": CLIENT_ID,
88
+ "client_secret": CLIENT_SECRET,
89
+ "redirect_uri": "http://localhost:3000/callback",
90
+ "grant_type": "authorization_code"
91
+ })
92
+ token_data = token_response.json()
93
+ access_token = token_data.get("access_token")
94
+ id_info = id_token.verify_oauth2_token(token_data.get("id_token"), grequests.Request(), CLIENT_ID)
95
+
96
+ # Store user information in session
97
+ session['user'] = id_info
98
+
99
+ return redirect(url_for("profile"))
100
+
101
+ @app.route("/profile")
102
+ def profile():
103
+ user = session.get('user')
104
+ if user:
105
+ return render_template("profile.html", user=user)
106
+ else:
107
+ return "User not authenticated"
108
+
109
+ @app.route("/logout")
110
+ def logout():
111
+ session.pop('user', None)
112
+ return redirect(url_for("index"))
113
+
114
  if __name__ == "__main__":
115
+ app.run(debug=True)
116
+