File size: 10,792 Bytes
91af6e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b1cfc3
 
 
 
 
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
from openai import AzureOpenAI
import os
import ffmpeg
from typing import List
from moviepy.editor import VideoFileClip
import nltk
from sklearn.feature_extraction.text import TfidfVectorizer
import gradio as gr
from pytube import YouTube
import requests
import logging

nltk.download('punkt')
nltk.download('stopwords')


class VideoAnalytics:
    """
    Class for performing analytics on videos including transcription, summarization, topic generation,
    and extraction of important sentences.
    """

    def __init__(self):
      """
      Initialize the VideoAnalytics object.

      Args:
          hf_token (str): Hugging Face API token.
      """
      # Initialize AzureOpenAI client
      self.client = AzureOpenAI()

      # Initialize transcribed text variable
      self.transcribed_text = ""

      # API URL for accessing the Hugging Face model
      self.API_URL = "https://api-inference.huggingface.co/models/openai/whisper-large-v3"

      # Placeholder for Hugging Face API token
      hf_token = os.get_environ("HF_TOKEN")  # Replace this with the actual Hugging Face API token

      # Set headers for API requests with Hugging Face token
      self.headers = {"Authorization": f"Bearer {hf_token}"}

      # Configure logging settings
      logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

    def transcribe_video(self, vid: str) -> str:
      """
      Transcribe the audio of the video.

      Args:
          vid (str): Path to the video file.

      Returns:
          str: Transcribed text.
      """
      try:
          # Load the video file and extract audio
          video = VideoFileClip(vid)
          audio = video.audio

          # Write audio to a temporary file
          audio.write_audiofile("output_audio.mp3")
          audio_file = open("output_audio.mp3", "rb")

          # Define a helper function to query the Hugging Face model
          def query(data):
              response = requests.post(self.API_URL, headers=self.headers, data=data)
              return response.json()

          # Send audio data to the Hugging Face model for transcription
          output = query(audio_file)
          # Update the transcribed_text attribute with the transcription result
          self.transcribed_text = output["text"]
          # Return the transcribed text
          return output["text"]

      except Exception as e:
          logging.error(f"Error transcribing video: {e}")
          return ""

    def generate_video_summary(self) -> str:
        """
        Generate a summary of the transcribed video.

        Returns:
            str: Generated summary.
        """
        try:
          # Define a conversation between system and user
          conversation = [
            {"role": "system", "content": "You are a Summarizer"},
            {"role": "user", "content": f"""summarize the following text delimited by triple backticks.
                      In two format of Outputs given below:
                      Abstractive Summary:
                      Extractive Summary:
                      ```{self.transcribed_text}```
              """}
              ]
          # Generate completion using ChatGPT model
          response = self.client.chat.completions.create(
              model="ChatGPT",
              messages=conversation,
              temperature=0,
              max_tokens=1000
          )
          # Get the generated summary message
          message = response.choices[0].message.content
          return message
        except Exception as e:
            logging.error(f"Error generating video summary: {e}")
            return ""


    def generate_topics(self) -> str:
        """
        Generate topics from the transcribed video.

        Returns:
            str: Generated topics.
        """
        try:
          # Define a conversation between system and user
          conversation = [
            {"role": "system", "content": "You are a Topic Generator"},
            {"role": "user", "content": f"""generate single Topics from the following text don't make sentence for topic generation,delimited by triple backticks.
                      list out the topics:
                      Topics:
                      ```{self.transcribed_text}```
              """}
              ]
          # Generate completion using ChatGPT model
          response = self.client.chat.completions.create(
              model="ChatGPT",
              messages=conversation,
              temperature=0,
              max_tokens=1000
          )
          # Get the generated topics message
          message = response.choices[0].message.content
          return message
        except Exception as e:
            logging.error(f"Error generating topics: {e}")
            return ""

    def extract_video_important_sentence(self) -> str:
        """
        Extract important sentences from the transcribed video.

        Returns:
            str: Extracted important sentences.
        """
        try:
          # Tokenize the sentences
          sentences = nltk.sent_tokenize(self.transcribed_text)

          # Initialize TF-IDF vectorizer
          tfidf_vectorizer = TfidfVectorizer()

          # Fit the vectorizer on the summary sentences
          tfidf_matrix = tfidf_vectorizer.fit_transform(sentences)

          # Calculate sentence scores based on TF-IDF values
          sentence_scores = tfidf_matrix.sum(axis=1)

          # Create a list of (score, sentence) tuples
          sentence_rankings = [(score, sentence) for score, sentence in zip(sentence_scores, sentences)]

          # Sort sentences by score in descending order
          sentence_rankings.sort(reverse=True)

          # Set a threshold for selecting sentences
          threshold = 2 # Adjust as needed

          # Select sentences with scores above the threshold
          selected_sentences = [sentence for score, sentence in sentence_rankings if score >= threshold]

          # Join selected sentences to form the summary
          summary = '\n\n'.join(selected_sentences)

          return summary

        except Exception as e:
            logging.error(f"Error extracting important sentences: {e}")
            return ""

    def write_text_files(self, text: str, filename: str) -> None:
        """
        Write text to a file.

        Args:
            text (str): Text to be written to the file.
            filename (str): Name of the file.
        """
        try:
          file_path = f"{filename}.txt"
          with open(file_path, 'w') as file:
              # Write content to the file
              file.write(text)
        except Exception as e:
            logging.error(f"Error writing text to file: {e}")

    def Download(self, link: str) -> str:
        """
        Download a video from YouTube.

        Args:
            link (str): YouTube video link.

        Returns:
            str: Path to the downloaded video file.
        """
        try:
          # Initialize YouTube object with the provided link
          youtubeObject = YouTube(link)

          # Get the highest resolution stream
          youtubeObject = youtubeObject.streams.get_highest_resolution()
          try:
              # Attempt to download the video
              file_name = youtubeObject.download()
              return file_name
          except:
              # Log any errors that occur during video download
              logging.info("An error has occurred")

          logging.info("Download is completed successfully")

        except Exception as e:
            # Log any errors that occur during initialization of YouTube object
            logging.error(f"Error downloading video: {e}")
            return ""


    def main(self, video: str = None, input_path: str = None) -> tuple:
        """
        Perform video analytics.

        Args:
            video (str): Path to the video file.
            input_path (str): Input path for the video.

        Returns:
            tuple: Summary, important sentences, and topics.
        """
        try:
          # Download the video if input_path is provided, otherwise use the provided video path
          if input_path:
            input_path = self.Download(input_path)
            text = self.transcribe_video(input_path)
          elif video:
            text = self.transcribe_video(video)
            input_path = video

          # Generate summary, important sentences, and topics
          summary = self.generate_video_summary()
          self.write_text_files(summary,"Summary")
          important_sentences = self.extract_video_important_sentence()
          self.write_text_files(important_sentences,"Important_Sentence")
          topics = self.generate_topics()
          self.write_text_files(topics,"Topics")

          # Return the generated summary, important sentences, and topics
          return summary,important_sentences,topics

        except Exception as e:
            # Log any errors that occur during video analytics
            logging.error(f"Error in main function: {e}")
            return "", "", ""

    def gradio_interface(self):
        with gr.Blocks(css="style.css",theme=gr.themes.Soft()) as demo:
            gr.HTML("""<center><h1>Video Analytics</h1></center>""")
            with gr.Row():
              yt_link = gr.Textbox(label= "Youtube Link",placeholder="https://www.youtube.com/watch?v=")
            with gr.Row():
              video = gr.Video(sources="upload",height=200,width=300)
            with gr.Row():
              submit_btn = gr.Button(value="Submit")
            with gr.Tab("Summary"):
              with gr.Row():
                  summary = gr.Textbox(show_label=False,lines=10)
              with gr.Row():
                  summary_download = gr.DownloadButton(label="Download",value="Summary.txt",visible=True,size='lg',elem_classes="download_button")
            with gr.Tab("Important Sentences"):
              with gr.Row():
                  Important_Sentences = gr.Textbox(show_label=False,lines=10)
              with gr.Row():
                  sentence_download = gr.DownloadButton(label="Download",value="Important_Sentence.txt",visible=True,size='lg',elem_classes="download_button")
            with gr.Tab("Topics"):
              with gr.Row():
                  Topics = gr.Textbox(show_label=False,lines=10)
              with gr.Row():
                  topics_download = gr.DownloadButton(label="Download",value="Topics.txt",visible=True,size='lg',elem_classes="download_button")
              submit_btn.click(self.main,[video,yt_link],[summary,Important_Sentences,Topics])
        demo.launch()

if __name__ == "__main__":
  video_analytics = VideoAnalytics()
  video_analytics.gradio_interface()