-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract_text.py
More file actions
74 lines (59 loc) · 1.77 KB
/
extract_text.py
File metadata and controls
74 lines (59 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import requests
from dotenv import load_dotenv
import os
import json
from bs4 import BeautifulSoup
import cohere
load_dotenv()
token = os.environ.get("TOKEN")
# website_url = "https://www2.gnb.ca/content/gnb/en/gateways/about_nb/geography.html"
# print(token)
website_url = ""
def get_url(url_fetch):
website_url = url_fetch
return website_url
website_url = get_url("")
def get_message(website_url):
url = f'https://api.diffbot.com/v3/article?token={token}&url={website_url}'
headers = {"accept": "application/json"}
response = requests.get(url, headers=headers)
json_response = response.text
# Parse JSON
data = json.loads(json_response)
# print(data)
#Extract url
# print(url_page)
# Extract html of the webpage
html_content = data["objects"][0]["html"]
# print(html_content)
soup = BeautifulSoup(html_content, "html.parser")
tag_name = soup.find_all('p')
message = ""
for tag in tag_name:
message += tag.get_text()
# print(tag.get_text())
return message
# print(message)
api_key = os.getenv("CO_API_KEY")
co = cohere.Client(api_key)
# Keep track of historical responses
chat_history = []
max_turns = 10
def chatboxAPI(message):
# get user input
# message = input("Send the model a message: ")
#generate chat response
response = co.chat(
message=message,
model="command",
temperature=0.3,
chat_history=chat_history
)
# add message and answer to chat history
user_message = {"role": "USER", "text": message}
bot_message = {"role": "CHATBOT", "text": response.text}
chat_history.append(user_message)
chat_history.append(bot_message)
# TODO: change the # of max_turns
# print(response.text)
return response.text