File size: 2,785 Bytes
498aec5
 
 
2f5f716
498aec5
 
 
 
 
 
 
 
b9ad9a1
498aec5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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)