MedBot-Pub / app.py
amitagh's picture
fix err
b29011e verified
import gradio as gr
import os
from gradio_client import Client
import time
hf_key = os.environ['HF_API_KEY']
client_url = os.environ['BK_URL']
messages = [
{"role": "system", "content": "You are a helpful assistant."},
]
msg_count = 0
client = Client(client_url, hf_token=hf_key)
def get_llm_rsp(text):
rsp = client.predict(
text, # str in 'Input your question:' Textbox component
api_name="/get_llm_resp"
)
return rsp
def add_text(history, text):
global messages #message[list] is defined globally
history = history + [(text,'')]
messages = messages + [{"role":'user', 'content': text}]
return history, text
def generate_response(history, text):
global messages, msg_count
#arsp = bizbot([chatbot, text], api_name="bizbot-api")
response_msg = get_llm_rsp(text)
msg_count = msg_count + 1
messages = messages + [{"role":'assistant', 'content': response_msg}]
for char in response_msg:
history[-1][1] += char
time.sleep(0.03)
yield history
def calc_cost():
global msg_count
return msg_count
with gr.Blocks() as demo:
gr.Markdown(value="## MedBot")
gr.Markdown(value="Welcome to MedBot – Your Personal Health Assistant. I am here to provide information on various diseases and medical conditions. Please keep in mind that while I can offer valuable insights, my responses are not a substitute for professional medical advice, diagnosis, or treatment.")
gr.Markdown(value="Bot helps with medical information on several common medical disorders, conditions, tests, and treatments, including high-profile diseases such as \
Alzheimer’s disease, cancer, and heart attack. It uses language that laypersons can understand.")
#gr.Markdown(value="It supports multiple Indian languages like English, Marathi (मराठी), Hindi (हिन्दी), Bengali (বাংলা), Telugu (తెలుగు),Tamil (தமிழ்), Urdu (اردو), Gujarati (ગુજરાતી), Kannada (ಕನ್ನಡ), Punjabi (ਪੰਜਾਬੀ),Malayalam (മലയാളം), Odia (ଓଡ଼ିଆ),Assamese (অসমীয়া). You will get the response in the language you ask the question.")
gr.Markdown(value="**For accurate and personalized guidance, always consult with a qualified healthcare professional. If you have a specific question about a particular disease or health concern, feel free to ask, and I'll do my best to assist you with reliable and informative responses.\
Your health is important, and I'm here to support you on your journey to better understanding various medical conditions. Let's explore together, keeping your well-being in mind at all times.**")
chatbot = gr.Chatbot(value=[], label="MedBot")
txt = gr.Textbox(
label="Input your question:",
placeholder="Enter text and press enter",
) #.style(container=False)
gr.Examples(
label="Question examples",
examples=[["Why do we get fever?"],
["What is cancer?"],
["What is treatment for cancer?"],
],
inputs=[txt],)
btn = gr.Button("Submit")
count_msg_view = gr.Textbox(label='Number of questions answered in current conversation:',value=0)
#btn2 = gr.Button("Clear and Start New Conversation ")
txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
generate_response, inputs =[chatbot, txt],outputs = chatbot,).then(
calc_cost, outputs=count_msg_view)
btn.click(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
generate_response, inputs =[chatbot, txt],outputs = chatbot,).then(
calc_cost, outputs=count_msg_view)
#Button click action to clear conversation
#btn2.click(clear_conv,inputs=chatbot, outputs=[chatbot, count_msg_view])
demo.queue()
demo.launch(debug=True)