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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import openai
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 6 |
+
|
| 7 |
+
############################
|
| 8 |
+
|
| 9 |
+
def generate_rule(no_login,low_activity,repeated_course,low_marks,second_lang):
|
| 10 |
+
body = " "
|
| 11 |
+
|
| 12 |
+
if no_login == "Yes" or low_activity == "Yes":
|
| 13 |
+
body += "\n\n - Have you found any <a href = ""https://www.student.unsw.edu.au/moodle/support""> technical difficulties using Moodle?</a>"
|
| 14 |
+
body += "\n\n - Did you know there are <a href = ""https://www.student.unsw.edu.au/time-management""> six steps to time management for success</a>?"
|
| 15 |
+
if repeated_course == "Yes":
|
| 16 |
+
body += "\n\n - Did you know that accessing an <a href=""https://www.student.unsw.edu.au/advising"">academic adviser</a> is free?"
|
| 17 |
+
if second_lang == "Yes":
|
| 18 |
+
body += "\n\n - Refining your <a href=""https://www.student.unsw.edu.au/english"">communication skills</a> is critical for success. Did you know there are opportunities available?"
|
| 19 |
+
body += "\n\n - Students often need to deliver oral presentations in tutorials and seminars. Check out the resources below for helpful tips on presentations and public speaking: <a href=""https://www.student.unsw.edu.au/support-oral-presentations"">Support for Oral Presentations</a>."
|
| 20 |
+
if low_marks == "Yes":
|
| 21 |
+
body += "\n\n - Did you know that learning all <a href = ""https://www.student.unsw.edu.au/study-hacks"">the hacks you need for success</a> is available?"
|
| 22 |
+
body += "\n\n - Do you want to <a href = ""https://www.student.unsw.edu.au/skills"">develop and refine your academic skills</a> more?"
|
| 23 |
+
body += "\n\n - Did you know that accessing an <a href=""https://www.student.unsw.edu.au/advising"">academic adviser</a> is free?"
|
| 24 |
+
if no_login == "No" and low_activity == "No" and repeated_course == "No" and repeated_course == "No" and low_marks == "No":
|
| 25 |
+
body += "\n\n - Did you know that accessing an <a href=""https://www.student.unsw.edu.au/advising"">academic adviser</a> is free?"
|
| 26 |
+
body += "\n\n - Have you found any <a href = ""https://www.student.unsw.edu.au/moodle/support""> technical difficulties using Moodle?</a>"
|
| 27 |
+
|
| 28 |
+
return body
|
| 29 |
+
|
| 30 |
+
def generate_link(no_login,low_activity,repeated_course,low_marks,second_lang):
|
| 31 |
+
body = " "
|
| 32 |
+
|
| 33 |
+
if no_login == "Yes" or low_activity == "Yes":
|
| 34 |
+
body += "\n\n""https://www.student.unsw.edu.au/moodle/support"" "
|
| 35 |
+
body += "\n\n""https://www.student.unsw.edu.au/time-management"" "
|
| 36 |
+
if repeated_course == "Yes":
|
| 37 |
+
body += "\n\n""https://www.student.unsw.edu.au/advising"" "
|
| 38 |
+
if second_lang == "Yes":
|
| 39 |
+
body += "\n\n""https://www.student.unsw.edu.au/english"" "
|
| 40 |
+
body += "\n\n""https://www.student.unsw.edu.au/support-oral-presentations"" "
|
| 41 |
+
if low_marks == "Yes":
|
| 42 |
+
body += "\n\n""https://www.student.unsw.edu.au/study-hacks"" "
|
| 43 |
+
body += "\n\n""https://www.student.unsw.edu.au/skills"" "
|
| 44 |
+
body += "\n\n""https://www.student.unsw.edu.au/advising"" "
|
| 45 |
+
if no_login == "No" and low_activity == "No" and repeated_course == "No" and repeated_course == "No" and low_marks == "No":
|
| 46 |
+
body += "\n\n""https://www.student.unsw.edu.au/advising"" "
|
| 47 |
+
body += "\n\n""https://www.student.unsw.edu.au/moodle/support"" "
|
| 48 |
+
|
| 49 |
+
return body
|
| 50 |
+
|
| 51 |
+
def generate_ai_email(query,persona,temperature,no_login,low_activity,repeated_course,low_marks,second_lang):
|
| 52 |
+
|
| 53 |
+
rule_context = generate_link(no_login,low_activity,repeated_course,low_marks,second_lang)
|
| 54 |
+
|
| 55 |
+
prompt = f"\"{persona}\". \n\n In your email, provide suggestions and the following hyper links: \n \"{rule_context}\".\n\n Note that each link should appear only once, remove duplicate links. \n\n Also, here's some additional info from lecturer to the student: \"{query}\". Use Australian spelling."
|
| 56 |
+
|
| 57 |
+
response = openai.ChatCompletion.create(
|
| 58 |
+
model="gpt-4",
|
| 59 |
+
messages=[
|
| 60 |
+
{
|
| 61 |
+
"role": "system",
|
| 62 |
+
"content": prompt
|
| 63 |
+
},
|
| 64 |
+
{
|
| 65 |
+
"role": "user",
|
| 66 |
+
"content": " "
|
| 67 |
+
},
|
| 68 |
+
],
|
| 69 |
+
temperature=temperature,
|
| 70 |
+
max_tokens=3000,
|
| 71 |
+
top_p=1,
|
| 72 |
+
frequency_penalty=0,
|
| 73 |
+
presence_penalty=0
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
text = response["choices"][0]["message"]["content"]
|
| 77 |
+
return text
|
| 78 |
+
|
| 79 |
+
def generate_rule_email(no_login,low_activity,repeated_course,low_marks,second_lang):
|
| 80 |
+
body = "Hello\n\n I hope this email finds you well. I wanted to check in and see how you're doing with your current coursework and if you have any questions or concerns. Please don't hesitate to reach out if you need any assistance or guidance.\n Suggestions:\n"
|
| 81 |
+
|
| 82 |
+
body += generate_rule(no_login,low_activity,repeated_course,low_marks,second_lang)
|
| 83 |
+
|
| 84 |
+
return body
|
| 85 |
+
|
| 86 |
+
############################
|
| 87 |
+
|
| 88 |
+
title = "Email Generator v0.2"
|
| 89 |
+
|
| 90 |
+
with gr.Blocks(title=title) as demo:
|
| 91 |
+
gr.Markdown("""
|
| 92 |
+
# Email Generator v0.2
|
| 93 |
+
Generate message. Select the conditions for a dummy student. Put your additional note in the left input window and select a tone.
|
| 94 |
+
""")
|
| 95 |
+
with gr.Tab("Email Content"):
|
| 96 |
+
with gr.Row():
|
| 97 |
+
with gr.Column():
|
| 98 |
+
no_login = gr.Radio(["Yes", "No"],
|
| 99 |
+
label="No Moodle login in last 7 days",
|
| 100 |
+
value="Yes",
|
| 101 |
+
type="value"
|
| 102 |
+
)
|
| 103 |
+
low_activity = gr.Radio(["Yes", "No"],
|
| 104 |
+
label="Low Moodle activity",
|
| 105 |
+
value="Yes",
|
| 106 |
+
type="value"
|
| 107 |
+
)
|
| 108 |
+
repeated_course = gr.Radio(["Yes", "No"],
|
| 109 |
+
label="Repeated Course",
|
| 110 |
+
value="No",
|
| 111 |
+
type="value"
|
| 112 |
+
)
|
| 113 |
+
low_marks = gr.Radio(["Yes", "No"],
|
| 114 |
+
label="Low marks",
|
| 115 |
+
value="No",
|
| 116 |
+
type="value"
|
| 117 |
+
)
|
| 118 |
+
second_lang = gr.Radio(["Yes", "No"],
|
| 119 |
+
label="English as a second language",
|
| 120 |
+
value="No",
|
| 121 |
+
type="value"
|
| 122 |
+
)
|
| 123 |
+
with gr.Row():
|
| 124 |
+
temperature = gr.Number(label="LLM temperature",
|
| 125 |
+
info="Numerical value only, 0-2",
|
| 126 |
+
value=1,
|
| 127 |
+
step = 0.1
|
| 128 |
+
)
|
| 129 |
+
with gr.Column():
|
| 130 |
+
email_input1 = gr.Textbox(label="Additional info",
|
| 131 |
+
info="Additional info to be included on top of standard rules, message for student, etc. Select from an example below or start typing",
|
| 132 |
+
lines=5,
|
| 133 |
+
value=" ",
|
| 134 |
+
)
|
| 135 |
+
gr.Examples(["I understand you applied for special consideration","I've never seen you in class","I heard you lost your pet"], inputs=[email_input1])
|
| 136 |
+
email_input2 = gr.Textbox(label="Persona",
|
| 137 |
+
info="Temporary input window for persona",
|
| 138 |
+
lines=5,
|
| 139 |
+
value=" ",
|
| 140 |
+
)
|
| 141 |
+
gr.Examples(["You are a friendly professor who is trying to give the student a reminder.","You are an angry professor who is trying to give the student a warning."], inputs=[email_input2])
|
| 142 |
+
with gr.Column():
|
| 143 |
+
email_rule = gr.Textbox(
|
| 144 |
+
label="Rule-based Email",
|
| 145 |
+
info="Email generated based on rules without knowledge of additional info",
|
| 146 |
+
lines=15,
|
| 147 |
+
value="Please scroll if message is long")
|
| 148 |
+
email_rule_button = gr.Button("Generate Rule-based Email")
|
| 149 |
+
with gr.Column():
|
| 150 |
+
email_ai = gr.Textbox(
|
| 151 |
+
label="AI-generated Email",
|
| 152 |
+
info="Combination of rules and additional info you typed",
|
| 153 |
+
lines=15,
|
| 154 |
+
value="Depending on message content, please wait up to 30s, please scroll if message is long",
|
| 155 |
+
)
|
| 156 |
+
email_ai_button = gr.Button("Generate AI Email")
|
| 157 |
+
|
| 158 |
+
email_ai_button.click(generate_ai_email, inputs=[email_input1,email_input2,temperature,no_login,low_activity,repeated_course,low_marks,second_lang], outputs=[email_ai], api_name="gen")
|
| 159 |
+
email_rule_button.click(generate_rule_email, inputs=[no_login,low_activity,repeated_course,low_marks,second_lang], outputs=[email_rule])
|
| 160 |
+
|
| 161 |
+
demo.launch()
|
requirements (3).txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
openai
|
| 2 |
+
gradio
|