Spaces:
Sleeping
Sleeping
Delete paraphrase_post.py
Browse files- paraphrase_post.py +0 -101
paraphrase_post.py
DELETED
@@ -1,101 +0,0 @@
|
|
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_core.output_parsers import StrOutputParser
|
6 |
-
import requests
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
def is_shortened_url(url): # It is checking whether it is a shorten url or regular website url
|
11 |
-
try:
|
12 |
-
response = requests.head(url, allow_redirects=True)
|
13 |
-
final_url = response.url
|
14 |
-
if final_url != url:
|
15 |
-
return True
|
16 |
-
return False
|
17 |
-
except requests.exceptions.RequestException as e:
|
18 |
-
print("Error:", e)
|
19 |
-
return False
|
20 |
-
|
21 |
-
def expand_short_url(short_url): # It is converting shorten url to regular url
|
22 |
-
try:
|
23 |
-
response = requests.head(short_url, allow_redirects=True)
|
24 |
-
if response.status_code == 200:
|
25 |
-
return response.url
|
26 |
-
else:
|
27 |
-
print("Error: Short URL couldn't be expanded.")
|
28 |
-
return None
|
29 |
-
except requests.exceptions.RequestException as e:
|
30 |
-
print("Error:", e)
|
31 |
-
return None
|
32 |
-
|
33 |
-
def get_original_url(url):
|
34 |
-
if is_shortened_url(url):
|
35 |
-
return expand_short_url(url)
|
36 |
-
else:
|
37 |
-
return url
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
# Below function extract the post only content from complete web page content and parraphrase the extracted post
|
43 |
-
|
44 |
-
def paraphrased_post(url,model):
|
45 |
-
loader=WebBaseLoader([url],encoding='utf-8')
|
46 |
-
docs = loader.load()
|
47 |
-
|
48 |
-
template="""You are a helpful LinkedIn webscrapper. You are provided with a data , extract the content of the post only.
|
49 |
-
{docs}"""
|
50 |
-
prompt = ChatPromptTemplate.from_template(template)
|
51 |
-
template2="""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.
|
52 |
-
{data}"""
|
53 |
-
chain = prompt | model | StrOutputParser()
|
54 |
-
analysis_prompt = ChatPromptTemplate.from_template(template2)
|
55 |
-
|
56 |
-
composed_chain = {"data": chain} | analysis_prompt | model | StrOutputParser()
|
57 |
-
phrased_post=composed_chain.invoke({"docs":docs})
|
58 |
-
|
59 |
-
data2=extract_data(phrased_post , model)
|
60 |
-
keywords=data2['Keywords'][:3]
|
61 |
-
take_aways=data2['Take Aways'][:3]
|
62 |
-
highlights=data2['Highlights'][:3]
|
63 |
-
return phrased_post,keywords , take_aways, highlights
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
# Below function extract the details such as keywords , Take aways , highlights and questions
|
70 |
-
def extract_data(post_data ,model):
|
71 |
-
keywords = ResponseSchema(name="Keywords",
|
72 |
-
description="These are the keywords extracted from LinkedIn post",type="list")
|
73 |
-
|
74 |
-
Take_aways = ResponseSchema(name="Take Aways",
|
75 |
-
description="These are the take aways extracted from LinkedIn post", type= "list")
|
76 |
-
Highlights=ResponseSchema(name="Highlights",
|
77 |
-
description="These are the highlights extracted from LinkedIn post", type= "list")
|
78 |
-
|
79 |
-
|
80 |
-
response_schema = [
|
81 |
-
keywords,
|
82 |
-
Take_aways,
|
83 |
-
Highlights
|
84 |
-
|
85 |
-
]
|
86 |
-
output_parser = StructuredOutputParser.from_response_schemas(response_schema)
|
87 |
-
format_instructions = output_parser.get_format_instructions()
|
88 |
-
|
89 |
-
template = """
|
90 |
-
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 .
|
91 |
-
From the following text message, extract the following information:
|
92 |
-
|
93 |
-
text message: {content}
|
94 |
-
{format_instructions}
|
95 |
-
"""
|
96 |
-
|
97 |
-
prompt_template = ChatPromptTemplate.from_template(template)
|
98 |
-
messages = prompt_template.format_messages(content=post_data, format_instructions=format_instructions)
|
99 |
-
response = model(messages)
|
100 |
-
output_dict= output_parser.parse(response.content)
|
101 |
-
return output_dict
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|