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) 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 CHATGPT_TEMPLATE = '''
caption that tells about what a member of our makerspace has made. Add the Turn this forum post into an two paragraph summary that tells about what a member of our makerspace has made.
hashtags: #makerspace #yyc #maker #diy #calgary and several relevant to the post Add the hashtags: #makerspace #yyc #maker #diy #calgary and several relevant to the post at the end.
at the end. Include a sentence explaining that this was made at Calgary Include one sentence explaining that this was made at Calgary Protospace, a makerspace that's non-profit and volunteer run.
Protospace, a makerspace that's non-profit and community ran. Only say the Only mention the member's name once.
member's name once. Use no more than 1000 characters. Write in third person. Keep it short, use no more than 1000 characters.
Write in third person.
Use neutral sounding phrases.
Title: {} Title: {}
Member: {} Member: {}
Post Body: Post Body:
``` ```
{} {}
```''' ```
'''
try: try:
data = json.load(open('data/data.json')) data = json.load(open('data/data.json'))
@ -114,8 +117,8 @@ def api_chatgpt(prompt):
data = dict( data = dict(
messages=thread, messages=thread,
model='gpt-4-1106-preview', model='gpt-3.5-turbo', #'gpt-4-1106-preview',
temperature=0.5, temperature=0.8,
user='protogram', user='protogram',
max_tokens=1000, 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))) logging.error('Problem with getting member name: {} - {}'.format(e.__class__.__name__, str(e)))
return False return False
def generate_caption(topic): def generate_caption(topic, prev_captions='N/A'):
title = topic['title'] title = topic['title']
username = topic['post_stream']['posts'][0]['username'] 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] 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') 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) prompt = CHATGPT_TEMPLATE.format(title, member, body)
logging.info('Generating caption for: {}\n{}'.format(title, 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']: if data['states'][topic_id]['status'] in ['POSTED', 'ERROR']:
continue continue
except KeyError: except KeyError:
break pass
else: # for loop
return False
return topic_id return topic_id
return False
async def send_data_to_admin(state): async def send_data_to_admin(state):
try: try:
@ -263,6 +267,13 @@ async def process_topics():
logging.info('Next valid topic ID: {}'.format(topic_id)) 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']: if topic_id not in data['states']:
data['states'][topic_id] = dict( data['states'][topic_id] = dict(
status='NEW', status='NEW',
@ -280,7 +291,7 @@ async def process_topics():
store_data() store_data()
continue continue
caption = generate_caption(topic) caption = generate_caption(topic, prev_captions)
state['caption'] = caption state['caption'] = caption
result = await send_data_to_admin(state) result = await send_data_to_admin(state)