File size: 4,227 Bytes
dde1cc7 | 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 | 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)
|