Spaces:
Sleeping
Sleeping
Commit
·
a9eb918
1
Parent(s):
3d30741
Create app.py with english functionality
Browse filesCreate the app.py file with the dependencies and streamlit interface and implement the english pipelines
app.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import yt_dlp
|
| 2 |
+
import whisper
|
| 3 |
+
import os
|
| 4 |
+
import transformers
|
| 5 |
+
from transformers import pipeline
|
| 6 |
+
import torch
|
| 7 |
+
from summarizer import Summarizer
|
| 8 |
+
|
| 9 |
+
#Download youtube video
|
| 10 |
+
def download_audio(link):
|
| 11 |
+
with yt_dlp.YoutubeDL({'extract_audio': True, 'format': 'bestaudio', 'outtmpl': 'video.mp3'}) as video:
|
| 12 |
+
info_dict = video.extract_info(link, download = True)
|
| 13 |
+
video_title = info_dict['title']
|
| 14 |
+
video.download(link)
|
| 15 |
+
return video_title
|
| 16 |
+
|
| 17 |
+
#def portuguese_sum_pipeline(link):
|
| 18 |
+
# video_title = download_audio(link)
|
| 19 |
+
|
| 20 |
+
def english_sum_pipeline(link):
|
| 21 |
+
video_title = download_audio(link)
|
| 22 |
+
|
| 23 |
+
#audio-to-text
|
| 24 |
+
transcriptor = whisper.load_model("base.en")
|
| 25 |
+
directory = os.getcwd()
|
| 26 |
+
result = model.transcribe(os.path.join(directory, "video.mp3"))
|
| 27 |
+
transcript_text = result["text"]
|
| 28 |
+
|
| 29 |
+
#extractive summarization
|
| 30 |
+
extractive_model = Summarizer()
|
| 31 |
+
extractive = extractive_model(transcript_text, num_sentences=15)
|
| 32 |
+
|
| 33 |
+
#abstractive summarization
|
| 34 |
+
device_num = 0 if torch.cuda.is_available() else -1
|
| 35 |
+
abstractive_summarizer = pipeline("summarization", model="facebook/bart-large-cnn", tokenizer="facebook/bart-large-cnn", device=device_num)
|
| 36 |
+
output_text = abstractive_summarizer(extractive)[0]["summary_text"]
|
| 37 |
+
|
| 38 |
+
return transcript_text, output_text
|
| 39 |
+
|
| 40 |
+
def english_qa_pipeline(question, context):
|
| 41 |
+
nlp = pipeline("question-answering", model='distilbert-base-uncased-distilled-squad')
|
| 42 |
+
result = nlp(question=question, context=context)
|
| 43 |
+
return result["answer"]
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
#Collect inputs and create the interface
|
| 48 |
+
def main():
|
| 49 |
+
header = st.container()
|
| 50 |
+
model = st.container()
|
| 51 |
+
model_1, model_2 = st.columns(2)
|
| 52 |
+
qa = st.container
|
| 53 |
+
qa_1, qa_2 = st.columns(2)
|
| 54 |
+
|
| 55 |
+
with header:
|
| 56 |
+
st.title("TuringVideos")
|
| 57 |
+
st.write("Este trabalho visa a criação de uma interface capaz de sumarizar e responder perguntas sobre um determinado vídeo em português ou inglês!")
|
| 58 |
+
|
| 59 |
+
with model:
|
| 60 |
+
st.header("Modelo para sumarização")
|
| 61 |
+
|
| 62 |
+
with model_1:
|
| 63 |
+
language = st.selectbox('Qual a linguagem do seu modelo?', ('Português (pt)', 'Inglês (en)', 'Outra'))
|
| 64 |
+
link = st.text_area(label="Coloque o link do seu vídeo do YouTube!", height=25, placeholder="Digite seu link...")
|
| 65 |
+
submit_1 = st.button('Gerar sumário!')
|
| 66 |
+
|
| 67 |
+
with model_2:
|
| 68 |
+
if submit_1:
|
| 69 |
+
if language == 'Português (pt)':
|
| 70 |
+
#outputs = portuguese_sum_pipeline(link)
|
| 71 |
+
st.write("Modelo ainda não implementado.")
|
| 72 |
+
|
| 73 |
+
elif language == 'Inglês (en)':
|
| 74 |
+
outputs = english_sum_pipeline(link)
|
| 75 |
+
|
| 76 |
+
else:
|
| 77 |
+
st.write("Erro na seleção de linguagem.")
|
| 78 |
+
|
| 79 |
+
st.write("Sumário.....................................................................: \n {} \n \n".format(outputs[1]))
|
| 80 |
+
|
| 81 |
+
with qa:
|
| 82 |
+
st.header("Modelo para resposta de perguntas")
|
| 83 |
+
|
| 84 |
+
with model_1:
|
| 85 |
+
question = st.text_area(label="Digite sua pergunta para o modelo", height=25, placeholder="Digite sua pergunta...")
|
| 86 |
+
submit = st.button('Gerar resposta!')
|
| 87 |
+
|
| 88 |
+
with model_2:
|
| 89 |
+
if submit_2:
|
| 90 |
+
if language == 'Português (pt)':
|
| 91 |
+
#answer = portuguese_qa_pipeline(link)
|
| 92 |
+
st.write("Modelo ainda não implementado.")
|
| 93 |
+
|
| 94 |
+
elif language == 'Inglês (en)':
|
| 95 |
+
answer = english_qa_pipeline(question, outputs[0])
|
| 96 |
+
|
| 97 |
+
else:
|
| 98 |
+
st.write("Erro na seleção de linguagem.")
|
| 99 |
+
|
| 100 |
+
st.write("Resposta.....................................................................: \n {} \n \n".format(answer))
|
| 101 |
+
|
| 102 |
+
main()
|