DrishtiSharma commited on
Commit
130b915
Β·
verified Β·
1 Parent(s): bd82d31

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -56
app.py CHANGED
@@ -2,14 +2,17 @@
2
 
3
  import os
4
  import chromadb
5
- from chromadb import Client, Settings
6
  import streamlit as st
 
7
  from langchain_huggingface import HuggingFaceEmbeddings
8
  from langchain_chroma import Chroma
9
  from langchain_groq import ChatGroq
10
  from langchain.memory import ConversationBufferMemory
11
  from langchain.chains import ConversationalRetrievalChain
12
  from PyPDF2 import PdfReader
 
 
 
13
 
14
  # Clear ChromaDB cache to fix tenant issue
15
  chromadb.api.client.SharedSystemClient.clear_system_cache()
@@ -28,23 +31,15 @@ def process_and_store_pdfs(uploaded_files):
28
  for page in reader.pages:
29
  texts.append(page.extract_text())
30
 
31
- # Combine and embed the texts
32
  embeddings = HuggingFaceEmbeddings()
33
- vectorstore = Chroma.from_texts(texts, embedding=embeddings)
34
  return vectorstore
35
 
36
  # Function to set up the chat chain
37
  def chat_chain(vectorstore):
38
- llm = ChatGroq(model="llama-3.1-70b-versatile",
39
- temperature=0,
40
- groq_api_key=GROQ_API_KEY)
41
  retriever = vectorstore.as_retriever()
42
- memory = ConversationBufferMemory(
43
- llm=llm,
44
- output_key="answer",
45
- memory_key="chat_history",
46
- return_messages=True
47
- )
48
 
49
  chain = ConversationalRetrievalChain.from_llm(
50
  llm=llm,
@@ -56,50 +51,96 @@ def chat_chain(vectorstore):
56
  )
57
  return chain
58
 
59
- # Streamlit UI configuration
60
- st.set_page_config(
61
- page_title="Multi Doc Chat",
62
- page_icon="πŸ“š",
63
- layout="centered"
64
- )
65
-
66
- st.title("Chat with Your DocsπŸ“š")
67
-
68
- # File uploader for PDFs
69
- uploaded_files = st.file_uploader("Upload PDF files", accept_multiple_files=True, type=["pdf"])
70
-
71
- # Process PDFs and initialize the vectorstore
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  if uploaded_files:
73
- with st.spinner("Processing files..."):
74
- vectorstore = process_and_store_pdfs(uploaded_files)
75
- st.session_state.vectorstore = vectorstore
76
- st.session_state.conversational_chain = chat_chain(vectorstore)
77
- st.success("Files successfully processed! You can now chat with your documents.")
78
-
79
- # Initialize chat history
80
- if "chat_history" not in st.session_state:
81
- st.session_state.chat_history = []
82
-
83
- # Display chat history
84
- for message in st.session_state.chat_history:
85
- with st.chat_message(message["role"]):
86
- st.markdown(message["content"])
87
-
88
- # User input
89
- if "conversational_chain" in st.session_state:
90
- user_input = st.chat_input("Ask AI...")
91
- if user_input:
92
- st.session_state.chat_history.append({"role": "user", "content": user_input})
93
-
94
- with st.chat_message("user"):
95
- st.markdown(user_input)
96
-
97
- with st.chat_message("assistant"):
98
- # Generate response
99
- response = st.session_state.conversational_chain({"question": user_input})
100
- assistant_response = response["answer"]
101
-
102
- st.markdown(assistant_response)
103
- st.session_state.chat_history.append({"role": "assistant", "content": assistant_response})
104
  else:
105
  st.info("Please upload PDF files to start chatting.")
 
2
 
3
  import os
4
  import chromadb
 
5
  import streamlit as st
6
+ from base64 import b64decode
7
  from langchain_huggingface import HuggingFaceEmbeddings
8
  from langchain_chroma import Chroma
9
  from langchain_groq import ChatGroq
10
  from langchain.memory import ConversationBufferMemory
11
  from langchain.chains import ConversationalRetrievalChain
12
  from PyPDF2 import PdfReader
13
+ from gtts import gTTS
14
+ from pydub import AudioSegment
15
+ from pydub.playback import play
16
 
17
  # Clear ChromaDB cache to fix tenant issue
18
  chromadb.api.client.SharedSystemClient.clear_system_cache()
 
31
  for page in reader.pages:
32
  texts.append(page.extract_text())
33
 
 
34
  embeddings = HuggingFaceEmbeddings()
