Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
from pydrive.auth import GoogleAuth
|
| 4 |
+
from pydrive.drive import GoogleDrive
|
| 5 |
+
from oauth2client.service_account import ServiceAccountCredentials
|
| 6 |
+
import gradio as gr
|
| 7 |
+
|
| 8 |
+
|
| 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 |
+
|
| 14 |
+
for file in file_list:
|
| 15 |
+
if file['mimeType'] == 'video/mp4' and file['title'] in video_names:
|
| 16 |
+
output_file = os.path.join(save_directory, file['title'])
|
| 17 |
+
file.GetContentFile(output_file)
|
| 18 |
+
found_videos.append(output_file)
|
| 19 |
+
|
| 20 |
+
return found_videos
|
| 21 |
+
|
| 22 |
+
def view_videos(folder_id, video_names):
|
| 23 |
+
video_directory = "Videos"
|
| 24 |
+
os.makedirs(video_directory, exist_ok=True)
|
| 25 |
+
|
| 26 |
+
found_videos = download_videos_from_drive(drive, folder_id, video_directory, video_names)
|
| 27 |
+
return found_videos
|
| 28 |
+
|
| 29 |
+
def display_text(text):
|
| 30 |
+
relevant_videos = get_relevant_videos(text)
|
| 31 |
+
videos_from_drive = view_videos(drive_folder_id, relevant_videos)
|
| 32 |
+
return videos_from_drive
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
config_file_path = os.path.join(os.path.dirname(__file__), "config.json")
|
| 36 |
+
with open(config_file_path, 'r') as file:
|
| 37 |
+
config = json.load(file)
|
| 38 |
+
|
| 39 |
+
drive_config = config['drive_config']
|
| 40 |
+
drive_folder_id = config['drive_folder_id']
|
| 41 |
+
|
| 42 |
+
gauth = GoogleAuth()
|
| 43 |
+
gauth.credentials = ServiceAccountCredentials.from_json_keyfile_dict(drive_config,
|
| 44 |
+
['https://www.googleapis.com/auth/drive'])
|
| 45 |
+
drive = GoogleDrive(gauth)
|
| 46 |
+
|
| 47 |
+
text_interface = gr.Interface(
|
| 48 |
+
fn=display_text,
|
| 49 |
+
inputs="text",
|
| 50 |
+
outputs=gr.outputs.Video(label="Selected Videos", type="files")
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
text_interface.launch()
|