MedBot-Pub / app.py
amitagh's picture
change url
b9ad9a1 verified
raw
history blame
No virus
2.79 kB
import gradio as gr
import os
from gradio_client import Client
import time
hf_key = os.environ['HF_API_KEY']
messages = [
{"role": "system", "content": "You are a helpful assistant."},
]
msg_count = 0
client = Client("https://amitagh-medbot-back.hf.space/--replicas/m3ig8/", 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, ""
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="Bot helps with medical information on nearly 1,700 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="**Please use info cautiously. Please consult a doctor for final treatment.**")
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=[["What is ultrasound?"],
["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)