dongwook-chan commited on
Commit
2e9ea78
โ€ข
1 Parent(s): 0c2babc

Finished developing back-end

Browse files
flask_session/2029240f6d1128be89ddc32729463129 DELETED
Binary file (9 Bytes)
 
flask_session/4b3a5c21ed5e4f46a96be9fe6a0bfb18 DELETED
Binary file (68 Bytes)
 
flask_session/c9f52df6baf57fcc03af8634ade01a4c DELETED
Binary file (68 Bytes)
 
ice_breaking_challenge/background_task.py CHANGED
@@ -1,21 +1,52 @@
1
- from flask import session
2
  import time
3
  import random
 
 
 
 
 
4
  from ice_breaking_challenge import model, quiz_generated, quiz_results
 
5
 
6
- def background_task(sid, qna):
7
  """Session์— ์ €์žฅ๋œ ์‘๋‹ต์„ ๋ฐ”ํƒ•์œผ๋กœ Gemma ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•œ ํ€ด์ฆˆ ์ƒ์„ฑ"""
 
 
 
 
8
  quiz_generated[sid] = False
9
- generated_quizzes = generate_quiz(qna)
10
  quiz_results[sid] = generated_quizzes
11
  # ํ€ด์ฆˆ ์ƒ์„ฑ ์™„๋ฃŒ
12
  quiz_generated[sid] = True
13
 
14
- def generate_quiz(data):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  global model
16
  generated_quizzes = []
17
 
18
- selected_data = random.sample(data, min(10, len(data)))
 
19
 
20
  template_input="""
21
  <instruction>
@@ -23,18 +54,38 @@ def generate_quiz(data):
23
  """
24
  for row in selected_data:
25
  question, answer, name = row
26
-
27
  input_text = template_input.format(question=question, answer=answer)
28
-
29
- response = model.generate(input_text, max_length=512)
30
 
 
 
31
  lines = response.split('\n')
32
  lines = [line.strip() for line in lines]
33
- index = lines.index('<response>')
34
- question_generated = lines[index+1]
35
- multiple_choice_generated = lines[index + 2]
36
- answer_generated = lines[index + 3]
37
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  generated_quizzes.append([name, question_generated, multiple_choice_generated, answer_generated])
39
 
40
  return generated_quizzes
 
 
1
  import time
2
  import random
3
+ import re
4
+
5
+ from flask import session
6
+ import pandas as pd
7
+
8
  from ice_breaking_challenge import model, quiz_generated, quiz_results
9
+ from ice_breaking_challenge.google_sheets import QUESTIONS
10
 
11
+ def background_task(sid, qna: pd.DataFrame, team_size: int):
12
  """Session์— ์ €์žฅ๋œ ์‘๋‹ต์„ ๋ฐ”ํƒ•์œผ๋กœ Gemma ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•œ ํ€ด์ฆˆ ์ƒ์„ฑ"""
13
+ import time
14
+ start_time = time.time()
15
+
16
+ global quiz_results
17
  quiz_generated[sid] = False
18
+ generated_quizzes = generate_quiz(qna, team_size)
19
  quiz_results[sid] = generated_quizzes
20
  # ํ€ด์ฆˆ ์ƒ์„ฑ ์™„๋ฃŒ
21
  quiz_generated[sid] = True
22
 
