BC-Pub / app.py
amitagh's picture
get_cust_rsp
5c37cb9 verified
import gradio as gr
import time
import json
import os
from PIL import Image
from gradio_client import Client
from dataclasses import dataclass
hf_key = os.environ['HF_API_KEY']
client = Client("amitagh/BC-Chat-pvt", hf_token=hf_key)
def get_cust_rsp(history, text):
result = client.predict(
history=history,
text=text,
api_name="/handle_user_ques"
)
print(result)
return result
def handle_user_ques(history, text):
start_time = time.time()
llm_response = get_cust_rsp(history,text)
history.append((text, llm_response))
# Calculate and print elapsed time
end_time = time.time()
felapsed_time = end_time - start_time
print(f"Text LLM rsp Execution time: {felapsed_time:.4f} seconds")
start_time = time.time()
audio_file_path = ""
#audio_file_path = convet_tts(llm_response)
# Calculate and print elapsed time
end_time = time.time()
felapsed_time = end_time - start_time
print(f"TTS rsp Execution time: {felapsed_time:.4f} seconds")
#return history, "", audio_file_path
return history, ""
def clear_chat(history, text):
"""
Resets the chat history and the lead object.
Args:
history (str): The current chat history.
text (str): The current text.
Returns:
tuple: A tuple of two empty strings.
"""
history = ""
return "", ""
# Create the Gradio interface
with gr.Blocks() as bc_chatbot:
#gr.set_static_paths(paths=["."])
#image_path = "Shivneri-Chat-Banner.png"
#gr.HTML(f"""<img src="/file={image_path}" width="750" height="300">""")
gr.Markdown(value="## BC Chatbot")
with gr.Row():
with gr.Column(scale=1):
chatbot = gr.Chatbot(value=[], label="BC")
txt = gr.Textbox(
label="Input your question:",
placeholder="Enter text and press enter",
) #.style(container=False)
examples = gr.Examples(
label="Question examples",
examples=[["What is this book about?"], ["What is Ashram?"], ["What are the different Ashrams?"], ["What is Bramhacharya?"],
["What is Grihastha?"], ["What is Vanaprastha?"], ["What is Sannyasa?"], ["What are key takeaways of each stage?"], ["what is the importance of shlokas in Bhagwadgita?"],
["What are the different types of dialogues?"], ["On personal life how can shlokas help?"]],
#["Too slow & Time consuming"], ["Fully automated, consistent"], ["Hired more people, but that is costly. Tried a automated solution but it was complex and is inconsistent"],],
inputs=[txt],)
#output_audio = gr.Audio(autoplay=True, visible=False)
btn = gr.Button("Submit")
clear_btn = gr.Button("Clear Chat")
#app_txt = gr.Textbox(label = "App info", interactive=False)
#app_but = gr.Button("App info")
#app_but.click(
# app_inst.get_app_info,
# inputs=[],
# outputs=[app_txt],
#)
#txt.submit(handle_customer_response, [chatbot, txt], [chatbot, txt, output_audio])
#btn.click(handle_customer_response, [chatbot, txt], [chatbot, txt, output_audio])
txt.submit(get_cust_rsp, [chatbot, txt], [chatbot, txt])
btn.click(get_cust_rsp, [chatbot, txt], [chatbot, txt])
clear_btn.click(
clear_chat,
[chatbot, txt], [chatbot, txt]
)
bc_chatbot.queue()
bc_chatbot.launch(debug=True)