dongwook-chan commited on
Commit
f74d62a
β€’
1 Parent(s): 21c4e4d

Connect to Google Sheets

Browse files
credentials.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"installed":{"client_id":"41519890488-4qdl7mkrl3i34oj1pr6hfn124v4mbng8.apps.googleusercontent.com","project_id":"ice-breaking-challenge","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"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","redirect_uris":["http://localhost"]}}
ice_breaking_challenge/__init__.py CHANGED
@@ -3,12 +3,16 @@ import os
3
  from flask import Flask, session
4
  from flask_session import Session
5
  from ice_breaking_challenge.models.model_loader import load_model_with_lora
 
6
 
7
  model = None
8
 
9
  def create_app(test_config=None):
10
  """Create and configure an instance of the Flask application."""
11
  global model
 
 
 
12
  app = Flask(__name__, instance_relative_config=True)
13
  app.config.from_mapping(
14
  # a default secret that should be overridden by instance config
 
3
  from flask import Flask, session
4
  from flask_session import Session
5
  from ice_breaking_challenge.models.model_loader import load_model_with_lora
6
+ from ice_breaking_challenge.google_sheets import load_google_sheets
7
 
8
  model = None
9
 
10
  def create_app(test_config=None):
11
  """Create and configure an instance of the Flask application."""
12
  global model
13
+
14
+ load_google_sheets()
15
+
16
  app = Flask(__name__, instance_relative_config=True)
17
  app.config.from_mapping(
18
  # a default secret that should be overridden by instance config
ice_breaking_challenge/google_sheets.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os.path
2
+
3
+ from google.auth.transport.requests import Request
4
+ from google.oauth2.credentials import Credentials
5
+ from google_auth_oauthlib.flow import InstalledAppFlow
6
+ from googleapiclient.discovery import build
7
+ from googleapiclient.errors import HttpError
8
+
9
+ # If modifying these scopes, delete the file token.json.
10
+ SCOPES = ["https://www.googleapis.com/auth/spreadsheets"]
11
+
12
+ # The ID and range of a sample spreadsheet.
13
+ # SAMPLE_SPREADSHEET_ID = "1Z3doUDpoUmXs7p4-g7f8q3XZIBhH_Lz2w51bnpUFigw"
14
+ SAMPLE_SPREADSHEET_ID = "1NJGhpMsfUySHJcMBNpLTEnt-r5Co6qwRahenIa9D55k"
15
+ SAMPLE_RANGE_NAME = "Responses!I2:I30"
16
+
17
+ SERVICE = None
18
+
19
+
20
+ def load_google_sheets():
21
+ global SERVICE
22
+ """Shows basic usage of the Sheets API.
23
+ Prints values from a sample spreadsheet.
24
+ """
25
+ creds = None
26
+ # The file token.json stores the user's access and refresh tokens, and is
27
+ # created automatically when the authorization flow completes for the first
28
+ # time.
29
+ if os.path.exists("token.json"):
30
+ creds = Credentials.from_authorized_user_file("token.json", SCOPES)
31
+ # If there are no (valid) credentials available, let the user log in.
32
+ if not creds or not creds.valid:
33
+ if creds and creds.expired and creds.refresh_token:
34
+ creds.refresh(Request())
35
+ else:
36
+ flow = InstalledAppFlow.from_client_secrets_file(
37
+ "credentials.json", SCOPES
38
+ )
39
+ creds = flow.run_local_server(port=0)
40
+ # Save the credentials for the next run
41
+ with open("token.json", "w") as token:
42
+ token.write(creds.to_json())
43
+
44
+ SERVICE = build("sheets", "v4", credentials=creds)
45
+
46
+ def get_team_numbers() -> list[str]:
47
+ sheet = SERVICE.spreadsheets()
48
+ result = (
49
+ sheet.values()
50
+ .get(
51
+ spreadsheetId=SAMPLE_SPREADSHEET_ID,
52
+ range=SAMPLE_RANGE_NAME
53
+ )
54
+ .execute()
55
+ )
56
+ values = result.get("values", [])
57
+
58
+ if not values:
59
+ print("No data found.")
60
+ return
61
+
62
+ return [value[0] for value in values]
ice_breaking_challenge/models/model_loader.py CHANGED
@@ -18,11 +18,16 @@ def load_model_with_lora(model_name:str = MODEL_NAME, lora_weight_path: str = LO
18
 
19
 
20
  model.backbone.load_lora_weights(lora_weight_path)
21
- question_crawling="λ‚˜μ˜ 이런 점은 일할 λ•Œ 도움이 돼!?"
22
- answer_crawling="λ‚˜λˆ„κ³  μ‹Άμ–΄ν•˜λŠ” 마음? μ£Όλ³€ μ‚¬λžŒλ“€μ€ 그만 퍼주라고 ν•˜κΈ°λ„ ν•˜μ§€λ§Œ, λ‚΄κ°€ ν΄λΌμ΄μ–ΈνŠΈλ‘œλΆ€ν„° λˆμ„ 벌고자 ν•˜λŠ” 것이 μ•„λ‹ˆλΌ μ‘°κΈˆμ΄λΌλ„ 더 μ±™κ²¨μ£Όκ³ μž ν•˜λŠ” λ§ˆμŒμ„ κ°€μ‘Œμ„ λ•Œ κ²°κ΅­ λ‚˜μ˜ λΈŒλžœλ“œκ°€ 훨씬 더 컀질 수 μžˆλ‹€λŠ” 믿음이 μžˆλ‹€."
 
 
 
 
 
 
 
23
 
24
- input_text = f"{question_crawling} {answer_crawling}"
25
-
26
  print(model.generate(input_text, max_length=512))
27
 
28
  return model
 
18
 
19
 
20
  model.backbone.load_lora_weights(lora_weight_path)
21
+ # question_crawling="λ‚˜μ˜ 이런 점은 일할 λ•Œ 도움이 돼!?"
22
+ question_crawling="μ¦κ²¨λ³΄λŠ” μœ νŠœλ²„?"
23
+ # answer_crawling="λ‚˜λˆ„κ³  μ‹Άμ–΄ν•˜λŠ” 마음? μ£Όλ³€ μ‚¬λžŒλ“€μ€ 그만 퍼주라고 ν•˜κΈ°λ„ ν•˜μ§€λ§Œ, λ‚΄κ°€ ν΄λΌμ΄μ–ΈνŠΈλ‘œλΆ€ν„° λˆμ„ 벌고자 ν•˜λŠ” 것이 μ•„λ‹ˆλΌ μ‘°κΈˆμ΄λΌλ„ 더 μ±™κ²¨μ£Όκ³ μž ν•˜λŠ” λ§ˆμŒμ„ κ°€μ‘Œμ„ λ•Œ κ²°κ΅­ λ‚˜μ˜ λΈŒλžœλ“œκ°€ 훨씬 더 컀질 수 μžˆλ‹€λŠ” 믿음이 μžˆλ‹€."
24
+ answer_crawling="μ„ λ°”, λ“œλ¦½μΉ˜λŠ” 게 제 μ·¨ν–₯μž…λ‹ˆλ‹€."
25
+
26
+ input_text = f"""
27
+ <instruction>
28
+ Using the text: {question_crawling} {answer_crawling}, create a new multiple-choice question with 4 answer options.
29
+ """
30
 
 
 
31
  print(model.generate(input_text, max_length=512))
32
 
33
  return model
ice_breaking_challenge/qr.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  from flask import Blueprint
2
  from flask import flash
3
  from flask import g
@@ -10,6 +12,7 @@ from flask import session
10
 
11
  from .auth import login_required
12
  from .db import get_db
 
13
 
14
 
15
  bp = Blueprint("qr", __name__, url_prefix="/qr")
@@ -21,4 +24,12 @@ def qr() -> None:
21
  case "GET":
22
  return render_template("qr.html")
23
  case "POST": # qr.htmlμ—μ„œ `λ‹€μŒ` λ²„νŠΌ λˆŒλ €μ„ λ•Œ
24
- return render_template("introduction.html")
 
 
 
 
 
 
 
 
 
1
+ from collections import Counter
2
+
3
  from flask import Blueprint
4
  from flask import flash
5
  from flask import g
 
12
 
13
  from .auth import login_required
14
  from .db import get_db
15
+ from ice_breaking_challenge.google_sheets import get_team_numbers
16
 
17
 
18
  bp = Blueprint("qr", __name__, url_prefix="/qr")
 
24
  case "GET":
25
  return render_template("qr.html")
26
  case "POST": # qr.htmlμ—μ„œ `λ‹€μŒ` λ²„νŠΌ λˆŒλ €μ„ λ•Œ
27
+ counter = Counter(get_team_numbers())
28
+ if session.get("team_number") not in counter:
29
+ flash("ν•΄λ‹Ή νŒ€ λ²ˆν˜Έκ°€ μ‘΄μž¬ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.")
30
+ return render_template("index.html")
31
+ if counter[session.get("team_number")] == session["team_size"]:
32
+ return render_template("introduction.html")
33
+ else:
34
+ flash("섀문을 μ™„λ£Œν•˜μ§€ μ•Šμ€ νŒ€μ›μ΄ μžˆμŠ΅λ‹ˆλ‹€.")
35
+ return render_template("qr.html")
requirements.txt CHANGED
@@ -3,4 +3,11 @@ Flask-Session
3
  tensorflow
4
  keras
5
  keras-nlp
6
- tensorflow-text
 
 
 
 
 
 
 
 
3
  tensorflow
4
  keras
5
  keras-nlp
6
+ tensorflow-text
7
+ google
8
+ google-api-core
9
+ google-api-python-client
10
+ google-auth
11
+ google-auth-httplib2
12
+ google-auth-oauthlib
13
+ google-common-protos
token.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"token": "ya29.a0AcM612wxloLuLRCUhc53Pra8ugAHcUjNY6Qv1NEUZg0MZkDI3JU2JrI1_5b-tV0YeyoA5J8JgdFfTBjRhC9U_o36s1arZwIFJlGgc2yr0OVrfpen1NMU-jWIiuTAqA9FrfOIsMruNraxXR0y1dW8RNHdP4gwjcf5ukBgqLdVqAaCgYKAZASARMSFQHGX2MiE34UvuAvpMm3dk8CdQJqWg0177", "refresh_token": "1//0eNvRcc0OMNKPCgYIARAAGA4SNwF-L9Irom_jI7Oy6m71V1i05HPrPON8nR1N90q-fFcY0U-TxLK33r9K1qNSv4Jr1__o3hXxNEo", "token_uri": "https://oauth2.googleapis.com/token", "client_id": "41519890488-4qdl7mkrl3i34oj1pr6hfn124v4mbng8.apps.googleusercontent.com", "client_secret": "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", "scopes": ["https://www.googleapis.com/auth/spreadsheets"], "universe_domain": "googleapis.com", "account": "", "expiry": "2024-09-28T18:31:01.030411Z"}