khanhbdang commited on
Commit
b44e38c
·
verified ·
1 Parent(s): 92ba4ff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -30
app.py CHANGED
@@ -1,42 +1,64 @@
 
1
  import torch
2
  from transformers import AutoTokenizer, AutoModelForCausalLM
3
 
 
4
  model_id = "Writer/Palmyra-Med-70B-32k"
5
 
6
- tokenizer = AutoTokenizer.from_pretrained(model_id)
 
 
 
 
 
 
 
 
 
7
 
8
- model = AutoModelForCausalLM.from_pretrained(
9
- model_id,
10
- torch_dtype=torch.float16,
11
- device_map="auto",
12
- attn_implementation="flash_attention_2",
13
- )
14
 
15
- messages = [
16
- {
17
- "role": "system",
18
- "content": "You are a highly knowledgeable and experienced expert in the healthcare and biomedical field, possessing extensive medical knowledge and practical expertise.",
19
- },
20
- {
21
- "role": "user",
22
- "content": "Does danzhi Xiaoyao San ameliorate depressive-like behavior by shifting toward serotonin via the downregulation of hippocampal indoleamine 2,3-dioxygenase?",
23
- },
24
- ]
25
-
26
- input_ids = tokenizer.apply_chat_template(
27
- messages, tokenize=True, add_generation_prompt=True, return_tensors="pt"
28
  )
29
 
30
- gen_conf = {
31
- "max_new_tokens": 256,
32
- "eos_token_id": [tokenizer.eos_token_id, tokenizer.convert_tokens_to_ids("<|eot_id|>")],
33
- "temperature": 0.0,
34
- "top_p": 0.9,
35
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
- with torch.inference_mode():
38
- output_id = model.generate(input_ids, **gen_conf)
 
39
 
40
- output_text = tokenizer.decode(output_id[0][input_ids.shape[1] :])
41
 
42
- print(output_text)
 
 
 
 
1
+ import streamlit as st
2
  import torch
3
  from transformers import AutoTokenizer, AutoModelForCausalLM
4
 
5
+ # Define the model and tokenizer
6
  model_id = "Writer/Palmyra-Med-70B-32k"
7
 
8
+ @st.cache(allow_output_mutation=True)
9
+ def load_model():
10
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
11
+ model = AutoModelForCausalLM.from_pretrained(
12
+ model_id,
13
+ torch_dtype=torch.float16,
14
+ device_map="auto",
15
+ attn_implementation="flash_attention_2",
16
+ )
17
+ return tokenizer, model
18
 
19
+ tokenizer, model = load_model()
20
+
21
+ # Define Streamlit app
22
+ st.title("Medical Query Model")
 
 
23
 
24
+ st.write(
25
+ "You are interacting with a highly knowledgeable medical model. Enter your medical question below:"
 
 
 
 
 
 
 
 
 
 
 
26
  )
27
 
28
+ user_input = st.text_area("Your Question")
29
+
30
+ if st.button("Get Response"):
31
+ if user_input:
32
+ # Prepare input for the model
33
+ messages = [
34
+ {
35
+ "role": "system",
36
+ "content": "You are a highly knowledgeable and experienced expert in the healthcare and biomedical field, possessing extensive medical knowledge and practical expertise.",
37
+ },
38
+ {
39
+ "role": "user",
40
+ "content": user_input,
41
+ },
42
+ ]
43
+
44
+ input_ids = tokenizer.apply_chat_template(
45
+ messages, tokenize=True, add_generation_prompt=True, return_tensors="pt"
46
+ )
47
+
48
+ gen_conf = {
49
+ "max_new_tokens": 256,
50
+ "eos_token_id": [tokenizer.eos_token_id, tokenizer.convert_tokens_to_ids("")],
51
+ "temperature": 0.0,
52
+ "top_p": 0.9,
53
+ }
54
 
55
+ # Generate response
56
+ with torch.no_grad():
57
+ output_id = model.generate(input_ids, **gen_conf)
58
 
59
+ output_text = tokenizer.decode(output_id[0][input_ids.shape[1]:], skip_special_tokens=True)
60
 
61
+ st.write("Response:")
62
+ st.write(output_text)
63
+ else:
64
+ st.warning("Please enter a question.")