Spaces:
Build error
Build error
Implement emotions
Browse files
app.py
CHANGED
@@ -7,17 +7,22 @@ from langchain.chains import LLMChain
|
|
7 |
import datetime
|
8 |
|
9 |
NUM_WORDS_DEFAULT = 20
|
|
|
|
|
|
|
10 |
PROMPT_TEMPLATE = PromptTemplate(
|
11 |
-
input_variables=["original_words", "num_words"],
|
12 |
-
template="Restate, into an HTML div,
|
13 |
-
"
|
14 |
)
|
15 |
|
|
|
16 |
# initial_prompt = "Restate, into an HTML div, in about 60 words, the following, with emotions of admiration, in a formal manner, in a passive voice: "
|
17 |
|
18 |
|
19 |
def set_openai_api_key(api_key, openai_api_key, temperature, llm_chain):
|
20 |
if api_key:
|
|
|
21 |
openai_api_key = api_key
|
22 |
|
23 |
os.environ["OPENAI_API_KEY"] = api_key
|
@@ -28,29 +33,38 @@ def set_openai_api_key(api_key, openai_api_key, temperature, llm_chain):
|
|
28 |
return openai_api_key, llm_chain
|
29 |
|
30 |
|
31 |
-
def
|
32 |
-
|
33 |
-
|
34 |
-
print("temperature: ", temperature)
|
35 |
-
|
36 |
-
openai.api_key = openai_api_key
|
37 |
-
response = openai.Completion.create(
|
38 |
-
model="text-davinci-003",
|
39 |
-
prompt=prompt,
|
40 |
-
temperature=temperature,
|
41 |
-
max_tokens=2000,
|
42 |
-
top_p=1,
|
43 |
-
frequency_penalty=0,
|
44 |
-
presence_penalty=0
|
45 |
-
)
|
46 |
-
return response.choices[0].text
|
47 |
-
|
48 |
-
|
49 |
-
def desc2sheet(desc, openai_api_key, temperature, llm_chain, num_words):
|
50 |
if not openai_api_key or openai_api_key == "":
|
51 |
return "<pre>Please paste your OpenAI API key (see https://beta.openai.com)</pre>"
|
52 |
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
return html
|
55 |
|
56 |
|
@@ -66,38 +80,132 @@ def update_num_words(slider, state):
|
|
66 |
return state
|
67 |
|
68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
block = gr.Blocks(css=".gradio-container {background-color: lightgray}")
|
70 |
|
71 |
with block:
|
72 |
openai_api_key_state = gr.State()
|
73 |
-
temperature_state = gr.State(
|
74 |
llm_chain_state = gr.State()
|
75 |
num_words_state = gr.State(NUM_WORDS_DEFAULT)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
with gr.Row():
|
78 |
-
temperature_slider = gr.Slider(label="GPT Temperature", value=
|
79 |
-
|
|
|
80 |
openai_api_key_textbox = gr.Textbox(placeholder="Paste your OpenAI API key",
|
81 |
show_label=False, lines=1, type='password')
|
82 |
|
83 |
table = gr.Markdown("")
|
84 |
with gr.Row():
|
85 |
request = gr.Textbox(label="GPT prompt: " + PROMPT_TEMPLATE.template_format,
|
86 |
-
value="
|
87 |
-
placeholder="Ex:
|
88 |
|
89 |
submit = gr.Button(value="Create", variant="secondary").style(full_width=False)
|
90 |
|
91 |
num_words_slider = gr.Slider(label="Number of words", value=NUM_WORDS_DEFAULT, minimum=10, maximum=100, step=10)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
|
93 |
submit.click(desc2sheet,
|
94 |
inputs=[
|
95 |
-
request, openai_api_key_state, temperature_state, llm_chain_state, num_words_state
|
|
|
|
|
|
|
96 |
outputs=[table])
|
97 |
|
98 |
request.submit(desc2sheet,
|
99 |
inputs=[
|
100 |
-
|
|
|
|
|
|
|
101 |
outputs=[table])
|
102 |
|
103 |
openai_api_key_textbox.change(set_openai_api_key,
|
@@ -109,9 +217,4 @@ with block:
|
|
109 |
inputs=[temperature_slider, temperature_state],
|
110 |
outputs=[temperature_state])
|
111 |
|
112 |
-
num_words_slider.change(update_num_words,
|
113 |
-
inputs=[num_words_slider, num_words_state],
|
114 |
-
outputs=[num_words_state])
|
115 |
-
|
116 |
-
|
117 |
block.launch()
|
|
|
7 |
import datetime
|
8 |
|
9 |
NUM_WORDS_DEFAULT = 20
|
10 |
+
FORMALITY_DEFAULT = "Casual"
|
11 |
+
TEMPERATURE_DEFAULT = 0.5
|
12 |
+
EMOTION_DEFAULT = "N/A"
|
13 |
PROMPT_TEMPLATE = PromptTemplate(
|
14 |
+
input_variables=["original_words", "num_words", "formality", "emotions"],
|
15 |
+
template="Restate, into an HTML div, using approximately {num_words} words, in a {formality} manner, "
|
16 |
+
"expressing {emotions} the following: \n{original_words}\n",
|
17 |
)
|
18 |
|
19 |
+
|
20 |
# initial_prompt = "Restate, into an HTML div, in about 60 words, the following, with emotions of admiration, in a formal manner, in a passive voice: "
|
21 |
|
22 |
|
23 |
def set_openai_api_key(api_key, openai_api_key, temperature, llm_chain):
|
24 |
if api_key:
|
25 |
+
print("temperature: ", temperature)
|
26 |
openai_api_key = api_key
|
27 |
|
28 |
os.environ["OPENAI_API_KEY"] = api_key
|
|
|
33 |
return openai_api_key, llm_chain
|
34 |
|
35 |
|
36 |
+
def desc2sheet(desc, openai_api_key, temperature, llm_chain, num_words, formality,
|
37 |
+
anticipation_level, joy_level, trust_level,
|
38 |
+
fear_level, surprise_level, sadness_level, disgust_level, anger_level):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
if not openai_api_key or openai_api_key == "":
|
40 |
return "<pre>Please paste your OpenAI API key (see https://beta.openai.com)</pre>"
|
41 |
|
42 |
+
llm_chain.llm.temperature = temperature
|
43 |
+
print("llm_chain.llm.temperature: ", llm_chain.llm.temperature)
|
44 |
+
|
45 |
+
emotions = ""
|
46 |
+
if anticipation_level != "N/A":
|
47 |
+
emotions = emotions + anticipation_level + ", "
|
48 |
+
if joy_level != "N/A":
|
49 |
+
emotions = emotions + joy_level + ", "
|
50 |
+
if trust_level != "N/A":
|
51 |
+
emotions = emotions + trust_level + ", "
|
52 |
+
if fear_level != "N/A":
|
53 |
+
emotions = emotions + fear_level + ", "
|
54 |
+
if surprise_level != "N/A":
|
55 |
+
emotions = emotions + surprise_level + ", "
|
56 |
+
if sadness_level != "N/A":
|
57 |
+
emotions = emotions + sadness_level + ", "
|
58 |
+
if disgust_level != "N/A":
|
59 |
+
emotions = emotions + disgust_level + ", "
|
60 |
+
if anger_level != "N/A":
|
61 |
+
emotions = emotions + anger_level + ", "
|
62 |
+
|
63 |
+
if emotions == "":
|
64 |
+
emotions = "no emotion, "
|
65 |
+
|
66 |
+
html = llm_chain.run({'original_words': desc, 'num_words': num_words, 'formality': formality.lower(),
|
67 |
+
'emotions': emotions.lower()})
|
68 |
return html
|
69 |
|
70 |
|
|
|
80 |
return state
|
81 |
|
82 |
|
83 |
+
def update_formality(radio, state):
|
84 |
+
if radio:
|
85 |
+
state = radio
|
86 |
+
return state
|
87 |
+
|
88 |
+
|
89 |
+
def update_foo(widget, state):
|
90 |
+
if widget:
|
91 |
+
state = widget
|
92 |
+
return state
|
93 |
+
|
94 |
+
|
95 |
block = gr.Blocks(css=".gradio-container {background-color: lightgray}")
|
96 |
|
97 |
with block:
|
98 |
openai_api_key_state = gr.State()
|
99 |
+
temperature_state = gr.State(TEMPERATURE_DEFAULT)
|
100 |
llm_chain_state = gr.State()
|
101 |
num_words_state = gr.State(NUM_WORDS_DEFAULT)
|
102 |
+
formality_state = gr.State(FORMALITY_DEFAULT)
|
103 |
+
anticipation_level_state = gr.State(EMOTION_DEFAULT)
|
104 |
+
joy_level_state = gr.State(EMOTION_DEFAULT)
|
105 |
+
trust_level_state = gr.State(EMOTION_DEFAULT)
|
106 |
+
fear_level_state = gr.State(EMOTION_DEFAULT)
|
107 |
+
surprise_level_state = gr.State(EMOTION_DEFAULT)
|
108 |
+
sadness_level_state = gr.State(EMOTION_DEFAULT)
|
109 |
+
disgust_level_state = gr.State(EMOTION_DEFAULT)
|
110 |
+
anger_level_state = gr.State(EMOTION_DEFAULT)
|
111 |
|
112 |
with gr.Row():
|
113 |
+
temperature_slider = gr.Slider(label="GPT Temperature", value=TEMPERATURE_DEFAULT, minimum=0.0, maximum=1.0,
|
114 |
+
step=0.1)
|
115 |
+
title = gr.Markdown("""<h3><center>GPT-3.5 Express-inator</center></h3>""")
|
116 |
openai_api_key_textbox = gr.Textbox(placeholder="Paste your OpenAI API key",
|
117 |
show_label=False, lines=1, type='password')
|
118 |
|
119 |
table = gr.Markdown("")
|
120 |
with gr.Row():
|
121 |
request = gr.Textbox(label="GPT prompt: " + PROMPT_TEMPLATE.template_format,
|
122 |
+
value="The quick brown fox jumped over the lazy dog.",
|
123 |
+
placeholder="Ex: The quick brown fox jumped over the lazy dog.")
|
124 |
|
125 |
submit = gr.Button(value="Create", variant="secondary").style(full_width=False)
|
126 |
|
127 |
num_words_slider = gr.Slider(label="Number of words", value=NUM_WORDS_DEFAULT, minimum=10, maximum=100, step=10)
|
128 |
+
num_words_slider.change(update_num_words,
|
129 |
+
inputs=[num_words_slider, num_words_state],
|
130 |
+
outputs=[num_words_state])
|
131 |
+
|
132 |
+
formality_radio = gr.Radio(label="Formality", choices=["Casual", "Polite", "Honorific"], value=FORMALITY_DEFAULT)
|
133 |
+
formality_radio.change(update_formality,
|
134 |
+
inputs=[formality_radio, formality_state],
|
135 |
+
outputs=[formality_state])
|
136 |
+
|
137 |
+
with gr.Accordion("Emotions", open=False):
|
138 |
+
anticipation_level_radio = gr.Radio(label="Anticipation level",
|
139 |
+
choices=[EMOTION_DEFAULT, "Interest", "Anticipation", "Vigilance"],
|
140 |
+
value=EMOTION_DEFAULT)
|
141 |
+
anticipation_level_radio.change(update_foo,
|
142 |
+
inputs=[anticipation_level_radio, anticipation_level_state],
|
143 |
+
outputs=[anticipation_level_state])
|
144 |
+
|
145 |
+
joy_level_radio = gr.Radio(label="Joy level",
|
146 |
+
choices=[EMOTION_DEFAULT, "Serenity", "Joy", "Ecstasy"],
|
147 |
+
value=EMOTION_DEFAULT)
|
148 |
+
joy_level_radio.change(update_foo,
|
149 |
+
inputs=[joy_level_radio, joy_level_state],
|
150 |
+
outputs=[joy_level_state])
|
151 |
+
|
152 |
+
trust_level_radio = gr.Radio(label="Trust level",
|
153 |
+
choices=[EMOTION_DEFAULT, "Acceptance", "Trust", "Admiration"],
|
154 |
+
value=EMOTION_DEFAULT)
|
155 |
+
trust_level_radio.change(update_foo,
|
156 |
+
inputs=[trust_level_radio, trust_level_state],
|
157 |
+
outputs=[trust_level_state])
|
158 |
+
|
159 |
+
fear_level_radio = gr.Radio(label="Fear level",
|
160 |
+
choices=[EMOTION_DEFAULT, "Apprehension", "Fear", "Terror"],
|
161 |
+
value=EMOTION_DEFAULT)
|
162 |
+
fear_level_radio.change(update_foo,
|
163 |
+
inputs=[fear_level_radio, fear_level_state],
|
164 |
+
outputs=[fear_level_state])
|
165 |
+
|
166 |
+
surprise_level_radio = gr.Radio(label="Surprise level",
|
167 |
+
choices=[EMOTION_DEFAULT, "Distraction", "Surprise", "Amazement"],
|
168 |
+
value=EMOTION_DEFAULT)
|
169 |
+
surprise_level_radio.change(update_foo,
|
170 |
+
inputs=[surprise_level_radio, surprise_level_state],
|
171 |
+
outputs=[surprise_level_state])
|
172 |
+
|
173 |
+
sadness_level_radio = gr.Radio(label="Sadness level",
|
174 |
+
choices=[EMOTION_DEFAULT, "Pensiveness", "Sadness", "Grief"],
|
175 |
+
value=EMOTION_DEFAULT)
|
176 |
+
sadness_level_radio.change(update_foo,
|
177 |
+
inputs=[sadness_level_radio, sadness_level_state],
|
178 |
+
outputs=[sadness_level_state])
|
179 |
+
|
180 |
+
disgust_level_radio = gr.Radio(label="Disgust level",
|
181 |
+
choices=[EMOTION_DEFAULT, "Boredom", "Disgust", "Loathing"],
|
182 |
+
value=EMOTION_DEFAULT)
|
183 |
+
disgust_level_radio.change(update_foo,
|
184 |
+
inputs=[disgust_level_radio, disgust_level_state],
|
185 |
+
outputs=[disgust_level_state])
|
186 |
+
|
187 |
+
anger_level_radio = gr.Radio(label="Anger level",
|
188 |
+
choices=[EMOTION_DEFAULT, "Annoyance", "Anger", "Rage"],
|
189 |
+
value=EMOTION_DEFAULT)
|
190 |
+
anger_level_radio.change(update_foo,
|
191 |
+
inputs=[anger_level_radio, anger_level_state],
|
192 |
+
outputs=[anger_level_state])
|
193 |
+
|
194 |
|
195 |
submit.click(desc2sheet,
|
196 |
inputs=[
|
197 |
+
request, openai_api_key_state, temperature_state, llm_chain_state, num_words_state,
|
198 |
+
formality_state,
|
199 |
+
anticipation_level_state, joy_level_state, trust_level_state, fear_level_state,
|
200 |
+
surprise_level_state, sadness_level_state, disgust_level_state, anger_level_state],
|
201 |
outputs=[table])
|
202 |
|
203 |
request.submit(desc2sheet,
|
204 |
inputs=[
|
205 |
+
request, openai_api_key_state, temperature_state, llm_chain_state, num_words_state,
|
206 |
+
formality_state,
|
207 |
+
anticipation_level_state, joy_level_state, trust_level_state, fear_level_state,
|
208 |
+
surprise_level_state, sadness_level_state, disgust_level_state, anger_level_state],
|
209 |
outputs=[table])
|
210 |
|
211 |
openai_api_key_textbox.change(set_openai_api_key,
|
|
|
217 |
inputs=[temperature_slider, temperature_state],
|
218 |
outputs=[temperature_state])
|
219 |
|
|
|
|
|
|
|
|
|
|
|
220 |
block.launch()
|