Jobanpreet commited on
Commit
c5abee7
·
verified ·
1 Parent(s): 691432b

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +241 -0
app.py ADDED
@@ -0,0 +1,241 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_community.document_loaders import WebBaseLoader
2
+ from langchain.prompts import ChatPromptTemplate
3
+ from langchain.output_parsers import ResponseSchema
4
+ from langchain.output_parsers import StructuredOutputParser
5
+ from langchain.prompts import PromptTemplate
6
+ from langchain.chat_models import ChatOpenAI
7
+ from langchain.chains import LLMChain
8
+ from dotenv import load_dotenv
9
+ import requests
10
+ import streamlit as st
11
+ import re
12
+ import openai
13
+
14
+
15
+ load_dotenv()
16
+
17
+ def 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.url
21
+ if final_url != url:
22
+ return True
23
+ return False
24
+ except requests.exceptions.RequestException as e:
25
+ print("Error:", e)
26
+ return False
27
+
28
+ def 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.url
33
+ else:
34
+ print("Error: Short URL couldn't be expanded.")
35
+ return None
36
+ except requests.exceptions.RequestException as e:
37
+ print("Error:", e)
38
+ return None
39
+
40
+ def get_original_url(url):
41
+ if is_shortened_url(url):
42
+ return expand_short_url(url)
43
+ else:
44
+ return url
45
+
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 it
49
+ def 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, highlights
79
+
80
+
81
+ def 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
+ Highlights
94
+
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 extractor.
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_dict
113
+
114
+
115
+
116
+
117
+
118
+ # def main():
119
+ # st.title("Paraphrase LinkedIn Post")
120
+
121
+ # # Initialize SessionState dictionary
122
+ # session_state = st.session_state
123
+
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 two numbers
134
+ # url = st.sidebar.text_input("Enter URL:", placeholder="Enter URL here...")
135
+
136
+ # # Button to calculate sum
137
+ # if st.sidebar.button("Submit"):
138
+ # if url:
139
+ # original_url = get_original_url(url)
140
+ # 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)
141
+
142
+ # if match:
143
+ # session_state.paraphrase, session_state.keywords, session_state.take_aways, session_state.highlights = paraphrased_post(url)
144
+
145
+ # else:
146
+ # st.sidebar.error("Put a valid LinkedIn post url only")
147
+
148
+ # st.write(session_state.paraphrase)
149
+
150
+ # if st.sidebar.button("Show Keywords") and session_state.keywords:
151
+ # st.write("Keywords:")
152
+ # for i, statement in enumerate(session_state.keywords, start=1):
153
+ # st.write(f"{i}. {statement}")
154
+
155
+
156
+ # if st.sidebar.button("Show Take Aways") and session_state.take_aways:
157
+ # st.write("Take Aways:")
158
+ # for i, statement in enumerate(session_state.take_aways, start=1):
159
+ # st.write(f"{i}. {statement}")
160
+
161
+ # if st.sidebar.button("Show Highlights") and session_state.highlights:
162
+ # st.write("Highlights:")
163
+ # for i, statement in enumerate(session_state.highlights, start=1):
164
+ # st.write(f"{i}. {statement}")
165
+
166
+ # if __name__ == "__main__":
167
+ # main()
168
+
169
+
170
+
171
+
172
+
173
+
174
+
175
+ import pyperclip
176
+
177
+ def main():
178
+ st.title("Paraphrase LinkedIn Post")
179
+
180
+ # Initialize SessionState dictionary
181
+ session_state = st.session_state
182
+
183
+ if 'paraphrase' not in session_state:
184
+ session_state.paraphrase = ""
185
+ if 'keywords' not in session_state:
186
+ session_state.keywords = ""
187
+ if 'take_aways' not in session_state:
188
+ session_state.take_aways = ""
189
+ if 'highlights' not in session_state:
190
+ session_state.highlights = ""
191
+
192
+ # User input for URL
193
+ url = st.sidebar.text_input("Enter URL:", placeholder="Enter URL here...")
194
+
195
+ # Button to submit URL
196
+ if st.sidebar.button("Submit"):
197
+ if url:
198
+ original_url = get_original_url(url)
199
+ 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)
200
+
201
+ if match:
202
+ session_state.paraphrase, session_state.keywords, session_state.take_aways, session_state.highlights = paraphrased_post(url)
203
+
204
+ else:
205
+ st.sidebar.error("Put a valid LinkedIn post URL only")
206
+
207
+
208
+ st.text_area("Paraphrase:", value=session_state.paraphrase, height=400)
209
+ if st.button("Copy Text"):
210
+ # Copy the content of the textarea to the clipboard
211
+ pyperclip.copy(session_state.paraphrase)
212
+ st.success("Text copied to clipboard!")
213
+
214
+ if st.sidebar.button("Show Keywords") and session_state.keywords:
215
+ st.write("Keywords:")
216
+ for i, statement in enumerate(session_state.keywords, start=1):
217
+ st.write(f"{i}. {statement}")
218
+
219
+
220
+ if st.sidebar.button("Show Take Aways") and session_state.take_aways:
221
+ st.write("Take Aways:")
222
+ for i, statement in enumerate(session_state.take_aways, start=1):
223
+ st.write(f"{i}. {statement}")
224
+
225
+ if st.sidebar.button("Show Highlights") and session_state.highlights:
226
+ st.write("Highlights:")
227
+ for i, statement in enumerate(session_state.highlights, start=1):
228
+ st.write(f"{i}. {statement}")
229
+
230
+ if __name__ == "__main__":
231
+ main()
232
+
233
+
234
+
235
+
236
+
237
+
238
+
239
+
240
+
241
+