ShortsAI / find_steps.py
gamora's picture
files
dde1cc7
Raw
History Blame Contribute Delete
4.23 kB
from sentence_transformers import SentenceTransformer, util
import json
from datetime import datetime
# Function to read and parse the JSON file
def find_next_step(data, current_context):
try:
# Read the JSON file
# with open(file_path, 'r') as file:
# data = json.load(file)
# Initialize counters
total_videos = len(data)
total_scenes = 0
max_score=0.0
step={
"path" :"",
"start":"",
"end": "",
}
# Iterate through each video entry
print(f"Processing {total_videos} videos...\n")
for index, video in enumerate(data, 1):
video_type = video.get('type', 'Unknown')
video_path = video.get('path', 'No path provided')
scenes = video.get('scenes', [])
scene_count = len(scenes)
total_scenes += scene_count
# Iterate through each scene in the video
for scene_index, scene in enumerate(scenes, 1):
timestamp = scene.get('timestamp', 'No timestamp')
description = scene.get('description', 'No description')
time_obj = datetime.strptime(timestamp, "%M:%S.%f")
start = time_obj.minute * 60 + time_obj.second + time_obj.microsecond / 1_000_000
score=compare_phrases(description,current_context)
if score > max_score:
if scene_index+1<len(scenes):
next_scene=scenes[scene_index+1]
next_timestamp=next_scene.get('timestamp', 'No timestamp')
time_obj = datetime.strptime(next_timestamp, "%M:%S.%f")
stop = time_obj.minute * 60 + time_obj.second + time_obj.microsecond / 1_000_000
step={
"path" :video_path,
"start":start,
"end": stop,
"description":description
}
else:
step={
"path" :video_path,
"start":start,
"end": "",
"description":description
}
max_score=score
if "Error" in description:
print("[Note: This scene contains an error]")
# Print overall summary
print(f"Summary:")
print(f" Total Videos: {total_videos}")
print(f" Total Scenes: {total_scenes}")
except FileNotFoundError:
print(f"Error: File '{file_path}' not found.")
except json.JSONDecodeError:
print("Error: Invalid JSON format.")
except Exception as e:
print(f"Error: An unexpected error occurred: {str(e)}")
return step
def compare_phrases(phrase1,phrase2="Passing around a dj mixing a song"):
model = SentenceTransformer('all-MiniLM-L6-v2',local_files_only=True)
# Get embeddings
embedding1 = model.encode(phrase1, convert_to_tensor=True)
embedding2 = model.encode(phrase2, convert_to_tensor=True)
# Compute cosine similarity
similarity_score = util.cos_sim(embedding1, embedding2)[0][0]
# print(f"Similarity score: {similarity_score:.4f}")
# print(phrase1)
return similarity_score
def find_all_steps(data, context):
context = context.replace("\n", "").lower()
all_actions=context.split(".")
steps=[]
for action in all_actions:
step=find_next_step(data,action)
steps.append(step)
return steps
if __name__ == "__main__":
file_path = '/Users/georgia.bucea/products/ShortsAI/all_files_metadata.json' # Update this with the actual file path
context = """I am walking around San Francisco and passing around the big white building. I am walking around a music band and DJ mixing a song. I am showing a demo on the phone, I am explaining the demo on the mobile phone"""
# Read the JSON file
with open(file_path, 'r') as file:
data = json.load(file)
steps=find_all_steps(data,context)
print(steps)