Justin Grammens commited on
Commit
22f3fba
·
1 Parent(s): ce313d9

Add application file

Browse files
Files changed (1) hide show
  1. app.py +93 -0
app.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ from langchain.prompts import PromptTemplate
4
+ from langchain_openai import ChatOpenAI
5
+ from langchain.schema import HumanMessage
6
+
7
+ # Set your OpenAI API key (replace 'your-api-key' with the actual key)
8
+ #os.environ["OPENAI_API_KEY"] = "your-api-key"
9
+
10
+ # Initialize the OpenAI model
11
+ llm = ChatOpenAI(model="gpt-4", temperature=0.5)
12
+
13
+ # Define a reusable prompt template using LangChain
14
+ template = """
15
+ Turn these questions and answers into a four-paragraph essay about my approach to leadership.
16
+ Please write the essay in the first person, using a conversational and engaging style,
17
+ as if you're chatting with a friend. Use simple, relatable language and avoid formalities.
18
+ Add personal touches and insights to make it feel genuine and approachable.
19
+
20
+ Questions and Answers:
21
+ 1. Three words that describe the culture: {q1}
22
+ 2. How personal values align with work: {q2}
23
+ 3. What drives me to come to work each day: {q3}
24
+ 4. Areas I'm working on to be a better leader: {q4}
25
+ 5. How I stay connected with daily operations: {q5}
26
+ 6. What I want for the people I lead: {q6}
27
+ 7. How I show dignity and respect: {q7}
28
+ 8. How people perceive me: {q8}
29
+ 9. How others describe my management style: {q9}
30
+ 10. Three things you need to excel with me: {q10}
31
+ 11. What people should always feel free to do: {q11}
32
+ """
33
+
34
+ # Create a LangChain prompt template object
35
+ prompt = PromptTemplate(
36
+ input_variables=["q1", "q2", "q3", "q4", "q5", "q6", "q7", "q8", "q9", "q10", "q11"],
37
+ template=template,
38
+ )
39
+
40
+ # Function to generate the essay using the template and answers
41
+ def stream_leadership_essay(*answers):
42
+ # Format the prompt with the answers
43
+ filled_prompt = prompt.format(
44
+ q1=answers[0], q2=answers[1], q3=answers[2], q4=answers[3], q5=answers[4],
45
+ q6=answers[5], q7=answers[6], q8=answers[7], q9=answers[8], q10=answers[9], q11=answers[10]
46
+ )
47
+
48
+ # Send the prompt as a HumanMessage and stream the response
49
+ response_stream = llm.stream([HumanMessage(content=filled_prompt)])
50
+
51
+ # Yield the content chunk-by-chunk
52
+ essay = ""
53
+ for chunk in response_stream:
54
+ essay += chunk.content
55
+ yield essay, filled_prompt # Yield partial essay to the Gradio output in real-time
56
+
57
+ # Define the Gradio interface (form layout)
58
+ with gr.Blocks() as demo:
59
+ gr.Markdown("## Leadership Insights Form")
60
+
61
+ inputs = [
62
+ gr.Textbox(label="Three words that describe the culture we are building include..."),
63
+ gr.Textbox(label="My personal values align with our work in the following ways..."),
64
+ gr.Textbox(label="Here’s what drives me to get up and come to work each day..."),
65
+ gr.Textbox(label="I’m working on the following areas to be a better leader for my team."),
66
+ gr.Textbox(label="I stay connected with the daily realities of our operations by..."),
67
+ gr.Textbox(label="Here’s what I want for the people I lead and work with. I want them to..."),
68
+ gr.Textbox(label="I take these actions to show dignity and respect to those I work with."),
69
+ gr.Textbox(label="At times, people can perceive me as..."),
70
+ gr.Textbox(label="Others have described my management style as..."),
71
+ gr.Textbox(label="To excel with me, you need to do three things..."),
72
+ gr.Textbox(label="When working with me, always feel free to...")
73
+ ]
74
+
75
+ submit_button = gr.Button("Submit")
76
+
77
+ # Output area for the response summary
78
+
79
+ output = gr.Textbox(label="Generated Leadership Essay", lines=10)
80
+
81
+ prompt_template = gr.Textbox(label="This is the prompt that was sent to ChatGPT", lines=10)
82
+
83
+ # Link the submit button with the function
84
+ submit_button.click(
85
+ stream_leadership_essay,
86
+ inputs=inputs,
87
+ outputs=[output, prompt_template]
88
+ )
89
+
90
+ # Launch the Gradio app
91
+ demo.launch()
92
+
93
+