new
Browse files
app.py
ADDED
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import time
|
3 |
+
import json
|
4 |
+
import os
|
5 |
+
|
6 |
+
from PIL import Image
|
7 |
+
from gradio_client import Client
|
8 |
+
from dataclasses import dataclass
|
9 |
+
|
10 |
+
|
11 |
+
hf_key = os.environ['HF_API_KEY']
|
12 |
+
|
13 |
+
client = Client("amitagh/BC-Chat-pvt", hf_token=hf_key)
|
14 |
+
def get_cust_rsp(history, text):
|
15 |
+
|
16 |
+
result = client.predict(
|
17 |
+
history=history,
|
18 |
+
text=text,
|
19 |
+
api_name="/handle_user_ques"
|
20 |
+
)
|
21 |
+
print(result)
|
22 |
+
|
23 |
+
return result
|
24 |
+
|
25 |
+
def handle_user_ques(history, text):
|
26 |
+
start_time = time.time()
|
27 |
+
|
28 |
+
llm_response = get_cust_rsp(history,text)
|
29 |
+
|
30 |
+
history.append((text, llm_response["answer"]))
|
31 |
+
# Calculate and print elapsed time
|
32 |
+
end_time = time.time()
|
33 |
+
felapsed_time = end_time - start_time
|
34 |
+
print(f"Text LLM rsp Execution time: {felapsed_time:.4f} seconds")
|
35 |
+
|
36 |
+
start_time = time.time()
|
37 |
+
|
38 |
+
audio_file_path = ""
|
39 |
+
#audio_file_path = convet_tts(llm_response)
|
40 |
+
|
41 |
+
# Calculate and print elapsed time
|
42 |
+
end_time = time.time()
|
43 |
+
felapsed_time = end_time - start_time
|
44 |
+
print(f"TTS rsp Execution time: {felapsed_time:.4f} seconds")
|
45 |
+
|
46 |
+
#return history, "", audio_file_path
|
47 |
+
return history, ""
|
48 |
+
|
49 |
+
|
50 |
+
def clear_chat(history, text):
|
51 |
+
"""
|
52 |
+
Resets the chat history and the lead object.
|
53 |
+
Args:
|
54 |
+
history (str): The current chat history.
|
55 |
+
text (str): The current text.
|
56 |
+
Returns:
|
57 |
+
tuple: A tuple of two empty strings.
|
58 |
+
"""
|
59 |
+
history = ""
|
60 |
+
return "", ""
|
61 |
+
|
62 |
+
# Create the Gradio interface
|
63 |
+
with gr.Blocks() as bc_chatbot:
|
64 |
+
#gr.set_static_paths(paths=["."])
|
65 |
+
#image_path = "Shivneri-Chat-Banner.png"
|
66 |
+
|
67 |
+
#gr.HTML(f"""<img src="/file={image_path}" width="750" height="300">""")
|
68 |
+
gr.Markdown(value="## BC Chatbot")
|
69 |
+
with gr.Row():
|
70 |
+
with gr.Column(scale=1):
|
71 |
+
chatbot = gr.Chatbot(value=[], label="BC")
|
72 |
+
|
73 |
+
|
74 |
+
txt = gr.Textbox(
|
75 |
+
label="Input your question:",
|
76 |
+
placeholder="Enter text and press enter",
|
77 |
+
) #.style(container=False)
|
78 |
+
examples = gr.Examples(
|
79 |
+
label="Question examples",
|
80 |
+
examples=[["What is this book about?"], ["What is Ashram?"], ["What are the different Ashrams?"], ["What is Bramhacharya?"],
|
81 |
+
["What is Grihastha?"], ["What is Vanaprastha?"], ["What is Sannyasa?"], ["What are key takeaways of each stage?"], ["what is the importance of shlokas in Bhagwadgita?"],
|
82 |
+
["What are the different types of dialogues?"], ["On personal life how can shlokas help?"]],
|
83 |
+
#["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"],],
|
84 |
+
inputs=[txt],)
|
85 |
+
#output_audio = gr.Audio(autoplay=True, visible=False)
|
86 |
+
btn = gr.Button("Submit")
|
87 |
+
|
88 |
+
clear_btn = gr.Button("Clear Chat")
|
89 |
+
|
90 |
+
#app_txt = gr.Textbox(label = "App info", interactive=False)
|
91 |
+
#app_but = gr.Button("App info")
|
92 |
+
#app_but.click(
|
93 |
+
# app_inst.get_app_info,
|
94 |
+
# inputs=[],
|
95 |
+
# outputs=[app_txt],
|
96 |
+
#)
|
97 |
+
|
98 |
+
|
99 |
+
#txt.submit(handle_customer_response, [chatbot, txt], [chatbot, txt, output_audio])
|
100 |
+
#btn.click(handle_customer_response, [chatbot, txt], [chatbot, txt, output_audio])
|
101 |
+
txt.submit(handle_user_ques, [chatbot, txt], [chatbot, txt])
|
102 |
+
btn.click(handle_user_ques, [chatbot, txt], [chatbot, txt])
|
103 |
+
clear_btn.click(
|
104 |
+
clear_chat,
|
105 |
+
[chatbot, txt], [chatbot, txt]
|
106 |
+
)
|
107 |
+
|
108 |
+
|
109 |
+
bc_chatbot.queue()
|
110 |
+
bc_chatbot.launch(debug=True)
|