-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsummarization.py
69 lines (56 loc) · 2.54 KB
/
summarization.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
import os
import google.generativeai as genai
from datetime import datetime as dt
import discord
def get_messages(guild_id):
"""
Get all messages from a specific guild.
Args:
guild_id: The unique identifier for the guild.
Returns:
A list of all messages from the guild.
"""
messages = []
try:
for file in os.listdir(f'message_logs/{guild_id}_message_logs'):
with open(f'message_logs/{guild_id}_message_logs/{file}', 'r', encoding='utf-8') as f:
messages.extend(f.readlines())
except Exception as e:
print(f"An error occurred while reading messages from {guild_id}: {e}")
print(os.getcwd())
return messages
def summarize_messages(guild_id, messages, role):
"""
Summarizes the messages from a guild using the Hugging Face pipeline.
Args:
guild_id: The unique identifier for the guild.
messages: A list of messages from a guild.
Returns:
A summary of the messages.
"""
genai.configure(api_key=os.getenv('GOOGLE_API_KEY'))
model = genai.GenerativeModel('gemini-1.5-flash')
prompt = f"""
<s>[INST] I have a long string of Discord chat logs from an entire month. I need a summary of all the events that occured as if a blog writer is writing about a group of people as a narrator.
Instructions:
* Give the episode a title that reflects the main themes of the month.
* Present the key events, discussions, and announcements in a conversational tone as if the writer is talking to the reader.
* Maintain an engaging and informative tone.
* Focus on the most significant topics and avoid unnecessary details.
* Include a closing statement that summarizes the main takeaways from the month.
* The summary should contain 1700-1800 characters including punctuation. The character count should not exceed 1800 characters no matter what.
* Don't leave too much space between sentences and paragraphs. Don't use too many line breaks or too much punctuation.
* Try to use nicknames whenever possible to make the summary more engaging and to reduce the word count.
Chat Logs:
{messages}
[/INST]
"""
while(True):
response = model.generate_content(prompt)
if len(f"{role.mention} {response.text}") <= 2000:
break
# Export to txt file
now = dt.now()
with open(f'summarizations/{guild_id}_{now.year}_{now.month}.txt', 'a', encoding='utf-8') as f:
f.write(response.text)
return response.text