|
| 1 | +import os |
| 2 | +import time |
| 3 | +import logging |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +from dotenv import load_dotenv |
| 7 | +from ragie import Ragie |
| 8 | +from moviepy import VideoFileClip |
| 9 | + |
| 10 | +load_dotenv() |
| 11 | + |
| 12 | +logging.basicConfig(level=logging.INFO) |
| 13 | +logger = logging.getLogger(__name__) |
| 14 | + |
| 15 | +# initialize ragie client |
| 16 | +ragie = Ragie( |
| 17 | + auth=os.getenv('RAGIE_API_KEY'), |
| 18 | +) |
| 19 | + |
| 20 | +# Remove previous docs from index |
| 21 | +def clear_index(): |
| 22 | + while True: |
| 23 | + try: |
| 24 | + # List all documents |
| 25 | + response = ragie.documents.list() |
| 26 | + documents = response.result.documents |
| 27 | + |
| 28 | + # Process each document |
| 29 | + for document in documents: |
| 30 | + try: |
| 31 | + ragie.documents.delete( |
| 32 | + document_id=document.id |
| 33 | + ) |
| 34 | + logger.info(f"Deleted document {document.id}") |
| 35 | + except Exception as e: |
| 36 | + logger.error(f"Failed to delete document {document.id}: {str(e)}") |
| 37 | + raise |
| 38 | + |
| 39 | + # Check if there are more documents |
| 40 | + if not response.result.pagination.next_cursor: |
| 41 | + logger.warning("No more documents\n") |
| 42 | + break |
| 43 | + |
| 44 | + except Exception as e: |
| 45 | + logger.error(f"Failed to retrieve or process documents: {str(e)}") |
| 46 | + raise |
| 47 | + |
| 48 | +# Ingest data from a directory into the Ragie index |
| 49 | +def ingest_data(directory): |
| 50 | + # Get list of files in directory |
| 51 | + directory_path = Path(directory) |
| 52 | + files = os.listdir(directory_path) |
| 53 | + |
| 54 | + for file in files: |
| 55 | + try: |
| 56 | + file_path = directory_path / file |
| 57 | + # Read file content |
| 58 | + with open(file_path, mode='rb') as f: |
| 59 | + file_content = f.read() |
| 60 | + # Create document in Ragie |
| 61 | + response = ragie.documents.create(request={ |
| 62 | + "file": { |
| 63 | + "file_name": file, |
| 64 | + "content": file_content, |
| 65 | + }, |
| 66 | + "mode": { |
| 67 | + "video": "audio_video", |
| 68 | + "audio": True |
| 69 | + } |
| 70 | + }) |
| 71 | + # Wait for document to be ready |
| 72 | + while True: |
| 73 | + res = ragie.documents.get(document_id=response.id) |
| 74 | + if res.status == "ready": |
| 75 | + break |
| 76 | + |
| 77 | + time.sleep(2) |
| 78 | + |
| 79 | + logger.info(f"Successfully uploaded {file}") |
| 80 | + |
| 81 | + except Exception as e: |
| 82 | + logger.error(f"Failed to process file {file}: {str(e)}") |
| 83 | + continue |
| 84 | + |
| 85 | +# Retrieve data from the Ragie index |
| 86 | +def retrieve_data(query): |
| 87 | + try: |
| 88 | + logger.info(f"Retrieving data for query: {query}") |
| 89 | + retrieval_response = ragie.retrievals.retrieve(request={ |
| 90 | + "query": query |
| 91 | + }) |
| 92 | + |
| 93 | + content = [ |
| 94 | + { |
| 95 | + **chunk.document_metadata, |
| 96 | + "text": chunk.text, |
| 97 | + "document_name": chunk.document_name, |
| 98 | + "start_time": chunk.metadata.get("start_time"), |
| 99 | + "end_time": chunk.metadata.get("end_time") |
| 100 | + } |
| 101 | + for chunk in retrieval_response.scored_chunks |
| 102 | + ] |
| 103 | + |
| 104 | + logger.info(f"Successfully retrieved {len(content)} chunks") |
| 105 | + return content |
| 106 | + |
| 107 | + except Exception as e: |
| 108 | + logger.error(f"Failed to retrieve data: {str(e)}") |
| 109 | + raise |
| 110 | + |
| 111 | +def chunk_video(document_name, start_time, end_time, directory="videos"): |
| 112 | + # Create output filename |
| 113 | + output_dir = Path("video_chunks") |
| 114 | + output_dir.mkdir(parents=True, exist_ok=True) |
| 115 | + |
| 116 | + chunk_filename = f"video_chunk_{start_time:.1f}_{end_time:.1f}.mp4" |
| 117 | + output_path = output_dir / chunk_filename |
| 118 | + |
| 119 | + with VideoFileClip(directory + "/" + document_name) as video: |
| 120 | + video_duration = video.duration |
| 121 | + actual_end_time = min(end_time, video_duration) if end_time is not None else video_duration |
| 122 | + |
| 123 | + video_chunk = video.subclipped(start_time, actual_end_time) |
| 124 | + video_chunk.write_videofile(str(output_path)) |
| 125 | + |
| 126 | + return output_path |
| 127 | + |
| 128 | + |
| 129 | +if __name__ == "__main__": |
| 130 | + clear_index() |
| 131 | + ingest_data("videos") |
| 132 | + print(retrieve_data("What is the main topic of the video?")) |
0 commit comments