-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenPDF.py
More file actions
99 lines (92 loc) · 7.32 KB
/
genPDF.py
File metadata and controls
99 lines (92 loc) · 7.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from fpdf import FPDF
import json
from openai import OpenAI
client = OpenAI()
def title(pdf, text):
pdf.set_font("Tinos", size=32)
pdf.cell(0, 10, txt=text, ln=1, align="C")
def studentinfo(pdf):
pdf.set_font("Tinos", size=12)
pdf.cell(0, 20, txt="Name: ____________________ Date: _______________ Period: __________", ln=2, align="C")
def choice(pdf, num, question, answers):
pdf.set_font("Tinos", size=12)
pdf.multi_cell(0, 5, txt=f"{num}. {question}", align="L")
pdf.cell(0, 7, txt=f" A: {answers[0]}", ln=2, align="L")
pdf.cell(0, 7, txt=f" B: {answers[1]}", ln=2, align="L")
pdf.cell(0, 7, txt=f" C: {answers[2]}", ln=2, align="L")
pdf.cell(0, 7, txt=f" D: {answers[3]}", ln=2, align="L")
def shortresponse(pdf, num, question):
pdf.set_font("Tinos", size=12)
pdf.multi_cell(0, 5, txt=f"{num}. {question}", align="L")
pdf.cell(0, 10, txt=" _____________________________________________________________________", ln=2, align="L")
pdf.cell(0, 10, txt=" _____________________________________________________________________", ln=2, align="L")
pdf.cell(0, 10, txt=" _____________________________________________________________________", ln=2, align="L")
def truefalse(pdf, num, question):
pdf.set_font("Tinos", size=12)
pdf.multi_cell(0, 5, txt=f"{num}. {question}", align="L")
pdf.cell(0, 7, txt=" True", ln=2, align="L")
pdf.cell(0, 7, txt=" False", ln=2, align="L")
def matching(pdf, num, question, pair1, pair2, pair3, pair4, pair5):
pdf.set_font("Tinos", size=12)
pdf.multi_cell(0, 5, txt=f"{num}. {question}", align="L")
pdf.cell(0, 7, txt=f" {pair1[0]} {pair1[1]}", ln=2, align="L")
pdf.cell(0, 7, txt=f" {pair2[0]} {pair2[1]}", ln=2, align="L")
pdf.cell(0, 7, txt=f" {pair3[0]} {pair3[1]}", ln=2, align="L")
pdf.cell(0, 7, txt=f" {pair4[0]} {pair4[1]}", ln=2, align="L")
pdf.cell(0, 7, txt=f" {pair5[0]} {pair5[1]}", ln=2, align="L")
def longresponse(pdf, num, question):
pdf.add_page()
pdf.set_font("Tinos", size=12)
pdf.multi_cell(0, 5, txt=f"{num}. {question}", align="L")
pdf.cell(0, 10, txt=" _____________________________________________________________________", ln=2, align="L")
pdf.cell(0, 10, txt=" _____________________________________________________________________", ln=2, align="L")
pdf.cell(0, 10, txt=" _____________________________________________________________________", ln=2, align="L")
pdf.cell(0, 10, txt=" _____________________________________________________________________", ln=2, align="L")
pdf.cell(0, 10, txt=" _____________________________________________________________________", ln=2, align="L")
pdf.cell(0, 10, txt=" _____________________________________________________________________", ln=2, align="L")
pdf.cell(0, 10, txt=" _____________________________________________________________________", ln=2, align="L")
pdf.cell(0, 10, txt=" _____________________________________________________________________", ln=2, align="L")
pdf.cell(0, 10, txt=" _____________________________________________________________________", ln=2, align="L")
pdf.cell(0, 10, txt=" _____________________________________________________________________", ln=2, align="L")
pdf.cell(0, 10, txt=" _____________________________________________________________________", ln=2, align="L")
pdf.cell(0, 10, txt=" _____________________________________________________________________", ln=2, align="L")
pdf.cell(0, 10, txt=" _____________________________________________________________________", ln=2, align="L")
pdf.cell(0, 10, txt=" _____________________________________________________________________", ln=2, align="L")
pdf.cell(0, 10, txt=" _____________________________________________________________________", ln=2, align="L")
pdf.cell(0, 10, txt=" _____________________________________________________________________", ln=2, align="L")
pdf.cell(0, 10, txt=" _____________________________________________________________________", ln=2, align="L")
pdf.cell(0, 10, txt=" _____________________________________________________________________", ln=2, align="L")
def text(pdf, content):
pdf.multi_cell(0, 5, txt=content, align="L")
def from_json(jsond):
pdf = FPDF()
data = json.loads(jsond)
pdf.add_font('Tinos', '', 'Tinos-Regular.ttf', uni=True)
pdf.add_page()
title(pdf, data["title"])
studentinfo(pdf)
if data["questions"]:
for question in data["questions"]:
if(question["type"] == "choice"):
choice(pdf, question["args"]["num"], question["args"]["question"], question["args"]["answers"])
if(question["type"] == "shortresponse"):
shortresponse(pdf, question["args"]["num"], question["args"]["question"])
if(question["type"] == "longresponse"):
longresponse(pdf, question["args"]["num"], question["args"]["question"])
if(question["type"] == "truefalse"):
truefalse(pdf, question["args"]["num"], question["args"]["question"])
if(question["type"] == "text"):
text(pdf, question["args"]["content"])
pdf.output(data["title"] + ".pdf")
user = input("What should i make?: ")
completion = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": 'You make paper worksheets for students. You must return a JSON response that includes: "questions": an array that includes multiple questions in it and "title": a value for the title of the worksheet that must be no longer than 30 letters (will also be a filename so do not use any illegal characters). You have access to many forms of input (although all do not have to be used) such as: [{"type": "choice", "args": {"num": 1, "question": "Example question", "answers": ["answer A", "answer B", "answer C", "answer D"]}} (do not include answer letters, as they are already added.), {"type: "shortresponse", "args": {"num": 2, "question": "A 3 line short response field."}}, {"type": "truefalse", "args": {"num": 3, "question": "True or false statement."}}, {"type": "longresponse", "args": {"num": 4, "question": "A 18 line response field that if used, should only be placed at the end of the test due to it making a new page."}}, {"type": "text", "args": {"content": "A textbox that is placed inside the worksheet"}}] TIP: do not mention anything placed in parenthesis in the worksheets'},
{"role": "user", "content": "An example test."},
{"role": "assistant", "content": '{"title": "Example Test","questions": [{"type": "choice","args": {"num": 1,"question": "What is the capital of France?","answers": ["Berlin", "Madrid", "Paris", "Rome"]}},{"type": "shortresponse","args": {"num": 2,"question": "What is the primary function of the respiratory system?"}},{"type": "truefalse","args": {"num": 3,"question": "The Earth revolves around the sun in a circular orbit."}},{"type": "longresponse","args": {"num": 4,"question": "Describe the process of photosynthesis in plants."}}]}'},
{"role": "user", "content": user}
]
)
print(completion.choices[0].message.content)
from_json(completion.choices[0].message.content)