Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,92 +1,3 @@
|
|
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 |
|
@@ -96,85 +7,85 @@ else:
|
|
96 |
|
97 |
|
98 |
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
#
|
108 |
-
|
109 |
-
#
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
#
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
#
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
|
129 |
-
#
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
#
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
#
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
#
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
#
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
#
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
#
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
|
2 |
|
3 |
|
|
|
7 |
|
8 |
|
9 |
|
10 |
+
import os
|
11 |
+
import streamlit as st
|
12 |
+
from sentence_transformers import SentenceTransformer, util
|
13 |
+
from groq import Groq
|
14 |
+
from PyPDF2 import PdfReader
|
15 |
+
|
16 |
+
|
17 |
+
|
18 |
+
# Initialize the retriever and Groq client
|
19 |
+
retriever = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
|
20 |
+
# client = Groq(api_key=groq_api) # Replace with your actual Groq API key
|
21 |
+
key = os.getenv("groq_api")
|
22 |
+
client = Groq(api_key = key)
|
23 |
+
|
24 |
+
# Knowledge base (documents) and embeddings
|
25 |
+
documents = [
|
26 |
+
"Retrieval-Augmented Generation (RAG) is an AI framework that combines the strengths of retrieval-based and generative models.",
|
27 |
+
"The main components of a RAG system are the retriever and the generator.",
|
28 |
+
"A key benefit of Retrieval-Augmented Generation is that it can produce more accurate responses compared to standalone generative models.",
|
29 |
+
"The retrieval process in a RAG system often relies on embedding-based models, like Sentence-BERT or DPR.",
|
30 |
+
"Common use cases of RAG include chatbots, customer support systems, and knowledge retrieval for business intelligence."
|
31 |
+
]
|
32 |
+
document_embeddings = retriever.encode(documents, convert_to_tensor=True)
|
33 |
+
|
34 |
+
# Function to retrieve top relevant document and truncate context if too long
|
35 |
+
def retrieve(query, top_k=1, max_tokens=100):
|
36 |
+
query_embedding = retriever.encode(query, convert_to_tensor=True)
|
37 |
+
hits = util.semantic_search(query_embedding, document_embeddings, top_k=top_k)
|
38 |
+
top_docs = [documents[hit['corpus_id']] for hit in hits[0]]
|
39 |
|
40 |
+
# Truncate context to max_tokens if necessary
|
41 |
+
context = top_docs[0] if hits[0] else ""
|
42 |
+
context = ' '.join(context.split()[:max_tokens]) # Limit to max_tokens words
|
43 |
+
return context
|
44 |
+
|
45 |
+
# Function to generate response using Groq
|
46 |
+
def generate_response(query, context):
|
47 |
+
response = client.chat.completions.create(
|
48 |
+
messages=[
|
49 |
+
{
|
50 |
+
"role": "user",
|
51 |
+
"content": f"Context: {context} Question: {query} Answer:"
|
52 |
+
}
|
53 |
+
],
|
54 |
+
model="gemma2-9b-it"
|
55 |
+
)
|
56 |
+
return response.choices[0].message.content
|
57 |
+
|
58 |
+
# Function to handle PDF upload and text extraction
|
59 |
+
def extract_text_from_pdf(file):
|
60 |
+
pdf_reader = PdfReader(file)
|
61 |
+
text = ""
|
62 |
+
for page in pdf_reader.pages:
|
63 |
+
text += page.extract_text()
|
64 |
+
return text
|
65 |
+
|
66 |
+
# Function to update knowledge base with new content from PDF
|
67 |
+
def update_knowledge_base(pdf_text):
|
68 |
+
global documents, document_embeddings
|
69 |
+
documents.append(pdf_text)
|
70 |
+
document_embeddings = retriever.encode(documents, convert_to_tensor=True)
|
71 |
+
|
72 |
+
# Streamlit app layout
|
73 |
+
st.title("RAG-based Question Answering App")
|
74 |
+
st.write("Upload a PDF, ask questions based on its content, and get answers!")
|
75 |
+
|
76 |
+
# Upload PDF file
|
77 |
+
uploaded_file = st.file_uploader("Upload a PDF file", type="pdf")
|
78 |
+
if uploaded_file:
|
79 |
+
pdf_text = extract_text_from_pdf(uploaded_file)
|
80 |
+
update_knowledge_base(pdf_text)
|
81 |
+
st.write("PDF content successfully added to the knowledge base.")
|
82 |
+
|
83 |
+
# Question input
|
84 |
+
question = st.text_input("Enter your question:")
|
85 |
+
if question:
|
86 |
+
retrieved_context = retrieve(question)
|
87 |
+
if retrieved_context:
|
88 |
+
answer = generate_response(question, retrieved_context)
|
89 |
+
else:
|
90 |
+
answer = "I have no knowledge about this topic."
|
91 |
+
st.write("Answer:", answer)
|