feat: add new examples from pocketflow-academy

This commit is contained in:
Alan ALves
2025-03-19 10:31:04 -03:00
parent 84720ceebd
commit 557a14f695
129 changed files with 13455 additions and 0 deletions
+70
View File
@@ -0,0 +1,70 @@
# PocketFlow Text Converter
A practical example demonstrating how to use PocketFlow to create an interactive text converter. This example showcases important concepts like data flow between nodes, user choice-based branching, and state management using the shared store.
## Features
- Convert text to UPPERCASE
- Convert text to lowercase
- Reverse text
- Remove extra spaces
- Interactive command-line interface
- Continuous flow with option to process multiple texts
## Project Structure
```
.
├── flow.py # Nodes and flow implementation
├── main.py # Application entry point
└── README.md # Documentation
```
## Implementation Details
1. **TextInput Node**:
- `prep()`: Gets text input from user
- `post()`: Shows options menu and returns action based on choice
2. **TextTransform Node**:
- `prep()`: Gets text and choice from shared store
- `exec()`: Applies the chosen transformation
- `post()`: Shows result and asks if continue
3. **Flow Structure**:
- Input → Transform → (loop back to Input or exit)
- Demonstrates branching based on actions ("transform", "input", "exit")
## How to Run
1. Create a virtual environment:
```bash
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
```
2. Install dependencies:
```bash
pip install -r requirements.txt
```
3. Run the example:
```bash
python main.py
```
## What You'll Learn
This example demonstrates several important PocketFlow concepts:
- **Node Architecture**: How to structure logic using prep/exec/post pattern
- **Flow Control**: How to use actions to control flow between nodes
- **Shared Store**: How to share data between nodes
- **Interactivity**: How to create interactive flows with user input
- **Branching**: How to implement different paths based on choices
## Additional Resources
- [PocketFlow Documentation](https://the-pocket.github.io/PocketFlow/)
- [Flow Guide](https://the-pocket.github.io/PocketFlow/flow.html)
- [Node Guide](https://the-pocket.github.io/PocketFlow/node.html)
+67
View File
@@ -0,0 +1,67 @@
from pocketflow import Node, Flow
class TextInput(Node):
def prep(self, shared):
"""Get text input from user."""
if "text" not in shared:
text = input("\nEnter text to convert: ")
shared["text"] = text
return shared["text"]
def post(self, shared, prep_res, exec_res):
print("\nChoose transformation:")
print("1. Convert to UPPERCASE")
print("2. Convert to lowercase")
print("3. Reverse text")
print("4. Remove extra spaces")
print("5. Exit")
choice = input("\nYour choice (1-5): ")
if choice == "5":
return "exit"
shared["choice"] = choice
return "transform"
class TextTransform(Node):
def prep(self, shared):
return shared["text"], shared["choice"]
def exec(self, inputs):
text, choice = inputs
if choice == "1":
return text.upper()
elif choice == "2":
return text.lower()
elif choice == "3":
return text[::-1]
elif choice == "4":
return " ".join(text.split())
else:
return "Invalid option!"
def post(self, shared, prep_res, exec_res):
print("\nResult:", exec_res)
if input("\nConvert another text? (y/n): ").lower() == 'y':
shared.pop("text", None) # Remove previous text
return "input"
return "exit"
class EndNode(Node):
pass
# Create nodes
text_input = TextInput()
text_transform = TextTransform()
end_node = EndNode()
# Connect nodes
text_input - "transform" >> text_transform
text_transform - "input" >> text_input
text_transform - "exit" >> end_node
# Create flow
flow = Flow(start=text_input)
+16
View File
@@ -0,0 +1,16 @@
from flow import flow
def main():
print("\nWelcome to Text Converter!")
print("=========================")
# Initialize shared store
shared = {}
# Run the flow
flow.run(shared)
print("\nThank you for using Text Converter!")
if __name__ == "__main__":
main()
@@ -0,0 +1 @@
pocketflow>=0.1.0