import gradio as gr import os import logging from langchain_core.prompts import ChatPromptTemplate from langchain_core.output_parsers import StrOutputParser from langchain_openai import ChatOpenAI from langchain_core.messages import AIMessage, HumanMessage from langchain_core.runnables import ( RunnableBranch, RunnableLambda, RunnablePassthrough, RunnableParallel, ) from langchain_core.prompts.prompt import PromptTemplate import requests import tempfile from langchain.memory import ConversationBufferWindowMemory import time import logging from langchain.chains import ConversationChain import torch import torchaudio from transformers import pipeline, AutoModelForSpeechSeq2Seq, AutoProcessor import numpy as np import threading from langchain_openai import OpenAIEmbeddings from langchain_pinecone import PineconeVectorStore from langchain.chains import RetrievalQA embeddings = OpenAIEmbeddings(api_key=os.environ['OPENAI_API_KEY']) def initialize_gpt_model(): return ChatOpenAI(api_key=os.environ['OPENAI_API_KEY'], temperature=0, model='gpt-4o') gpt_model = initialize_gpt_model() gpt_embeddings = OpenAIEmbeddings(api_key=os.environ['OPENAI_API_KEY']) gpt_vectorstore = PineconeVectorStore(index_name="radardata10312024", embedding=gpt_embeddings) gpt_retriever = gpt_vectorstore.as_retriever(search_kwargs={'k': 5}) # Pinecone setup from pinecone import Pinecone pc = Pinecone(api_key=os.environ['PINECONE_API_KEY']) index_name ="radardata10312024" vectorstore = PineconeVectorStore(index_name=index_name, embedding=embeddings) retriever = vectorstore.as_retriever(search_kwargs={'k': 5}) chat_model = ChatOpenAI(api_key=os.environ['OPENAI_API_KEY'], temperature=0, model='gpt-4o') #code for history conversational_memory = ConversationBufferWindowMemory( memory_key='chat_history', k=10, return_messages=True ) template =f"""Hello there! As your friendly and knowledgeable guide here in Birmingham, Alabama.Give the short ,precise,crisp and straight-foreward response of maximum 2 sentences and dont greet. {{context}} Question: {{question}} Helpful Answer:""" QA_CHAIN_PROMPT= PromptTemplate(input_variables=["context", "question"], template=template) def build_qa_chain(prompt_template): qa_chain = RetrievalQA.from_chain_type( llm=chat_model, chain_type="stuff", retriever=retriever, chain_type_kwargs={"prompt": prompt_template} ) return qa_chain # Return the qa_chain object # Instantiate the QA Chain using the defined prompt template qa_chain = build_qa_chain(QA_CHAIN_PROMPT) # Define the function to clear input and output def clear_fields(): return [],"",None # Function to generate audio with Eleven Labs TTS def generate_audio_elevenlabs(text): XI_API_KEY = os.environ['ELEVENLABS_API'] VOICE_ID = 'ehbJzYLQFpwbJmGkqbnW' tts_url = f"https://api.elevenlabs.io/v1/text-to-speech/{VOICE_ID}/stream" headers = { "Accept": "application/json", "xi-api-key": XI_API_KEY } data = { "text": str(text), "model_id": "eleven_multilingual_v2", "voice_settings": { "stability": 1.0, "similarity_boost": 0.0, "style": 0.60, "use_speaker_boost": False } } response = requests.post(tts_url, headers=headers, json=data, stream=True) if response.ok: with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as f: for chunk in response.iter_content(chunk_size=1024): if chunk: f.write(chunk) audio_path = f.name logging.debug(f"Audio saved to {audio_path}") return audio_path # Return audio path for automatic playback else: logging.error(f"Error generating audio: {response.text}") return None import time # Main function to handle mode selection with character-by-character streaming def handle_mode_selection(mode, chat_history, question): if mode == "Normal Chatbot": chat_history.append((question, "")) # Append user question with an empty response initially # Get response from Pinecone using the qa_chain response = qa_chain({"query": question, "context": ""}) response_text = response['result'] # Stream each character in the response text to the chat history for i, char in enumerate(response_text): chat_history[-1] = (question, chat_history[-1][1] + char) # Update the last message yield chat_history, "", None # Yield updated chat history time.sleep(0.05) # Small delay to simulate streaming elif mode == "Voice to Voice Conversation": response_text = qa_chain({"query": question, "context": ""})['result'] audio_path = generate_audio_elevenlabs(response_text) yield [], "", audio_path # Only output the audio response without updating chatbot history # Function to add a user's message to the chat history and clear the input box def add_message(history, message): if message.strip(): history.append((message, "")) # Add the user's message to the chat history only if it's not empty return history, "" # Clear the input box # Define function to generate a streaming response def chat_with_bot(messages): user_message = messages[-1][0] # Get the last user message (input) messages[-1] = (user_message, "") # Prepare a placeholder for the bot's response response = get_response(user_message) # Assume `get_response` is a generator function # Stream each character in the response and update the history progressively for character in response: messages[-1] = (user_message, messages[-1][1] + character) yield messages # Stream each updated chunk time.sleep(0.05) # Adjust delay as needed for real-time effect yield messages # Final yield to complete the response # Function to generate audio with Eleven Labs TTS from the last bot response def generate_audio_from_last_response(history): # Get the most recent bot response from the chat history if history and len(history) > 0: recent_response = history[-1][1] # The second item in the tuple is the bot response text if recent_response: return generate_audio_elevenlabs(recent_response) return None # Define example prompts examples = [ ["what are the tree care services at alabama?"], ["where from i studies undergrade in marketing from alabama?"], ["what from i get tourism recreation center?"], ["where from i will get a retail loan and from which institute?"], ["where i will look for good dentist at alabama?"] ] # Function to insert the prompt into the textbox when clicked def insert_prompt(current_text, prompt): return prompt[0] if prompt else current_text # Define the ASR model with Whisper model_id = 'openai/whisper-large-v3' device = "cuda:0" if torch.cuda.is_available() else "cpu" torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32 model = AutoModelForSpeechSeq2Seq.from_pretrained(model_id, torch_dtype=torch_dtype).to(device) processor = AutoProcessor.from_pretrained(model_id) pipe_asr = pipeline( "automatic-speech-recognition", model=model, tokenizer=processor.tokenizer, feature_extractor=processor.feature_extractor, max_new_tokens=128, chunk_length_s=15, batch_size=16, torch_dtype=torch_dtype, device=device, return_timestamps=True ) # Define the function to reset the state after 10 seconds def auto_reset_state(): time.sleep(5) return None, "" # Reset the state and clear input text def transcribe_function(stream, new_chunk): try: sr, y = new_chunk[0], new_chunk[1] except TypeError: print(f"Error chunk structure: {type(new_chunk)}, content: {new_chunk}") return stream, "", None # Ensure y is not empty and is at least 1-dimensional if y is None or len(y) == 0: return stream, "", None y = y.astype(np.float32) max_abs_y = np.max(np.abs(y)) if max_abs_y > 0: y = y / max_abs_y # Ensure stream is also at least 1-dimensional before concatenation if stream is not None and len(stream) > 0: stream = np.concatenate([stream, y]) else: stream = y # Process the audio data for transcription result = pipe_asr({"array": stream, "sampling_rate": sr}, return_timestamps=False) full_text = result.get("text", "") # Start a thread to reset the state after 10 seconds threading.Thread(target=auto_reset_state).start() return stream, full_text, full_text # Define the function to clear the state and input text def clear_transcription_state(): return None, "" with gr.Blocks(theme="rawrsor1/Everforest") as demo: chatbot = gr.Chatbot([], elem_id="RADAR", bubble_full_width=False) with gr.Row(): with gr.Column(): mode_selection = gr.Radio( choices=["Normal Chatbot", "Voice to Voice Conversation"], label="Mode Selection", value="Normal Chatbot" ) with gr.Row(): with gr.Column(): question_input = gr.Textbox(label="Ask a Question", placeholder="Type your question here...") audio_input = gr.Audio(sources=["microphone"], streaming=True, type='numpy', every=0.1, label="Speak to Ask") submit_voice_btn = gr.Button("Submit Voice") with gr.Column(): audio_output = gr.Audio(label="Audio", type="filepath", autoplay=True, interactive=False) with gr.Row(): with gr.Column(): get_response_btn = gr.Button("Get Response") with gr.Column(): clear_state_btn = gr.Button("Clear State") with gr.Column(): generate_audio_btn = gr.Button("Generate Audio") with gr.Column(): clean_btn = gr.Button("Clean") with gr.Row(): with gr.Column(): gr.Markdown("