Spaces:
Runtime error
Runtime error
Paulie-Aditya
commited on
Commit
Β·
a88b5a8
1
Parent(s):
f597e32
update using medical chatbot
Browse files- .gitignore +2 -0
- README.md +0 -2
- app.py +46 -42
- backend.py +39 -0
.gitignore
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
*.venv
|
2 |
+
*.gitattributes
|
README.md
CHANGED
@@ -8,5 +8,3 @@ sdk_version: 5.0.1
|
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
---
|
11 |
-
|
12 |
-
An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
|
|
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
---
|
|
|
|
app.py
CHANGED
@@ -1,62 +1,66 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
|
|
3 |
|
4 |
"""
|
5 |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
6 |
"""
|
7 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
|
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
-
|
20 |
-
for val in history:
|
21 |
-
if val[0]:
|
22 |
-
messages.append({"role": "user", "content": val[0]})
|
23 |
-
if val[1]:
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
|
26 |
-
|
27 |
|
28 |
-
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
|
39 |
-
|
40 |
-
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
"""
|
44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
45 |
-
"""
|
46 |
demo = gr.ChatInterface(
|
47 |
respond,
|
48 |
-
additional_inputs=[
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
],
|
60 |
)
|
61 |
|
62 |
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
+
from backend import MedicalAssistant
|
4 |
|
5 |
"""
|
6 |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
7 |
"""
|
8 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
9 |
+
assistant = MedicalAssistant()
|
10 |
|
11 |
+
# def respond(
|
12 |
+
# message,
|
13 |
+
# history: list[tuple[str, str]],
|
14 |
+
# system_message,
|
15 |
+
# max_tokens,
|
16 |
+
# temperature,
|
17 |
+
# top_p,
|
18 |
+
# ):
|
19 |
+
# messages = [{"role": "system", "content": system_message}]
|
20 |
|
21 |
+
# for val in history:
|
22 |
+
# if val[0]:
|
23 |
+
# messages.append({"role": "user", "content": val[0]})
|
24 |
+
# if val[1]:
|
25 |
+
# messages.append({"role": "assistant", "content": val[1]})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
+
# messages.append({"role": "user", "content": message})
|
28 |
|
29 |
+
# response = ""
|
30 |
|
31 |
+
# for message in client.chat_completion(
|
32 |
+
# messages,
|
33 |
+
# max_tokens=max_tokens,
|
34 |
+
# stream=True,
|
35 |
+
# temperature=temperature,
|
36 |
+
# top_p=top_p,
|
37 |
+
# ):
|
38 |
+
# token = message.choices[0].delta.content
|
39 |
|
40 |
+
# response += token
|
41 |
+
# yield response
|
42 |
|
43 |
+
def respond(
|
44 |
+
message,
|
45 |
+
history: list[tuple[str, str]]
|
46 |
+
):
|
47 |
+
response = assistant.generate_response(message)
|
48 |
+
return response
|
49 |
|
|
|
|
|
|
|
50 |
demo = gr.ChatInterface(
|
51 |
respond,
|
52 |
+
# additional_inputs=[
|
53 |
+
# gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
54 |
+
# gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
55 |
+
# gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
56 |
+
# gr.Slider(
|
57 |
+
# minimum=0.1,
|
58 |
+
# maximum=1.0,
|
59 |
+
# value=0.95,
|
60 |
+
# step=0.05,
|
61 |
+
# label="Top-p (nucleus sampling)",
|
62 |
+
# ),
|
63 |
+
# ],
|
64 |
)
|
65 |
|
66 |
|
backend.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
+
|
4 |
+
class MedicalAssistant:
|
5 |
+
def __init__(self, model_name="sethuiyer/Medichat-Llama3-8B", device="cuda"):
|
6 |
+
self.device = device
|
7 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
+
self.model = AutoModelForCausalLM.from_pretrained(model_name).to(self.device)
|
9 |
+
self.sys_message = '''
|
10 |
+
You are an AI Medical Assistant trained on a vast dataset of health information. Please be thorough and
|
11 |
+
provide an informative answer. If you don't know the answer to a specific medical inquiry, advise seeking professional help.
|
12 |
+
'''
|
13 |
+
|
14 |
+
def format_prompt(self, question):
|
15 |
+
messages = [
|
16 |
+
{"role": "system", "content": self.sys_message},
|
17 |
+
{"role": "user", "content": question}
|
18 |
+
]
|
19 |
+
prompt = self.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
20 |
+
return prompt
|
21 |
+
|
22 |
+
def generate_response(self, question, max_new_tokens=512):
|
23 |
+
prompt = self.format_prompt(question)
|
24 |
+
inputs = self.tokenizer(prompt, return_tensors="pt").to(self.device)
|
25 |
+
with torch.no_grad():
|
26 |
+
outputs = self.model.generate(**inputs, max_new_tokens=max_new_tokens, use_cache=True)
|
27 |
+
answer = self.tokenizer.batch_decode(outputs, skip_special_tokens=True)[0].strip()
|
28 |
+
return answer
|
29 |
+
|
30 |
+
# if __name__ == "__main__":
|
31 |
+
# assistant = MedicalAssistant()
|
32 |
+
# question = '''
|
33 |
+
# Symptoms:
|
34 |
+
# Dizziness, headache, and nausea.
|
35 |
+
|
36 |
+
# What is the differential diagnosis?
|
37 |
+
# '''
|
38 |
+
# response = assistant.generate_response(question)
|
39 |
+
# print(response)
|