Spaces:
Sleeping
Sleeping
Commit
·
c7b0462
1
Parent(s):
a5ef683
Update app.py
Browse files
app.py
CHANGED
|
@@ -39,101 +39,123 @@ def load_extractive():
|
|
| 39 |
trust_remote_code = True,
|
| 40 |
)
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
def english_sum_pipeline(link):
|
| 54 |
-
download_audio(link)
|
| 55 |
-
transcript_text = get_transcription_en("video.mp3")
|
| 56 |
-
|
| 57 |
-
#extractive summarization
|
| 58 |
-
extractive_model = Summarizer()
|
| 59 |
-
extractive = extractive_model(transcript_text, num_sentences=15)
|
| 60 |
-
|
| 61 |
-
#abstractive summarization
|
| 62 |
-
device_num = 0 if torch.cuda.is_available() else -1
|
| 63 |
-
abstractive_summarizer = pipeline("summarization", model="facebook/bart-large-cnn", tokenizer="facebook/bart-large-cnn", device=device_num)
|
| 64 |
-
output_text = abstractive_summarizer(extractive)[0]["summary_text"]
|
| 65 |
-
|
| 66 |
-
return transcript_text, output_text
|
| 67 |
-
|
| 68 |
-
def english_qa_pipeline(questions, context):
|
| 69 |
-
nlp = pipeline("question-answering", model='distilbert-base-uncased-distilled-squad')
|
| 70 |
-
answers = []
|
| 71 |
-
for question in questions:
|
| 72 |
-
result = nlp(question=question, context=context)
|
| 73 |
-
answers.append(result["answer"])
|
| 74 |
-
return answers
|
| 75 |
-
'''
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
def main():
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
with st.sidebar:
|
| 80 |
-
|
|
|
|
| 81 |
|
| 82 |
with st.form("data_collection"):
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
min_value=0.1, max_value=0.9,
|
| 85 |
-
value=0.
|
| 86 |
)
|
| 87 |
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
| 89 |
submitted = st.form_submit_button("Submit")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
if submitted:
|
| 91 |
st.success('Dados coletados!', icon="✅")
|
| 92 |
else:
|
| 93 |
st.error('Dados ainda não coletados!', icon="🚨")
|
| 94 |
-
|
| 95 |
-
main()
|
| 96 |
-
|
| 97 |
-
def old_main():
|
| 98 |
-
'''
|
| 99 |
-
#Collect inputs and create the interface
|
| 100 |
-
def main():
|
| 101 |
-
header = st.container()
|
| 102 |
-
model = st.container()
|
| 103 |
-
model_1, model_2 = st.columns(2)
|
| 104 |
-
qa = st.container()
|
| 105 |
-
qa_1, qa_2 = st.columns(2)
|
| 106 |
-
|
| 107 |
-
with header:
|
| 108 |
-
st.title("TuringVideos")
|
| 109 |
-
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!")
|
| 110 |
-
|
| 111 |
-
with model:
|
| 112 |
-
st.header("Modelo para sumarização")
|
| 113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
with model_1:
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
with model_2:
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
else:
|
| 132 |
-
st.write("Erro na seleção de linguagem.")
|
| 133 |
-
|
| 134 |
-
st.write("Sumário.....................................................................: \n {} \n \n".format(outputs[1]))
|
| 135 |
-
st.write("Resposta....................................................................: \n")
|
| 136 |
-
|
| 137 |
-
for i in range(len(answers)):
|
| 138 |
-
st.write(questions[i] + ": " + answers[i])
|
| 139 |
-
'''
|
|
|
|
| 39 |
trust_remote_code = True,
|
| 40 |
)
|
| 41 |
|
| 42 |
+
#Load QA pipeline via HuggingFace
|
| 43 |
+
@st.cache_resource
|
| 44 |
+
def load_qa():
|
| 45 |
+
return pipeline("question-answering",
|
| 46 |
+
model='distilbert-base-uncased-distilled-squad'
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
#Download punkt function from nltk
|
| 50 |
+
@st.cache_data
|
| 51 |
+
def load_nltk():
|
| 52 |
+
nltk.download("punkt")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
#Make the ASR task
|
| 55 |
+
@st.cache_data
|
| 56 |
+
def audio_speech_recognition(model_pipeline, video="video.mp3"):
|
| 57 |
+
return model_pipeline(video, batch_size=8)["text"].strip()
|
| 58 |
+
|
| 59 |
+
#Make the Summarization task
|
| 60 |
+
@st.cache_data
|
| 61 |
+
def text_summarization(model_pipeline, full_text, ratio):
|
| 62 |
+
sentences = nltk.sent_tokenize(full_text)
|
| 63 |
+
extractive_sentences = model_pipeline({"sentences": sentences}, strategy="ratio", strategy_args=ratio)
|
| 64 |
+
extractive_text = " ".join(extractive_sentences[0])
|
| 65 |
+
return extractive_text.strip()
|
| 66 |
+
|
| 67 |
+
#Make the QA task
|
| 68 |
+
@st.cache_data
|
| 69 |
+
def answer_questions(model_pipeline, full_text, questionings):
|
| 70 |
+
answers = []
|
| 71 |
+
for question in questionings:
|
| 72 |
+
result = model_pipeline(question=question, context=full_text)
|
| 73 |
+
answers.append(result["answer"])
|
| 74 |
+
return answers
|
| 75 |
+
|
| 76 |
def main():
|
| 77 |
|
| 78 |
+
header = st.container()
|
| 79 |
+
model = st.container()
|
| 80 |
+
model_1, model_2 = st.columns(2)
|
| 81 |
+
|
| 82 |
with st.sidebar:
|
| 83 |
+
|
| 84 |
+
st.title(":red[Turing]Videos")
|
| 85 |
|
| 86 |
with st.form("data_collection"):
|
| 87 |
+
|
| 88 |
+
language = st.selectbox('Qual a linguagem do seu modelo?',
|
| 89 |
+
('Português (pt)', 'Inglês (en)', 'Outra')
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
link = st.text_area(label="Coloque o link do seu vídeo do YouTube:",
|
| 93 |
+
height=25, placeholder="Digite seu link...")
|
| 94 |
+
|
| 95 |
+
compression_rate = st.slider(label="Selecione a taxa de compressão:",
|
| 96 |
min_value=0.1, max_value=0.9,
|
| 97 |
+
value=0.25, step=0.05
|
| 98 |
)
|
| 99 |
|
| 100 |
+
questions = st.text_area(label="Coloque suas perguntas separadas por vírgula!",
|
| 101 |
+
height=50, placeholder="Digite suas perguntas..."
|
| 102 |
+
).split(",")
|
| 103 |
+
|
| 104 |
submitted = st.form_submit_button("Submit")
|
| 105 |
+
|
| 106 |
+
seconds = st.select_slider(label="Digite a duração do seu vídeo para otimização:",
|
| 107 |
+
options = ["5 min", "15 min", "30 min", "45 min", "1 h"],
|
| 108 |
+
value = "15 min",
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
seconds = seconds.replace(" min", "")
|
| 112 |
+
seconds = int(seconds) * 60
|
| 113 |
+
|
| 114 |
if submitted:
|
| 115 |
st.success('Dados coletados!', icon="✅")
|
| 116 |
else:
|
| 117 |
st.error('Dados ainda não coletados!', icon="🚨")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
+
with header:
|
| 120 |
+
st.title(":red[Turing]Videos")
|
| 121 |
+
st.subheader("Este projeto inovador utiliza técnicas avançadas de inteligência artificial para simplificar e acelerar a compreensão de conteúdo audiovisual. Ao enfrentar o desafio da sobrecarga de informações em vídeos, a solução oferece uma abordagem eficiente para extrair os pontos-chave, permitindo aos usuários economizar tempo e concentrar-se nas informações essenciais.",
|
| 122 |
+
divider = “red”
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
with model:
|
| 126 |
+
if submitted:
|
| 127 |
+
with st.spinner("Carregando modelos..."):
|
| 128 |
+
|
| 129 |
+
if language == "Inglês (en)":
|
| 130 |
+
download_audio(link)
|
| 131 |
+
load_nltk()
|
| 132 |
+
whisper = load_whisper(seconds)
|
| 133 |
+
extractive = load_extractive()
|
| 134 |
+
qa_model = load_qa()
|
| 135 |
+
|
| 136 |
+
elif language = "Português (pt)":
|
| 137 |
+
st.header("Modelo ainda não implementado.")
|
| 138 |
+
|
| 139 |
+
else:
|
| 140 |
+
st.header("Erro na seleção de linguagem.")
|
| 141 |
+
|
| 142 |
+
with st.spinner("Transcrevendo texto..."):
|
| 143 |
+
transcript_text = audio_speech_recognition(whisper)
|
| 144 |
+
|
| 145 |
with model_1:
|
| 146 |
+
st.header("Texto Sumarizado:")
|
| 147 |
+
with st.spinner("Carregando sumarização..."):
|
| 148 |
+
summary = text_summarization(extractive, transcript_text, compression_rate)
|
| 149 |
+
st.subheader(summary)
|
| 150 |
+
|
| 151 |
with model_2:
|
| 152 |
+
st.header("Resposta das perguntas:")
|
| 153 |
+
with st.spinner("Carregando respostas..."):
|
| 154 |
+
answers = answer_questions(qa_model, transcript_text, questions)
|
| 155 |
+
|
| 156 |
+
for i in range(len(answers)):
|
| 157 |
+
st.subheader(questions[i])
|
| 158 |
+
st.subheader(answers[i])
|
| 159 |
+
st.write("\n")
|
| 160 |
+
|
| 161 |
+
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|