speech-analyzer / app.py
DrishtiSharma's picture
Update app.py
c9dab1d verified
raw
history blame
4.12 kB
import streamlit as st
from openai import OpenAI
import os
from wordcloud import WordCloud
import matplotlib.pyplot as plt
from fpdf import FPDF
import tempfile
# Load environment variables
MODEL = 'gpt-4'
client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
st.title('Audio Analyzer')
# Upload audio file
audio_file = st.file_uploader("Upload an audio file", type=["mp3", "wav", "m4a"])
if audio_file:
# Display audio player
st.audio(audio_file)
# Transcribe audio using Whisper API
with st.spinner("Transcribing audio..."):
transcription = client.audio.transcriptions.create(
model="whisper-1",
file=audio_file,
)
st.subheader("Transcription")
st.markdown(transcription.text)
# Summarize in multiple formats
with st.spinner("Generating summary..."):
summary_response = client.chat.completions.create(
model=MODEL,
messages=[
{"role": "system", "content": "Summarize the transcription in two formats:\n1. A concise paragraph\n2. Key points in bullet form."},
{"role": "user", "content": f"Here is the audio transcription: {transcription.text}"}
],
temperature=0,
)
st.subheader("Summary in Multiple Formats")
summary_text = summary_response.choices[0].message.content
st.markdown(summary_text)
# Sentiment analysis
with st.spinner("Analyzing sentiment..."):
sentiment_response = client.chat.completions.create(
model=MODEL,
messages=[
{"role": "system", "content": "You are an AI sentiment analyzer. Analyze the sentiment of the transcription as positive, negative, or neutral and explain your reasoning."},
{"role": "user", "content": f"Here is the audio transcription: {transcription.text}"}
],
temperature=0,
)
st.subheader("Sentiment Analysis")
sentiment_text = sentiment_response.choices[0].message.content
st.markdown(sentiment_text)
# Generate Sentiment Word Cloud
with st.spinner("Generating sentiment word cloud..."):
wordcloud = WordCloud(width=800, height=400, background_color='white').generate(transcription.text)
st.subheader("Word Cloud of Sentiment Analysis")
fig, ax = plt.subplots()
ax.imshow(wordcloud, interpolation='bilinear')
ax.axis('off')
st.pyplot(fig)
# Extract keywords and entities
with st.spinner("Extracting keywords and entities..."):
keywords_response = client.chat.completions.create(
model=MODEL,
messages=[
{"role": "system", "content": "Extract the key topics, keywords, and named entities (like people, places, or organizations) from the transcription."},
{"role": "user", "content": f"Here is the audio transcription: {transcription.text}"}
],
temperature=0,
)
st.subheader("Key Topics and Keywords")
keywords_text = keywords_response.choices[0].message.content
st.markdown(keywords_text)
# Prepare analysis results
analysis_results = f"""
### Transcription:
{transcription.text}
### Summary:
{summary_text}
### Sentiment Analysis:
{sentiment_text}
### Key Topics and Keywords:
{keywords_text}
"""
# Export as TXT
st.subheader("Export Analysis Results")
st.download_button("Download as TXT", analysis_results, file_name="audio_analysis.txt")
# Export as PDF
def create_pdf(content):
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=12)
# Add each line of content to the PDF
for line in content.split("\n"):
pdf.multi_cell(0, 10, line)
# Save to a temporary file
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmpfile:
pdf.output(tmpfile.name)
return tmpfile.name
pdf_file = create_pdf(analysis_results)
with open(pdf_file, "rb") as file:
st.download_button("Download as PDF", file, file_name="audio_analysis.pdf", mime="application/pdf")