-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAgentic_Workflow.py
291 lines (236 loc) · 10.5 KB
/
Agentic_Workflow.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import os
import json
from PIL import Image
import pyttsx3
import io
import hashlib
import streamlit as st
import cloudinary
import cloudinary.uploader
import cloudinary.api
from langgraph import Stategraph
from langchain.vectorstores import Chroma
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.schema import Document
from langchain.chat_models import ChatOpenAI
from langchain.prompts import ChatPromptTemplate
from langchain.chains import LLMChain
# Step 1: Load and preprocess the VIST dataset
def preprocess_vist(json_file, image_dir):
"""
Load and preprocess the VIST dataset to create (image, text) pairs.
Args:
json_file: Path to the VIST JSON file.
image_dir: Path to the directory containing images.
Returns:
A list of tuples [(image_path, text), ...].
"""
data = json.load(open(json_file))
image_text_pairs = []
for story in data["annotations"]:
photo_ids = story["photo_ids"] # IDs of images in the story
sentences = story["story"] # Corresponding sentences for each photo
for photo_id, sentence in zip(photo_ids, sentences):
image_path = os.path.join(image_dir, f"{photo_id}.jpg")
if os.path.exists(image_path):
image_text_pairs.append((image_path, sentence))
return image_text_pairs
# Step 3: Store embeddings in LangChain's Chroma vectorstore
def store_embeddings_in_chroma(image_text_pairs, persist_directory):
"""
Store embeddings in LangChain's Chroma vectorstore.
Args:
embeddings: List of (embedding, metadata) pairs.
persist_directory: Path to store the Chroma vectorstore.
"""
meta_data = []
for image_path, text in image_text_pairs:
metadata = {"image_path": image_path, "text": text}
meta_data.append((metadata))
documents = [
Document(page_content=metadata["text"], metadata={"image_path": metadata["image_path"]})
for metadata in meta_data
]
hf_embeddings = HuggingFaceEmbeddings(model_name="openai/clip-vit-base-patch32")
# Create Chroma vectorstore
vectorstore = Chroma.from_documents(
documents=documents,
embedding=hf_embeddings,
persist_directory=persist_directory
)
vectorstore.persist()
# Function to hash an image
def hash_image(image):
"""Generate a hash for the image to detect changes."""
img_byte_arr = io.BytesIO()
image.save(img_byte_arr, format="PNG")
return hashlib.md5(img_byte_arr.getvalue()).hexdigest()
# Function to speak text
def speak_text(text):
engine = pyttsx3.init()
engine.say(text)
engine.runAndWait()
# Callback function for button press
def handle_speak(story):
bot_message = story[0]["content"]
st.session_state.speak_triggered = False
speak_text(bot_message)
# Function to upload an image and return a URL or encoded string
def get_image_url(image_path):
# Configure Cloudinary with your credentials
cloudinary.config(
cloud_name='cloud_name', # Replace with your Cloudinary cloud name
api_key='api_key', # Replace with your Cloudinary API key
api_secret='secret_key' # Replace with your Cloudinary API secret
)
# Upload the image to Cloudinary
upload_result = cloudinary.uploader.upload(image_path)
# Get the public URL of the uploaded image
image_url = upload_result.get('secure_url')
if image_url:
print(f"Image uploaded successfully! Public URL: {image_url}")
return image_url
else:
print("Failed to upload image.")
return None
def agentic_workflow(image_url):
# Initialize Stategraph
workflow = Stategraph()
# Step 1: Define nodes
workflow.add_node("Start", lambda: print("Workflow started"))
# LLM Node
story_llm = ChatOpenAI(model="OpenGVLab/InternVL2-40B", temperature=0.7, max_tokens=512, timeout=None, max_retries=2)
def llm_node(context):
print("LLM Node executed")
return context
workflow.add_node("LLM", llm_node)
# Tool Node
chroma_vectorstore = Chroma(persist_directory="path_to_chroma_vectorstore")
hf_embeddings = HuggingFaceEmbeddings(model_name="openai/clip-vit-base-patch32")
def tool_node(context):
print("Tool Node executed")
# Generate embedding for the user-uploaded image
image_embedding = hf_embeddings.embed_documents([context["image_path"]])
# Search for relevant documents in the vectorstore
relevant_docs = chroma_vectorstore.similarity_search_by_vector(image_embedding[0], k=2)
if relevant_docs:
context["retrieved_docs"] = relevant_docs
return "Retriever"
else:
return "Generator"
workflow.add_node("Tools", tool_node)
# Retriever Node
def retriever_node(context):
print("Retriever Node executed")
# Store retrieved embeddings for story generation
context["retrieved_embeddings"] = [doc.page_content for doc in context["retrieved_docs"]]
return context
workflow.add_node("Retriever", retriever_node)
# Generator Node
def generator_node(context):
print("Generator Node executed")
"""Generate a narrative tying together the image and related story texts (if any)."""
# Extract textual information from retrieved embeddings
if "retrieved_embeddings" in context:
retrieved_texts = ";".join([embedding["text"] for embedding in context["retrieved_embeddings"]])
prompt_template = """
You are a creative storyteller. Given the following image and related story texts (might be relevant and helpful for you to generate the desired output), create an engaging and coherent narrative:
{image}
{external_relevant_context}
Use external relevant context (separated by a semicolon ";") if necessary to enhance the story, ensuring it remains engaging and meaningful.
"""
prompt = ChatPromptTemplate.from_template(prompt_template).format(
image=image_url,
external_relevant_context=retrieved_texts
)
else:
prompt_template = """
Given the following image, use your imagination and reasoning abilities to craft an engaging and immersive story.
Think about the environment, the potential characters, the emotions that might arise from this scene, and any
unexpected events that could unfold. Be creative, and bring the story to life by describing the atmosphere,
characters, and events in a detailed and captivating way.
{image}
"""
prompt = ChatPromptTemplate.from_template(prompt_template).format(
image=image_url)
chain = LLMChain(llm=story_llm, prompt=prompt)
story = chain.run()
context["story"] = story
return context
workflow.add_node("Generator", generator_node)
# End Node
def end_node(context):
st.success("Story generated successfully!")
st.session_state.speak_triggered = True
st.text_area("Generated Story:", context["story"])
st.session_state.story_history.append(
{"role": "assistant", "content": context["story"]}
)
st.button(
"Speak",
on_click=handle_speak,
args=(st.session_state.story_history,)
)
return context
workflow.add_node("End", end_node)
# Step 2: Add edges
workflow.add_edge("Start", "LLM")
workflow.add_edge("LLM", "Tools")
workflow.add_conditional_edge("Tools", "Retriever", condition=lambda context: "retrieved_docs" in context and context["retrieved_docs"])
workflow.add_conditional_edge("Tools", "Generator", condition=lambda context: "retrieved_docs" not in context)
workflow.add_edge("Retriever", "Generator")
workflow.add_edge("Generator", "End")
return workflow
# Streamlit App
def run_streamlit_app():
st.title("Visual Storytelling Generator")
if "story_history" not in st.session_state:
st.session_state.story_history = []
if "image_hash" not in st.session_state:
st.session_state.image_hash = None
if "speak_triggered" not in st.session_state:
st.session_state.speak_triggered = False # To track which message's Speak button was pressed
# Upload image files
uploaded_image = st.file_uploader("Upload images", accept_multiple_files=True, type=["png", "jpg", "jpeg"])
if uploaded_image:
image = Image.open(uploaded_image)
st.image(image, caption="Uploaded Image", use_container_width=True)
# Save the image to a file
image.save("user_uploaded_photo.png")
# Get image URL or encode image
image_url = get_image_url("user_uploaded_photo.png")
# Check if the image has changed
current_image_hash = hash_image(image)
if st.session_state.image_hash is None:
st.session_state.image_hash = current_image_hash
elif st.session_state.image_hash != current_image_hash:
st.session_state.story_history = [] # Reset story history
st.session_state.image_hash = current_image_hash
st.warning("Image has changed!")
# Run Agentic Workflow
with st.spinner("Running the Agentic AI Workflow to Generate the Story..."):
context = {
"image_path": image_url
}
workflow = agentic_workflow(image_url)
workflow.run(context)
# Display Story
if not st.session_state.speak_triggered:
st.text_area("Generated Story:", st.session_state.story_history[0]["content"])
st.button(
"Speak",
on_click=handle_speak,
args=(st.session_state.story_history,)
)
if __name__ == "__main__":
# Paths
vist_json = "path_to_vist_annotations.json"
image_dir = "path_to_vist_images"
persist_dir = "path_to_chroma_vectorstore"
with st.spinner("Loading the Dataset, Generating and Saving the Embeddings..."):
# Preprocess the dataset
image_text_pairs = preprocess_vist(vist_json, image_dir)
# Store embeddings in Chroma vectorstore
store_embeddings_in_chroma(image_text_pairs, persist_dir)
st.success("Embeddings stored in Chroma vectorstore successfully!")
run_streamlit_app()