Update app.py
Browse files
app.py
CHANGED
|
@@ -1,147 +1,4 @@
|
|
| 1 |
# #!/usr/bin/env python3
|
| 2 |
-
# import cv2
|
| 3 |
-
# import base64
|
| 4 |
-
# import time
|
| 5 |
-
# import os
|
| 6 |
-
# import json
|
| 7 |
-
# import argparse
|
| 8 |
-
# import sys
|
| 9 |
-
# from openai import OpenAI
|
| 10 |
-
# from dotenv import load_dotenv
|
| 11 |
-
# from gtts import gTTS
|
| 12 |
-
|
| 13 |
-
# def main():
|
| 14 |
-
# # Set up argument parsing
|
| 15 |
-
# parser = argparse.ArgumentParser(description='Video Explanation Agent')
|
| 16 |
-
# parser.add_argument('--video-file', required=True, help='Path to the video file to process')
|
| 17 |
-
# parser.add_argument('--prompt-file', required=True, help='Path to the file containing the explanation prompt')
|
| 18 |
-
# args = parser.parse_args()
|
| 19 |
-
|
| 20 |
-
# # Check if files exist
|
| 21 |
-
# if not os.path.exists(args.video_file):
|
| 22 |
-
# print(json.dumps({
|
| 23 |
-
# "error": f"Video file not found: {args.video_file}"
|
| 24 |
-
# }))
|
| 25 |
-
# sys.exit(1)
|
| 26 |
-
|
| 27 |
-
# if not os.path.exists(args.prompt_file):
|
| 28 |
-
# print(json.dumps({
|
| 29 |
-
# "error": f"Prompt file not found: {args.prompt_file}"
|
| 30 |
-
# }))
|
| 31 |
-
# sys.exit(1)
|
| 32 |
-
|
| 33 |
-
# # Load environment variables from .env file
|
| 34 |
-
# load_dotenv()
|
| 35 |
-
|
| 36 |
-
# # Get the OpenAI API key from the environment
|
| 37 |
-
# api_key = os.getenv("OPENAI_API_KEY", "<your OpenAI API key if not set as env var>")
|
| 38 |
-
# client = OpenAI(api_key=api_key)
|
| 39 |
-
|
| 40 |
-
# # Read the custom prompt from the file
|
| 41 |
-
# with open(args.prompt_file, 'r') as f:
|
| 42 |
-
# custom_prompt = f.read().strip()
|
| 43 |
-
|
| 44 |
-
# # Open the video file
|
| 45 |
-
# video = cv2.VideoCapture(args.video_file)
|
| 46 |
-
# if not video.isOpened():
|
| 47 |
-
# print(json.dumps({
|
| 48 |
-
# "error": f"Failed to open video file: {args.video_file}"
|
| 49 |
-
# }))
|
| 50 |
-
# sys.exit(1)
|
| 51 |
-
|
| 52 |
-
# # Extract frames from video
|
| 53 |
-
# base64Frames = []
|
| 54 |
-
# frame_count = 0
|
| 55 |
-
# total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 56 |
-
|
| 57 |
-
# # Calculate sampling rate to get around 20-30 frames total
|
| 58 |
-
# if total_frames > 0:
|
| 59 |
-
# sampling_rate = max(1, total_frames // 25)
|
| 60 |
-
# else:
|
| 61 |
-
# sampling_rate = 50 # Default fallback
|
| 62 |
-
|
| 63 |
-
# while video.isOpened():
|
| 64 |
-
# success, frame = video.read()
|
| 65 |
-
# if not success:
|
| 66 |
-
# break
|
| 67 |
-
|
| 68 |
-
# # Only take every Nth frame to reduce processing
|
| 69 |
-
# if frame_count % sampling_rate == 0:
|
| 70 |
-
# _, buffer = cv2.imencode(".jpg", frame)
|
| 71 |
-
# base64Frames.append(base64.b64encode(buffer).decode("utf-8"))
|
| 72 |
-
|
| 73 |
-
# frame_count += 1
|
| 74 |
-
|
| 75 |
-
# video.release()
|
| 76 |
-
# print(f"Processed {len(base64Frames)} frames from {total_frames} total frames.", file=sys.stderr)
|
| 77 |
-
|
| 78 |
-
# # Create the timestamp for unique filenames
|
| 79 |
-
# timestamp = int(time.time())
|
| 80 |
-
# # Create data directory inside the vidExp-agent directory
|
| 81 |
-
# output_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data")
|
| 82 |
-
# os.makedirs(output_dir, exist_ok=True)
|
| 83 |
-
|
| 84 |
-
# # Generate explanation based on the custom prompt
|
| 85 |
-
# PROMPT_MESSAGES = [
|
| 86 |
-
# {
|
| 87 |
-
# "role": "user",
|
| 88 |
-
# "content": [
|
| 89 |
-
# f"{custom_prompt}",
|
| 90 |
-
# *map(lambda x: {"image": x, "resize": 768}, base64Frames),
|
| 91 |
-
# ],
|
| 92 |
-
# },
|
| 93 |
-
# ]
|
| 94 |
-
|
| 95 |
-
# params = {
|
| 96 |
-
# "model": "gpt-4o-mini",
|
| 97 |
-
# "messages": PROMPT_MESSAGES,
|
| 98 |
-
# "max_tokens": 500,
|
| 99 |
-
# }
|
| 100 |
-
|
| 101 |
-
# try:
|
| 102 |
-
# result = client.chat.completions.create(**params)
|
| 103 |
-
# explanation = result.choices[0].message.content
|
| 104 |
-
# print(f"Generated explanation based on provided prompt.", file=sys.stderr)
|
| 105 |
-
# except Exception as e:
|
| 106 |
-
# print(json.dumps({
|
| 107 |
-
# "error": f"Error generating explanation: {str(e)}"
|
| 108 |
-
# }))
|
| 109 |
-
# sys.exit(1)
|
| 110 |
-
|
| 111 |
-
# # Save the explanation as a text file
|
| 112 |
-
# explanation_file = os.path.join(output_dir, f"explanation_{timestamp}.txt")
|
| 113 |
-
# with open(explanation_file, "w") as f:
|
| 114 |
-
# f.write(explanation)
|
| 115 |
-
|
| 116 |
-
# # Generate audio from the explanation
|
| 117 |
-
# audio_filename = f"explanation_{timestamp}.mp3"
|
| 118 |
-
# audio_path = os.path.join(output_dir, audio_filename)
|
| 119 |
-
|
| 120 |
-
# try:
|
| 121 |
-
# # Default to English for TTS
|
| 122 |
-
# tts = gTTS(text=explanation, lang='en')
|
| 123 |
-
# tts.save(audio_path)
|
| 124 |
-
# print(f"Generated audio file.", file=sys.stderr)
|
| 125 |
-
# except Exception as e:
|
| 126 |
-
# print(json.dumps({
|
| 127 |
-
# "error": f"Error generating audio: {str(e)}"
|
| 128 |
-
# }))
|
| 129 |
-
# sys.exit(1)
|
| 130 |
-
|
| 131 |
-
# # Return the results as JSON
|
| 132 |
-
# result = {
|
| 133 |
-
# "success": True,
|
| 134 |
-
# "explanation": explanation,
|
| 135 |
-
# "explanationFilePath": explanation_file,
|
| 136 |
-
# "audioFilename": audio_filename,
|
| 137 |
-
# "audioFilePath": audio_path
|
| 138 |
-
# }
|
| 139 |
-
|
| 140 |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