Spaces:
Running
Running
KarthickAdopleAI
commited on
Commit
•
7473f11
1
Parent(s):
6f1058d
Update app.py
Browse files
app.py
CHANGED
@@ -27,7 +27,6 @@ class VideoAnalytics:
|
|
27 |
def __init__(self):
|
28 |
"""
|
29 |
Initialize the VideoAnalytics object.
|
30 |
-
|
31 |
Args:
|
32 |
hf_token (str): Hugging Face API token.
|
33 |
"""
|
@@ -39,16 +38,7 @@ class VideoAnalytics:
|
|
39 |
# Initialize transcribed text variable
|
40 |
self.transcribed_text = ""
|
41 |
|
42 |
-
|
43 |
-
self.API_URL = "https://api-inference.huggingface.co/models/openai/whisper-large-v3"
|
44 |
-
|
45 |
-
|
46 |
-
hf_token = os.getenv('HF_TOKEN')
|
47 |
-
# Placeholder for Hugging Face API token
|
48 |
-
self.hf_token = hf_token # Replace this with the actual Hugging Face API token
|
49 |
-
|
50 |
-
# Set headers for API requests with Hugging Face token
|
51 |
-
self.headers = {"Authorization": f"Bearer {self.hf_token}"}
|
52 |
|
53 |
# Initialize english text variable
|
54 |
self.english_text = ""
|
@@ -64,10 +54,8 @@ class VideoAnalytics:
|
|
64 |
def transcribe_video(self, vid: str) -> str:
|
65 |
"""
|
66 |
Transcribe the audio of the video.
|
67 |
-
|
68 |
Args:
|
69 |
vid (str): Path to the video file.
|
70 |
-
|
71 |
Returns:
|
72 |
str: Transcribed text.
|
73 |
"""
|
@@ -79,22 +67,13 @@ class VideoAnalytics:
|
|
79 |
# Write audio to a temporary file
|
80 |
audio.write_audiofile("output_audio.mp3")
|
81 |
audio_file = open("output_audio.mp3", "rb")
|
82 |
-
|
83 |
-
# Define a helper function to query the Hugging Face model
|
84 |
-
def query(data):
|
85 |
-
response = requests.post(self.API_URL, headers=self.headers, data=data)
|
86 |
-
return response.json()
|
87 |
-
|
88 |
-
# Send audio data to the Hugging Face model for transcription
|
89 |
-
output = query(audio_file)
|
90 |
-
|
91 |
-
print(output)
|
92 |
# Update the transcribed_text attribute with the transcription result
|
93 |
-
self.transcribed_text =
|
94 |
# Update the translation text into english_text
|
95 |
self.english_text = self.translation()
|
96 |
# Return the transcribed text
|
97 |
-
return
|
98 |
|
99 |
except Exception as e:
|
100 |
logging.error(f"Error transcribing video: {e}")
|
@@ -401,7 +380,7 @@ class VideoAnalytics:
|
|
401 |
video_ = VideoFileClip(input_path)
|
402 |
duration = video_.duration
|
403 |
video_.close()
|
404 |
-
if round(duration) <=
|
405 |
text = self.transcribe_video(input_path)
|
406 |
else:
|
407 |
return "Video Duration Above 10 Minutes,Try Below 10 Minutes Video","",""
|
@@ -409,7 +388,7 @@ class VideoAnalytics:
|
|
409 |
video_ = VideoFileClip(video)
|
410 |
duration = video_.duration
|
411 |
video_.close()
|
412 |
-
if round(duration) <=
|
413 |
text = self.transcribe_video(video)
|
414 |
input_path = video
|
415 |
else:
|
|
|
27 |
def __init__(self):
|
28 |
"""
|
29 |
Initialize the VideoAnalytics object.
|
|
|
30 |
Args:
|
31 |
hf_token (str): Hugging Face API token.
|
32 |
"""
|
|
|
38 |
# Initialize transcribed text variable
|
39 |
self.transcribed_text = ""
|
40 |
|
41 |
+
self.s2t_model = SpeechRecognitionModel("jonatasgrosman/wav2vec2-large-xlsr-53-english")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
# Initialize english text variable
|
44 |
self.english_text = ""
|
|
|
54 |
def transcribe_video(self, vid: str) -> str:
|
55 |
"""
|
56 |
Transcribe the audio of the video.
|
|
|
57 |
Args:
|
58 |
vid (str): Path to the video file.
|
|
|
59 |
Returns:
|
60 |
str: Transcribed text.
|
61 |
"""
|
|
|
67 |
# Write audio to a temporary file
|
68 |
audio.write_audiofile("output_audio.mp3")
|
69 |
audio_file = open("output_audio.mp3", "rb")
|
70 |
+
transcriptions = self.s2t_model.transcribe(["output_audio.mp3"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
# Update the transcribed_text attribute with the transcription result
|
72 |
+
self.transcribed_text = transcriptions[0]['transcription']
|
73 |
# Update the translation text into english_text
|
74 |
self.english_text = self.translation()
|
75 |
# Return the transcribed text
|
76 |
+
return transcriptions[0]['transcription']
|
77 |
|
78 |
except Exception as e:
|
79 |
logging.error(f"Error transcribing video: {e}")
|
|
|
380 |
video_ = VideoFileClip(input_path)
|
381 |
duration = video_.duration
|
382 |
video_.close()
|
383 |
+
if round(duration) <= 36000:
|
384 |
text = self.transcribe_video(input_path)
|
385 |
else:
|
386 |
return "Video Duration Above 10 Minutes,Try Below 10 Minutes Video","",""
|
|
|
388 |
video_ = VideoFileClip(video)
|
389 |
duration = video_.duration
|
390 |
video_.close()
|
391 |
+
if round(duration) <= 36000:
|
392 |
text = self.transcribe_video(video)
|
393 |
input_path = video
|
394 |
else:
|