File size: 4,654 Bytes
db60f58
 
 
 
08f932d
27ab1be
db60f58
e8edb00
db60f58
27ab1be
e8edb00
27ab1be
 
 
 
b79cefc
27ab1be
 
 
db60f58
27ab1be
 
e8edb00
27ab1be
 
e8edb00
27ab1be
 
e8edb00
27ab1be
 
e8edb00
27ab1be
 
 
 
db60f58
 
e8edb00
db60f58
 
 
 
 
4983616
db60f58
 
 
e8edb00
db60f58
 
 
27ab1be
db60f58
27ab1be
 
 
 
 
 
 
 
 
 
 
db60f58
27ab1be
 
 
 
 
db60f58
27ab1be
db60f58
e8edb00
27ab1be
db60f58
27ab1be
 
 
 
 
 
 
e8edb00
 
27ab1be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e8edb00
 
 
 
 
db60f58
 
 
 
 
 
 
 
 
 
 
e8edb00
db60f58
 
 
 
 
 
 
e8edb00
db60f58
 
 
 
e8edb00
db60f58
 
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from haystack import Pipeline
from haystack.components.builders import PromptBuilder
from haystack.components.generators import HuggingFaceAPIGenerator
from haystack.utils import Secret
from pathlib import Path  
from haystack.core.errors import PipelineDrawingError

# Template for generating detailed module content
content_template = """
You are an instructional designer. Create a detailed, educational Markdown module titled "{{name}}" that takes {{duration}} to read.

- Use **200 WPM** for general topics or **150 WPM** for technical ones.
- Target word count: {{word_count}} ±5%.
- Output valid **Markdown only** using the structure below.
- Do **not** include code fences, extra commentary, or repeat "### End".

### Structure:
# {{name}}
**Estimated Reading Time: {{duration}}**

## Introduction
- Brief intro to the topic.

## Key Concepts
{{formatted_subtopics}}

## Explanation
- Detailed explanation using paragraphs, bullets, and examples.

## Examples
- Include at least one relevant example.

## Summary
- Key takeaways.

### End
"""

# Create pipeline components
builder = PromptBuilder(template=content_template)
generator = HuggingFaceAPIGenerator(
    api_type="serverless_inference_api",
    api_params={"model": "meta-llama/Llama-3.2-3B-Instruct"},
    token=Secret.from_env_var("HF_API_TOKEN"),
    generation_kwargs={"max_new_tokens": 5000, "stop": ["<stop>"]},
)

pipeline = Pipeline()
pipeline.add_component("prompt_builder", builder)
pipeline.add_component("llm", generator)
pipeline.connect("prompt_builder", "llm")

# Save the pipeline as a YAML file and attempt to draw a diagram
with open("./pipelines/generate_module_content.yml", "w") as file:
    pipeline.dump(file)

try:
    pipeline.draw(path=Path("generate_module_content_pipeline.png"))
except PipelineDrawingError:
    print("⚠️ Mermaid diagram could not be generated (likely offline). Skipping pipeline drawing.")
except Exception as e:
    print(f"⚠️ Unexpected error while drawing pipeline: {e}")

# Ensure output directory exists
Path("./modules").mkdir(parents=True, exist_ok=True)

# Detect technical modules
def is_technical_topic(module_name, subtopics):
    keywords = ["API", "database", "code", "technical", "machine learning", "AI", "infrastructure", "engineering", "system", "security", "algorithm"]
    combined_text = f"{module_name} {' '.join(subtopics)}".lower()
    return any(keyword in combined_text for keyword in keywords)

# Generate content for all modules in the outline
def generate_content_for_outline(outline):
    content_results = {}

    for module in outline["modules"]:
        is_technical = is_technical_topic(module['name'], module['subtopics'])
        reading_speed = 150 if is_technical else 200
        minutes = int(module['duration'].split()[0])
        word_count = int(minutes * reading_speed)
        formatted_subtopics = "\n".join([f"- {sub}" for sub in module["subtopics"]])

        print(f"Generating '{module['name']}' at {reading_speed} WPM → {word_count} words")

        try:
            result = pipeline.run({
                "prompt_builder": {
                    "name": module['name'],
                    "duration": module['duration'],
                    "word_count": word_count,
                    "formatted_subtopics": formatted_subtopics,
                }
            })

            markdown_output = result["llm"]["replies"][0]
            content_results[module["name"]] = markdown_output

            # Save to .md file
            filename = module["name"].replace(" ", "_").lower()
            with open(f"./modules/{filename}.md", "w", encoding="utf-8") as f:
                f.write(markdown_output)

        except Exception as e:
            print(f"Error generating content for module '{module['name']}': {e}")
            content_results[module["name"]] = {"error": str(e)}

    return content_results

# Example outline to test the script
updated_outline = {
    "title": "Customer Service Essentials",
    "modules": [
        {
            "name": "Introduction to Customer Service",
            "duration": "10 minutes",
            "subtopics": [
                "Definition of customer service",
                "Importance of customer service",
            ],
        },
        {
            "name": "Handling Customer Complaints",
            "duration": "20 minutes",
            "subtopics": [
                "Types of customer complaints",
                "How to handle customer complaints",
            ],
        },
    ],
}

# Generate content for the updated outline
module_content = generate_content_for_outline(updated_outline)
print(module_content)