Jobanpreet commited on
Commit
d7b05de
·
verified ·
1 Parent(s): 33f2710

Delete paraphrase_post.py

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