Spaces:
Runtime error
Runtime error
new
Browse files
app.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
from gradio_client import Client
|
| 4 |
+
|
| 5 |
+
hf_key = os.environ['HF_API_KEY']
|
| 6 |
+
|
| 7 |
+
messages = [
|
| 8 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
| 9 |
+
]
|
| 10 |
+
msg_count = 0
|
| 11 |
+
|
| 12 |
+
client = Client("https://amitagh-medbot-back.hf.space/--replicas/2ewvx/", hf_token=hf_key)
|
| 13 |
+
def get_llm_rsp(text):
|
| 14 |
+
rsp = client.predict(
|
| 15 |
+
text, # str in 'Input your question:' Textbox component
|
| 16 |
+
api_name="/get_llm_resp"
|
| 17 |
+
)
|
| 18 |
+
return rsp
|
| 19 |
+
|
| 20 |
+
def add_text(history, text):
|
| 21 |
+
global messages #message[list] is defined globally
|
| 22 |
+
history = history + [(text,'')]
|
| 23 |
+
messages = messages + [{"role":'user', 'content': text}]
|
| 24 |
+
return history, ""
|
| 25 |
+
|
| 26 |
+
def generate_response(history, text):
|
| 27 |
+
global messages, msg_count
|
| 28 |
+
|
| 29 |
+
#arsp = bizbot([chatbot, text], api_name="bizbot-api")
|
| 30 |
+
response_msg = get_llm_rsp(text)
|
| 31 |
+
|
| 32 |
+
msg_count = msg_count + 1
|
| 33 |
+
messages = messages + [{"role":'assistant', 'content': response_msg}]
|
| 34 |
+
|
| 35 |
+
for char in response_msg:
|
| 36 |
+
|
| 37 |
+
history[-1][1] += char
|
| 38 |
+
time.sleep(0.03)
|
| 39 |
+
yield history
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def calc_cost():
|
| 43 |
+
global msg_count
|
| 44 |
+
return msg_count
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
with gr.Blocks() as demo:
|
| 48 |
+
|
| 49 |
+
gr.Markdown(value="## MedBot")
|
| 50 |
+
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 \
|
| 51 |
+
Alzheimer’s disease, cancer, and heart attack. It uses language that laypersons can understand,")
|
| 52 |
+
gr.Markdown(value="**Please use info cautiously. Please consult a doctor for final treatment.**")
|
| 53 |
+
chatbot = gr.Chatbot(value=[], label="MedBot")
|
| 54 |
+
|
| 55 |
+
txt = gr.Textbox(
|
| 56 |
+
label="Input your question:",
|
| 57 |
+
placeholder="Enter text and press enter",
|
| 58 |
+
) #.style(container=False)
|
| 59 |
+
gr.Examples(
|
| 60 |
+
label="Question examples",
|
| 61 |
+
examples=[["What is ultrasound?"],
|
| 62 |
+
["What is cancer?"],
|
| 63 |
+
["What is treatment for cancer?"],
|
| 64 |
+
],
|
| 65 |
+
inputs=[txt],)
|
| 66 |
+
btn = gr.Button("Submit")
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
count_msg_view = gr.Textbox(label='Number of questions answered in current conversation:',value=0)
|
| 70 |
+
#btn2 = gr.Button("Clear and Start New Conversation ")
|
| 71 |
+
|
| 72 |
+
txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
|
| 73 |
+
generate_response, inputs =[chatbot, txt],outputs = chatbot,).then(
|
| 74 |
+
calc_cost, outputs=count_msg_view)
|
| 75 |
+
btn.click(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
|
| 76 |
+
generate_response, inputs =[chatbot, txt],outputs = chatbot,).then(
|
| 77 |
+
calc_cost, outputs=count_msg_view)
|
| 78 |
+
|
| 79 |
+
#Button click action to clear conversation
|
| 80 |
+
#btn2.click(clear_conv,inputs=chatbot, outputs=[chatbot, count_msg_view])
|
| 81 |
+
|
| 82 |
+
demo.queue()
|
| 83 |
+
demo.launch(debug=True)
|