Anupam007 commited on
Commit
895ee79
·
verified ·
1 Parent(s): 90e9f53

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +153 -0
app.py ADDED
@@ -0,0 +1,153 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ import torch
4
+ import moviepy.editor as mp
5
+ import os
6
+ from googleapiclient.discovery import build
7
+ from google.oauth2 import credentials
8
+ from google_auth_oauthlib.flow import InstalledAppFlow
9
+ from google.auth.transport.requests import Request
10
+ from googleapiclient.http import MediaIoBaseDownload
11
+ from googleapiclient.errors import HttpError
12
+ import uuid
13
+
14
+ # Device setup
15
+ device = "cpu" # Using CPU to avoid CUDA memory issues
16
+ print(f"Using device: {device}")
17
+
18
+ # Preload models
19
+ try:
20
+ transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-large-v2", device=device)
21
+ summarizer = pipeline("summarization", model="facebook/bart-large-cnn", device=device)
22
+ except Exception as e:
23
+ print(f"Error loading models: {e}. Make sure you have internet connectivity and sufficient resources.")
24
+ transcriber = None
25
+ summarizer = None
26
+
27
+
28
+
29
+ # Google Drive API setup
30
+ SCOPES = ['https://www.googleapis.com/auth/drive.readonly']
31
+ CLIENT_SECRET_FILE = 'client_secrets.json' # Relative path
32
+ APPLICATION_NAME = 'MyMeetNotes'
33
+
34
+ def get_credentials():
35
+ if not os.path.exists(CLIENT_SECRET_FILE):
36
+ raise FileNotFoundError(
37
+ "The 'client_secrets.json' file is missing. Please ensure it’s in the same directory as the script. See: "
38
+ "https://developers.google.com/drive/api/v3/quickstart/python"
39
+ )
40
+ creds = None
41
+ if os.path.exists('token.json'):
42
+ creds = credentials.Credentials.from_authorized_user_file('token.json', SCOPES)
43
+ if not creds or not creds.valid:
44
+ if creds and creds.expired and creds.refresh_token:
45
+ creds.refresh(Request())
46
+ else:
47
+ flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRET_FILE, SCOPES)
48
+ creds = flow.run_local_server(port=0)
49
+ with open('token.json', 'w') as token:
50
+ token.write(creds.to_json())
51
+ return creds
52
+
53
+ def list_google_meet_recordings():
54
+ try:
55
+ creds = get_credentials()
56
+ service = build('drive', 'v3', credentials=creds)
57
+ results = service.files().list(
58
+ pageSize=10, fields="nextPageToken, files(id, name, mimeType)",
59
+ q="name contains 'Meeting recording' and mimeType contains 'video/'").execute()
60
+ items = results.get('files', [])
61
+ if not items:
62
+ return ["No Google Meet recordings found."]
63
+ return [f"{item['name']} (ID: {item['id']})" for item in items]
64
+ except HttpError as error:
65
+ return [f"An error occurred: {error}"]
66
+ except Exception as e:
67
+ return [f"An unexpected error occurred: {e}"]
68
+
69
+ def download_google_meet_recording(file_id):
70
+ try:
71
+ creds = get_credentials()
72
+ service = build('drive', 'v3', credentials=creds)
73
+ file_name = f"temp_recording_{file_id}_{uuid.uuid4()}.mp4" # removed os.path.join(os.getcwd(), ...) for simplicity in local usage
74
+ print(f"Downloading file ID: {file_id} to {file_name}")
75
+
76
+ request = service.files().get_media(fileId=file_id)
77
+ with open(file_name, 'wb') as fh:
78
+ downloader = MediaIoBaseDownload(fh, request)
79
+ done = False
80
+ while not done:
81
+ status, done = downloader.next_chunk()
82
+ print(f"Download progress: {int(status.progress() * 100)}%")
83
+
84
+ if os.path.exists(file_name):
85
+ print(f"Download complete. File saved as: {file_name}")
86
+ return file_name
87
+ else:
88
+ return f"Error: File '{file_name}' not found after download."
89
+ except HttpError as error:
90
+ return f"HTTP error during download: {error}"
91
+ except Exception as e:
92
+ return f"Unexpected error during download: {e}"
93
+
94
+ def generate_meeting_notes(file_id):
95
+ if transcriber is None or summarizer is None:
96
+ return "Error: Models failed to load. Check your internet connection and resources."
97
+ try:
98
+ download_result = download_google_meet_recording(file_id)
99
+ if isinstance(download_result, str) and "Error" in download_result:
100
+ return download_result
101
+ file_name = download_result
102
+
103
+ if not os.path.exists(file_name):
104
+ return f"Error: Downloaded file '{file_name}' not found."
105
+
106
+ print(f"Extracting audio from {file_name}")
107
+ video = mp.VideoFileClip(file_name)
108
+ audio_file = f"temp_audio_{uuid.uuid4()}.wav"
109
+ video.audio.write_audiofile(audio_file)
110
+ print("Audio extraction complete.")
111
+
112
+ print("Starting transcription...")
113
+ transcription = transcriber(audio_file)["text"]
114
+ print("Transcription complete.")
115
+
116
+ print("Starting summarization...")
117
+ summary = summarizer(transcription, max_length=1024, min_length=100, do_sample=False)
118
+ meeting_notes = summary[0]["summary_text"]
119
+ print("Summarization complete.")
120
+
121
+ return meeting_notes
122
+
123
+ except Exception as e:
124
+ return f"Error in processing: {str(e)}"
125
+ finally:
126
+ try:
127
+ if 'video' in locals() and video: # Check if video exists
128
+ video.close()
129
+ if 'audio_file' in locals() and os.path.exists(audio_file):
130
+ os.remove(audio_file)
131
+ if 'file_name' in locals() and os.path.exists(file_name): # Check if file_name exists
132
+ os.remove(file_name)
133
+ except Exception as cleanup_error:
134
+ print(f"Cleanup error: {cleanup_error}")
135
+
136
+ def launch_gradio():
137
+ with gr.Blocks() as iface:
138
+ gr.Markdown("# Automatic Meeting Notes Generator from Google Meet")
139
+ with gr.Row():
140
+ with gr.Column():
141
+ file_id_input = gr.Textbox(label="Google Meet Recording File ID", placeholder="Enter the Google Drive File ID")
142
+ generate_button = gr.Button("Generate Notes")
143
+ recording_list_button = gr.Button("List Recordings")
144
+ with gr.Column():
145
+ output_text = gr.Textbox(label="Meeting Notes")
146
+ recording_list = gr.Textbox(label="Available Recordings")
147
+
148
+ generate_button.click(fn=generate_meeting_notes, inputs=file_id_input, outputs=output_text)
149
+ recording_list_button.click(fn=list_google_meet_recordings, inputs=None, outputs=recording_list)
150
+ iface.launch(debug=True)
151
+
152
+ if __name__ == "__main__":
153
+ launch_gradio()