Compare commits

..

No commits in common. "44902513fd759e652b47180fa8e7ada2e7e153c4" and "ae416242e25f02d138fdfd9233554bbad72aed65" have entirely different histories.

41
main.py
View File

@ -20,22 +20,19 @@ 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 = ''' CHATGPT_TEMPLATE = '''Turn this forum post into an two paragraph instagram
Turn this forum post into an two paragraph summary that tells about what a member of our makerspace has made. caption that tells about what a member of our makerspace has made. Add the
Add the hashtags: #makerspace #yyc #maker #diy #calgary and several relevant to the post at the end. hashtags: #makerspace #yyc #maker #diy #calgary and several relevant to the post
Include one sentence explaining that this was made at Calgary Protospace, a makerspace that's non-profit and volunteer run. at the end. Include a sentence explaining that this was made at Calgary
Only mention the member's name once. Protospace, a makerspace that's non-profit and community ran. Only say the
Keep it short, use no more than 1000 characters. member's name once. Use no more than 1000 characters. Write in third person.
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'))
@ -117,8 +114,8 @@ def api_chatgpt(prompt):
data = dict( data = dict(
messages=thread, messages=thread,
model='gpt-3.5-turbo', #'gpt-4-1106-preview', model='gpt-4-1106-preview',
temperature=0.8, temperature=0.5,
user='protogram', user='protogram',
max_tokens=1000, max_tokens=1000,
) )
@ -154,7 +151,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, prev_captions='N/A'): def generate_caption(topic):
title = topic['title'] title = topic['title']
username = topic['post_stream']['posts'][0]['username'] username = topic['post_stream']['posts'][0]['username']
@ -170,7 +167,6 @@ def generate_caption(topic, prev_captions='N/A'):
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))
@ -225,11 +221,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:
pass break
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:
@ -267,13 +263,6 @@ 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',
@ -291,7 +280,7 @@ async def process_topics():
store_data() store_data()
continue continue
caption = generate_caption(topic, prev_captions) caption = generate_caption(topic)
state['caption'] = caption state['caption'] = caption
result = await send_data_to_admin(state) result = await send_data_to_admin(state)