Spaces:
Runtime error
Runtime error
Commit
·
d8b182e
1
Parent(s):
6b90fe2
initial
Browse files- requirements.txt +5 -0
- spel.py +170 -0
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
yt_dlp
|
2 |
+
ffmpeg-python
|
3 |
+
gradio
|
4 |
+
huggingface_hub
|
5 |
+
transformers
|
spel.py
ADDED
@@ -0,0 +1,170 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""## Divide the video into one 10s before and one 10s after"""
|
2 |
+
|
3 |
+
# Login to HuggingFace somehow (todo)
|
4 |
+
|
5 |
+
"""## Convert to audio
|
6 |
+
|
7 |
+
## Get model
|
8 |
+
"""
|
9 |
+
|
10 |
+
from transformers import pipeline
|
11 |
+
pipe = pipeline(model="Nathanotal/whisper-small-hi") # change to "your-username/the-name-you-picked"
|
12 |
+
|
13 |
+
def transcribe(audio):
|
14 |
+
text = pipe(audio)["text"]
|
15 |
+
|
16 |
+
return text
|
17 |
+
|
18 |
+
"""## Download and trim the video"""
|
19 |
+
|
20 |
+
# Commented out IPython magic to ensure Python compatibility.
|
21 |
+
def downloadAndTranscribeVideo(source_url):
|
22 |
+
"""**Input url to youtube video**"""
|
23 |
+
if "=" in source_url:
|
24 |
+
id = source_url.split('=', 1)[1]
|
25 |
+
else:
|
26 |
+
id = source_url.split('/')[-1]
|
27 |
+
|
28 |
+
# Empty folder
|
29 |
+
# %rm -rf '/content/drive/My Drive/ID2223/LAB2/'
|
30 |
+
|
31 |
+
"""**Create output folder**"""
|
32 |
+
|
33 |
+
# change this to /content/drive/My Drive/folder_you_want
|
34 |
+
output_folder = '/content/drive/My Drive/ID2223/LAB2/'
|
35 |
+
|
36 |
+
import os
|
37 |
+
def my_mkdirs(folder):
|
38 |
+
if os.path.exists(folder)==False:
|
39 |
+
os.makedirs(folder)
|
40 |
+
my_mkdirs('/content/tmp/')
|
41 |
+
|
42 |
+
my_mkdirs(output_folder)
|
43 |
+
|
44 |
+
# Get URLs to video file and audio file
|
45 |
+
# Attempt to get 720p clip, else get best possible quality
|
46 |
+
try:
|
47 |
+
video_url, audio_url = !yt-dlp -g -f bv[height=720][ext=webm]+ba[ext=m4a] "{source_url}"
|
48 |
+
except:
|
49 |
+
video_url, audio_url = !yt-dlp -g -f bv[ext=webm]+ba[ext=m4a] "{source_url}"
|
50 |
+
|
51 |
+
print('Video:', video_url)
|
52 |
+
print('Audio:', audio_url)
|
53 |
+
|
54 |
+
"""**Download part of video and audio files**"""
|
55 |
+
|
56 |
+
temp_video = "temp_video.mkv"
|
57 |
+
temp_audio = "temp_audio.m4a"
|
58 |
+
|
59 |
+
# Download video file (first 10 seconds)
|
60 |
+
!ffmpeg -probesize 10M -y -i "{video_url}" -ss 00:00:00 -t 00:00:10 -c copy "{output_folder}{temp_video}"
|
61 |
+
|
62 |
+
# Download audio file (first 10 seconds)
|
63 |
+
!ffmpeg -probesize 10M -y -i "{audio_url}" -ss 00:00:00 -t 00:00:10 -c copy "{output_folder}{temp_audio}"
|
64 |
+
|
65 |
+
|
66 |
+
"""**MUX video and audio files**"""
|
67 |
+
temp_output = "output.mp4"
|
68 |
+
|
69 |
+
# MUX video and audio files into final output [mkv]
|
70 |
+
!ffmpeg -hide_banner -loglevel error -y -i "{output_folder}{temp_video}" -i "{output_folder}{temp_audio}" -c copy "{output_folder}{temp_output}"
|
71 |
+
|
72 |
+
first10Video = "first10Video.mp4"
|
73 |
+
second10Video = "second10Video.mp4"
|
74 |
+
|
75 |
+
!ffmpeg -hide_banner -loglevel error -y -i "{output_folder}{temp_output}" -ss 00:00:00 -to 00:00:05 -c copy "{output_folder}{first10Video}"
|
76 |
+
!ffmpeg -hide_banner -loglevel error -y -i "{output_folder}{temp_output}" -ss 00:00:05 -to 00:00:10 -c copy "{output_folder}{second10Video}"
|
77 |
+
|
78 |
+
first10Audio = "first10Audio.m4a"
|
79 |
+
second10Audio = "second10Audio.m4a"
|
80 |
+
|
81 |
+
!ffmpeg -hide_banner -loglevel error -y -i "{output_folder}{first10Video}" -vn -acodec copy "{output_folder}{first10Audio}"
|
82 |
+
!ffmpeg -hide_banner -loglevel error -y -i "{output_folder}{second10Video}" -vn -acodec copy "{output_folder}{second10Audio}"
|
83 |
+
|
84 |
+
first10AudioFinal = "first10AudioFinal.mp3"
|
85 |
+
second10AudioFinal = "second10AudioFinal.mp3"
|
86 |
+
|
87 |
+
!ffmpeg -i "{output_folder}{first10Audio}" -c:v copy -c:a libmp3lame -q:a 4 "{output_folder}{first10AudioFinal}"
|
88 |
+
!ffmpeg -i "{output_folder}{second10Audio}" -c:v copy -c:a libmp3lame -q:a 4 "{output_folder}{second10AudioFinal}"
|
89 |
+
|
90 |
+
firstVideoText = transcribe('/content/drive/My Drive/ID2223/LAB2/first10AudioFinal.mp3')
|
91 |
+
secondVideoText = transcribe('/content/drive/My Drive/ID2223/LAB2/second10AudioFinal.mp3')
|
92 |
+
|
93 |
+
# Delete temporary files
|
94 |
+
!rm "{output_folder}{temp_video}"
|
95 |
+
!rm "{output_folder}{temp_audio}"
|
96 |
+
|
97 |
+
return firstVideoText, secondVideoText
|
98 |
+
|
99 |
+
# print(downloadAndTranscribeVideo('https://www.youtube.com/watch?v=93WrIPY4_4E'))
|
100 |
+
|
101 |
+
"""## Build UI"""
|
102 |
+
|
103 |
+
from transformers import pipeline
|
104 |
+
import gradio as gr
|
105 |
+
|
106 |
+
def calculateSimilarity(texta, actualText):
|
107 |
+
texta = texta.lower().strip()
|
108 |
+
actualText = actualText.lower().strip()
|
109 |
+
textaL = texta.split(" ")
|
110 |
+
actualTextL = actualText.split(" ")
|
111 |
+
|
112 |
+
totalWords = len(actualTextL)
|
113 |
+
matchingWords = 0
|
114 |
+
|
115 |
+
for word in textaL:
|
116 |
+
if word in actualTextL:
|
117 |
+
matchingWords += 1
|
118 |
+
|
119 |
+
return int(100*(matchingWords / totalWords))
|
120 |
+
|
121 |
+
|
122 |
+
def game(videoLink, loadVideo, audio1, audio2, theState):
|
123 |
+
theState = theState or []
|
124 |
+
firstText = "test"
|
125 |
+
secondText = "test"
|
126 |
+
if loadVideo:
|
127 |
+
firstText, secondText = downloadAndTranscribeVideo(videoLink)
|
128 |
+
theState = [firstText, secondText]
|
129 |
+
return "/content/drive/My Drive/ID2223/LAB2/first10Video.mp4", firstText, "", "", "", "", "", "/content/drive/My Drive/ID2223/LAB2/second10Video.mp4", "", theState
|
130 |
+
elif len(theState) == 0:
|
131 |
+
return "/content/drive/My Drive/ID2223/LAB2/first10Video.mp4", "", "", "", "", "", "", "/content/drive/My Drive/ID2223/LAB2/second10Video.mp4", "", theState
|
132 |
+
else:
|
133 |
+
firstText, secondText = theState[0], theState[1]
|
134 |
+
|
135 |
+
t1 = transcribe(audio1)
|
136 |
+
t2 = transcribe(audio2)
|
137 |
+
t1Res = calculateSimilarity(t1, secondText)
|
138 |
+
t2Res = calculateSimilarity(t2, secondText)
|
139 |
+
|
140 |
+
res = 'The game is even, everybody wins!'
|
141 |
+
if t1Res > t2Res:
|
142 |
+
res = 'Player 1 won!'
|
143 |
+
elif t1Res < t2Res:
|
144 |
+
res = 'Player 2 won!'
|
145 |
+
|
146 |
+
return "/content/drive/My Drive/ID2223/LAB2/first10Video.mp4", firstText, t1, str(t1Res) + '% match', t2, str(t2Res) + '% match', res, "/content/drive/My Drive/ID2223/LAB2/second10Video.mp4", secondText, theState
|
147 |
+
|
148 |
+
# exInputs = [[None], [None], ["/content/ut.webm"]]
|
149 |
+
|
150 |
+
gameInterface = gr.Interface(fn=game,
|
151 |
+
inputs=[gr.Textbox(label='Link to video'),
|
152 |
+
gr.Checkbox(label='Load a new video'),
|
153 |
+
gr.Audio(source="microphone", type="filepath", label='Player 1\'s guess'),
|
154 |
+
gr.Audio(source="microphone", type="filepath", label='Player 2\'s guess'),
|
155 |
+
"state"],
|
156 |
+
outputs=[gr.Video(label='First ten seconds'),
|
157 |
+
gr.Textbox(label='Transcription of first ten seconds'),
|
158 |
+
gr.Textbox(label='Transcription for player 1'),
|
159 |
+
gr.Textbox(label='Percentage match:'),
|
160 |
+
gr.Textbox(label='Transcription for player 2'),
|
161 |
+
gr.Textbox(label='Percentage match:'),
|
162 |
+
gr.Textbox(label='Result:'),
|
163 |
+
gr.Video(label='Next ten seconds'),
|
164 |
+
gr.Textbox(label='Transcription of next ten seconds'),
|
165 |
+
"state"],
|
166 |
+
title="Game",
|
167 |
+
description="Description of game"
|
168 |
+
)
|
169 |
+
|
170 |
+
gameInterface.launch()
|