File size: 11,696 Bytes
22f3fba 59f148c ff2758d 22f3fba 8a56a77 59f148c 22f3fba 8a56a77 5cdff8a 8a56a77 22f3fba 9d76faa 22f3fba 019be42 22f3fba 7a39102 22f3fba 7a39102 22f3fba 8a56a77 9d76faa 8a56a77 9d76faa 8a56a77 5827eb5 ff2758d 8a56a77 ff2758d 22f3fba 59f148c 7a39102 1ec9871 7a39102 1ec9871 59f148c 7a39102 22f3fba 1ec9871 22f3fba 7a39102 1ec9871 22f3fba 9d76faa 22f3fba 8a56a77 ff2758d 22f3fba fe62f5d 1ec9871 9d76faa fe62f5d 22f3fba 7a39102 9d76faa 22f3fba fe62f5d 22f3fba 9d76faa 22f3fba 9d76faa 22f3fba 9d76faa 22f3fba 7a39102 22f3fba 7a39102 |
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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
import gradio as gr
import os
import requests
from langchain.prompts import PromptTemplate
from langchain_openai import ChatOpenAI
from langchain.schema import HumanMessage
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
secretcode = os.environ["SECRETCODE"]
# Replace with your verified sender email
from_email = os.environ["PLP_RETURN_EMAIL_ADDRESS"]
# Initialize the OpenAI model
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=.4)
# Define a reusable prompt template using LangChain
template = """
Turn these questions and answers into a five-paragraph (six-sentences equals one paragraph) essay about my approach to leadership. Please be sure to include summaries of all questions that have been answered. Please write the essay in the first person, using a conversational and engaging style, as if talking with a friend. Use simple, relatable language and avoid formalities. If needed, add a little content to add texture and depth to answers as long as it aligns with the research on effective leadership.
Questions and Answers:
1. Treating others with dignity and respect is critical. I strive to accomplish this by: {q1}
2. A few of my core values are: {q2}
3. I believe in continuous learning. Here’s how I stay committed to my development: {q3}
4. When I’m at my best, I: {q4}
5. Under stress, you may notice that I: {q5}
6. I gain energy and inspiration from: {q6}
7. A personal motto or philosophy I live by is: {q7}
8. My favorite quote about leadership is ___ because of ____ : {q8}
9. Others have described my leadership or management style as: {q9}
10. People who work for me are most successful when they: {q10}
11. People sometimes perceive me as: {q11}
12. To gain my trust and respect, you should: {q12}
13. You have my full permission to: {q13}
14. “Hot-button” behaviors that frustrate me include: {q14}
15. Here’s how I like to give and receive feedback: {q15}
16. If we encounter a disagreement, I prefer to resolve it by: {q16}
17. Three things you should know about our department’s culture are: {q17}
18. Three things that define the culture of our organization include: {q18}
19. I love our work because we: {q19}
20. The type of environment we strive to create for our team is: {q20}
21. Key principles or practices that make our department unique are: {q21}
22. Our team’s mission and what it means to us day-to-day is: {q22}
23. The kind of reputation we aim to build as a department/organization is: {q23}
24. We define success in our department by: {q24}
"""
# Create a LangChain prompt template object
prompt = PromptTemplate(
input_variables=["q1", "q2", "q3", "q4", "q5", "q6", "q7", "q8", "q9", "q10",
"q11", "q12", "q13", "q14", "q15", "q16", "q17", "q18", "q19", "q20", "q21", "q22", "q23", "q24"],
template=template,
)
def send_sendgrid_email(essay, email):
print(from_email)
message = Mail(
from_email=from_email,
to_emails=email,
subject='Personal Leadership Profile Results',
html_content=essay)
try:
sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
response = sg.send(message)
print(response.status_code)
print(response.body)
print(response.headers)
return "Email Sent"
except Exception as e:
print(e)
return "Email Failed"
# Add an alert box to notify the user if the email was sent correctly
def show_alert(result):
gr.Info(result)
# Function to generate the essay using the template and answers
def stream_leadership_essay(*answers):
# Check if the email is valid
if "@" not in answers[28]:
yield "Invalid email address. Please enter a valid email address."
return
if (answers[29] != secretcode):
yield "Invalid secret code. Please try again."
return
# Format the prompt with the answers
# Ensure at least 10 properties are filled, but only the ones that are textboxes
filled_answers = [answer for i, answer in enumerate(answers[:27]) if isinstance(inputs[i], gr.Textbox) and answer.strip() != ""]
if len(filled_answers) < 10:
yield "Please complete at least 10 questions with a minimum of two sentences for each answer."
return
filled_prompt = prompt.format(
q1=answers[1], q2=answers[2], q3=answers[3], q4=answers[4],
q5=answers[5], q6=answers[6], q7=answers[7], q8=answers[8],
q9=answers[10], q10=answers[11],q11=answers[12], q12=answers[13],
q13=answers[14], q14=answers[15], q15=answers[16], q16=answers[17],
q17=answers[19], q18=answers[20], q19=answers[21], q20=answers[22],
q21=answers[23], q22=answers[24], q23=answers[25], q24=answers[26]
)
# Send the prompt as a HumanMessage and stream the response
response_stream = llm.stream([HumanMessage(content=filled_prompt)])
# Yield the content chunk-by-chunk
essay = ""
for chunk in response_stream:
essay += chunk.content
#yield essay, filled_prompt # Yield partial essay to the Gradio output in real-time
yield essay
result = send_sendgrid_email(essay, answers[28])
print(result)
show_alert(result)
# Define the Gradio interface (form layout)
with gr.Blocks(
theme=gr.themes.Soft()
) as demo:
gr.Markdown("# Personal Leadership Profile")
gr.Markdown("""
According to 40 years of research by Kouzes and Posner, the most influential leaders have a clear Personal Leadership Philosophy (PLP) and communicate it frequently. Like a great coach, you have an explicit philosophy or approach to work that is clearly communicated to others.
Whether you’re a seasoned leader or just getting started in a leadership role, a PLP will allow you to articulate your values, goals, and the type of leader you aspire to be.
The result is a strategic tool to help you improve your culture.
**Please choose TEN or more of the following questions to answer. Please provide a minimum of two sentences for each answer.**
Based on your answers, we will build a “draft” for you to revise, build upon, and use as a roadmap.
Thank you for completing the PLP questions - It will be a cornerstone of your continued growth and development.
""")
inputs = [
gr.Markdown("### About Me"),
gr.Textbox(label="Treating others with dignity and respect is critical. I strive to accomplish this by..."),
gr.Textbox(label="A few of my core values are..."),
gr.Textbox(label="I believe in continuous learning. Here’s how I stay committed to my development..."),
gr.Textbox(label="When I’m at my best, I..."),
gr.Textbox(label="Under stress, you may notice that I..."),
gr.Textbox(label="I gain energy and inspiration from..."),
gr.Textbox(label="A personal motto or philosophy I live by is..."),
gr.Textbox(label="My favorite quote about leadership is ______ because _______"),
gr.Markdown("### About Working With Me:"),
gr.Textbox(label="Others have described my leadership or management style as..."),
gr.Textbox(label="People who work for me are most successful when they..."),
gr.Textbox(label="People sometimes perceive me as..."),
gr.Textbox(label="To gain my trust and respect, you should..."),
gr.Textbox(label="You have my full permission to..."),
gr.Textbox(label="\"Hot-button\" behaviors that frustrate me include..."),
gr.Textbox(label="Here’s how I like to give and receive feedback..."),
gr.Textbox(label="If we encounter a disagreement, I prefer to resolve it by..."),
gr.Markdown("### About Our Department/Organization:"),
gr.Textbox(label="Three things you should know about our department’s culture are..."),
gr.Textbox(label="Three things that define the culture of our organization include..."),
gr.Textbox(label="I love our work because we..."),
gr.Textbox(label="The type of environment we strive to create for our team is..."),
gr.Textbox(label="Key principles or practices that make our department unique are..."),
gr.Textbox(label="Our team’s mission and what it means to us day-to-day is..."),
gr.Textbox(label="The kind of reputation we aim to build as a department/organization is..."),
gr.Textbox(label="We define success in our department by..."),
gr.Markdown("### Validation Input:"),
gr.Textbox(label="We will email you a copy of your PLP. Please enter your address below."),
gr.Textbox(label="Enter the password to generate your PLP.")
]
gr.Markdown("### I acknowledge that this is for learning purposes only.")
submit_button = gr.Button("Submit")
# Output area for the response summary
output = gr.Textbox(label="Your generated Personal Leadership Profile", lines=10)
#prompt_template = gr.Textbox(label="This is the prompt that was sent to ChatGPT", lines=10)
# Link the submit button with the function
submit_button.click(
stream_leadership_essay,
inputs=inputs,
#outputs=[output, prompt_template]
outputs=output
)
# Launch the Gradio app
demo.launch()
# Automated testing function
"""
def test_form_submission():
mock_data = [
"I treat others with dignity and respect by listening to their concerns.",
"My core values are integrity, honesty, and empathy.",
"I stay committed to my development by reading books and attending workshops.",
"When I’m at my best, I am focused and productive.",
"Under stress, you may notice that I become quiet and introspective.",
"I gain energy and inspiration from nature and music.",
"A personal motto I live by is 'Never give up.'",
"My favorite quote about leadership is 'Leadership is not about being in charge. It is about taking care of those in your charge.' because it emphasizes the importance of caring for others.",
"Others have described my leadership style as supportive and empowering.",
"People who work for me are most successful when they are proactive and communicative.",
"People sometimes perceive me as reserved and thoughtful.",
"To gain my trust and respect, you should be honest and reliable.",
"You have my full permission to provide constructive feedback.",
"Hot-button behaviors that frustrate me include dishonesty and lack of accountability.",
"Here’s how I like to give and receive feedback: I prefer direct and constructive feedback.",
"If we encounter a disagreement, I prefer to resolve it by having an open and honest conversation.",
"Three things you should know about our department’s culture are...",
"Three things that define the culture of our organization include...",
"I love our work because we...",
"The type of environment we strive to create for our team is...",
"Key principles or practices that make our department unique are...",
"Our team’s mission and what it means to us day-to-day is...",
"The kind of reputation we aim to build as a department/organization is...",
"We define success in our department by...",
"PLP"
]
# Simulate form submission
result_generator = stream_leadership_essay(*mock_data)
for result in result_generator:
print(result)
# Run the test
test_form_submission()
""" |