-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprompt_app.py
245 lines (196 loc) · 11 KB
/
prompt_app.py
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
239
240
241
242
243
244
245
import os
__import__('pysqlite3')
import sys
sys.modules['sqlite3'] = sys.modules.pop('pysqlite3')
import sqlite3
import pandas as pd
import streamlit as st
from langchain.chat_models import ChatOpenAI
from langchain.chains import LLMChain
from langchain.tools import Tool
from langchain.utilities import WikipediaAPIWrapper, GoogleSearchAPIWrapper
from langchain.globals import set_llm_cache
from langchain.cache import InMemoryCache
set_llm_cache(InMemoryCache())
os.environ['OPENAI_API_KEY'] = st.secrets['OPENAI_API_KEY']
os.environ['GOOGLE_CSE_ID'] = st.secrets['GOOGLE_CSE_ID']
os.environ['GOOGLE_API_KEY'] = st.secrets['GOOGLE_API_KEY']
#Import Kaggle data: https://www.kaggle.com/datasets/crowdflower/political-social-media-posts?resource=download
#Retrieve Data
data = pd.read_csv('political_social_media.csv', encoding_errors= "ignore")
#LangChain Crash Course: Build a AutoGPT app in 25 minutes!: https://www.youtube.com/watch?v=MlK6SIjcjE8
#Use to run Streamlit: python -m streamlit run prompt_app.py
#Finetuning for Tone: https://blog.langchain.dev/chat-loaders-finetune-a-chatmodel-in-your-voice/
#Creates session state for fine-tuned
if 'headline' not in st.session_state:
st.session_state.headline_id = None
if 'press' not in st.session_state:
st.session_state.press_id = None
if 'twitter' not in st.session_state:
st.session_state.twitter_id = None
if 'facebook' not in st.session_state:
st.session_state.facebook_id = None
if 'instagram' not in st.session_state:
st.session_state.instagram_id = None
if 'google' not in st.session_state:
st.session_state.google_id = None
if 'wiki' not in st.session_state:
st.session_state.wiki_id = None
#Creates session state for baseline
if 'headline2' not in st.session_state:
st.session_state.headline2_id = None
if 'press2' not in st.session_state:
st.session_state.press2_id = None
if 'twitter2' not in st.session_state:
st.session_state.twitter2_id = None
if 'facebook2' not in st.session_state:
st.session_state.facebook2_id = None
if 'instagram2' not in st.session_state:
st.session_state.instagram2_id = None
def finestate(headline, press_release, twitter, facebook, instagram, google_research, wiki_research):
#Uses session state to store fine-tuned variables
st.session_state.headline_id = headline
st.session_state.press_id = press_release
st.session_state.twitter_id = twitter
st.session_state.facebook_id = facebook
st.session_state.instagram_id = instagram
st.session_state.google_id = google_research
st.session_state.wiki_id = wiki_research
#Adds returned results to tab 1 and uses expanders to separate topics
with st.expander("Press Release"):
st.write(st.session_state.press_id)
with st.expander("Tweet"):
st.write(st.session_state.twitter_id)
with st.expander("Facebook Post"):
st.write(st.session_state.facebook_id)
with st.expander("Instagram Post"):
st.write(st.session_state.instagram_id)
with st.expander("Google Research"):
st.info(st.session_state.google_id)
with st.expander("Wikipedia Research"):
st.info(st.session_state.wiki_id)
return st.session_state
def defstate(headline2, press_release2, twitter2, facebook2, instagram2):
#Uses session state to store default variables
st.session_state.headline2_id = headline2
st.session_state.press2_id = press_release2
st.session_state.twitter2_id = twitter2
st.session_state.facebook2_id = facebook2
st.session_state.instagram2_id = instagram2
#Adds returned results to tab 2 and uses expanders to separate topics
with st.expander("Press Release"):
st.write(st.session_state.press2_id)
with st.expander("Tweet"):
st.write(st.session_state.twitter2_id)
with st.expander("Facebook Post"):
st.write(st.session_state.facebook2_id)
with st.expander("Instagram Post"):
st.write(st.session_state.instagram2_id)
return st.session_state
#Create function to generate fine-tuned content
@st.cache_resource(show_spinner="Fetching data from OpenAI")
def generate_fine(prompt):
#Returns response to prompt: What Political Issue Should I Write About?
#Runs the Generative AI model using fine-tuned model and few shot prompting
from Finetuned import headline_prompt, press_template, twitter_template, facebook_template, instagram_template
llm = ChatOpenAI(temperature=0.5, model = "ft:gpt-3.5-turbo-0613:personal::84XCwFjs")
headline_chain = LLMChain(llm=llm, prompt=headline_prompt, verbose = True, output_key = "headline")
press_chain = LLMChain(llm=llm, prompt=press_template, verbose = True, output_key = "press_release")
twitter_chain = LLMChain(llm=llm, prompt=twitter_template, verbose = True, output_key = "twitter")
facebook_chain = LLMChain(llm=llm, prompt=facebook_template, verbose = True, output_key = "facebook")
instagram_chain = LLMChain(llm=llm, prompt=instagram_template, verbose = True, output_key = "instagram")
#Creates wikipedia and google search instances
search = GoogleSearchAPIWrapper()
tool = Tool(
name="Google Search",
description="Search Google for recent results.",
func=search.run,
)
wiki = WikipediaAPIWrapper()
wiki_research = wiki.run(prompt)
google_research = tool.run(prompt)
#Feeds prompts into OpenAI LLM chains
headline = headline_chain.run(prompt)
st.write("Headline: " + headline)
st.write("Your content is being generated. I am checking a number of sources and crafting an optimal solution for you - please give me a moment (~30 seconds).")
press_release = press_chain.run(headline=headline,wikipedia_research=wiki_research,google=google_research)
twitter = twitter_chain.run(press_release=press_release,headline=headline)
facebook = facebook_chain.run(twitter=twitter,headline=headline)
instagram = instagram_chain.run(facebook=facebook,headline=headline)
return headline, press_release, twitter, facebook, instagram, google_research, wiki_research
#Create function to generate default content
@st.cache_resource(show_spinner="Fetching data from OpenAI")
def generate_default(prompt):
#Returns response to prompt: What Political Issue Should I Write About?
#Runs the Generative AI model using basic model and limited prompting
from Baseline import headline_prompt2, press_template2, twitter_template2, facebook_template2, instagram_template2
llm2 = ChatOpenAI(temperature=0.5, model='gpt-3.5-turbo-16k-0613')
headline_chain2 = LLMChain(llm=llm2, prompt=headline_prompt2, verbose = True, output_key = "headline2",)
press_chain2 = LLMChain(llm=llm2, prompt=press_template2, verbose = True, output_key = "press_release2")
twitter_chain2 = LLMChain(llm=llm2, prompt=twitter_template2, verbose = True, output_key = "twitter2")
facebook_chain2 = LLMChain(llm=llm2, prompt=facebook_template2, verbose = True, output_key = "facebook2")
instagram_chain2 = LLMChain(llm=llm2, prompt=instagram_template2, verbose = True, output_key = "instagram2")
#Feeds prompts into OpenAI LLM chains
headline2 = headline_chain2.run(prompt)
st.write("Headline: " + headline2)
st.write("Your content is being generated. I am checking a number of sources and crafting an optimal solution for you - please give me a moment (~4 minutes). If I am taking longer than expected, that is because OpenAI is overloaded with requests, so I am re-running on your behalf.")
press_release2 = press_chain2.run(headline2=headline2)
twitter2 = twitter_chain2.run(press_release2=press_release2,headline2=headline2)
facebook2 = facebook_chain2.run(twitter2=twitter2,headline2=headline2)
instagram2 = instagram_chain2.run(facebook2=facebook2,headline2=headline2)
return headline2, press_release2, twitter2, facebook2, instagram2
#App Framework
#Introduces app and ingests prompt provided by user
#Color Palette 1: https://coolors.co/palette/cc8b86-f9eae1-7d4f50-d1be9c-aa998f
#Color Palette 2: https://coolors.co/palette/e8d1c5-eddcd2-fff1e6-f0efeb-eeddd3-edede8
st.image('Logo/Political Banter-logos_transparent.png')
st.header('The Next Generation of Political Tech')
st.write('Learn more about Political Banter in the side bar!')
prompt = st.text_input('What Political Issue Should I Write About?')
#Creates sidebar
with st.sidebar:
st.title('About Political Banter')
st.header('Using this tool is as simple as telling Political Banter what political issues you want it to write about.')
st.markdown('Political Banter was created by finetuning an OpenAI chatGPT model based on a Kaggle database of Tweets by politicians from across the United States. Additional prompting was also used to guide the algorithm to craft catchy political content in the form of a headline, press release, tweet, facebook post, and instagram post.')
st.markdown('Political Banter uses caching, so your topic will be saved for future-reruns. To generate new content, either submit a new topic or clear the chache using the button below.')
def clear_cache():
st.cache_resource.clear()
st.sidebar.button("Refresh Program",on_click=clear_cache, type='primary')
st.write('Learn more about the Kaggle dataset that was used to inform the tone and voice of Political Banter via the data tab and the following link: https://www.kaggle.com/datasets/crowdflower/political-social-media-posts?resource=download')
#Creates radio button widget
model = st.radio(
"Which GenAI model would you like to use?",
["Fine-Tuned OpenAI Model","Default OpenAI Model"],
captions = ["Includes Fine-Tuned OpenAI Model and Few Shot Prompts","Uses Default OpenAI Model and Basic Prompts"]
)
st.write('Please do not change the settings until after the content is generated. Otherwise, your content will not be generated.')
#Creates tabs to separate app features
tab1, tab2, tab3 = st.tabs(['Political Banter','Default','Data'])
#Runs finetuned model and generates outputs onto tab 1
with tab1:
if model == "Fine-Tuned OpenAI Model":
#Creates button for generating content
finebutton = st.button("Generate Fine-Tuned Content", type='primary')
#Runs button to generate content
if finebutton:
if prompt:
headline, press_release, twitter, facebook, instagram, google_research, wiki_research = generate_fine(prompt)
finestate(headline, press_release, twitter, facebook, instagram, google_research, wiki_research)
else:
st.write("Please select the Fine-Tuned OpenAI Model setting to generate new content")
#Runs default model and generates outputs onto tab 2
with tab2:
if model == "Default OpenAI Model":
#Creates button for generating content
defbutton = st.button("Generate Default Content", type='primary')
#Runs button to generate content
if defbutton:
if prompt:
headline2, press_release2, twitter2, facebook2, instagram2 = generate_default(prompt)
defstate(headline2, press_release2, twitter2, facebook2, instagram2)
else:
st.write("Please select the Default OpenAI Model setting to generate new content")
#Adds data table to tab 3
with tab3:
st.write("This data was ingested into the fine tuning GenerativeAI Model used to build the Political Banter App and can be found at: https://www.kaggle.com/datasets/crowdflower/political-social-media-posts?resource=download")
st.write(data)