Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
-
import
|
|
|
|
| 4 |
from pydrive.auth import GoogleAuth
|
| 5 |
from pydrive.drive import GoogleDrive
|
| 6 |
from oauth2client.service_account import ServiceAccountCredentials
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
# Function to download videos from Drive
|
| 9 |
def download_videos_from_drive(drive, folder_id, save_directory, video_names):
|
|
|
|
| 10 |
file_list = drive.ListFile({'q': f"'{folder_id}' in parents and trashed=false"}).GetList()
|
|
|
|
| 11 |
os.makedirs(save_directory, exist_ok=True)
|
| 12 |
found_videos = []
|
| 13 |
|
|
@@ -19,22 +43,46 @@ def download_videos_from_drive(drive, folder_id, save_directory, video_names):
|
|
| 19 |
|
| 20 |
return found_videos
|
| 21 |
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
gauth = GoogleAuth()
|
| 24 |
-
|
|
|
|
| 25 |
drive = GoogleDrive(gauth)
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
| 40 |
|
|
|
|
|
|
| 1 |
+
import subprocess as sp
|
| 2 |
+
|
| 3 |
+
sp.check_call(['pip', 'install', 'pydrive'])
|
| 4 |
+
sp.check_call(['pip', 'install', 'transformers'])
|
| 5 |
+
sp.check_call(['pip', 'install', 'requests'])
|
| 6 |
import os
|
| 7 |
import json
|
| 8 |
+
import subprocess as sp
|
| 9 |
+
import requests
|
| 10 |
from pydrive.auth import GoogleAuth
|
| 11 |
from pydrive.drive import GoogleDrive
|
| 12 |
from oauth2client.service_account import ServiceAccountCredentials
|
| 13 |
+
import gradio as gr
|
| 14 |
+
|
| 15 |
+
# Fetching config from Hugging Face Spaces
|
| 16 |
+
url = "https://huggingface.co/spaces/CS482Project/Milestone_5-Search_UI_and_pitch_video_voiceover/raw/main/config.json"
|
| 17 |
+
|
| 18 |
+
response = requests.get(url)
|
| 19 |
+
if response.status_code == 200:
|
| 20 |
+
config = response.json()
|
| 21 |
+
drive_config = config.get('drive_config')
|
| 22 |
+
drive_folder_id = config.get('drive_folder_id')
|
| 23 |
+
if drive_config and drive_folder_id:
|
| 24 |
+
print(drive_config)
|
| 25 |
+
print(drive_folder_id)
|
| 26 |
+
else:
|
| 27 |
+
print("Drive config or folder ID not found in the JSON.")
|
| 28 |
+
else:
|
| 29 |
+
print("Failed to fetch data. Status code:", response.status_code)
|
| 30 |
|
|
|
|
| 31 |
def download_videos_from_drive(drive, folder_id, save_directory, video_names):
|
| 32 |
+
print("Fetching video list from Google Drive...")
|
| 33 |
file_list = drive.ListFile({'q': f"'{folder_id}' in parents and trashed=false"}).GetList()
|
| 34 |
+
print("Total files in Drive folder:", len(file_list)) # Print total files in the Drive folder
|
| 35 |
os.makedirs(save_directory, exist_ok=True)
|
| 36 |
found_videos = []
|
| 37 |
|
|
|
|
| 43 |
|
| 44 |
return found_videos
|
| 45 |
|
| 46 |
+
def view_videos(folder_id, video_names):
|
| 47 |
+
video_directory = "Videos"
|
| 48 |
+
os.makedirs(video_directory, exist_ok=True)
|
| 49 |
+
|
| 50 |
+
print("Downloading videos from Google Drive...")
|
| 51 |
+
found_videos = download_videos_from_drive(drive, folder_id, video_directory, video_names)
|
| 52 |
+
print("Videos downloaded:", found_videos)
|
| 53 |
+
return found_videos
|
| 54 |
+
|
| 55 |
+
def display_text(text):
|
| 56 |
+
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
|
| 57 |
+
videos_from_drive = view_videos(drive_folder_id, relevant_videos)
|
| 58 |
+
return videos_from_drive
|
| 59 |
+
|
| 60 |
+
# Authentication with Google Drive
|
| 61 |
gauth = GoogleAuth()
|
| 62 |
+
gauth.credentials = ServiceAccountCredentials.from_json_keyfile_dict(drive_config,
|
| 63 |
+
['https://www.googleapis.com/auth/drive'])
|
| 64 |
drive = GoogleDrive(gauth)
|
| 65 |
|
| 66 |
+
# Gradio Interface
|
| 67 |
+
def display_videos(text):
|
| 68 |
+
videos = display_text(text)
|
| 69 |
+
video_tags = ""
|
| 70 |
+
for video in videos:
|
| 71 |
+
abs_video_path = os.path.abspath(video)
|
| 72 |
+
if os.path.exists(abs_video_path):
|
| 73 |
+
video_tag = f"<video width='320' height='240' controls><source src='file://{abs_video_path}' type='video/mp4'></video><br>"
|
| 74 |
+
print("Generated video tag:", video_tag) # Print generated video tag
|
| 75 |
+
video_tags += video_tag
|
| 76 |
+
else:
|
| 77 |
+
print(f"Video file '{video}' not found.")
|
| 78 |
+
return video_tags
|
| 79 |
|
| 80 |
+
iface = gr.Interface(
|
| 81 |
+
fn=display_videos,
|
| 82 |
+
inputs="text",
|
| 83 |
+
outputs="html",
|
| 84 |
+
title="Video Viewer",
|
| 85 |
+
description="Enter text to fetch and display videos from Google Drive"
|
| 86 |
+
)
|
| 87 |
|
| 88 |
+
iface.launch()
|