|
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)) |
|
|
|
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 = "" |
|
|
|
|
|
|
|
end_time = time.time() |
|
felapsed_time = end_time - start_time |
|
print(f"TTS rsp Execution time: {felapsed_time:.4f} seconds") |
|
|
|
|
|
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 "", "" |
|
|
|
|
|
with gr.Blocks() as bc_chatbot: |
|
|
|
|
|
|
|
|
|
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", |
|
) |
|
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?"]], |
|
|
|
inputs=[txt],) |
|
|
|
btn = gr.Button("Submit") |
|
|
|
clear_btn = gr.Button("Clear Chat") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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) |