Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,83 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
-
from sentence_transformers import SentenceTransformer, util
|
| 4 |
from groq import Groq
|
| 5 |
-
from PyPDF2 import PdfReader
|
| 6 |
-
|
| 7 |
-
# Set your Groq API key here or use environment variable
|
| 8 |
-
GROQ_API_TOKEN = os.getenv("groq_api")
|
| 9 |
-
client = Groq(api_key=GROQ_API_TOKEN)
|
| 10 |
-
|
| 11 |
-
# Initialize the SentenceTransformer model for embeddings
|
| 12 |
-
retriever = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
|
| 13 |
-
|
| 14 |
-
# Knowledge base (documents) and embeddings
|
| 15 |
-
documents = []
|
| 16 |
-
document_embeddings = None
|
| 17 |
-
|
| 18 |
-
# Function to retrieve top relevant document
|
| 19 |
-
def retrieve(query, top_k=3): # Retrieve top 3 relevant documents
|
| 20 |
-
if document_embeddings is None:
|
| 21 |
-
return None
|
| 22 |
-
query_embedding = retriever.encode(query, convert_to_tensor=True)
|
| 23 |
-
hits = util.semantic_search(query_embedding, document_embeddings, top_k=top_k)
|
| 24 |
-
top_docs = [documents[hit['corpus_id']] for hit in hits[0]]
|
| 25 |
-
return ' '.join(top_docs) if hits[0] else None # Concatenate the top documents
|
| 26 |
-
|
| 27 |
-
# Function to generate response using Groq
|
| 28 |
-
def generate_response(query, context):
|
| 29 |
-
# Limit context size to prevent exceeding token limits
|
| 30 |
-
max_context_length = 200 # Adjust this number based on your needs
|
| 31 |
-
if len(context.split()) > max_context_length:
|
| 32 |
-
context = ' '.join(context.split()[:max_context_length]) # Truncate context to the first N words
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
response = client.chat.completions.create(
|
| 35 |
-
messages=[{
|
| 36 |
-
|
| 37 |
-
"content": f"Context: {context}\nQuestion: {query}\nAnswer:"
|
| 38 |
-
}],
|
| 39 |
-
model="gemma2-9b-it"
|
| 40 |
)
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
#
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
document_embeddings = retriever.encode(documents, convert_to_tensor=True)
|
| 57 |
-
|
| 58 |
-
# Streamlit app layout
|
| 59 |
-
st.title("RAG-based PDF Question Answering App")
|
| 60 |
-
st.write("Upload a PDF, ask questions based on its content, and get answers!")
|
| 61 |
-
|
| 62 |
-
# Upload PDF file
|
| 63 |
-
uploaded_file = st.file_uploader("Upload a PDF file", type="pdf")
|
| 64 |
-
if uploaded_file:
|
| 65 |
-
pdf_text = extract_text_from_pdf(uploaded_file)
|
| 66 |
-
if pdf_text:
|
| 67 |
-
update_knowledge_base(pdf_text)
|
| 68 |
-
st.success("PDF content successfully added to the knowledge base.")
|
| 69 |
-
else:
|
| 70 |
-
st.warning("No text could be extracted from the PDF.")
|
| 71 |
-
|
| 72 |
-
# Question input
|
| 73 |
-
question = st.text_input("Enter your question:")
|
| 74 |
-
if question:
|
| 75 |
-
retrieved_context = retrieve(question)
|
| 76 |
-
if retrieved_context:
|
| 77 |
-
answer = generate_response(question, retrieved_context)
|
| 78 |
-
st.write("Answer:", answer)
|
| 79 |
else:
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
|
| 83 |
|
|
|
|
| 1 |
+
# # Set your Groq API key here or use environment variable
|
| 2 |
+
# GROQ_API_TOKEN = os.getenv("groq_api")
|
| 3 |
+
# client = Groq(api_key=GROQ_API_TOKEN)
|
| 4 |
+
|
| 5 |
import os
|
| 6 |
+
import ffmpeg
|
| 7 |
+
import whisper
|
| 8 |
import streamlit as st
|
|
|
|
| 9 |
from groq import Groq
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# Set the title and description of the app
|
| 12 |
+
st.title("Audio/Video Transcription and Summarization")
|
| 13 |
+
st.write("Upload your audio or video file, and this app will transcribe the audio and provide a summary of the transcription.")
|
| 14 |
+
|
| 15 |
+
# Retrieve the API key from environment variables or Streamlit secrets
|
| 16 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY") or st.secrets["GROQ_API_KEY"]
|
| 17 |
+
os.environ["GROQ_API_KEY"] = GROQ_API_KEY
|
| 18 |
+
|
| 19 |
+
# Create a temporary directory if it does not exist
|
| 20 |
+
temp_dir = "temp"
|
| 21 |
+
os.makedirs(temp_dir, exist_ok=True)
|
| 22 |
+
|
| 23 |
+
# Upload the audio or video file
|
| 24 |
+
uploaded_file = st.file_uploader("Choose an audio or video file...", type=["mp4", "mov", "avi", "mkv", "wav", "mp3"])
|
| 25 |
+
|
| 26 |
+
# Function to extract audio from video
|
| 27 |
+
def extract_audio(video_path, audio_path="temp/temp_audio.wav"):
|
| 28 |
+
"""Extracts audio from video."""
|
| 29 |
+
try:
|
| 30 |
+
# Run ffmpeg command with stderr capture for better error handling
|
| 31 |
+
ffmpeg.input(video_path).output(audio_path).run(overwrite_output=True, capture_stdout=True, capture_stderr=True)
|
| 32 |
+
except ffmpeg.Error as e:
|
| 33 |
+
st.error("FFmpeg error encountered: " + e.stderr.decode())
|
| 34 |
+
return audio_path
|
| 35 |
+
|
| 36 |
+
# Function to transcribe audio to text using Whisper model
|
| 37 |
+
def transcribe_audio(audio_path):
|
| 38 |
+
"""Transcribes audio to text using Whisper model."""
|
| 39 |
+
model = whisper.load_model("base") # Load the Whisper model
|
| 40 |
+
result = model.transcribe(audio_path)
|
| 41 |
+
return result["text"]
|
| 42 |
+
|
| 43 |
+
# Function to summarize text using Groq API
|
| 44 |
+
def summarize_text(text):
|
| 45 |
+
"""Summarizes text using Groq API."""
|
| 46 |
+
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 47 |
response = client.chat.completions.create(
|
| 48 |
+
messages=[{"role": "user", "content": f"Summarize the following text: {text}"}],
|
| 49 |
+
model="llama3-8b-8192"
|
|
|
|
|
|
|
|
|
|
| 50 |
)
|
| 51 |
+
summary = response.choices[0].message.content
|
| 52 |
+
return summary
|
| 53 |
+
|
| 54 |
+
# Complete function to process audio or video
|
| 55 |
+
def process_media(media_file):
|
| 56 |
+
"""Processes audio or video: extracts audio, transcribes it, and summarizes the transcription."""
|
| 57 |
+
# Save the uploaded file to a temporary path
|
| 58 |
+
temp_file_path = os.path.join(temp_dir, media_file.name)
|
| 59 |
+
with open(temp_file_path, "wb") as f:
|
| 60 |
+
f.write(media_file.getbuffer())
|
| 61 |
+
|
| 62 |
+
# Determine if the file is a video or audio based on the file extension
|
| 63 |
+
if media_file.name.endswith(('.mp4', '.mov', '.avi', '.mkv')):
|
| 64 |
+
# Step 1: Extract audio from video
|
| 65 |
+
audio_path = extract_audio(temp_file_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
else:
|
| 67 |
+
audio_path = temp_file_path # If it's already audio, use it as is
|
| 68 |
+
|
| 69 |
+
# Step 2: Transcribe audio to text
|
| 70 |
+
transcription = transcribe_audio(audio_path)
|
| 71 |
+
st.write("### Transcription:")
|
| 72 |
+
st.write(transcription)
|
| 73 |
+
|
| 74 |
+
# Step 3: Summarize transcription
|
| 75 |
+
summary = summarize_text(transcription)
|
| 76 |
+
st.write("### Summary:")
|
| 77 |
+
st.write(summary)
|
| 78 |
+
|
| 79 |
+
# Clean up temporary files if needed
|
| 80 |
+
os.remove(temp_file_path)
|
| 81 |
+
if media_file.name.endswith(('.mp4', '.mov', '.avi', '.mkv')):
|
| 82 |
+
os.remove(audio_path)
|
| 83 |
+
|
| 84 |
+
# Run the app
|
| 85 |
+
if uploaded_file is not None:
|
| 86 |
+
process_media(uploaded_file)
|
| 87 |
+
else:
|
| 88 |
+
st.warning("Please upload a file.")
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
|
| 94 |
|
| 95 |
|
| 96 |
|