from gradio_client import Client from hugchat import hugchat from hugchat.login import Login from gtts import gTTS import gradio as gr client = Client("https://sanchit-gandhi-whisper-large-v2.hf.space/") chat_client = Client("https://huggingfaceh4-falcon-chat.hf.space/") retrieval = Client("https://warlord-k-iiti-similarity.hf.space/") n_conv = 0 init_prompt ="## Instruction: You are an AI language model and must return truthful responses as per the information below\n ##Input: Information: My name is IITIGPT. I am a helpful and truthful chatbot. I can help answer any questions about the IIT Indore campus." info="Information: \n" q_prompt="\n ##Instruction: Please provide an appropriate response to the following: \n" def login_hf(): email="jackrudolf584@gmail.com" passwd="Qwertypassword1" # login sign = Login(email, passwd) cookies = sign.login() # Create a ChatBot chatbot = hugchat.ChatBot(cookies=cookies.get_dict()) # or cookie_path="usercookies/.json" chatbot.chat(init_prompt) print("Logged In") return chatbot def change_conv(): # Create a new conversation id = chatbot.new_conversation() chatbot.change_conversation(id) chatbot.chat(init_prompt) chatbot.cookies = {} def answer_question(question): global n_conv if(n_conv > 3): n_conv = 0 change_conv(chatbot) information = retrieval.predict(question, api_name = "/predict") answer = chatbot.chat(info+ information +q_prompt+" "+question+"""\n ##Response:""") n_conv+=1 return answer def answer(question): information = retrieval.predict(question, api_name = "/predict") answer=chat_client.predict( "Howdy!", "abc.json", "You are an AI language model and must return truthful responses as per the information below\n ##Input: Information: Your name is IITIGPT. You are a helpful and truthful chatbot. You can help answer any questions about the IIT Indore campus." +information+question, # str in 'Type an input and press Enter' Textbox component 0.8, 0.9, fn_index=4 ) with open(answer, 'rb') as f: data = f.read() temp=json.load(data) print(temp) return temp # chatbot = login_hf() def file_to_text(audio_fpath): result = client.predict( audio_fpath, "transcribe", # str in 'Audio input' Radio component api_name="/predict" ) return result def text_file(text): tts = gTTS(text, lang = "en") tts.save("abc.mp3") return "abc.mp3" def main(filename): text = file_to_text(filename) print(text) # answer = answer_question(text) answer = answer(text) print(answer) output = text_file(answer) return output with gr.Blocks() as demo: with gr.Row(): input = gr.Audio(source = "upload", type = "filepath", label = "Input") output = gr.Audio(type = "filepath", label = "Output") btn = gr.Button("Run") btn.click(main, [input], [output]) if __name__ == "__main__": demo.launch()