Spaces:
Running
Running
KarthickAdopleAI
commited on
Commit
•
82dac81
1
Parent(s):
e21cd35
Update app.py
Browse files
app.py
CHANGED
@@ -37,9 +37,8 @@ class VideoAnalytics:
|
|
37 |
"""
|
38 |
# Initialize AzureOpenAI client
|
39 |
self.client = AzureOpenAI()
|
40 |
-
|
41 |
-
|
42 |
-
self.mistral_client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1",token=hf_token)
|
43 |
|
44 |
# Initialize transcribed text variable
|
45 |
self.transcribed_text = ""
|
@@ -87,40 +86,39 @@ class VideoAnalytics:
|
|
87 |
logging.error(f"Error occurred while converting MP3 to WAV: {e}")
|
88 |
raise e
|
89 |
|
90 |
-
def split_audio(self, input_file: str) ->
|
91 |
"""
|
92 |
-
Split an audio file into segments of
|
93 |
|
94 |
Args:
|
95 |
input_file (str): Path to the input audio file.
|
96 |
|
97 |
Returns:
|
98 |
-
|
99 |
"""
|
100 |
try:
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
except
|
121 |
-
|
122 |
return []
|
123 |
-
|
124 |
# Function to recognize speech in the audio file
|
125 |
def transcribe_audio(self,path: str,lang: str):
|
126 |
"""Transcribe speech from an audio file."""
|
@@ -205,6 +203,55 @@ class VideoAnalytics:
|
|
205 |
logging.error(f"Error transcribing video: {e}")
|
206 |
return ""
|
207 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
208 |
def generate_video_summary(self,model) -> str:
|
209 |
"""
|
210 |
Generate a summary of the transcribe_video.
|
@@ -216,10 +263,9 @@ class VideoAnalytics:
|
|
216 |
# Define a conversation between system and user
|
217 |
conversation = [
|
218 |
{"role": "system", "content": "You are a Summarizer"},
|
219 |
-
{"role": "user", "content": f"""summarize the following text delimited by triple backticks.Output must in english.give me a detailed summary.
|
220 |
-
In
|
221 |
Abstractive Summary:
|
222 |
-
Extractive Summary:
|
223 |
```{self.english_text}```
|
224 |
"""}
|
225 |
]
|
@@ -237,12 +283,12 @@ class VideoAnalytics:
|
|
237 |
elif model == "Mixtral":
|
238 |
task = "summary"
|
239 |
# Generate answer using Mixtral model
|
240 |
-
prompt = f"""<s>[INST]
|
241 |
-
|
242 |
-
|
243 |
-
Extractive Summary:
|
244 |
```data:{self.english_text}```[/INST]"""
|
245 |
result = self.generate(prompt)
|
|
|
246 |
return result
|
247 |
|
248 |
except Exception as e:
|
@@ -546,10 +592,13 @@ class VideoAnalytics:
|
|
546 |
input_path = video
|
547 |
else:
|
548 |
return "Video Duration Above 10 Minutes,Try Below 10 Minutes Video","","",None,None,None
|
|
|
549 |
# Generate summary, important sentences, and topics
|
550 |
summary = self.generate_video_summary(model)
|
551 |
-
self.
|
552 |
-
|
|
|
|
|
553 |
important_sentences = self.extract_video_important_sentence(model)
|
554 |
self.write_text_files(important_sentences,"Important_Sentence")
|
555 |
important_sentences_voice = self.save_audio_with_gtts(important_sentences,"important_sentences.mp3")
|
@@ -558,7 +607,7 @@ class VideoAnalytics:
|
|
558 |
topics_voice = self.save_audio_with_gtts(topics,"topics.mp3")
|
559 |
|
560 |
# Return the generated summary, important sentences, and topics
|
561 |
-
return
|
562 |
|
563 |
except Exception as e:
|
564 |
# Log any errors that occur during video analytics
|
@@ -608,7 +657,7 @@ class VideoAnalytics:
|
|
608 |
result = gr.Textbox(label='Answer',lines=10)
|
609 |
submit_btn.click(self.main,[video,yt_link,model_selection],[summary,Important_Sentences,Topics,summary_audio,important_sentence_audio,topics_audio])
|
610 |
question.submit(self.video_qa,[question,model],result)
|
611 |
-
demo.launch(
|
612 |
|
613 |
if __name__ == "__main__":
|
614 |
video_analytics = VideoAnalytics()
|
|
|
37 |
"""
|
38 |
# Initialize AzureOpenAI client
|
39 |
self.client = AzureOpenAI()
|
40 |
+
hf_key = os.getenv("HF_TOKEN")
|
41 |
+
self.mistral_client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1",token=hf_key)
|
|
|
42 |
|
43 |
# Initialize transcribed text variable
|
44 |
self.transcribed_text = ""
|
|
|
86 |
logging.error(f"Error occurred while converting MP3 to WAV: {e}")
|
87 |
raise e
|
88 |
|
89 |
+
def split_audio(self, input_file: str) -> List[AudioSegment]:
|
90 |
"""
|
91 |
+
Split an audio file into segments of fixed length.
|
92 |
|
93 |
Args:
|
94 |
input_file (str): Path to the input audio file.
|
95 |
|
96 |
Returns:
|
97 |
+
List[AudioSegment]: List containing segments of the input audio.
|
98 |
"""
|
99 |
try:
|
100 |
+
# Load the audio file
|
101 |
+
audio = AudioSegment.from_file(input_file)
|
102 |
+
|
103 |
+
# Define segment length in milliseconds (5 minutes = 300,000 milliseconds)
|
104 |
+
segment_length = 60000
|
105 |
+
|
106 |
+
# Split the audio into segments
|
107 |
+
segments = []
|
108 |
+
for i, start_time in enumerate(range(0, len(audio), segment_length)):
|
109 |
+
# Calculate end time for current segment
|
110 |
+
end_time = start_time + segment_length if start_time + segment_length < len(audio) else len(audio)
|
111 |
+
|
112 |
+
# Extract segment
|
113 |
+
segment = audio[start_time:end_time]
|
114 |
+
|
115 |
+
# Append segment to list
|
116 |
+
segments.append(segment)
|
117 |
+
|
118 |
+
return segments
|
119 |
+
except CouldntDecodeError as e:
|
120 |
+
logging.error(f"Error decoding audio: {e}")
|
121 |
return []
|
|
|
122 |
# Function to recognize speech in the audio file
|
123 |
def transcribe_audio(self,path: str,lang: str):
|
124 |
"""Transcribe speech from an audio file."""
|
|
|
203 |
logging.error(f"Error transcribing video: {e}")
|
204 |
return ""
|
205 |
|
206 |
+
def extractive_summary(self,text: str):
|
207 |
+
"""
|
208 |
+
Generate an extractive summary of the input text.
|
209 |
+
|
210 |
+
Args:
|
211 |
+
text (str): The input text to be summarized.
|
212 |
+
|
213 |
+
Returns:
|
214 |
+
str: The extractive summary of the input text.
|
215 |
+
"""
|
216 |
+
try:
|
217 |
+
article_text =text
|
218 |
+
# Removing Square Brackets and Extra Spaces
|
219 |
+
article_text = re.sub(r'\[[0-9]*\]', ' ', article_text)
|
220 |
+
article_text = re.sub(r'\s+', ' ', article_text)
|
221 |
+
# Removing special characters and digits
|
222 |
+
formatted_article_text = re.sub('[^a-zA-Z]', ' ', article_text )
|
223 |
+
formatted_article_text = re.sub(r'\s+', ' ', formatted_article_text)
|
224 |
+
sentence_list = nltk.sent_tokenize(article_text)
|
225 |
+
stopwords = nltk.corpus.stopwords.words('english')
|
226 |
+
|
227 |
+
word_frequencies = {}
|
228 |
+
for word in nltk.word_tokenize(formatted_article_text):
|
229 |
+
if word not in stopwords:
|
230 |
+
if word not in word_frequencies.keys():
|
231 |
+
word_frequencies[word] = 1
|
232 |
+
else:
|
233 |
+
word_frequencies[word] += 1
|
234 |
+
maximum_frequncy = max(word_frequencies.values())
|
235 |
+
for word in word_frequencies.keys():
|
236 |
+
word_frequencies[word] = (word_frequencies[word]/maximum_frequncy)
|
237 |
+
sentence_scores = {}
|
238 |
+
for sent in sentence_list:
|
239 |
+
for word in nltk.word_tokenize(sent.lower()):
|
240 |
+
if word in word_frequencies.keys():
|
241 |
+
if len(sent.split(' ')) < 30:
|
242 |
+
if sent not in sentence_scores.keys():
|
243 |
+
sentence_scores[sent] = word_frequencies[word]
|
244 |
+
else:
|
245 |
+
sentence_scores[sent] += word_frequencies[word]
|
246 |
+
import heapq
|
247 |
+
summary_sentences = heapq.nlargest(12, sentence_scores, key=sentence_scores.get)
|
248 |
+
|
249 |
+
summary = ' '.join(summary_sentences)
|
250 |
+
return summary
|
251 |
+
except Exception as e:
|
252 |
+
logging.error(f"Error occurred during summarization: {e}")
|
253 |
+
return ""
|
254 |
+
|
255 |
def generate_video_summary(self,model) -> str:
|
256 |
"""
|
257 |
Generate a summary of the transcribe_video.
|
|
|
263 |
# Define a conversation between system and user
|
264 |
conversation = [
|
265 |
{"role": "system", "content": "You are a Summarizer"},
|
266 |
+
{"role": "user", "content": f"""summarize the following text delimited by triple backticks.Output must in english.give me a detailed summary.abstractive summary working be like summary of what about the given text.don't make bullet points write like a passage.
|
267 |
+
In this format of Outputs given below:
|
268 |
Abstractive Summary:
|
|
|
269 |
```{self.english_text}```
|
270 |
"""}
|
271 |
]
|
|
|
283 |
elif model == "Mixtral":
|
284 |
task = "summary"
|
285 |
# Generate answer using Mixtral model
|
286 |
+
prompt = f"""<s>[INST]summarize the following text delimited by triple backticks.Output must in english.give me a detailed summary.abstractive summary working be like summary of what about the given text.don't make bullet points write like a passage.
|
287 |
+
In this format of Outputs given below:
|
288 |
+
Abstractive Summary:
|
|
|
289 |
```data:{self.english_text}```[/INST]"""
|
290 |
result = self.generate(prompt)
|
291 |
+
print("self.english_text",self.english_text)
|
292 |
return result
|
293 |
|
294 |
except Exception as e:
|
|
|
592 |
input_path = video
|
593 |
else:
|
594 |
return "Video Duration Above 10 Minutes,Try Below 10 Minutes Video","","",None,None,None
|
595 |
+
overall_summary = ""
|
596 |
# Generate summary, important sentences, and topics
|
597 |
summary = self.generate_video_summary(model)
|
598 |
+
extractive_summary = self.extractive_summary(self.english_text)
|
599 |
+
overall_summary = summary + "\n\n Extractive Summary: \n\n" + extractive_summary
|
600 |
+
self.write_text_files(overall_summary,"Summary")
|
601 |
+
summary_voice = self.save_audio_with_gtts(overall_summary,"summary.mp3")
|
602 |
important_sentences = self.extract_video_important_sentence(model)
|
603 |
self.write_text_files(important_sentences,"Important_Sentence")
|
604 |
important_sentences_voice = self.save_audio_with_gtts(important_sentences,"important_sentences.mp3")
|
|
|
607 |
topics_voice = self.save_audio_with_gtts(topics,"topics.mp3")
|
608 |
|
609 |
# Return the generated summary, important sentences, and topics
|
610 |
+
return overall_summary,important_sentences,topics,summary_voice,important_sentences_voice,topics_voice
|
611 |
|
612 |
except Exception as e:
|
613 |
# Log any errors that occur during video analytics
|
|
|
657 |
result = gr.Textbox(label='Answer',lines=10)
|
658 |
submit_btn.click(self.main,[video,yt_link,model_selection],[summary,Important_Sentences,Topics,summary_audio,important_sentence_audio,topics_audio])
|
659 |
question.submit(self.video_qa,[question,model],result)
|
660 |
+
demo.launch()
|
661 |
|
662 |
if __name__ == "__main__":
|
663 |
video_analytics = VideoAnalytics()
|