reverted speech_recognition module
Browse filesFiles changed (2)
- app.py+147-147
- requirements.txt
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221233 unmodified lines12712812913013113213389 unmodified lines2232242252262272282292302312322332342352362372382392402412422432442451 unmodified line247248249250251252253254255256257258259260261262263264# import os# from langchain_community.vectorstores import Chroma# from langchain_google_genai import GoogleGenerativeAIEmbeddings, ChatGoogleGenerativeAI# from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder# from langchain.chains import create_history_aware_retriever, create_retrieval_chain# from langchain.chains.combine_documents import create_stuff_documents_chain# from langchain_core.messages import HumanMessage, AIMessage# from dotenv import load_dotenv# import streamlit as st# # Load environment variables# load_dotenv()# # Set up directories and embeddings# cwd = os.getcwd()# db = os.path.join(cwd, 'db1')# embeddings = GoogleGenerativeAIEmbeddings(model='models/embedding-001')# # Initialize Chroma database# vector_db = Chroma(persist_directory=db, embedding_function=embeddings)# retriever = vector_db.as_retriever(search_type="similarity", search_kwargs={"k": 5})# # Set up the LLM# llm = ChatGoogleGenerativeAI(model='gemini-1.5-flash')# # Prompt to contextualize questions# contextualize_q_system_prompt = (# "Given a chat history and the latest user question "# "which might reference context in the chat history, "# "formulate a standalone question which can be understood "# "without the chat history. Do NOT answer the question, just "# "return a suitable question that helps you to search anything related to the question in the vector DB"# )# contextualize_q_prompt = ChatPromptTemplate.from_messages(# [# ("system", contextualize_q_system_prompt),# MessagesPlaceholder("chat_history"),# ("human", "{input}"),# ]# )# # Create a history-aware retriever# history_aware_retriever = create_history_aware_retriever(# llm, retriever, contextualize_q_prompt# )# # Prompt for question-answering# qa_system_prompt = (# "You are an assistant for helping people to know about our company. Use "# "the following pieces of retrieved context to answer the "# "question. "# "You are IqBot"# "You provide the answer short and crsip, if user needs more you can elaborate as per user's need."# "you are built by IQ TechMax and only respond our company related queries"# "you can provide IQ TechMax headquarters address is Address: Suite 102, 1st Floor at Door #108, 1st Main Road, Rajiv Gandhi Salai, Burma Industrial Colony, Thiruvalluvar Nagar, Perungudi, Chennai, Tamil Nadu 600096"# "If user asking about IQ TechMax means only provide the Branches details in Chennai Headquarters|Bengaluru | France | USA otherwise dont share. If user asking more details in these branches guide to contact sales@iqtechmax.com"# "\n\n"# "{context}"# )# qa_prompt = ChatPromptTemplate.from_messages(# [# ("system", qa_system_prompt),# MessagesPlaceholder("chat_history"),# ("human", "{input}"),# ]# )# # Create the question-answering chain# question_answer_chain = create_stuff_documents_chain(llm, qa_prompt)# rag_chain = create_retrieval_chain(history_aware_retriever, question_answer_chain)# # Streamlit UI# def chat(user_input, chat_history=None):# if chat_history is None:# chat_history = [] # Initialize chat history if not provided# # Process the user's query through the retrieval chain# result = rag_chain.invoke({"input": user_input, "chat_history": chat_history})# # Clean up the response# response = result["answer"].strip()# # Convert words surrounded by asterisks into bold text# response = response.replace("*", "")# # Remove unnecessary starting phrases# unwanted_phrases = [# "Based on the provided text,",# "From the retrieved context,",# "According to the information,",# ]# for phrase in unwanted_phrases:# if response.startswith(phrase):# response = response.replace(phrase, "").strip()# # Create message instances# ai_message = AIMessage(content=response)# chat_history.append(ai_message)# chat_history.append(HumanMessage(content=user_input))# return chat_history# # Streamlit App# def main():# st.title("IQ TechMax Chatbot")# st.subheader("Ask questions related to IQ TechMax")# # Input field# user_input = st.text_input("Your Question:", "")# if user_input:# chat_history = chat(user_input)# for message in chat_history:# st.write(message.content)# if __name__ == '__main__':# main()import osfrom langchain_community.vectorstores import Chromafrom langchain_google_genai import GoogleGenerativeAIEmbeddings, ChatGoogleGenerativeAI3 unmodified linesfrom langchain_core.messages import HumanMessage, AIMessagefrom dotenv import load_dotenvimport streamlit as stimport speech_recognition as sr# Load environment variablesload_dotenv()89 unmodified linesreturn chat_history# Start/Stop Speech Recognitiondef start_mic(chat_history):if 'recognizer' not in st.session_state:st.session_state.recognizer = sr.Recognizer()recognizer = st.session_state.recognizertry:with sr.Microphone() as source:st.write("Listening...")audio = recognizer.listen(source)user_input = recognizer.recognize_google(audio)chat_history = chat(user_input, chat_history)except Exception as e:st.write("Sorry, could not recognize the speech. Please try again.")return chat_history# Streamlit Appdef main():st.title("IQ TechMax Chatbot")1 unmodified line# Input fielduser_input = st.text_input("Your Question:", "")chat_history = st.session_state.get('chat_history', [])if user_input:chat_history = chat(user_input, chat_history)st.session_state.chat_history = chat_historyfor message in chat_history:st.write(message.content)# Speech Recognitionmic_button = st.button("🎤 Microphone", key="mic_btn")if mic_button:chat_history = start_mic(chat_history)if __name__ == '__main__':main()1233 unmodified lines78910111289 unmodified lines1021031041051061071 unmodified line109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264import osfrom langchain_community.vectorstores import Chromafrom langchain_google_genai import GoogleGenerativeAIEmbeddings, ChatGoogleGenerativeAI3 unmodified linesfrom langchain_core.messages import HumanMessage, AIMessagefrom dotenv import load_dotenvimport streamlit as st# Load environment variablesload_dotenv()89 unmodified linesreturn chat_history# Streamlit Appdef main():st.title("IQ TechMax Chatbot")1 unmodified line# Input fielduser_input = st.text_input("Your Question:", "")if user_input:chat_history = chat(user_input)for message in chat_history:st.write(message.content)if __name__ == '__main__':main()# import os# from langchain_community.vectorstores import Chroma# from langchain_google_genai import GoogleGenerativeAIEmbeddings, ChatGoogleGenerativeAI# from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder# from langchain.chains import create_history_aware_retriever, create_retrieval_chain# from langchain.chains.combine_documents import create_stuff_documents_chain# from langchain_core.messages import HumanMessage, AIMessage# from dotenv import load_dotenv# import streamlit as st# import speech_recognition as sr# # Load environment variables# load_dotenv()# # Set up directories and embeddings# cwd = os.getcwd()# db = os.path.join(cwd, 'db1')# embeddings = GoogleGenerativeAIEmbeddings(model='models/embedding-001')# # Initialize Chroma database# vector_db = Chroma(persist_directory=db, embedding_function=embeddings)# retriever = vector_db.as_retriever(search_type="similarity", search_kwargs={"k": 5})# # Set up the LLM# llm = ChatGoogleGenerativeAI(model='gemini-1.5-flash')# # Prompt to contextualize questions# contextualize_q_system_prompt = (# "Given a chat history and the latest user question "# "which might reference context in the chat history, "# "formulate a standalone question which can be understood "# "without the chat history. Do NOT answer the question, just "# "return a suitable question that helps you to search anything related to the question in the vector DB"# )# contextualize_q_prompt = ChatPromptTemplate.from_messages(# [# ("system", contextualize_q_system_prompt),# MessagesPlaceholder("chat_history"),# ("human", "{input}"),# ]# )# # Create a history-aware retriever# history_aware_retriever = create_history_aware_retriever(# llm, retriever, contextualize_q_prompt# )# # Prompt for question-answering# qa_system_prompt = (# "You are an assistant for helping people to know about our company. Use "# "the following pieces of retrieved context to answer the "# "question. "# "You are IqBot"# "You provide the answer short and crsip, if user needs more you can elaborate as per user's need."# "you are built by IQ TechMax and only respond our company related queries"# "you can provide IQ TechMax headquarters address is Address: Suite 102, 1st Floor at Door #108, 1st Main Road, Rajiv Gandhi Salai, Burma Industrial Colony, Thiruvalluvar Nagar, Perungudi, Chennai, Tamil Nadu 600096"# "If user asking about IQ TechMax means only provide the Branches details in Chennai Headquarters|Bengaluru | France | USA otherwise dont share. If user asking more details in these branches guide to contact sales@iqtechmax.com"# "\n\n"# "{context}"# )# qa_prompt = ChatPromptTemplate.from_messages(# [# ("system", qa_system_prompt),# MessagesPlaceholder("chat_history"),# ("human", "{input}"),# ]# )# # Create the question-answering chain# question_answer_chain = create_stuff_documents_chain(llm, qa_prompt)# rag_chain = create_retrieval_chain(history_aware_retriever, question_answer_chain)# # Streamlit UI# def chat(user_input, chat_history=None):# if chat_history is None:# chat_history = [] # Initialize chat history if not provided# # Process the user's query through the retrieval chain# result = rag_chain.invoke({"input": user_input, "chat_history": chat_history})# # Clean up the response# response = result["answer"].strip()# # Convert words surrounded by asterisks into bold text# response = response.replace("*", "")# # Remove unnecessary starting phrases# unwanted_phrases = [# "Based on the provided text,",# "From the retrieved context,",# "According to the information,",# ]# for phrase in unwanted_phrases:# if response.startswith(phrase):# response = response.replace(phrase, "").strip()# # Create message instances# ai_message = AIMessage(content=response)# chat_history.append(ai_message)# chat_history.append(HumanMessage(content=user_input))# return chat_history# # Start/Stop Speech Recognition# def start_mic(chat_history):# if 'recognizer' not in st.session_state:# st.session_state.recognizer = sr.Recognizer()# recognizer = st.session_state.recognizer# try:# with sr.Microphone() as source:# st.write("Listening...")# audio = recognizer.listen(source)# user_input = recognizer.recognize_google(audio)# chat_history = chat(user_input, chat_history)# except Exception as e:# st.write("Sorry, could not recognize the speech. Please try again.")# return chat_history# # Streamlit App# def main():# st.title("IQ TechMax Chatbot")# st.subheader("Ask questions related to IQ TechMax")# # Input field# user_input = st.text_input("Your Question:", "")# chat_history = st.session_state.get('chat_history', [])# if user_input:# chat_history = chat(user_input, chat_history)# st.session_state.chat_history = chat_history# for message in chat_history:# st.write(message.content)# # Speech Recognition# mic_button = st.button("🎤 Microphone", key="mic_btn")# if mic_button:# chat_history = start_mic(chat_history)# if __name__ == '__main__':# main()
app.py+147-147
requirements.txt