| |
|
|
| import gradio as gr |
| import os |
| import json |
| import shutil |
|
|
| |
| from modules.video_analyzer import analyze_video_for_ppe |
| from modules.rag_indexer import index_analysis_data |
| from modules.rag_query import run_query |
|
|
| |
| VIDEO_FILENAME = "uploaded_video.mp4" |
| RAW_ANALYSIS_FILE = 'raw_analysis.json' |
| DB_PATH = "./chroma_db" |
| COLLECTION_NAME = 'video_analysis_data' |
|
|
| def pipeline_fn(video_file, user_query): |
| """ |
| The main function connecting the Gradio inputs to the RAG pipeline. |
| |
| Args: |
| video_file: The temporary file object from Gradio (gr.File). |
| user_query: The text question from Gradio (gr.Textbox). |
| |
| Returns: |
| The text response from the RAG query. |
| """ |
| if video_file is None: |
| return "Error: Please upload a video file first." |
| if not user_query: |
| return "Error: Please enter a question to query the video analysis." |
|
|
| |
| |
| |
| try: |
| |
| temp_video_path = os.path.join(os.getcwd(), VIDEO_FILENAME) |
| shutil.copy(video_file.name, temp_video_path) |
| print(f"Copied uploaded file to: {temp_video_path}") |
| except Exception as e: |
| return f"File handling error: {e}" |
|
|
| |
| print("\n--- STAGE 1: Analyzing Video ---") |
| |
| analysis_results = analyze_video_for_ppe( |
| video_path=temp_video_path, |
| frames_per_sec= 2 |
| ) |
| |
| |
| with open(RAW_ANALYSIS_FILE, 'w') as f: |
| json.dump(analysis_results, f, indent=4) |
| |
| |
| print("\n--- STAGE 2: Indexing Analysis Data ---") |
| |
| index_analysis_data(json_file=RAW_ANALYSIS_FILE, collection_name=COLLECTION_NAME) |
|
|
| |
| print("\n--- STAGE 3: Executing RAG Query ---") |
| rag_answer = run_query(user_query) |
| |
| |
| os.remove(temp_video_path) |
| os.remove(RAW_ANALYSIS_FILE) |
| |
| return rag_answer |
|
|
| |
|
|
| |
| video_input = gr.File( |
| label="Upload Video File (.mp4, .mov, etc.)", |
| file_types=["video"], |
| type="filepath" |
| ) |
| query_input = gr.Textbox( |
| label="Ask a Question about the Video Content", |
| placeholder="e.g., What are people doing in the video?", |
| lines=2 |
| ) |
|
|
| |
| output_textbox = gr.Textbox( |
| label="RAG Analysis Result", |
| lines=10, |
| interactive=False |
| ) |
|
|
| |
| demo = gr.Interface( |
| fn=pipeline_fn, |
| inputs=[video_input, query_input], |
| outputs=output_textbox, |
| title="๐ Video Content RAG Pipeline", |
| description="Upload a video, and ask a question. The pipeline runs object detection, indexes the data, and uses Gemini to answer your question based on the analysis.", |
| ) |
|
|
| if __name__ == "__main__": |
| print("Launching Gradio App...") |
| |
| demo.launch() |