35
+ vectorstore = Chroma.from_texts(texts, embedding=embeddings, persist_directory="vector_db_dir")
36
  return vectorstore
37
 
38
  # Function to set up the chat chain
39
  def chat_chain(vectorstore):
40
+ llm = ChatGroq(model="llama-3.1-70b-versatile", temperature=0, groq_api_key=GROQ_API_KEY)
 
 
41
  retriever = vectorstore.as_retriever()
42
+ memory = ConversationBufferMemory(output_key="answer", memory_key="chat_history", return_messages=True)
 
 
 
 
 
43
 
44
  chain = ConversationalRetrievalChain.from_llm(
45
  llm=llm,
 
51
  )
52
  return chain
53
 
54
+ # Function to record audio using JavaScript
55
+ RECORD_JS = """
56
+ const sleep = time => new Promise(resolve => setTimeout(resolve, time));
57
+ const b2text = blob => new Promise(resolve => {
58
+ const reader = new FileReader();
59
+ reader.onloadend = e => resolve(e.srcElement.result);
60
+ reader.readAsDataURL(blob);
61
+ });
62
+ var record = time => new Promise(async resolve => {
63
+ stream = await navigator.mediaDevices.getUserMedia({ audio: true });
64
+ recorder = new MediaRecorder(stream);
65
+ chunks = [];
66
+ recorder.ondataavailable = e => chunks.push(e.data);
67
+ recorder.start();
68
+ await sleep(time);
69
+ recorder.onstop = async () => {
70
+ blob = new Blob(chunks);
71
+ text = await b2text(blob);
72
+ resolve(text);
73
+ };
74
+ recorder.stop();
75
+ });
76
+ """
77
+
78
+ def record_audio(seconds=5):
79
+ """Record audio via JavaScript and save it as a .wav file."""
80
+ st.write("Recording...")
81
+ from streamlit.components.v1 import html
82
+ html(f'<script>{RECORD_JS}</script>', height=0)
83
+ b64_audio = st.experimental_js("record", seconds * 1000)
84
+ audio_bytes = b64decode(b64_audio.split(",")[1])
85
+ with open("recorded_audio.wav", "wb") as f:
86
+ f.write(audio_bytes)
87
+ st.success("Audio recorded and saved!")
88
+ return "recorded_audio.wav"
89
+
90
+ # Transcribe audio using Groq Whisper
91
+ from groq import Groq
92
+ def transcribe_audio(filepath):
93
+ client = Groq(api_key=GROQ_API_KEY)
94
+ with open(filepath, "rb") as file:
95
+ transcription = client.audio.transcriptions.create(
96
+ file=(filepath, file.read()),
97
+ model="distil-whisper-large-v3-en",
98
+ response_format="json",
99
+ language="en"
100
+ )
101
+ return transcription.text
102
+
103
+ # Text-to-Speech Function
104
+ def text_to_speech(response):
105
+ tts = gTTS(text=response, lang='en')
106
+ tts.save("response.mp3")
107
+ sound = AudioSegment.from_file("response.mp3")
108
+ play(sound)
109
+
110
+ # Streamlit UI
111
+ st.title("Chat with PDFs via Audio πŸŽ™οΈπŸ“š")
112
+
113
+ uploaded_files = st.file_uploader("Upload PDF Files", accept_multiple_files=True, type=["pdf"])
114
  if uploaded_files:
115
+ vectorstore = process_and_store_pdfs(uploaded_files)
116
+ chain = chat_chain(vectorstore)
117
+ st.success("PDFs processed! Ready to chat.")
118
+
119
+ # User options for input
120
+ input_mode = st.radio("Choose input method:", ["Text", "Audio"])
121
+
122
+ # Text input
123
+ if input_mode == "Text":
124
+ user_input = st.text_input("Ask your question:")
125
+ if user_input:
126
+ with st.spinner("Thinking..."):
127
+ response = chain({"question": user_input})["answer"]
128
+ st.write(f"**Response:** {response}")
129
+ text_to_speech(response)
130
+
131
+ # Audio input
132
+ elif input_mode == "Audio":
133
+ if st.button("Record Audio"):
134
+ audio_file = record_audio(5)
135
+ st.audio(audio_file)
136
+
137
+ st.write("Transcribing audio...")
138
+ question = transcribe_audio(audio_file)
139
+ st.write(f"**You said:** {question}")
140
+
141
+ with st.spinner("Thinking..."):
142
+ response = chain({"question": question})["answer"]
143
+ st.write(f"**Response:** {response}")
144
+ text_to_speech(response)
 
145
  else:
146
  st.info("Please upload PDF files to start chatting.")