Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import ffmpeg
|
3 |
+
import whisper
|
4 |
+
import streamlit as st
|
5 |
+
from groq import Groq
|
6 |
+
|
7 |
+
# Set the title and description of the app
|
8 |
+
st.title("Audio/Video Transcription and Summarization")
|
9 |
+
st.write("Upload your audio or video file, and this app will transcribe the audio and provide a summary of the transcription.")
|
10 |
+
|
11 |
+
# Get the API key from user input (You may want to use Streamlit secrets management)
|
12 |
+
GROQ_API_KEY = st.text_input("Enter your Groq API Key:")
|
13 |
+
os.environ["GROQ_API_KEY"] = GROQ_API_KEY
|
14 |
+
|
15 |
+
# Upload the audio or video file
|
16 |
+
uploaded_file = st.file_uploader("Choose an audio or video file...", type=["mp4", "mov", "avi", "mkv", "wav", "mp3"])
|
17 |
+
|
18 |
+
# Function to extract audio from video
|
19 |
+
def extract_audio(video_path, audio_path="temp_audio.wav"):
|
20 |
+
"""Extracts audio from video."""
|
21 |
+
try:
|
22 |
+
# Run ffmpeg command with stderr capture for better error handling
|
23 |
+
ffmpeg.input(video_path).output(audio_path).run(overwrite_output=True, capture_stdout=True, capture_stderr=True)
|
24 |
+
except ffmpeg.Error as e:
|
25 |
+
st.error("FFmpeg error encountered: " + e.stderr.decode())
|
26 |
+
return audio_path
|
27 |
+
|
28 |
+
# Function to transcribe audio to text using Whisper model
|
29 |
+
def transcribe_audio(audio_path):
|
30 |
+
"""Transcribes audio to text using Whisper model."""
|
31 |
+
model = whisper.load_model("base") # Load the Whisper model
|
32 |
+
result = model.transcribe(audio_path)
|
33 |
+
return result["text"]
|
34 |
+
|
35 |
+
# Function to summarize text using Groq API
|
36 |
+
def summarize_text(text):
|
37 |
+
"""Summarizes text using Groq API."""
|
38 |
+
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
39 |
+
response = client.chat.completions.create(
|
40 |
+
messages=[{"role": "user", "content": f"Summarize the following text: {text}"}],
|
41 |
+
model="llama3-8b-8192"
|
42 |
+
)
|
43 |
+
summary = response.choices[0].message.content
|
44 |
+
return summary
|
45 |
+
|
46 |
+
# Complete function to process audio or video
|
47 |
+
def process_media(media_file):
|
48 |
+
"""Processes audio or video: extracts audio, transcribes it, and summarizes the transcription."""
|
49 |
+
# Save the uploaded file to a temporary path
|
50 |
+
temp_file_path = f"temp/{media_file.name}"
|
51 |
+
with open(temp_file_path, "wb") as f:
|
52 |
+
f.write(media_file.getbuffer())
|
53 |
+
|
54 |
+
# Determine if the file is a video or audio based on the file extension
|
55 |
+
if media_file.name.endswith(('.mp4', '.mov', '.avi', '.mkv')):
|
56 |
+
# Step 1: Extract audio from video
|
57 |
+
audio_path = extract_audio(temp_file_path)
|
58 |
+
else:
|
59 |
+
audio_path = temp_file_path # If it's already audio, use it as is
|
60 |
+
|
61 |
+
# Step 2: Transcribe audio to text
|
62 |
+
transcription = transcribe_audio(audio_path)
|
63 |
+
st.write("### Transcription:")
|
64 |
+
st.write(transcription)
|
65 |
+
|
66 |
+
# Step 3: Summarize transcription
|
67 |
+
summary = summarize_text(transcription)
|
68 |
+
st.write("### Summary:")
|
69 |
+
st.write(summary)
|
70 |
+
|
71 |
+
# Clean up temporary files if needed
|
72 |
+
os.remove(temp_file_path)
|
73 |
+
if media_file.name.endswith(('.mp4', '.mov', '.avi', '.mkv')):
|
74 |
+
os.remove(audio_path)
|
75 |
+
|
76 |
+
# Run the app
|
77 |
+
if uploaded_file is not None and GROQ_API_KEY:
|
78 |
+
process_media(uploaded_file)
|
79 |
+
else:
|
80 |
+
st.warning("Please upload a file and enter your Groq API key.")
|