Spaces:
Sleeping
Sleeping
Umang Chaudhry
commited on
Commit
•
8aad46a
1
Parent(s):
c9221ac
Merge pull request #109 from vanderbilt-data-science/sources-to-prompt
Browse files- app.py +8 -8
- free_speech_app/FreeSpeechPromptsResponses.py +13 -14
app.py
CHANGED
@@ -79,11 +79,11 @@ if authentication_status:
|
|
79 |
|
80 |
principles = st.text_input("My Principles (Paste Principles here)", placeholder = "Enter the main principles of your life you wish this response to uphold", value=user_data["principles"] if user_data and "principles" in user_data else "")
|
81 |
writing_style = st.text_input("My Writing Style (Paste Examples)", placeholder = "Provide examples of your writing style here", value=user_data["writing_style"] if user_data and "writing_style" in user_data else "")
|
82 |
-
sources = st.text_area("Sources (This autopopulates for your reference)", value=st.session_state.sources if 'sources' in st.session_state else '', key = 'sources_key', height = 100)
|
83 |
|
84 |
# Update button
|
85 |
if st.button("Save Changes"):
|
86 |
-
db.put({"key": username, "principles": principles, "writing_style": writing_style, "
|
87 |
|
88 |
if page == "OpenAI API Key Setup":
|
89 |
st.title("OpenAI API Key Setup")
|
@@ -136,20 +136,20 @@ if authentication_status:
|
|
136 |
|
137 |
if chat_mdl is not None:
|
138 |
if user_data is None:
|
139 |
-
draft_response, background_text
|
140 |
st.session_state.draft_response = draft_response.content
|
141 |
st.session_state.background_text = background_text
|
142 |
-
st.session_state.sources_text = sources_text
|
143 |
st.session_state.background_info = background_text
|
144 |
-
st.session_state.sources = sources_text
|
145 |
st.rerun()
|
146 |
else:
|
147 |
-
draft_response, background_text
|
148 |
st.session_state.draft_response = draft_response.content
|
149 |
st.session_state.background_text = background_text
|
150 |
-
st.session_state.sources_text = sources_text
|
151 |
st.session_state.background_info = background_text
|
152 |
-
st.session_state.sources = sources_text
|
153 |
st.rerun()
|
154 |
|
155 |
|
|
|
79 |
|
80 |
principles = st.text_input("My Principles (Paste Principles here)", placeholder = "Enter the main principles of your life you wish this response to uphold", value=user_data["principles"] if user_data and "principles" in user_data else "")
|
81 |
writing_style = st.text_input("My Writing Style (Paste Examples)", placeholder = "Provide examples of your writing style here", value=user_data["writing_style"] if user_data and "writing_style" in user_data else "")
|
82 |
+
#sources = st.text_area("Sources (This autopopulates for your reference)", value=st.session_state.sources if 'sources' in st.session_state else '', key = 'sources_key', height = 100)
|
83 |
|
84 |
# Update button
|
85 |
if st.button("Save Changes"):
|
86 |
+
db.put({"key": username, "principles": principles, "writing_style": writing_style, "api_key": encrypted_api_key})
|
87 |
|
88 |
if page == "OpenAI API Key Setup":
|
89 |
st.title("OpenAI API Key Setup")
|
|
|
136 |
|
137 |
if chat_mdl is not None:
|
138 |
if user_data is None:
|
139 |
+
draft_response, background_text = generate_custom_response(original_post, chat_mdl, "", "")
|
140 |
st.session_state.draft_response = draft_response.content
|
141 |
st.session_state.background_text = background_text
|
142 |
+
#st.session_state.sources_text = sources_text
|
143 |
st.session_state.background_info = background_text
|
144 |
+
#st.session_state.sources = sources_text
|
145 |
st.rerun()
|
146 |
else:
|
147 |
+
draft_response, background_text = generate_custom_response(original_post, chat_mdl, user_data['principles'], user_data['writing_style'])
|
148 |
st.session_state.draft_response = draft_response.content
|
149 |
st.session_state.background_text = background_text
|
150 |
+
#st.session_state.sources_text = sources_text
|
151 |
st.session_state.background_info = background_text
|
152 |
+
#st.session_state.sources = sources_text
|
153 |
st.rerun()
|
154 |
|
155 |
|
free_speech_app/FreeSpeechPromptsResponses.py
CHANGED
@@ -26,8 +26,12 @@ Next, here are some principles I consider particularly important to me:
|
|
26 |
\n{principles}\n
|
27 |
Here are some examples of the style in which I write:
|
28 |
\n{writing_style}\n
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
31 |
Using all the information I have provided, draft an appropriate response to the offensive post in question. Be sure to write as if you are posting on social media with no more than {word_limit} words. Do not acknowledge the original author of the hate speech post."""
|
32 |
|
33 |
try:
|
@@ -39,13 +43,13 @@ try:
|
|
39 |
|
40 |
PROMPT_TEMPLATE = PromptTemplate(
|
41 |
template=PROMPT_TEMPLATE_TEXT,
|
42 |
-
input_variables=["original_post", "background_info", "principles", "writing_style", "
|
43 |
|
44 |
except FileNotFoundError:
|
45 |
print(DEAFULT_PROMPT_TEMPLATE_TEXT)
|
46 |
PROMPT_TEMPLATE = PromptTemplate(
|
47 |
template=DEAFULT_PROMPT_TEMPLATE_TEXT,
|
48 |
-
input_variables=["original_post", "background_info", "principles", "writing_style", "
|
49 |
|
50 |
|
51 |
|
@@ -76,10 +80,6 @@ def call_chatGPT(query):
|
|
76 |
# %% ../nbs/free-speech-prompts.ipynb 12
|
77 |
def generate_custom_prompt(original_post, principles=None, writing_style=None, word_limit=None):
|
78 |
|
79 |
-
# Get database and query retriever
|
80 |
-
|
81 |
-
sources_db = DeepLake(dataset_path="hub://vanderbilt-dsi/counterspeech-resources", embedding = OpenAIEmbeddings())
|
82 |
-
|
83 |
# Use defaults in the case of None
|
84 |
|
85 |
if principles is None:
|
@@ -89,29 +89,28 @@ def generate_custom_prompt(original_post, principles=None, writing_style=None, w
|
|
89 |
writing_style="I have no examples of my writing style."
|
90 |
|
91 |
if word_limit is None:
|
92 |
-
word_limit="
|
93 |
|
94 |
retriever_query = original_post
|
95 |
background_query = "I found this post which I suspect to be hate speech. Can you please explain the background information I need to understand this post? Keep in mind terms/codes commonly used by extremist/hate groups such as Nazis, TERFS, alt-right, etc."
|
96 |
background_query = background_query + "\n" + original_post
|
97 |
background_info = call_chatGPT(background_query)
|
98 |
-
sources = query_retriever(sources_db, retriever_query)
|
99 |
|
100 |
# Fill the prompt
|
101 |
-
filled_prompt = PROMPT_TEMPLATE.format(original_post=original_post, background_info=background_info, principles=principles, writing_style=writing_style,
|
102 |
|
103 |
-
return filled_prompt, background_info
|
104 |
|
105 |
# %% ../nbs/free-speech-prompts.ipynb 13
|
106 |
def generate_custom_response(original_post, chat_mdl, principles=None, writing_style=None, word_limit=None):
|
107 |
|
108 |
# create customized prompt
|
109 |
-
customized_prompt, background_info
|
110 |
|
111 |
# get response
|
112 |
draft_response = get_chat_model_response(chat_mdl, customized_prompt)
|
113 |
|
114 |
-
return draft_response, background_info
|
115 |
|
116 |
# %% ../nbs/free-speech-prompts.ipynb 14
|
117 |
def regenerate_custom_response(chat_mdl, regenerate_prompt, draft_response):
|
|
|
26 |
\n{principles}\n
|
27 |
Here are some examples of the style in which I write:
|
28 |
\n{writing_style}\n
|
29 |
+
\n{writing_style}\n
|
30 |
+
Finally, here are some best practices when responding to hate speech:
|
31 |
+
1. Keep your response short and to the point
|
32 |
+
2. Use either a humorous or empathetic tone - depending on the situation.
|
33 |
+
For instance, an empathetic response could be "This post is very painful for Jewish people to read".
|
34 |
+
A humorous response could be the use of sarcasm to discredit the hate speech.
|
35 |
Using all the information I have provided, draft an appropriate response to the offensive post in question. Be sure to write as if you are posting on social media with no more than {word_limit} words. Do not acknowledge the original author of the hate speech post."""
|
36 |
|
37 |
try:
|
|
|
43 |
|
44 |
PROMPT_TEMPLATE = PromptTemplate(
|
45 |
template=PROMPT_TEMPLATE_TEXT,
|
46 |
+
input_variables=["original_post", "background_info", "principles", "writing_style", "word_limit"])
|
47 |
|
48 |
except FileNotFoundError:
|
49 |
print(DEAFULT_PROMPT_TEMPLATE_TEXT)
|
50 |
PROMPT_TEMPLATE = PromptTemplate(
|
51 |
template=DEAFULT_PROMPT_TEMPLATE_TEXT,
|
52 |
+
input_variables=["original_post", "background_info", "principles", "writing_style", "word_limit"])
|
53 |
|
54 |
|
55 |
|
|
|
80 |
# %% ../nbs/free-speech-prompts.ipynb 12
|
81 |
def generate_custom_prompt(original_post, principles=None, writing_style=None, word_limit=None):
|
82 |
|
|
|
|
|
|
|
|
|
83 |
# Use defaults in the case of None
|
84 |
|
85 |
if principles is None:
|
|
|
89 |
writing_style="I have no examples of my writing style."
|
90 |
|
91 |
if word_limit is None:
|
92 |
+
word_limit="100"
|
93 |
|
94 |
retriever_query = original_post
|
95 |
background_query = "I found this post which I suspect to be hate speech. Can you please explain the background information I need to understand this post? Keep in mind terms/codes commonly used by extremist/hate groups such as Nazis, TERFS, alt-right, etc."
|
96 |
background_query = background_query + "\n" + original_post
|
97 |
background_info = call_chatGPT(background_query)
|
|
|
98 |
|
99 |
# Fill the prompt
|
100 |
+
filled_prompt = PROMPT_TEMPLATE.format(original_post=original_post, background_info=background_info, principles=principles, writing_style=writing_style, word_limit=word_limit)
|
101 |
|
102 |
+
return filled_prompt, background_info
|
103 |
|
104 |
# %% ../nbs/free-speech-prompts.ipynb 13
|
105 |
def generate_custom_response(original_post, chat_mdl, principles=None, writing_style=None, word_limit=None):
|
106 |
|
107 |
# create customized prompt
|
108 |
+
customized_prompt, background_info = generate_custom_prompt(original_post, principles, writing_style, word_limit)
|
109 |
|
110 |
# get response
|
111 |
draft_response = get_chat_model_response(chat_mdl, customized_prompt)
|
112 |
|
113 |
+
return draft_response, background_info
|
114 |
|
115 |
# %% ../nbs/free-speech-prompts.ipynb 14
|
116 |
def regenerate_custom_response(chat_mdl, regenerate_prompt, draft_response):
|