File size: 3,768 Bytes
53ce6a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d090b5
53ce6a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d090b5
53ce6a2
 
 
 
 
 
 
 
 
 
 
 
 
 
e4bc372
53ce6a2
 
 
 
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import os
from crewai import Agent, Task, Crew
from crewai_tools import PDFSearchTool
import openai

# Ensure your OpenAI API key and model settings are set
openai.api_key = os.getenv("OPENAI_API_KEY")

# Initialize the PDFSearchTool without a preset PDF
pdf_tool = PDFSearchTool()

# Agent 1: PDF Extractor Agent – extracts text from the specified page range.
pdf_extractor = Agent(
    role="PDF Extractor",
    goal="Extract the text content from the specified page range of the provided PDF.",
    backstory="You specialize in reading and processing PDFs to return useful study content.",
    verbose=True,
    tools=[pdf_tool]
)

# Agent 2: Flashcard Generator Agent – generates flashcards based on extracted text.
flashcard_generator = Agent(
    role="Flashcard Generator",
    goal="Generate flashcards with questions and answers based on the extracted text.",
    backstory="You create study flashcards to test understanding, ensuring each flashcard has a clear question and answer.",
    verbose=True
)

# Task 1: Extraction Task – Extract text from pages {page_range} of the PDF.
extraction_task = Task(
    description=(
        "Extract and return the text content from the PDF located at {pdf_file_path}."
    ),
    expected_output="A string containing the extracted text from the specified pages.",
    agent=pdf_extractor
)

# Task 2: Flashcard Generation Task – Generate {flashcard_count} flashcards from the extracted text.
flashcard_task = Task(
    description=(
        "Based on the following extracted text"
        "Generate {flashcard_count} flashcards. Each flashcard should have a question and a corresponding answer. "
        "Return the flashcards as a JSON list of objects with keys 'question' and 'answer'."
    ),
    expected_output="A JSON list of flashcards.",
    output_file="flashcards.json",
    agent=flashcard_generator,
    context=[extraction_task]
)


# In crewai_flashcard.py

# # Task 1: Extraction Task – Extract text from pages {page_range} of the PDF.
# extraction_task = Task(
#     description=(
#         "Extract and return the text content from pages page_range of the PDF located at pdf_file_path."
#     ),
#     expected_output="A string containing the extracted text from the specified pages.",
#     agent=pdf_extractor
# )

# # Task 2: Flashcard Generation Task – Generate {flashcard_count} flashcards from the extracted text.
# flashcard_task = Task(
#     description=(
#         "Based on the following extracted text:"
#         "Generate {flashcard_count} flashcards. Each flashcard should have a question and a corresponding answer. "
#         "Return the flashcards as a JSON list of objects with keys 'question' and 'answer'."
#     ),
#     expected_output="A JSON list of flashcards.",
#     agent=flashcard_generator,
#     context=[extraction_task]
# )




# Assemble the Crew
flashcard_crew = Crew(
    agents=[pdf_extractor, flashcard_generator],
    tasks=[extraction_task, flashcard_task],
    verbose=True
)

def generate_flashcards( pdf_file_path: str,flashcard_count: int):
  #generate_flashcards(pdf_file_path: str, page_range: str, flashcard_count: int):
    """
    Run the CrewAI system to extract text from specified pages and generate flashcards.
    
    Args:
        pdf_file_path (str): Path to the PDF file.
        page_range (str): Page range to extract text from (e.g., "1-5").
        flashcard_count (int): Number of flashcards to generate.
        
    Returns:
        str: JSON string containing the flashcards.
    """
    inputs = {
        "pdf_file_path": pdf_file_path,
        #"page_range": page_range,
        "flashcard_count": flashcard_count
    }
    results = flashcard_crew.kickoff(inputs=inputs)
    return results.raw #[flashcard_task.id]