-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathText_summary.py
More file actions
70 lines (48 loc) · 2.03 KB
/
Text_summary.py
File metadata and controls
70 lines (48 loc) · 2.03 KB
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
import sys
import crewai
from crewai import OpenAICompletion # This is an example; adjust if needed
def summarize_text_with_crewai(text, min_sentences=3, max_sentences=7):
"""
Creates a CrewAI agent with required fields and uses it to summarize the provided text.
This version uses an LLM instance and then calls the agent as a callable.
"""
# Create an LLM instance (adjust parameters if needed)
llm = OpenAICompletion(api_key="YOUR_API_KEY", model="gpt-3.5-turbo")
# Create an agent with the required fields and provide the LLM instance.
agent = crewai.Agent(
api_key="YOUR_API_KEY", # Replace with your actual API key
role="summarizer",
goal="Summarize the provided text into a concise summary of 3 to 7 sentences.",
backstory="I am an AI summarization agent built to condense long texts into a short, informative summary.",
llm=llm
)
# Use the agent as a callable. (Adjust the parameters according to CrewAI's API.)
response = agent("summarize", text=text, min_sentences=min_sentences, max_sentences=max_sentences)
# Expect the response to be a dictionary with a "summary" key.
summary = response.get("summary")
if not summary:
raise ValueError("No summary was returned from CrewAI.")
return summary
def main():
print("Please enter your text (up to 99,999 characters).")
print("When finished, press Enter on an empty line:")
# Read multi-line input until an empty line is entered.
lines = []
while True:
line = input()
if line.strip() == "":
break
lines.append(line)
text = "\n".join(lines)
# Truncate if text exceeds 99,999 characters.
if len(text) > 99999:
text = text[:99999]
try:
summary = summarize_text_with_crewai(text, min_sentences=3, max_sentences=7)
except Exception as e:
print("An error occurred while summarizing the text:", e)
return
print("\nSummary:")
print(summary)
if __name__ == "__main__":
main()