JustusI commited on
Commit
e4f1e05
·
verified ·
1 Parent(s): f42284a

Create crewai_flashcard.py

Browse files
Files changed (1) hide show
  1. crewai_flashcard.py +94 -0
crewai_flashcard.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # crewai_flashcard.py
2
+
3
+ import os
4
+ from crewai import Agent, Task, Crew
5
+ from crewai_tools import PDFSearchTool
6
+ import openai
7
+ from ast import literal_eval
8
+
9
+ # Set your OpenAI model
10
+ #os.environ["OPENAI_MODEL_NAME"] = 'gpt-4o' # or use 'gpt-4o-mini'
11
+ openai.api_key = os.getenv("OPENAI_API_KEY")
12
+
13
+ # Initialize the PDFSearchTool (without a preset PDF)
14
+ pdf_tool = PDFSearchTool()
15
+
16
+ # Agent 1: PDF Extractor Agent – extracts full text from the PDF.
17
+ pdf_extractor = Agent(
18
+ role="PDF Extractor",
19
+ goal="Extract the full text content from the provided PDF.",
20
+ backstory="You specialize in reading and processing PDFs to return useful study content.",
21
+ verbose=True,
22
+ tools=[pdf_tool]
23
+ )
24
+
25
+ # Agent 2: Flashcard Generator Agent – generates flashcards based on the extracted text.
26
+ flashcard_generator = Agent(
27
+ role="Flashcard Generator",
28
+ goal="Generate flashcards with questions and answers based on the extracted text.",
29
+ backstory="You create study flashcards to test understanding, ensuring each flashcard has a clear question and answer.",
30
+ verbose=True
31
+ )
32
+
33
+ # Task 1: Extraction Task – Extract full text from the PDF.
34
+ extraction_task = Task(
35
+ description=(
36
+ "Extract and return the full text content from the PDF located at {pdf_file_path}."
37
+ ),
38
+ expected_output="A string containing the extracted text from the PDF.",
39
+ agent=pdf_extractor
40
+ )
41
+
42
+ # Task 2: Flashcard Generation Task – Generate {flashcard_count} flashcards.
43
+ flashcard_task = Task(
44
+ description=(
45
+ "Based on the following extracted text:"
46
+ "Generate {flashcard_count} flashcards. Each flashcard should have a question and a corresponding answer. "
47
+ "Return the flashcards as a Python dictionary where each key is a flashcard question and its corresponding value is the answer."
48
+ ),
49
+ expected_output="A Python dictionary of flashcards.",
50
+ output_file="flashcards.json",
51
+ agent=flashcard_generator,
52
+ context=[extraction_task]
53
+ )
54
+
55
+ # Assemble the Crew
56
+ flashcard_crew = Crew(
57
+ agents=[pdf_extractor, flashcard_generator],
58
+ tasks=[extraction_task, flashcard_task],
59
+ verbose=True
60
+ )
61
+
62
+ def generate_flashcards(pdf_file_path: str, flashcard_count: int) -> str:
63
+ """
64
+ Runs the CrewAI flashcard generator to produce flashcards from the given PDF.
65
+
66
+ Args:
67
+ pdf_file_path (str): Path to the PDF file.
68
+ flashcard_count (int): Number of flashcards to generate.
69
+
70
+ Returns:
71
+ str: A string representation of a Python dictionary containing the flashcards.
72
+ """
73
+ inputs = {
74
+ "pdf_file_path": pdf_file_path,
75
+ "flashcard_count": flashcard_count
76
+ }
77
+ results = flashcard_crew.kickoff(inputs=inputs)
78
+ # Return the raw output (a string) from the flashcard task.
79
+ output = results.raw.strip()
80
+ return output
81
+
82
+ if __name__ == "__main__":
83
+ # For testing purposes
84
+ pdf_path = "sample.pdf" # Replace with a valid PDF file path
85
+ flashcard_count = 3
86
+ flashcards_str = generate_flashcards(pdf_path, flashcard_count)
87
+ try:
88
+ flashcards = literal_eval(flashcards_str)
89
+ print("Generated Flashcards:")
90
+ for question, answer in flashcards.items():
91
+ print("Q:", question)
92
+ print("A:", answer)
93
+ except Exception as e:
94
+ print("Error parsing flashcards:", e)