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|
|
|
|
|
|
|
|
|
| 1 |
+
import whisper
|
| 2 |
+
|
| 3 |
+
def transcribe_audio(audio_path):
|
| 4 |
+
model = whisper.load_model("base")
|
| 5 |
+
result = model.transcribe(audio_path)
|
| 6 |
+
return result["text"]
|
| 7 |
+
from pyannote.audio import Pipeline
|
| 8 |
+
|
| 9 |
+
def perform_speaker_diarization(audio_path):
|
| 10 |
+
pipeline = Pipeline.from_pretrained("pyannote/speaker-diarization@2.1", use_auth_token="YOUR_HUGGINGFACE_TOKEN")
|
| 11 |
+
diarization = pipeline(audio_path)
|
| 12 |
+
|
| 13 |
+
speaker_segments = []
|
| 14 |
+
for turn, _, speaker in diarization.itertracks(yield_label=True):
|
| 15 |
+
speaker_segments.append({
|
| 16 |
+
"start": turn.start,
|
| 17 |
+
"end": turn.end,
|
| 18 |
+
"speaker": speaker
|
| 19 |
+
})
|
| 20 |
+
return speaker_segments
|
| 21 |
+
|
| 22 |
+
from textblob import TextBlob
|
| 23 |
+
from sklearn.feature_extraction.text import CountVectorizer
|
| 24 |
+
from sklearn.decomposition import LatentDirichletAllocation
|
| 25 |
+
from collections import Counter
|
| 26 |
+
import nltk
|
| 27 |
+
from nltk.corpus import stopwords
|
| 28 |
+
import spacy
|
| 29 |
+
|
| 30 |
+
nltk.download('stopwords')
|
| 31 |
+
nltk.download('punkt')
|
| 32 |
+
|
| 33 |
+
# Load spaCy model for NER
|
| 34 |
+
nlp = spacy.load("en_core_web_sm")
|
| 35 |
+
|
| 36 |
+
def analyze_sentiment(text):
|
| 37 |
+
blob = TextBlob(text)
|
| 38 |
+
return blob.sentiment.polarity, blob.sentiment.subjectivity
|
| 39 |
+
|
| 40 |
+
def extract_keywords(text, top_n=5):
|
| 41 |
+
stop_words = set(stopwords.words("english"))
|
| 42 |
+
words = nltk.word_tokenize(text.lower())
|
| 43 |
+
filtered_words = [word for word in words if word.isalnum() and word not in stop_words]
|
| 44 |
+
word_counts = Counter(filtered_words)
|
| 45 |
+
return word_counts.most_common(top_n)
|
| 46 |
+
|
| 47 |
+
def perform_topic_modeling(text, num_topics=5, num_words=10):
|
| 48 |
+
vectorizer = CountVectorizer(stop_words="english", max_features=1000)
|
| 49 |
+
X = vectorizer.fit_transform([text])
|
| 50 |
+
lda = LatentDirichletAllocation(n_components=num_topics, random_state=42)
|
| 51 |
+
lda.fit(X)
|
| 52 |
+
|
| 53 |
+
topics = []
|
| 54 |
+
for idx, topic in enumerate(lda.components_):
|
| 55 |
+
top_words = [vectorizer.get_feature_names_out()[i] for i in topic.argsort()[:-num_words - 1:-1]]
|
| 56 |
+
topics.append(f"Topic {idx + 1}: {' '.join(top_words)}")
|
| 57 |
+
return topics
|
| 58 |
+
|
| 59 |
+
def extract_entities(text):
|
| 60 |
+
doc = nlp(text)
|
| 61 |
+
entities = [(ent.text, ent.label_) for ent in doc.ents]
|
| 62 |
+
return entities
|
| 63 |
+
|
| 64 |
+
def parse_query(query):
|
| 65 |
+
doc = nlp(query)
|
| 66 |
+
keywords = [token.text.lower() for token in doc if token.is_alpha and not token.is_stop]
|
| 67 |
+
intent = None
|
| 68 |
+
|
| 69 |
+
if any(word in ["how many", "count"] for word in keywords):
|
| 70 |
+
intent = "count"
|
| 71 |
+
elif any(word in ["list", "show me"] for word in keywords):
|
| 72 |
+
intent = "list"
|
| 73 |
+
elif any(word in ["sentiment", "polarity", "subjectivity"] for word in keywords):
|
| 74 |
+
intent = "sentiment"
|
| 75 |
+
elif any(word in ["theme", "topic", "main"] for word in keywords):
|
| 76 |
+
intent = "topic"
|
| 77 |
+
elif any(word in ["keyword", "common"] for word in keywords):
|
| 78 |
+
intent = "keyword"
|
| 79 |
+
elif any(word in ["entity", "name", "person", "organization"] for word in keywords):
|
| 80 |
+
intent = "ner"
|
| 81 |
+
return intent, keywords
|
| 82 |
+
|
| 83 |
+
def answer_question(query, qa_df):
|
| 84 |
+
intent, keywords = parse_query(query)
|
| 85 |
+
|
| 86 |
+
if intent == "count":
|
| 87 |
+
filtered = qa_df[qa_df["Transcript"].str.contains("|".join(keywords), case=False)]
|
| 88 |
+
return f"{len(filtered)} responses contain the keywords: {', '.join(keywords)}."
|
| 89 |
+
|
| 90 |
+
elif intent == "list":
|
| 91 |
+
filtered = qa_df[qa_df["Transcript"].str.contains("|".join(keywords), case=False)]["Transcript"].tolist()
|
| 92 |
+
return "\n".join(filtered) if filtered else "No matching responses found."
|
| 93 |
+
|
| 94 |
+
elif intent == "sentiment":
|
| 95 |
+
avg_polarity = qa_df["Sentiment_Polarity"].mean()
|
| 96 |
+
avg_subjectivity = qa_df["Sentiment_Subjectivity"].mean()
|
| 97 |
+
return f"Average Polarity: {avg_polarity:.2f}, Average Subjectivity: {avg_subjectivity:.2f}"
|
| 98 |
+
|
| 99 |
+
elif intent == "topic":
|
| 100 |
+
all_text = " ".join(qa_df["Transcript"])
|
| 101 |
+
topics = perform_topic_modeling(all_text)
|
| 102 |
+
return "\n".join(topics)
|
| 103 |
+
|
| 104 |
+
elif intent == "keyword":
|
| 105 |
+
all_text = " ".join(qa_df["Transcript"])
|
| 106 |
+
keywords = extract_keywords(all_text)
|
| 107 |
+
return ", ".join([word for word, count in keywords])
|
| 108 |
+
|
| 109 |
+
elif intent == "ner":
|
| 110 |
+
all_text = " ".join(qa_df["Transcript"])
|
| 111 |
+
entities = extract_entities(all_text)
|
| 112 |
+
return "\n".join([f"{entity} ({label})" for entity, label in entities])
|
| 113 |
+
|
| 114 |
+
else:
|
| 115 |
+
return "I'm not sure how to answer that. Try asking about counts, lists, sentiment, topics, keywords, or entities."
|
| 116 |
+
|
| 117 |
+
import gradio as gr
|
| 118 |
+
|
| 119 |
+
# Global variables to store processed data
|
| 120 |
+
qa_df = None
|
| 121 |
+
|
| 122 |
+
def process_audio(audio_path):
|
| 123 |
+
global qa_df
|
| 124 |
+
|
| 125 |
+
# Step 1: Transcribe audio
|
| 126 |
+
transcription = transcribe_audio(audio_path)
|
| 127 |
+
|
| 128 |
+
# Step 2: Perform speaker diarization
|
| 129 |
+
speaker_segments = perform_speaker_diarization(audio_path)
|
| 130 |
+
|
| 131 |
+
# Step 3: Analyze text
|
| 132 |
+
sentiment_polarity, sentiment_subjectivity = analyze_sentiment(transcription)
|
| 133 |
+
topics = perform_topic_modeling(transcription)
|
| 134 |
+
keywords = extract_keywords(transcription)
|
| 135 |
+
entities = extract_entities(transcription)
|
| 136 |
+
|
| 137 |
+
# Create a DataFrame
|
| 138 |
+
qa_df = pd.DataFrame({
|
| 139 |
+
"Speaker": [seg["speaker"] for seg in speaker_segments],
|
| 140 |
+
"Transcript": [transcription],
|
| 141 |
+
"Sentiment_Polarity": [sentiment_polarity],
|
| 142 |
+
"Sentiment_Subjectivity": [sentiment_subjectivity],
|
| 143 |
+
"Topics": [topics],
|
| 144 |
+
"Keywords": [keywords],
|
| 145 |
+
"Entities": [entities]
|
| 146 |
+
})
|
| 147 |
+
|
| 148 |
+
return "Audio processed successfully!"
|
| 149 |
+
|
| 150 |
+
# Gradio Interface
|
| 151 |
+
with gr.Blocks() as demo:
|
| 152 |
+
gr.Markdown("# Advanced Audio Analysis App")
|
| 153 |
+
audio_input = gr.Audio(label="Upload Audio File")
|
| 154 |
+
process_button = gr.Button("Process Audio")
|
| 155 |
+
status_output = gr.Textbox(label="Status")
|
| 156 |
+
|
| 157 |
+
question_input = gr.Textbox(label="Ask a Question")
|
| 158 |
+
answer_output = gr.Textbox(label="Answer")
|
| 159 |
+
|
| 160 |
+
process_button.click(process_audio, inputs=audio_input, outputs=status_output)
|
| 161 |
+
question_input.submit(answer_question, inputs=[question_input], outputs=answer_output)
|
| 162 |
+
|
| 163 |
+
demo.launch()
|