-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorchestrator.py
More file actions
150 lines (130 loc) · 6.21 KB
/
orchestrator.py
File metadata and controls
150 lines (130 loc) · 6.21 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import json
import os
from groq import Groq
import subprocess
import autogen as ag
class OrchestratorAgent(ag.Agent):
def __init__(self):
super().__init__()
# Initialize the Groq client with your API key
self.client = Groq(api_key="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")
def process_story_with_groq(self, story):
prompt = (
#Include a narrator - foremost
f"Analyze the following children's story with the steps below, and return only the requested information with no added commentary or explanations. It is crucial that you do not add any commentary, explanations, or additional information. Just specifically add these headings like **Major_Characters**, **Scenes**, **Character_Dialogues** and **Background_Scenery** wherever they get start. IT IS EXTREMELY CRUCIAL FOR THE HEADNGS TO BE EXACTLY THIS!!!\n\n"
f"1. Identify all major characters. For each major character, provide a name and brief description in this format: 'Major_Character_name': 'description'.\n\n"
f"2. Divide the story into short scenes, providing a brief description of each.\n\n"
f"3. Extract all (major and side) character dialogues, prefixed with ONLY the (major and side) character's name. If there are no dialogues, generate suitable ones for each (major and side) character, prefixed with ONLY the (major and side) character's name.\n\n"
f"4. For each scene, describe the background scenery. Do not mention any characters or scene names—only the setting details. I just want the background details. Strictly dont mention any story character, human-beings, animals, or any creatures of such sort in the background details and scenary.\n\n"
f"**Story:** {story}"
)
completion = self.client.chat.completions.create(
model="llama-3.2-90b-vision-preview",
messages=[
{
"role": "user",
"content": prompt
}
],
temperature=1,
max_tokens=1024,
top_p=1,
stream=True,
stop=None,
)
response_text = ""
for chunk in completion:
response_text += chunk.choices[0].delta.content or ""
return response_text.strip()
def save_response_to_file(self, response_text, filename):
with open(filename, "w") as f:
f.write(response_text)
def parse_and_generate_json_files(self):
# Read the groq_api_response.txt file
with open('groq_api_response.txt', 'r') as file:
lines = file.readlines()
# Initialize data structures
major_characters = {}
background_scenery = []
dialogues = {}
# Flags to identify sections
in_major_characters = False
in_scenes = False
in_character_dialogues = False
in_background_scenery = False
# Process the lines to extract information
for line in lines:
line = line.strip()
if line == "**Major_Characters**":
in_major_characters = True
in_scenes = False
in_character_dialogues = False
in_background_scenery = False
elif line == "**Scenes**":
in_major_characters = False
in_scenes = True
in_character_dialogues = False
in_background_scenery = False
elif line == "**Character_Dialogues**":
in_major_characters = False
in_scenes = False
in_character_dialogues = True
in_background_scenery = False
elif line == "**Background_Scenery**":
in_major_characters = False
in_scenes = False
in_character_dialogues = False
in_background_scenery = True
elif in_major_characters:
if line:
parts = line.split(":", 1)
character_name = parts[0].strip()
description = parts[1].strip()
major_characters[character_name] = description
elif in_character_dialogues:
if line:
parts = line.split(":", 1)
character_name = parts[0].strip()
dialogue = parts[1].strip()
if character_name in dialogues:
dialogues[character_name].append(dialogue)
else:
dialogues[character_name] = [dialogue]
elif in_background_scenery:
if line:
background_scenery.append(line)
# Create the image_agent_data.json file
image_agent_data = {
"Major Characters": major_characters,
"Background Scenery": background_scenery
}
with open('image_agent_data.json', 'w') as file:
json.dump(image_agent_data, file, indent=4)
# Create the audio_agent_data.json file
audio_agent_data = {
"dialogues": dialogues
}
with open('audio_agent_data.json', 'w') as file:
json.dump(audio_agent_data, file, indent=4)
def process_story(self, story):
response_text = self.process_story_with_groq(story)
print(f"Groq API Response: {response_text}") # Debugging: Print the response from Groq API
# Save the response to a text file
self.save_response_to_file(response_text, "groq_api_response.txt")
# Parse the text file and generate JSON files
self.parse_and_generate_json_files()
if __name__ == "__main__":
orchestrator = OrchestratorAgent()
story = input("Please enter the children's story you want to process: ")
orchestrator.process_story(story)
# Ensure the working directory is set to the directory containing the script
script_dir = os.path.dirname(os.path.abspath(__file__))
result = subprocess.run(
["python", "image_agent.py"],
cwd=script_dir,
capture_output=True,
text=True
)
# Print the output and error (if any) from the subprocess
print("STDOUT:", result.stdout)
print("STDERR:", result.stderr)