Spaces:
Sleeping
Sleeping
Update test.py
Browse files
test.py
CHANGED
|
@@ -1,17 +1,30 @@
|
|
| 1 |
-
#
|
| 2 |
-
|
| 3 |
import streamlit as st
|
| 4 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from datetime import datetime
|
| 6 |
from pydub import AudioSegment
|
| 7 |
-
import tempfile
|
| 8 |
import pytz
|
| 9 |
-
|
| 10 |
from langchain.chains import ConversationalRetrievalChain
|
| 11 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 12 |
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
|
| 13 |
from langchain_community.vectorstores import Chroma
|
| 14 |
from langchain_community.document_loaders import PyPDFLoader, TextLoader, CSVLoader
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
|
| 17 |
class DocumentRAG:
|
|
@@ -41,10 +54,12 @@ class DocumentRAG:
|
|
| 41 |
try:
|
| 42 |
documents = []
|
| 43 |
for uploaded_file in uploaded_files:
|
|
|
|
| 44 |
temp_file_path = tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(uploaded_file.name)[1]).name
|
| 45 |
with open(temp_file_path, "wb") as temp_file:
|
| 46 |
temp_file.write(uploaded_file.read())
|
| 47 |
|
|
|
|
| 48 |
if temp_file_path.endswith('.pdf'):
|
| 49 |
loader = PyPDFLoader(temp_file_path)
|
| 50 |
elif temp_file_path.endswith('.txt'):
|
|
@@ -54,6 +69,7 @@ class DocumentRAG:
|
|
| 54 |
else:
|
| 55 |
return f"Unsupported file type: {uploaded_file.name}"
|
| 56 |
|
|
|
|
| 57 |
try:
|
| 58 |
documents.extend(loader.load())
|
| 59 |
except Exception as e:
|
|
@@ -62,6 +78,7 @@ class DocumentRAG:
|
|
| 62 |
if not documents:
|
| 63 |
return "No valid documents were processed. Please check your files."
|
| 64 |
|
|
|
|
| 65 |
text_splitter = RecursiveCharacterTextSplitter(
|
| 66 |
chunk_size=1000,
|
| 67 |
chunk_overlap=200,
|
|
@@ -69,14 +86,16 @@ class DocumentRAG:
|
|
| 69 |
)
|
| 70 |
documents = text_splitter.split_documents(documents)
|
| 71 |
|
|
|
|
| 72 |
combined_text = " ".join([doc.page_content for doc in documents])
|
| 73 |
self.document_summary = self.generate_summary(combined_text)
|
| 74 |
|
|
|
|
| 75 |
embeddings = OpenAIEmbeddings(api_key=self.api_key)
|
| 76 |
self.document_store = Chroma.from_documents(
|
| 77 |
documents,
|
| 78 |
embeddings,
|
| 79 |
-
persist_directory=self.chroma_persist_dir
|
| 80 |
)
|
| 81 |
|
| 82 |
self.qa_chain = ConversationalRetrievalChain.from_llm(
|
|
@@ -119,6 +138,8 @@ class DocumentRAG:
|
|
| 119 |
|
| 120 |
try:
|
| 121 |
client = OpenAI(api_key=self.api_key)
|
|
|
|
|
|
|
| 122 |
script_response = client.chat.completions.create(
|
| 123 |
model="gpt-4",
|
| 124 |
messages=[
|
|
@@ -137,6 +158,7 @@ class DocumentRAG:
|
|
| 137 |
if not script:
|
| 138 |
return "Error: Failed to generate podcast script.", None
|
| 139 |
|
|
|
|
| 140 |
final_audio = AudioSegment.empty()
|
| 141 |
is_first_speaker = True
|
| 142 |
|
|
@@ -180,6 +202,47 @@ class DocumentRAG:
|
|
| 180 |
except Exception as e:
|
| 181 |
return f"Error generating podcast: {str(e)}", None
|
| 182 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
# Initialize RAG system in session state
|
| 185 |
if "rag_system" not in st.session_state:
|
|
@@ -200,23 +263,70 @@ with st.sidebar:
|
|
| 200 |
st.markdown("3. Ask questions.")
|
| 201 |
st.markdown("4. Create podcasts.")
|
| 202 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
# Main App
|
| 204 |
st.title("Document Analyzer and Podcast Generator")
|
| 205 |
|
|
|
|
|
|
|
| 206 |
uploaded_files = st.file_uploader("Upload files (PDF, TXT, CSV)", accept_multiple_files=True)
|
| 207 |
|
| 208 |
if st.button("Process Documents"):
|
| 209 |
if uploaded_files:
|
|
|
|
| 210 |
result = st.session_state.rag_system.process_documents(uploaded_files)
|
| 211 |
-
|
|
|
|
|
|
|
|
|
|
| 212 |
else:
|
| 213 |
st.warning("No files uploaded.")
|
| 214 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
if st.session_state.rag_system.document_summary:
|
| 216 |
-
st.subheader("Step 2: Generate Podcast")
|
| 217 |
if st.button("Generate Podcast"):
|
| 218 |
script, audio_path = st.session_state.rag_system.create_podcast()
|
| 219 |
if audio_path:
|
| 220 |
st.text_area("Generated Podcast Script", script, height=200)
|
| 221 |
st.audio(audio_path, format="audio/mp3")
|
| 222 |
-
st.success("Podcast generated successfully!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# to-do: make it multilingual
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
import os
|
| 4 |
+
from openai import OpenAI
|
| 5 |
+
import tempfile
|
| 6 |
+
from langchain.chains import ConversationalRetrievalChain
|
| 7 |
+
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
|
| 8 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 9 |
+
from langchain_community.vectorstores import Chroma
|
| 10 |
+
from langchain_community.document_loaders import (
|
| 11 |
+
PyPDFLoader,
|
| 12 |
+
TextLoader,
|
| 13 |
+
CSVLoader
|
| 14 |
+
)
|
| 15 |
from datetime import datetime
|
| 16 |
from pydub import AudioSegment
|
|
|
|
| 17 |
import pytz
|
| 18 |
+
|
| 19 |
from langchain.chains import ConversationalRetrievalChain
|
| 20 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 21 |
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
|
| 22 |
from langchain_community.vectorstores import Chroma
|
| 23 |
from langchain_community.document_loaders import PyPDFLoader, TextLoader, CSVLoader
|
| 24 |
+
import os
|
| 25 |
+
import tempfile
|
| 26 |
+
from datetime import datetime
|
| 27 |
+
import pytz
|
| 28 |
|
| 29 |
|
| 30 |
class DocumentRAG:
|
|
|
|
| 54 |
try:
|
| 55 |
documents = []
|
| 56 |
for uploaded_file in uploaded_files:
|
| 57 |
+
# Save uploaded file to a temporary location
|
| 58 |
temp_file_path = tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(uploaded_file.name)[1]).name
|
| 59 |
with open(temp_file_path, "wb") as temp_file:
|
| 60 |
temp_file.write(uploaded_file.read())
|
| 61 |
|
| 62 |
+
# Determine the loader based on the file type
|
| 63 |
if temp_file_path.endswith('.pdf'):
|
| 64 |
loader = PyPDFLoader(temp_file_path)
|
| 65 |
elif temp_file_path.endswith('.txt'):
|
|
|
|
| 69 |
else:
|
| 70 |
return f"Unsupported file type: {uploaded_file.name}"
|
| 71 |
|
| 72 |
+
# Load the documents
|
| 73 |
try:
|
| 74 |
documents.extend(loader.load())
|
| 75 |
except Exception as e:
|
|
|
|
| 78 |
if not documents:
|
| 79 |
return "No valid documents were processed. Please check your files."
|
| 80 |
|
| 81 |
+
# Split text for better processing
|
| 82 |
text_splitter = RecursiveCharacterTextSplitter(
|
| 83 |
chunk_size=1000,
|
| 84 |
chunk_overlap=200,
|
|
|
|
| 86 |
)
|
| 87 |
documents = text_splitter.split_documents(documents)
|
| 88 |
|
| 89 |
+
# Combine text for summary
|
| 90 |
combined_text = " ".join([doc.page_content for doc in documents])
|
| 91 |
self.document_summary = self.generate_summary(combined_text)
|
| 92 |
|
| 93 |
+
# Create embeddings and initialize retrieval chain
|
| 94 |
embeddings = OpenAIEmbeddings(api_key=self.api_key)
|
| 95 |
self.document_store = Chroma.from_documents(
|
| 96 |
documents,
|
| 97 |
embeddings,
|
| 98 |
+
persist_directory=self.chroma_persist_dir # Persistent directory for Chroma
|
| 99 |
)
|
| 100 |
|
| 101 |
self.qa_chain = ConversationalRetrievalChain.from_llm(
|
|
|
|
| 138 |
|
| 139 |
try:
|
| 140 |
client = OpenAI(api_key=self.api_key)
|
| 141 |
+
|
| 142 |
+
# Generate podcast script
|
| 143 |
script_response = client.chat.completions.create(
|
| 144 |
model="gpt-4",
|
| 145 |
messages=[
|
|
|
|
| 158 |
if not script:
|
| 159 |
return "Error: Failed to generate podcast script.", None
|
| 160 |
|
| 161 |
+
# Convert script to audio
|
| 162 |
final_audio = AudioSegment.empty()
|
| 163 |
is_first_speaker = True
|
| 164 |
|
|
|
|
| 202 |
except Exception as e:
|
| 203 |
return f"Error generating podcast: {str(e)}", None
|
| 204 |
|
| 205 |
+
def generate_summary(self, text):
|
| 206 |
+
"""Generate a summary of the provided text."""
|
| 207 |
+
if not self.api_key:
|
| 208 |
+
return "API Key not set. Please set it in the environment variables."
|
| 209 |
+
try:
|
| 210 |
+
client = OpenAI(api_key=self.api_key)
|
| 211 |
+
response = client.chat.completions.create(
|
| 212 |
+
model="gpt-4",
|
| 213 |
+
messages=[
|
| 214 |
+
{"role": "system", "content": "Summarize the document content concisely and provide 3-5 key points for discussion."},
|
| 215 |
+
{"role": "user", "content": text[:4000]}
|
| 216 |
+
],
|
| 217 |
+
temperature=0.3
|
| 218 |
+
)
|
| 219 |
+
return response.choices[0].message.content
|
| 220 |
+
except Exception as e:
|
| 221 |
+
return f"Error generating summary: {str(e)}"
|
| 222 |
+
|
| 223 |
+
def handle_query(self, question, history):
|
| 224 |
+
"""Handle user queries."""
|
| 225 |
+
if not self.qa_chain:
|
| 226 |
+
return history + [("System", "Please process the documents first.")]
|
| 227 |
+
try:
|
| 228 |
+
preface = """
|
| 229 |
+
Instruction: Respond in English. Be professional and concise, keeping the response under 300 words.
|
| 230 |
+
If you cannot provide an answer, say: "I am not sure about this question. Please try asking something else."
|
| 231 |
+
"""
|
| 232 |
+
query = f"{preface}\nQuery: {question}"
|
| 233 |
+
|
| 234 |
+
result = self.qa_chain({
|
| 235 |
+
"question": query,
|
| 236 |
+
"chat_history": [(q, a) for q, a in history]
|
| 237 |
+
})
|
| 238 |
+
|
| 239 |
+
if "answer" not in result:
|
| 240 |
+
return history + [("System", "Sorry, an error occurred.")]
|
| 241 |
+
|
| 242 |
+
history.append((question, result["answer"]))
|
| 243 |
+
return history
|
| 244 |
+
except Exception as e:
|
| 245 |
+
return history + [("System", f"Error: {str(e)}")]
|
| 246 |
|
| 247 |
# Initialize RAG system in session state
|
| 248 |
if "rag_system" not in st.session_state:
|
|
|
|
| 263 |
st.markdown("3. Ask questions.")
|
| 264 |
st.markdown("4. Create podcasts.")
|
| 265 |
|
| 266 |
+
# Streamlit UI
|
| 267 |
+
# Sidebar
|
| 268 |
+
#with st.sidebar:
|
| 269 |
+
#st.title("About")
|
| 270 |
+
#st.markdown(
|
| 271 |
+
#"""
|
| 272 |
+
#This app is inspired by the [RAG_HW HuggingFace Space](https://huggingface.co/spaces/wint543/RAG_HW).
|
| 273 |
+
#It allows users to:
|
| 274 |
+
#1. Upload and process documents
|
| 275 |
+
#2. Generate summaries
|
| 276 |
+
#3. Ask questions
|
| 277 |
+
#4. Create podcasts
|
| 278 |
+
#"""
|
| 279 |
+
#)
|
| 280 |
+
|
| 281 |
# Main App
|
| 282 |
st.title("Document Analyzer and Podcast Generator")
|
| 283 |
|
| 284 |
+
# Step 1: Upload and Process Documents
|
| 285 |
+
st.subheader("Step 1: Upload and Process Documents")
|
| 286 |
uploaded_files = st.file_uploader("Upload files (PDF, TXT, CSV)", accept_multiple_files=True)
|
| 287 |
|
| 288 |
if st.button("Process Documents"):
|
| 289 |
if uploaded_files:
|
| 290 |
+
# Process the uploaded files
|
| 291 |
result = st.session_state.rag_system.process_documents(uploaded_files)
|
| 292 |
+
if "successfully" in result:
|
| 293 |
+
st.success(result)
|
| 294 |
+
else:
|
| 295 |
+
st.error(result)
|
| 296 |
else:
|
| 297 |
st.warning("No files uploaded.")
|
| 298 |
|
| 299 |
+
# Step 2: Generate Summaries
|
| 300 |
+
st.subheader("Step 2: Generate Summaries")
|
| 301 |
+
if st.session_state.rag_system.document_summary:
|
| 302 |
+
st.text_area("Document Summary", st.session_state.rag_system.document_summary, height=200)
|
| 303 |
+
else:
|
| 304 |
+
st.info("Please process documents first to generate summaries.")
|
| 305 |
+
|
| 306 |
+
# Step 3: Ask Questions
|
| 307 |
+
st.subheader("Step 3: Ask Questions")
|
| 308 |
+
if st.session_state.rag_system.qa_chain:
|
| 309 |
+
history = []
|
| 310 |
+
user_question = st.text_input("Ask a question:")
|
| 311 |
+
if st.button("Submit Question"):
|
| 312 |
+
# Handle the user query
|
| 313 |
+
history = st.session_state.rag_system.handle_query(user_question, history)
|
| 314 |
+
for question, answer in history:
|
| 315 |
+
st.chat_message("user").write(question)
|
| 316 |
+
st.chat_message("assistant").write(answer)
|
| 317 |
+
else:
|
| 318 |
+
st.info("Please process documents first to enable Q&A.")
|
| 319 |
+
|
| 320 |
+
# Step 4: Generate Podcast
|
| 321 |
+
st.subheader("Step 4: Generate Podcast")
|
| 322 |
if st.session_state.rag_system.document_summary:
|
|
|
|
| 323 |
if st.button("Generate Podcast"):
|
| 324 |
script, audio_path = st.session_state.rag_system.create_podcast()
|
| 325 |
if audio_path:
|
| 326 |
st.text_area("Generated Podcast Script", script, height=200)
|
| 327 |
st.audio(audio_path, format="audio/mp3")
|
| 328 |
+
st.success("Podcast generated successfully! You can listen to it above.")
|
| 329 |
+
else:
|
| 330 |
+
st.error(script)
|
| 331 |
+
else:
|
| 332 |
+
st.info("Please process documents and generate summaries before creating a podcast.")
|