Sahibsingh12/linkedinpostcreator
0
1from langchain_community.document_loaders import WebBaseLoader2from langchain.prompts import ChatPromptTemplate3from langchain.output_parsers import ResponseSchema4from langchain.output_parsers import StructuredOutputParser5from langchain.prompts import PromptTemplate 6from langchain.chat_models import ChatOpenAI 7from langchain.chains import LLMChain8from dotenv import load_dotenv9import requests10import streamlit as st 11import re12import openai13 14 15load_dotenv()16 17def is_shortened_url(url): # It is checking whether it is a shorten url or regular website url 18 try:19 response = requests.head(url, allow_redirects=True)20 final_url = response.url21 if final_url != url:22 return True23 return False24 except requests.exceptions.RequestException as e:25 print("Error:", e)26 return False27 28def expand_short_url(short_url): # It is converting shorten url to regular url 29 try:30 response = requests.head(short_url, allow_redirects=True)31 if response.status_code == 200:32 return response.url33 else:34 print("Error: Short URL couldn't be expanded.")35 return None36 except requests.exceptions.RequestException as e:37 print("Error:", e)38 return None39 40def get_original_url(url):41 if is_shortened_url(url):42 return expand_short_url(url)43 else:44 return url45 46 47 48# This is the complete code where we are extracting content from the url using WebBaseLoader , using LLM to extract blog content only and then paraphrasing it49def paraphrased_post(url): 50 loader=WebBaseLoader([url],encoding='utf-8')51 docs = loader.load()52 53 template="""You are a helpful LinkedIn webscrapper. You are provided with a data , extract the content of the post only.54 {docs}"""55 56 57 prompt=PromptTemplate(template=template,input_variables=['docs'])58 llm=ChatOpenAI(temperature=0)59 chain=LLMChain(llm=llm,prompt=prompt)60 61 62 result=chain.invoke({'docs':docs},return_only_outputs=True)63 64 data=result['text']65 66 template="""You are a helpful LinkedIn post paraphraser and plagiarism remover bot. You are provided with LinkedIn post content and your task is to paraphrase it and remove plagiarism .Return the output in the format with spaces or stickers if present.67 {data}"""68 69 prompt2=PromptTemplate(template=template,input_variables=['data'])70 llm=ChatOpenAI(temperature=0)71 chain2=LLMChain(llm=llm,prompt=prompt2)72 73 result2=chain2({'data':data},return_only_outputs=True)74 data2=extract_data(result2['text'])75 keywords=data2['Keywords'][:3]76 take_aways=data2['Take Aways'][:3]77 highlights=data2['Highlights'][:3]78 return result2['text'] ,keywords , take_aways, highlights79 80 81def extract_data(post_data):82 keywords = ResponseSchema(name="Keywords",83 description="These are the keywords extracted from LinkedIn post",type="list")84 85 Take_aways = ResponseSchema(name="Take Aways",86 description="These are the take aways extracted from LinkedIn post", type= "list")87 Highlights=ResponseSchema(name="Highlights",88 description="These are the highlights extracted from LinkedIn post", type= "list")89 90 response_schema = [91 keywords,92 Take_aways,93 Highlights94 95 ]96 output_parser = StructuredOutputParser.from_response_schemas(response_schema)97 format_instructions = output_parser.get_format_instructions()98 99 template = """100 You are a helpful keywords , take aways and highlights extractor from the post of LinkedIn Bot. Your task is to extract relevant keywords , take aways and highlights in descending order of their scores in a list, means high relevant should be on the top .101 From the following text message, extract the following information:102 103 text message: {content}104 {format_instructions}105 """106 107 prompt_template = ChatPromptTemplate.from_template(template)108 messages = prompt_template.format_messages(content=post_data, format_instructions=format_instructions)109 llm = ChatOpenAI(temperature=0)110 response = llm(messages)111 output_dict= output_parser.parse(response.content)112 return output_dict113 114 115 116 117 118def main():119 st.title("LinkedIn Post Creator")120 121 # Initialize SessionState dictionary122 session_state = st.session_state123 124 if 'paraphrase' not in session_state:125 session_state.paraphrase = ""126 if 'keywords' not in session_state:127 session_state.keywords = ""128 if 'take_aways' not in session_state:129 session_state.take_aways = ""130 if 'highlights' not in session_state:131 session_state.highlights = ""132 133 # User input for URL134 url = st.sidebar.text_input("Enter URL:", placeholder="Enter URL here...")135 136 # Button to submit URL137 if st.sidebar.button("Submit"):138 try:139 if url:140 original_url = get_original_url(url)141 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)142 143 if match:144 session_state.paraphrase, session_state.keywords, session_state.take_aways, session_state.highlights = paraphrased_post(url)145 146 else:147 st.sidebar.error("Put a valid LinkedIn post URL only")148 except (openai.BadRequestError, TypeError) as e:149 st.sidebar.error("Put a valid LinkedIn post URL only")150 151 152 153 paraphrase_text=st.text_area("Paraphrase:", value=session_state.paraphrase, height=400)154 # import pyperclip155 # if st.button('Copy'): # For copying the content (Also install xclip (debian package) if error occured)156 # pyperclip.copy(paraphrase_text)157 # st.success('Text copied successfully!')158 159 if st.sidebar.button("Show Keywords") and session_state.keywords:160 st.write("Keywords:")161 for i, statement in enumerate(session_state.keywords, start=1):162 st.write(f"{i}. {statement}")163 164 165 if st.sidebar.button("Show Take Aways") and session_state.take_aways:166 st.write("Take Aways:")167 for i, statement in enumerate(session_state.take_aways, start=1):168 st.write(f"{i}. {statement}")169 170 if st.sidebar.button("Show Highlights") and session_state.highlights:171 st.write("Highlights:")172 for i, statement in enumerate(session_state.highlights, start=1):173 st.write(f"{i}. {statement}")174 175if __name__ == "__main__":176 main()177 178 179 180 181 182 183 184 185 186 187 