LinkedInpost / app.py
Jobanpreet's picture
Upload 4 files
46290fc verified
raw
history blame
5.73 kB
import streamlit as st
import re
import openai
from paraphrase_post import get_original_url , paraphrased_post
from advance_post import google_search , advanced_post
from langchain.chat_models import ChatOpenAI
from langchain_groq import ChatGroq
#from langchain import HuggingFaceHub
def main():
st.title("LinkedIn Post Creator")
# Initialize SessionState dictionary
session_state = st.session_state
if 'paraphrase' not in session_state:
session_state.paraphrase = ""
if 'keywords' not in session_state:
session_state.keywords = ""
if 'take_aways' not in session_state:
session_state.take_aways = ""
if 'highlights' not in session_state:
session_state.highlights = ""
if 'advancepost' not in session_state:
session_state.advancepost = ""
url = st.sidebar.text_input("Enter URL:", placeholder="Enter URL here...")
option = st.sidebar.selectbox('Select Model:', ('GPT-4',"Mixtral-8x7b","Gemma-7b"))
temperature= st.sidebar.select_slider(
'How much accurate post you want ?',
options=['Less accuracy', 9, 8, 7, 6, 5,4,3 ,2,1,'High accuracy'])
if temperature=='Less accuracy':
temperature=10
elif temperature=="High accuracy":
temperature=0
temperature=temperature/10
if option=="GPT-4":
api_key=st.sidebar.text_input("API Key:",placeholder="Enter OpenAI API Key...")
if api_key:
model=ChatOpenAI(model="gpt-4-turbo-preview" , temperature=temperature , api_key=api_key)
elif option=="Mixtral-8x7b":
api_key= st.sidebar.text_input("API Key:",placeholder="Enter Groq API Key...")
if api_key:
model = ChatGroq(temperature=temperature,groq_api_key=api_key, model_name="mixtral-8x7b-32768")
elif option=="Gemma-7b":
api_key=st.sidebar.text_input("API Key:",placeholder="Enter Groq API Key...")
if api_key:
model= ChatGroq(temperature=temperature,groq_api_key=api_key , model_name="Gemma-7b-It")
# elif option=="Llama-3":
# api_key=st.sidebar.text_input("API Key:",placeholder="Enter HuggingFace API Token...")
# if api_key:
# model=HuggingFaceHub(repo_id="mistralai/Mixtral-8x22B-Instruct-v0.1",huggingfacehub_api_token=api_key ,model_kwargs={"temperature":temperature})
if st.sidebar.button("Submit"):
if url:
if api_key:
original_url = get_original_url(url)
match = re.match(r"https?://(?:www\.)?linkedin\.com/(posts|feed|pulse)/.*", original_url) # checking domain and url page (means it should only be a post nothing else like login page or something else)
if match:
try:
session_state.paraphrase, session_state.keywords, session_state.take_aways, session_state.highlights = paraphrased_post(url , model)
except (openai.AuthenticationError) as e:
st.sidebar.error("Enter your valid API key")
else:
st.sidebar.error("Put a valid LinkedIn post url only")
else:
st.sidebar.error("Please enter API Key")
else:
st.sidebar.error("Please enter url")
paraphrase_text=st.text_area("Generated LinkedIn post",value=session_state.paraphrase, height=400)
# import pyperclip
# if st.button('Copy'):
# pyperclip.copy(paraphrase_text)
# st.success('Text copied successfully!')
if st.sidebar.toggle("Show Details") and session_state.keywords:
st.write("Keywords:")
for i, statement in enumerate(session_state.keywords, start=1):
st.write(f"{i}. {statement}")
st.write("Take Aways:")
for i, statement in enumerate(session_state.take_aways, start=1):
st.write(f"{i}. {statement}")
st.write("Highlights:")
for i, statement in enumerate(session_state.highlights, start=1):
st.write(f"{i}. {statement}")
#------------------------------------------------------------Advance LinkedIn post code below-----------------------------------------------------------------
if st.sidebar.toggle("Advance LinkedIn Post"):
google_api_key=st.sidebar.text_input("Google API Key:",placeholder="Enter Google Search API Key...")
search_engine_id=st.sidebar.text_input("Search Engine ID:",placeholder="Enter Search Engine ID...")
google_api_key = "AIzaSyDh-lkJh2Zef0t6UVqSu_w3njpucx40mDc"
search_engine_id = "44bbd32a2b2fc4418"
if st.sidebar.button("Generate Advance Post"):
if google_api_key:
if search_engine_id:
all_links =google_search(session_state.paraphrase ,model , google_api_key,search_engine_id)
session_state.advancepost = advanced_post(all_links ,model ,session_state.paraphrase)
# if len(docs)==0:
# st.sidebar.error("Please Check your both credentials carefully")
else:
st.sidebar.error("Please enter Search Engine ID")
else:
st.sidebar.error("Please enter Google API Key")
advance_post=st.text_area("Advance LinkedIn post",value=session_state.advancepost, height=400)
# if st.button('Copy Advanced Post'):
# pyperclip.copy(advance_post)
# st.success('Text copied successfully!')
#--------------------------------------------------------------------------------------------------------------------------------------------------------------
if __name__ == "__main__":
main()