-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
157 lines (133 loc) · 4.96 KB
/
game.py
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import sys
import subprocess
import requests
import openai
from bs4 import BeautifulSoup
import contextlib
import os
# Function to disable scraping output
@contextlib.contextmanager
def disable_scraping_output():
with open(os.devnull, 'w') as devnull:
old_stdout = sys.stdout
sys.stdout = devnull
try:
yield
finally:
sys.stdout = old_stdout
# Function to install a Python package
def install_package(package):
try:
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
except subprocess.CalledProcessError as e:
print(f"Installation failed. Error: {str(e)}")
if e.output is not None:
print(f"Output: {e.output.decode()}")
# Check if 'requests' package is installed, if not, install it
try:
import requests
except ImportError:
print("'requests' package not found. Installing...")
install_package('requests')
import requests
# Check if 'openai' package is installed, if not, install it
try:
import openai
except ImportError:
print("'openai' package not found. Installing...")
install_package('openai')
import openai
# Check if 'beautifulsoup4' package is installed, if not, install it
try:
from bs4 import BeautifulSoup
except ImportError:
print("'beautifulsoup4' package not found. Installing...")
install_package('beautifulsoup4')
from bs4 import BeautifulSoup
# Prompt the user to enter their OpenAI API key
api_key = input("Enter your OpenAI API key: ")
openai.api_key = api_key
# Function to scrape news data from the GB News website
def scrape_gbnews():
url = 'https://www.gbnews.uk/'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
articles = soup.find_all('div', class_='MuiCard-root MuiPaper-root jss491 MuiPaper-elevation1 MuiCard-elevation0 MuiPaper-rounded')
news_data = []
for article in articles:
try:
title = article.find('h2', class_='MuiTypography-root jss452 MuiTypography-h4 MuiTypography-gutterBottom').text.strip()
summary = article.find('p', class_='MuiTypography-root jss460 MuiTypography-body1').text.strip()
news_data.append({
'title': title,
'summary': summary
})
except AttributeError:
# Handle case when desired element is not found
continue
return news_data
# Function to scrape news data from the BBC News website
def scrape_bbcnews():
url = 'https://www.bbc.co.uk/news'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
articles = soup.find_all('div', class_='gs-c-promo')
news_data = []
for article in articles:
try:
title = article.find('h3', class_='gs-c-promo-heading__title').text.strip()
summary = article.find('p', class_='gs-c-promo-summary').text.strip()
news_data.append({
'title': title,
'summary': summary
})
except AttributeError:
# Handle case when desired element is not found
continue
return news_data
# Function to interact with the OpenAI model and generate a response
def get_model_response(prompt):
try:
response = openai.Completion.create(
engine='text-davinci-003',
prompt=prompt,
temperature=0.7,
max_tokens=100,
n=1,
stop=None,
timeout=10
)
return response.choices[0].text.strip()
except openai.OpenAIError as e:
print(f"Error generating AI response: {str(e)}")
return None
# Main game loop
while True:
with disable_scraping_output():
# Scrape news data from GB News
news_data = scrape_gbnews()
# If no news data is available from GB News, scrape from BBC News as a backup source
if not news_data:
news_data = scrape_bbcnews()
# Extract and condense news summary
condensed_summary = "\n".join(article['summary'] for article in news_data if article['summary'])
# Print the condensed news summary
print("\nCondensed News Summary:")
if condensed_summary:
print(f"In the UK, there is\n{condensed_summary}")
else:
print("No news data available.")
# User prompt
user_prompt = input("\nWhat will you do? ")
# Generate AI response
prompt = f"In the UK, there is\n{condensed_summary}\nUser Action: {user_prompt}"
ai_response = get_model_response(prompt)
# Print the AI-generated response
if ai_response:
print("\nAI Response:")
print(ai_response)
# Ask the user to play again or exit
play_again = input("\nDo you want to play again? (yes/no): ")
if play_again.lower() != 'yes':
break
print("\nThank you for playing the game!")