-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path02_how_to_create_stories_for_kids.py
58 lines (44 loc) · 1.37 KB
/
02_how_to_create_stories_for_kids.py
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
"""
Author: Rajib Deb
Date: 02/10/2024
Description: This is the driver program that starts the MasterChef crew.
"""
# from grandma_story.grandma_stories import GrandmaCrew
#
# if __name__ == "__main__":
# idea = "Unity is strength"
# crew = GrandmaCrew(idea=idea)
# result = crew.kickoff()
# print(result)
# Author: Rajib Deb
# A simple example showing how langgraph works
from langgraph.graph import MessageGraph
from openai import OpenAI
from grandma_story.grandma_stories import GrandmaCrew
def craft_story(state):
messages = state[-1]
crew = GrandmaCrew(idea=messages)
response = crew.kickoff()
return response
def convert_to_speech(state):
client = OpenAI()
messages = state[-1]
with client.audio.speech.with_streaming_response.create(
model="tts-1",
voice="alloy",
input=messages,
) as response:
response.stream_to_file("story.mp3")
return response
workflow = MessageGraph()
# Define the nodes we will cycle between
workflow.add_node("craft_story", craft_story)
workflow.add_node("convert_to_speech", convert_to_speech)
workflow.set_entry_point("craft_story")
workflow.add_edge("craft_story", "convert_to_speech")
workflow.set_finish_point("convert_to_speech")
app = workflow.compile()
story_idea = "Unity is strength"
response = app.invoke(story_idea)
print("-----")
print(response[-1])