File size: 3,221 Bytes
509f846
 
 
 
 
81aadd4
 
509f846
 
81aadd4
 
 
509f846
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a75e72c
81aadd4
509f846
81aadd4
509f846
81aadd4
 
 
 
 
 
 
 
 
 
 
509f846
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81aadd4
509f846
 
81aadd4
 
509f846
 
 
f18af8b
060c816
089b70d
509f846
f18af8b
509f846
c5837bc
509f846
 
 
b3e3369
509f846
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
import subprocess as sp

sp.check_call(['pip', 'install', 'pydrive'])
sp.check_call(['pip', 'install', 'transformers'])
sp.check_call(['pip', 'install', 'requests'])
import os
import json
import subprocess as sp
import requests
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from oauth2client.service_account import ServiceAccountCredentials
import gradio as gr

# Fetching config from Hugging Face Spaces
url = "https://huggingface.co/spaces/CS482Project/Milestone_5-Search_UI_and_pitch_video_voiceover/raw/main/config.json"

response = requests.get(url)
if response.status_code == 200:
    config = response.json()
    drive_config = config.get('drive_config')
    drive_folder_id = config.get('drive_folder_id')
    if drive_config and drive_folder_id:
        print(drive_config)
        print(drive_folder_id)
    else:
        print("Drive config or folder ID not found in the JSON.")
else:
    print("Failed to fetch data. Status code:", response.status_code)

def download_videos_from_drive(drive, folder_id, save_directory, video_names):
    print("Fetching video list from Google Drive...")
    file_list = drive.ListFile({'q': f"'{folder_id}' in parents and trashed=false"}).GetList()
    print("Total files in Drive folder:", len(file_list))  # Print total files in the Drive folder
    os.makedirs(save_directory, exist_ok=True)
    found_videos = []

    for file in file_list:
        if file['mimeType'] == 'video/mp4' and file['title'] in video_names:
            output_file = os.path.join(save_directory, file['title'])
            file.GetContentFile(output_file)
            found_videos.append(output_file)

    return found_videos

def view_videos(folder_id, video_names):
    video_directory = "Videos"
    os.makedirs(video_directory, exist_ok=True)
    
    print("Downloading videos from Google Drive...")
    found_videos = download_videos_from_drive(drive, folder_id, video_directory, video_names)
    print("Videos downloaded:", found_videos)
    return found_videos

def display_text(text):
    relevant_videos = ['Memphis_Police_Protests,_Super_Bowl_LVII_Matchup_Set_|_NPR_News_Now.mp4', "Maui's_Death_Toll_Rises_To_111_|_NPR_News_Now.mp4", 'Manhunt_Continues_For_Suspect_In_Maine_Shootings_|_NPR_News_Now.mp4', 'Jordan_Calls_For_3rd_Vote_Friday_On_His_Bid_For_House_Speaker_|_NPR_News_Now.mp4', 'Israel_Gives_1M_Civilians_In_Northern_Gaza_24_Hours_To_Evacuate_|_NPR_News_Now.mp4']  # Replace with video names  
    videos_from_drive = view_videos(drive_folder_id, relevant_videos)  
    return videos_from_drive

# Authentication with Google Drive
gauth = GoogleAuth()
gauth.credentials = ServiceAccountCredentials.from_json_keyfile_dict(drive_config,
                                                                    ['https://www.googleapis.com/auth/drive'])
drive = GoogleDrive(gauth)

# Gradio Interface
def display_videos(text):
    videos = display_text(text)
    print(videos[0])
    return videos

iface = gr.Interface(
    fn=display_videos,
    inputs="text",
    outputs=[gr.Video(),gr.Video(),gr.Video(),gr.Video(),gr.Video()],
    title="Video Viewer",
    description="Enter text to fetch and display videos from Google Drive"
)

iface.launch()