Spaces:
Running
Running
Add example prompt to demo
Browse files
app.py
CHANGED
@@ -12,7 +12,17 @@ openai.api_key = os.environ.get("OPENAI_API_KEY")
|
|
12 |
|
13 |
|
14 |
def create_prompt(sentence, paraphrase_type):
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
"messages": [
|
17 |
{
|
18 |
"role": "user",
|
@@ -24,7 +34,6 @@ def create_prompt(sentence, paraphrase_type):
|
|
24 |
}
|
25 |
]
|
26 |
}
|
27 |
-
return prompt
|
28 |
|
29 |
|
30 |
paraphrase_types = [
|
@@ -72,7 +81,7 @@ with gr.Blocks() as demo:
|
|
72 |
chatbot = gr.Chatbot()
|
73 |
types = gr.Dropdown(
|
74 |
paraphrase_types,
|
75 |
-
value="
|
76 |
multiselect=True,
|
77 |
allow_custom_value=True,
|
78 |
)
|
@@ -81,21 +90,65 @@ with gr.Blocks() as demo:
|
|
81 |
clear = gr.Button("Clear")
|
82 |
|
83 |
def user(user_message, history):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
return "", history + [[user_message, None]]
|
85 |
|
86 |
def generate_paraphrase(user_message, paraphrase_type, history):
|
87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
prompt = create_prompt(history[-1][0], paraphrase_type)
|
89 |
bot_message = openai.ChatCompletion.create(
|
90 |
model="ft:gpt-3.5-turbo-0613:personal::7xbU0xQ2",
|
91 |
messages=prompt["messages"],
|
92 |
)
|
93 |
-
history[-1][1] = ""
|
94 |
for character in bot_message.choices[0].message.content:
|
95 |
history[-1][1] += character
|
96 |
time.sleep(0.01)
|
97 |
yield history
|
98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
100 |
generate_paraphrase, [msg, types, chatbot], chatbot
|
101 |
)
|
|
|
12 |
|
13 |
|
14 |
def create_prompt(sentence, paraphrase_type):
|
15 |
+
"""
|
16 |
+
Creates a prompt for generating a paraphrase of a given sentence with specified types.
|
17 |
+
|
18 |
+
Args:
|
19 |
+
sentence (str): The original sentence to be paraphrased.
|
20 |
+
paraphrase_type (str): The type of paraphrase to be generated.
|
21 |
+
|
22 |
+
Returns:
|
23 |
+
dict: A dictionary containing the prompt message.
|
24 |
+
"""
|
25 |
+
return {
|
26 |
"messages": [
|
27 |
{
|
28 |
"role": "user",
|
|
|
34 |
}
|
35 |
]
|
36 |
}
|
|
|
37 |
|
38 |
|
39 |
paraphrase_types = [
|
|
|
81 |
chatbot = gr.Chatbot()
|
82 |
types = gr.Dropdown(
|
83 |
paraphrase_types,
|
84 |
+
value="Syntax/discourse structure changes",
|
85 |
multiselect=True,
|
86 |
allow_custom_value=True,
|
87 |
)
|
|
|
90 |
clear = gr.Button("Clear")
|
91 |
|
92 |
def user(user_message, history):
|
93 |
+
"""
|
94 |
+
This function takes in a user message and a history of previous messages, and returns an empty string and an updated history list with the user message appended to it.
|
95 |
+
|
96 |
+
Args:
|
97 |
+
- user_message (str): The message sent by the user.
|
98 |
+
- history (list): A list of previous messages, where each message is a list containing the message text and the bot's response.
|
99 |
+
|
100 |
+
Returns:
|
101 |
+
- A tuple containing an empty string and the updated history list.
|
102 |
+
"""
|
103 |
return "", history + [[user_message, None]]
|
104 |
|
105 |
def generate_paraphrase(user_message, paraphrase_type, history):
|
106 |
+
"""
|
107 |
+
Generates a paraphrase of the user's message using OpenAI's GPT-3 model.
|
108 |
+
|
109 |
+
Args:
|
110 |
+
user_message (str): The message to be paraphrased.
|
111 |
+
paraphrase_type (str): The type of paraphrase to generate.
|
112 |
+
history (list): A list of previous messages in the conversation.
|
113 |
+
|
114 |
+
Yields:
|
115 |
+
list: A list of previous messages in the conversation, including the new paraphrase.
|
116 |
+
"""
|
117 |
+
types_as_str = ",".join(paraphrase_type)
|
118 |
+
history[-1][1] = f"[System: {types_as_str}]\n\n"
|
119 |
prompt = create_prompt(history[-1][0], paraphrase_type)
|
120 |
bot_message = openai.ChatCompletion.create(
|
121 |
model="ft:gpt-3.5-turbo-0613:personal::7xbU0xQ2",
|
122 |
messages=prompt["messages"],
|
123 |
)
|
|
|
124 |
for character in bot_message.choices[0].message.content:
|
125 |
history[-1][1] += character
|
126 |
time.sleep(0.01)
|
127 |
yield history
|
128 |
|
129 |
+
chatbot.value = [
|
130 |
+
[
|
131 |
+
(
|
132 |
+
"These outlined a"
|
133 |
+
" theory of the photoelectric effect, explained Brownian"
|
134 |
+
" motion, introduced his special theory of relativity—a theory"
|
135 |
+
" which addressed the inability of classical mechanics to"
|
136 |
+
" account satisfactorily for the behavior of the"
|
137 |
+
" electromagnetic field—and demonstrated that if the special"
|
138 |
+
" theory is correct, mass and energy are equivalent to each"
|
139 |
+
" other."
|
140 |
+
),
|
141 |
+
(
|
142 |
+
"[System: Syntax/discourse structure changes]\n\nAnother of"
|
143 |
+
" the papers introduced Einstein's special theory of"
|
144 |
+
" relativity, which addressed the inability of classical"
|
145 |
+
" mechanics to account for the behavior of the electromagnetic"
|
146 |
+
" field, and demonstrated that, if the special theory is"
|
147 |
+
" correct, mass and energy are equivalent to each other."
|
148 |
+
),
|
149 |
+
]
|
150 |
+
]
|
151 |
+
|
152 |
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
153 |
generate_paraphrase, [msg, types, chatbot], chatbot
|
154 |
)
|