Spaces:
Runtime error
Runtime error
dongwook-chan
commited on
Commit
โข
2e9ea78
1
Parent(s):
0c2babc
Finished developing back-end
Browse files- flask_session/2029240f6d1128be89ddc32729463129 +0 -0
- flask_session/4b3a5c21ed5e4f46a96be9fe6a0bfb18 +0 -0
- flask_session/c9f52df6baf57fcc03af8634ade01a4c +0 -0
- ice_breaking_challenge/background_task.py +64 -13
- ice_breaking_challenge/google_sheets.py +26 -31
- ice_breaking_challenge/index.py +2 -2
- ice_breaking_challenge/qr.py +6 -2
- ice_breaking_challenge/quiz.py +15 -2
- ice_breaking_challenge/templates/quiz.html +9 -20
- ice_breaking_challenge/templates/topic.html +5 -10
- ice_breaking_challenge/topic.py +1 -2
- requirements.txt +1 -0
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
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 |
-
|
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 |
-
|
|
|
75 |
|
76 |
-
|
77 |
|
78 |
-
|
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
|
|
|
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")]
|
37 |
qna = get_questions_and_answers()
|
38 |
-
|
|
|
|
|
|
|
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
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(
|
16 |
-
<p id="answer
|
17 |
-
</li>
|
18 |
-
{% endfor %}
|
19 |
</ul>
|
20 |
{% else %}
|
21 |
<p>ํด์ฆ๊ฐ ์์ต๋๋ค. ๋ค์ ์๋ํด ์ฃผ์ธ์.</p>
|
22 |
{% endif %}
|
|
|
|
|
|
|
23 |
|
24 |
<script>
|
25 |
-
|
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
|
9 |
-
|
10 |
-
|
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
|