Tabish009 commited on
Commit
c378b1d
1 Parent(s): 5061f72

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -0
app.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+
4
+ # Load the model and tokenizer
5
+ @st.cache_resource
6
+ def load_model_and_tokenizer():
7
+ model_name_or_path = "m42-health/med42-70b"
8
+ model = AutoModelForCausalLM.from_pretrained(model_name_or_path, device_map="auto")
9
+ tokenizer = AutoTokenizer.from_pretrained(model_name_or_path)
10
+ return model, tokenizer
11
+
12
+ # Function to generate the response
13
+ @st.cache_data
14
+ def generate_response(prompt):
15
+ prompt_template = f'''
16
+ <|system|>: You are a helpful medical assistant created by M42 Health in the UAE.
17
+ <|prompter|>:{prompt}
18
+ <|assistant|>:
19
+ '''
20
+ input_ids = tokenizer(prompt_template, return_tensors='pt').input_ids.cuda()
21
+ output = model.generate(inputs=input_ids, temperature=0.7, do_sample=True, eos_token_id=tokenizer.eos_token_id, pad_token_id=tokenizer.pad_token_id, max_new_tokens=512)
22
+ response = tokenizer.decode(output[0], skip_special_tokens=True)
23
+ return response
24
+
25
+ # Streamlit app
26
+ def main():
27
+ st.title("Med42 - Clinical Large Language Model")
28
+ model, tokenizer = load_model_and_tokenizer()
29
+
30
+ prompt = st.text_area("Enter your medical query:")
31
+ if st.button("Submit"):
32
+ with st.spinner("Generating response..."):
33
+ response = generate_response(prompt)
34
+ st.write(response)
35
+
36
+ if __name__ == "__main__":
37
+ main()