import os import gradio as gr from langchain.chat_models import ChatOpenAI from langchain import LLMChain, PromptTemplate from langchain.memory import ConversationBufferMemory from gtts import gTTS import io import base64 import numpy as np import tempfile OPENAI_API_KEY = os.getenv('OPENAI_API_KEY') template = """ As a text generator, your main focus is to provide valuable information about mining acts, laws, and regulations specifically in India. Your responses should be restricted to queries regarding mining regulations and should avoid addressing unrelated topics such as general knowledge, full forms, celebrities, math problems, or programming. Ensure that your answers are accurate, concise, and helpful in guiding users through the complexities of mining legislation in India. Start by asking me any mining-related question you have, and I'll provide you with the information you need. {chat_history} User: {user_message} Chatbot:""" prompt = PromptTemplate( input_variables=["chat_history", "user_message"], template=template ) memory = ConversationBufferMemory(memory_key="chat_history") llm_chain = LLMChain( llm=ChatOpenAI(temperature='0.5', model_name="gpt-3.5-turbo"), prompt=prompt, verbose=True, memory=memory, ) def get_text_response(user_message, history): response = llm_chain.predict(user_message=user_message) return response # Function to generate audio from text response def get_audio_response(text_response): try: # Create a gTTS (Google Text-to-Speech) object from the text response tts = gTTS(text=text_response, lang='en') # You can specify the language here # Create a temporary file to save the audio with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_audio_file: tts.save(temp_audio_file.name) # Return the temporary file path return temp_audio_file.name except Exception as e: print(f"Error in get_audio_response: {str(e)}") return str(e) def get_combined_response(user_input, history): text_response = get_text_response(user_input, history) # Assuming you have a 'history' variable audio_response = get_audio_response(text_response) # Generate audio from the text response return text_response, audio_response input_interface = gr.Interface( fn=get_combined_response, inputs=gr.inputs.Textbox(label="User Input"), outputs=[ gr.outputs.Textbox(label="Text Response"), gr.outputs.Audio(label="Audio Response", type="numpy") ], title="Text and Speech Chatbot", description="Chatbot that provides text and speech responses.", ) if __name__ == "__main__": input_interface.launch()