Compare commits

..

2 Commits

41
main.py
View File

@ -20,19 +20,22 @@ TIMEZONE_CALGARY = pytz.timezone('America/Edmonton')
bot = TelegramClient('data/bot', secrets.API_ID, secrets.API_HASH).start(bot_token=secrets.API_TOKEN)
CHATGPT_TEMPLATE = '''Turn this forum post into an two paragraph instagram
caption that tells about what a member of our makerspace has made. Add the
hashtags: #makerspace #yyc #maker #diy #calgary and several relevant to the post
at the end. Include a sentence explaining that this was made at Calgary
Protospace, a makerspace that's non-profit and community ran. Only say the
member's name once. Use no more than 1000 characters. Write in third person.
CHATGPT_TEMPLATE = '''
Turn this forum post into an two paragraph summary that tells about what a member of our makerspace has made.
Add the hashtags: #makerspace #yyc #maker #diy #calgary and several relevant to the post at the end.
Include one sentence explaining that this was made at Calgary Protospace, a makerspace that's non-profit and volunteer run.
Only mention the member's name once.
Keep it short, use no more than 1000 characters.
Write in third person.
Use neutral sounding phrases.
Title: {}
Member: {}
Post Body:
```
{}
```'''
```
'''
try:
data = json.load(open('data/data.json'))
@ -114,8 +117,8 @@ def api_chatgpt(prompt):
data = dict(
messages=thread,
model='gpt-4-1106-preview',
temperature=0.5,
model='gpt-3.5-turbo', #'gpt-4-1106-preview',
temperature=0.8,
user='protogram',
max_tokens=1000,
)
@ -151,7 +154,7 @@ def get_portal_name_from_discourse(username):
logging.error('Problem with getting member name: {} - {}'.format(e.__class__.__name__, str(e)))
return False
def generate_caption(topic):
def generate_caption(topic, prev_captions='N/A'):
title = topic['title']
username = topic['post_stream']['posts'][0]['username']
@ -167,6 +170,7 @@ def generate_caption(topic):
filtered_lines = [line for line in lines if 'KB' not in line and 'MB' not in line]
body = '\n'.join(filtered_lines).replace('\n\n\n', '\n\n')
#prompt = CHATGPT_TEMPLATE.format(title, member, body, prev_captions)
prompt = CHATGPT_TEMPLATE.format(title, member, body)
logging.info('Generating caption for: {}\n{}'.format(title, body))
@ -221,11 +225,11 @@ def find_next_valid_topic_id(topic_ids):
if data['states'][topic_id]['status'] in ['POSTED', 'ERROR']:
continue
except KeyError:
break
else: # for loop
return False
pass
return topic_id
return topic_id
return False
async def send_data_to_admin(state):
try:
@ -263,6 +267,13 @@ async def process_topics():
logging.info('Next valid topic ID: {}'.format(topic_id))
try:
prev_states = list(data['states'].values())[-5:]
prev_captions = '\n\n'.join([x['caption'] for x in prev_states])
except:
prev_captions = 'N/A'
logging.info('Previous captions:\n{}\n\n'.format(prev_captions))
if topic_id not in data['states']:
data['states'][topic_id] = dict(
status='NEW',
@ -280,7 +291,7 @@ async def process_topics():
store_data()
continue
caption = generate_caption(topic)
caption = generate_caption(topic, prev_captions)
state['caption'] = caption
result = await send_data_to_admin(state)