Spaces:
Runtime error
Runtime error
Tanishq commited on
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import Wav2Vec2ForCTC, Wav2Vec2Tokenizer
|
| 3 |
+
import torchaudio
|
| 4 |
+
import torch
|
| 5 |
+
import spacy
|
| 6 |
+
from transformers import pipeline
|
| 7 |
+
|
| 8 |
+
nlp_ner = spacy.load("en_core_web_sm")
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def ner(text):
|
| 12 |
+
doc = nlp_ner(text)
|
| 13 |
+
entities = [(ent.text, ent.label_) for ent in doc.ents]
|
| 14 |
+
return entities
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def main():
|
| 18 |
+
st.title("Text and Speech Analysis")
|
| 19 |
+
|
| 20 |
+
option = st.radio("Choose an option:", ("Upload Audio", "Enter Text"))
|
| 21 |
+
|
| 22 |
+
if option == "Upload Audio":
|
| 23 |
+
audio_file = st.file_uploader("Upload an audio file", type=["mp3", "wav"])
|
| 24 |
+
|
| 25 |
+
if audio_file is not None:
|
| 26 |
+
text_result = process_input(audio_file)
|
| 27 |
+
st.success("Audio processed successfully!")
|
| 28 |
+
st.text(text_result)
|
| 29 |
+
process_and_display_text(text_result)
|
| 30 |
+
|
| 31 |
+
elif option == "Enter Text":
|
| 32 |
+
text_input = st.text_area("Enter your text here:")
|
| 33 |
+
|
| 34 |
+
if st.button("Submit"):
|
| 35 |
+
if text_input:
|
| 36 |
+
process_and_display_text(text_input)
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def process_input(audio_input):
|
| 40 |
+
model = Wav2Vec2ForCTC.from_pretrained("facebook/wav2vec2-base-960h")
|
| 41 |
+
tokenizer = Wav2Vec2Tokenizer.from_pretrained("facebook/wav2vec2-base-960h")
|
| 42 |
+
audio_input, _ = torchaudio.load(audio_input)
|
| 43 |
+
input_values = tokenizer(audio_input.squeeze().numpy(), return_tensors="pt").input_values
|
| 44 |
+
|
| 45 |
+
with torch.no_grad():
|
| 46 |
+
logits = model(input_values).logits
|
| 47 |
+
|
| 48 |
+
prediction_ids = torch.argmax(logits, dim=-1)
|
| 49 |
+
transcription = tokenizer.batch_decode(prediction_ids)[0]
|
| 50 |
+
return transcription
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def process_and_display_text(input_text):
|
| 54 |
+
summarization_pipeline = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 55 |
+
ner_results = dict(set(ner(input_text)))
|
| 56 |
+
summary = summarization_pipeline(
|
| 57 |
+
input_text, max_length=150, min_length=50, length_penalty=2.0, num_beams=4, temperature=0.7
|
| 58 |
+
)
|
| 59 |
+
st.write("Named Entities")
|
| 60 |
+
st.table(ner_results)
|
| 61 |
+
st.write("Summary")
|
| 62 |
+
st.write(summary[0]["summary_text"])
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
if __name__ == "__main__":
|
| 66 |
+
main()
|