23
+ end_time = time.time()
24
+ print(end_time - start_time)
25
+
26
+
27
+ def sample_quizzes(data: pd.DataFrame, team_size: int):
28
+ questions_size = min(10 // team_size + 1, 5)
29
+ sample_indices: dict[str, list[str]] = dict()
30
+ for row in data.itertuples():
31
+ sample_indices[row.Index] = random.sample(['Q1', 'Q2', 'Q3', 'Q4', 'Q5'], questions_size)
32
+
33
+ sample_quizzes = []
34
+ for key, value in sample_indices.items():
35
+ for question_number in value:
36
+ question = QUESTIONS.iloc[0][question_number]
37
+ answer = data.iloc[key][question_number]
38
+ name = data.iloc[key]['name']
39
+ sample_quizzes.append([question, answer, name])
40
+
41
+ return sample_quizzes[:2] # TODO
42
+
43
+
44
+ def generate_quiz(data: pd.DataFrame, team_size: int):
45
  global model
46
  generated_quizzes = []
47
 
48
+ # selected_data = random.sample(data, min(10, len(data)))
49
+ selected_data = sample_quizzes(data, team_size)
50
 
51
  template_input="""
52
  <instruction>
 
54
  """
55
  for row in selected_data:
56
  question, answer, name = row
57
+
58
  input_text = template_input.format(question=question, answer=answer)
 
 
59
 
60
+ response = model.generate(input_text, max_length=512)
61
+ print(response)
62
  lines = response.split('\n')
63
  lines = [line.strip() for line in lines]
64
+
65
+ opening_tag_counter = 0
66
+ line_index_to_remove = None
67
+ for i, line in enumerate(lines):
68
+ if bool(re.search('^<[^/]', line)):
69
+ opening_tag_counter += 1
70
+
71
+ if opening_tag_counter == 2:
72
+ line_index_to_remove = i
73
+ break
74
+
75
+ lines = lines[line_index_to_remove + 1:]
76
+ lines = [re.sub('<.\w+>', "", line) for line in lines]
77
+ lines = [line.strip() for line in lines]
78
+ lines = [line for line in lines if line]
79
+
80
+ print(lines)
81
+ print("==========================================")
82
+
83
+ question_generated = lines[0]
84
+ multiple_choice_generated = lines[1]
85
+ answer_generated = lines[2]
86
+
87
+ print(question_generated, multiple_choice_generated, answer_generated)
88
+ print("==========================================")
89
  generated_quizzes.append([name, question_generated, multiple_choice_generated, answer_generated])
90
 
91
  return generated_quizzes
ice_breaking_challenge/google_sheets.py CHANGED
@@ -4,8 +4,7 @@ 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
- from flask import session
9
 
10
  # If modifying these scopes, delete the file token.json.
11
  SCOPES = ["https://www.googleapis.com/auth/spreadsheets"]
@@ -18,9 +17,26 @@ SERVICE = None
18
 
19
  MAX_ROW = 100
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  def load_google_sheets():
23
- global SERVICE
24
  """Shows basic usage of the Sheets API.
25
  Prints values from a sample spreadsheet.
26
  """
@@ -45,39 +61,18 @@ def load_google_sheets():
45
 
46
  SERVICE = build("sheets", "v4", credentials=creds)
47
 
48
-
49
- def get_range(range_name: str) -> list[list[str]]:
50
- sheet = SERVICE.spreadsheets()
51
- result = (
52
- sheet.values()
53
- .get(
54
- spreadsheetId=SAMPLE_SPREADSHEET_ID,
55
- range=range_name
56
- )
57
- .execute()
58
- )
59
- values = result.get("values", [])
60
-
61
- return values
62
-
63
 
64
  def get_team_numbers() -> list[str]:
65
  values = get_range(f"Responses!I2:I{MAX_ROW}")
66
 
67
- return [value[0] for value in values]
68
-
69
- def get_questions_and_answers() -> list[list[str]]:
70
- question_values = get_range("Responses!B1:F1")
71
-
72
- questions = question_values[0]
73
 
74
- answers_values = get_range(f"Responses!B2:F{MAX_ROW}")
 
75
 
76
- name_values = get_range(f"Responses!K2:K{MAX_ROW}")
77
 
78
- questions_and_answers: list[list[str]] = []
79
- for answers, names in zip(answers_values, name_values):
80
- for question, answer, name in zip(questions, answers, names):
81
- questions_and_answers.append([question, answer, name])
82
 
83
- return questions_and_answers
 
4
  from google.oauth2.credentials import Credentials
5
  from google_auth_oauthlib.flow import InstalledAppFlow
6
  from googleapiclient.discovery import build
7
+ import pandas as pd
 
8
 
9
  # If modifying these scopes, delete the file token.json.
10
  SCOPES = ["https://www.googleapis.com/auth/spreadsheets"]
 
17
 
18
  MAX_ROW = 100
19
 
20
+ QUESTIONS = None
21
+
22
+
23
+ def get_range(range_name: str) -> list[list[str]]:
24
+ sheet = SERVICE.spreadsheets()
25
+ result = (
26
+ sheet.values()
27
+ .get(
28
+ spreadsheetId=SAMPLE_SPREADSHEET_ID,
29
+ range=range_name
30
+ )
31
+ .execute()
32
+ )
33
+ values = result.get("values", [])
34
+
35
+ return values
36
+
37
 
38
  def load_google_sheets():
39
+ global SERVICE, QUESTIONS
40
  """Shows basic usage of the Sheets API.
41
  Prints values from a sample spreadsheet.
42
  """
 
61
 
62
  SERVICE = build("sheets", "v4", credentials=creds)
63
 
64
+ QUESTIONS = pd.DataFrame(get_range(f"Responses!B1:F1"), columns=["Q1", "Q2", "Q3", "Q4", "Q5"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
  def get_team_numbers() -> list[str]:
67
  values = get_range(f"Responses!I2:I{MAX_ROW}")
68
 
69
+ return [int(value[0]) for value in values]
 
 
 
 
 
70
 
71
+ def get_questions_and_answers() -> pd.DataFrame:
72
+ columns = ["Q1", "Q2", "Q3", "Q4", "Q5", "Q6", "Q7", "team_number", "email_address", "name"]
73
 
74
+ answers_values = get_range(f"Responses!B2:K{MAX_ROW}")
75
 
76
+ df = pd.DataFrame(answers_values, columns=columns)
 
 
 
77
 
78
+ return df
ice_breaking_challenge/index.py CHANGED
@@ -21,6 +21,6 @@ def index() -> None:
21
  case "GET":
22
  return render_template("index.html")
23
  case "POST": # index.html์—์„œ `๋‹ค์Œ` ๋ฒ„ํŠผ ๋ˆŒ๋ €์„ ๋•Œ
24
- session["team_number"] = request.form.get("team_number")
25
- session["team_size"] = request.form.get("team_size")
26
  return redirect(url_for("qr.qr"))
 
21
  case "GET":
22
  return render_template("index.html")
23
  case "POST": # index.html์—์„œ `๋‹ค์Œ` ๋ฒ„ํŠผ ๋ˆŒ๋ €์„ ๋•Œ
24
+ session["team_number"] = int(request.form.get("team_number"))
25
+ session["team_size"] = int(request.form.get("team_size"))
26
  return redirect(url_for("qr.qr"))
ice_breaking_challenge/qr.py CHANGED
@@ -33,9 +33,13 @@ def qr() -> None:
33
  if session.get("team_number") not in counter:
34
  flash("ํ•ด๋‹น ํŒ€ ๋ฒˆํ˜ธ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.")
35
  return render_template("index.html")
36
- if counter[session.get("team_number")] == int(session["team_size"]):
37
  qna = get_questions_and_answers()
38
- Thread(target=background_task.background_task, args=(session.sid, qna)).start()
 
 
 
 
39
  # import time
40
  # time.sleep(100)
41
  return redirect(url_for("introduction.introduction"))
 
33
  if session.get("team_number") not in counter:
34
  flash("ํ•ด๋‹น ํŒ€ ๋ฒˆํ˜ธ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.")
35
  return render_template("index.html")
36
+ if counter[session.get("team_number")] >= int(session["team_size"]):
37
  qna = get_questions_and_answers()
38
+ team_size = session.get("team_size")
39
+ import time
40
+ Thread(target=background_task.background_task, args=(session.sid, qna, team_size)).start()
41
+
42
+
43
  # import time
44
  # time.sleep(100)
45
  return redirect(url_for("introduction.introduction"))
ice_breaking_challenge/quiz.py CHANGED
@@ -1,14 +1,27 @@
1
  from flask import Blueprint, render_template, session
 
2
 
3
  from ice_breaking_challenge import quiz_results
4
 
5
  bp = Blueprint("quiz", __name__, url_prefix="/quiz")
6
 
7
- @bp.route("/", methods=["GET"])
8
  def quiz():
9
  """quiz"""
10
  quizzes = quiz_results[session.sid]
11
- return render_template("quiz.html", quizzes=quizzes, length=len(quizzes))
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
  @bp.route("/finish", methods=["GET"])
14
  def finish():
 
1
  from flask import Blueprint, render_template, session
2
+ from flask import request, redirect, url_for
3
 
4
  from ice_breaking_challenge import quiz_results
5
 
6
  bp = Blueprint("quiz", __name__, url_prefix="/quiz")
7
 
8
+ @bp.route("/", methods=["GET", "POST"])
9
  def quiz():
10
  """quiz"""
11
  quizzes = quiz_results[session.sid]
12
+ if 'question_number' not in session:
13
+ session['question_number'] = 0
14
+ else:
15
+ session['question_number'] += 1
16
+
17
+ if session['question_number'] == 2: # TODO:
18
+ return redirect(url_for("quiz.finish"))
19
+
20
+ print(session['question_number'])
21
+ quiz = quizzes[session['question_number']]
22
+
23
+ return render_template("quiz.html", quiz=quiz)
24
+
25
 
26
  @bp.route("/finish", methods=["GET"])
27
  def finish():
ice_breaking_challenge/templates/quiz.html CHANGED
@@ -3,42 +3,31 @@
3
  {% block content %}
4
  <h1>ํ€ด์ฆˆ ์ƒ์„ฑ ๊ฒฐ๊ณผ</h1>
5
 
6
- {% if quizzes %}
7
  <ul>
8
- {% for quiz in quizzes %}
9
- <li>
10
  <h2>๋ฌธ์ œ ์ฃผ์ธ: {{ quiz[0] }}</h2> <!-- ์ด๋ฆ„ ๋ถ€๋ถ„ -->
11
  <h3>์งˆ๋ฌธ: {{ quiz[1] }}</h3> <!-- ์งˆ๋ฌธ ๋ถ€๋ถ„ -->
12
  <ul>
13
- { quiz[2] } <!-- 4์ง€์„ ๋‹ค ๋ณด๊ธฐ -->
14
  </ul>
15
- <button onclick="showAnswer('{{ loop.index0 }}')">์ •๋‹ต ๋ณด๊ธฐ</button>
16
- <p id="answer-{{ loop.index0 }}" style="display: none;">์ •๋‹ต: {{ quiz[3] }}</p> <!-- ์ •๋‹ต -->
17
- </li>
18
- {% endfor %}
19
  </ul>
20
  {% else %}
21
  <p>ํ€ด์ฆˆ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์‹œ๋„ํ•ด ์ฃผ์„ธ์š”.</p>
22
  {% endif %}
 
 
 
23
 
24
  <script>
25
- let currentQuizIndex = 0;
26
-
27
- function showAnswer(index) {
28
- const answer = document.getElementById(`answer-${index}`);
29
  if (answer.style.display === "none") {
30
  answer.style.display = "block";
31
  } else {
32
  answer.style.display = "none";
33
  }
34
- currentQuizIndex++;
35
-
36
- // ํ€ด์ฆˆ๋ฅผ ๋ชจ๋‘ ํ’€๊ณ  ๋‚˜๋ฉด ๋งˆ๋ฌด๋ฆฌ ํŽ˜์ด์ง€๋กœ ์ด๋™
37
- if (currentQuizIndex === { length }) {
38
- setTimeout(() => {
39
- window.location.href = "{{ url_for('quiz.finish') }}";
40
- }, 3000); // 3์ดˆ ํ›„ ์ž๋™ ์ด๋™
41
- }
42
  }
43
  </script>
44
  {% endblock %}
 
3
  {% block content %}
4
  <h1>ํ€ด์ฆˆ ์ƒ์„ฑ ๊ฒฐ๊ณผ</h1>
5
 
6
+ {% if quiz %}
7
  <ul>
 
 
8
  <h2>๋ฌธ์ œ ์ฃผ์ธ: {{ quiz[0] }}</h2> <!-- ์ด๋ฆ„ ๋ถ€๋ถ„ -->
9
  <h3>์งˆ๋ฌธ: {{ quiz[1] }}</h3> <!-- ์งˆ๋ฌธ ๋ถ€๋ถ„ -->
10
  <ul>
11
+ {{ quiz[2] }} <!-- 4์ง€์„ ๋‹ค ๋ณด๊ธฐ -->
12
  </ul>
13
+ <button onclick="showAnswer()">์ •๋‹ต ๋ณด๊ธฐ</button>
14
+ <p id="answer" style="display: none;">์ •๋‹ต: {{ quiz[3] }}</p> <!-- ์ •๋‹ต -->
 
 
15
  </ul>
16
  {% else %}
17
  <p>ํ€ด์ฆˆ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์‹œ๋„ํ•ด ์ฃผ์„ธ์š”.</p>
18
  {% endif %}
19
+ <form method="post" style="margin-top: 20px;">
20
+ <input type="submit" value="๋‹ค์Œ" style="padding: 10px 20px; font-size: 16px; cursor: pointer; border-radius: 5px; border: none; background-color: #2980b9; color: white;">
21
+ </form>
22
 
23
  <script>
24
+ function showAnswer() {
25
+ const answer = document.getElementById(`answer`);
 
 
26
  if (answer.style.display === "none") {
27
  answer.style.display = "block";
28
  } else {
29
  answer.style.display = "none";
30
  }
 
 
 
 
 
 
 
 
31
  }
32
  </script>
33
  {% endblock %}
ice_breaking_challenge/templates/topic.html CHANGED
@@ -1,18 +1,13 @@
1
  {% extends 'base.html' %}
2
 
3
  {% block content %}
4
- <h1 id="topic">{{ topic }}</h1>
5
 
6
  <script>
7
- // 20์ดˆ๋งˆ๋‹ค ํŽ˜์ด์ง€๋ฅผ ์ƒˆ๋กœ๊ณ ์นจํ•˜๋Š” ํ•จ์ˆ˜
8
- function updateTopic() {
9
- setTimeout(() => {
10
- window.location.reload(); // ํŽ˜์ด์ง€๋ฅผ ์ƒˆ๋กœ๊ณ ์นจํ•˜์—ฌ ์ƒˆ๋กœ์šด ์ฃผ์ œ๋ฅผ ๋ถˆ๋Ÿฌ์˜ด
11
- }, 20000); // 20์ดˆ
12
- }
13
-
14
- // ํŽ˜์ด์ง€๊ฐ€ ๋กœ๋“œ๋  ๋•Œ ์ฃผ์ œ ์—…๋ฐ์ดํŠธ ํ•จ์ˆ˜ ํ˜ธ์ถœ
15
- updateTopic();
16
  </script>
17
  {% endblock %}
18
 
 
1
  {% extends 'base.html' %}
2
 
3
  {% block content %}
4
+ <h1 id="topic">{{ topic }}</h1> <!-- ์„œ๋ฒ„์—์„œ ์ „๋‹ฌํ•œ ์ฃผ์ œ๋ฅผ ๋ฐ”๋กœ ํ‘œ์‹œ -->
5
 
6
  <script>
7
+ // 20์ดˆ๋งˆ๋‹ค ํŽ˜์ด์ง€ ์ƒˆ๋กœ๊ณ ์นจ
8
+ setInterval(function() {
9
+ window.location.reload(); // ํŽ˜์ด์ง€๋ฅผ ์ƒˆ๋กœ๊ณ ์นจํ•˜์—ฌ ์ƒˆ๋กœ์šด ์ฃผ์ œ๋ฅผ ์„œ๋ฒ„์—์„œ ๊ฐ€์ ธ์˜ด
10
+ }, 20000);
 
 
 
 
 
11
  </script>
12
  {% endblock %}
13
 
ice_breaking_challenge/topic.py CHANGED
@@ -1,4 +1,4 @@
1
- from flask import Blueprint, render_template, redirect, url_for
2
  from flask import session
3
  import random
4
  from ice_breaking_challenge import quiz_generated
@@ -17,4 +17,3 @@ def topic() -> None:
17
  else:
18
  selected_topic = random.choice(topics)
19
  return render_template("topic.html", topic=selected_topic)
20
-
 
1
+ from flask import Blueprint, render_template, redirect, url_for, jsonify
2
  from flask import session
3
  import random
4
  from ice_breaking_challenge import quiz_generated
 
17
  else:
18
  selected_topic = random.choice(topics)
19
  return render_template("topic.html", topic=selected_topic)
 
requirements.txt CHANGED
@@ -11,3 +11,4 @@ google-auth
11
  google-auth-httplib2
12
  google-auth-oauthlib
13
  google-common-protos
 
 
11
  google-auth-httplib2
12
  google-auth-oauthlib
13
  google-common-protos
14
+ pandas