galib45 commited on
Commit
91d3a6a
·
verified ·
1 Parent(s): d36c2bc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -0
app.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import transformers
2
+ import torch
3
+ import gradio as gr
4
+
5
+ # Initialize the model and pipeline
6
+ model_name_or_path = "m42-health/Llama3-Med42-8B"
7
+
8
+ pipeline = transformers.pipeline(
9
+ "text-generation",
10
+ model=model_name_or_path,
11
+ torch_dtype=torch.bfloat16,
12
+ device_map="auto",
13
+ )
14
+
15
+ # Define the system message for the chatbot personality
16
+ system_message = {
17
+ "role": "system",
18
+ "content": (
19
+ "You are a helpful, respectful, and honest medical assistant. "
20
+ "You are a second version of Med42 developed by the AI team at M42, UAE. "
21
+ "Always answer as helpfully as possible, while being safe. "
22
+ "Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. "
23
+ "Please ensure that your responses are socially unbiased and positive in nature. "
24
+ "If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. "
25
+ "If you don’t know the answer to a question, please don’t share false information."
26
+ ),
27
+ }
28
+
29
+ # Define stop tokens
30
+ stop_tokens = [
31
+ pipeline.tokenizer.eos_token_id,
32
+ pipeline.tokenizer.convert_tokens_to_ids("<|eot_id|>"),
33
+ ]
34
+
35
+ # Initialize the conversation history
36
+ conversation_history = [system_message]
37
+
38
+ def chat_with_model(user_input):
39
+ # Append user message to conversation history
40
+ conversation_history.append({"role": "user", "content": user_input})
41
+
42
+ # Format the conversation for input to the model
43
+ prompt = pipeline.tokenizer.apply_chat_template(
44
+ conversation_history, tokenize=False, add_generation_prompt=False
45
+ )
46
+
47
+ # Generate response
48
+ outputs = pipeline(
49
+ prompt,
50
+ max_new_tokens=512,
51
+ eos_token_id=stop_tokens,
52
+ do_sample=True,
53
+ temperature=0.4,
54
+ top_k=150,
55
+ top_p=0.75,
56
+ )
57
+
58
+ # Extract the generated response (the part after the prompt)
59
+ generated_text = outputs[0]["generated_text"][len(prompt):]
60
+
61
+ # Append the assistant's response to the conversation history
62
+ conversation_history.append({"role": "assistant", "content": generated_text})
63
+
64
+ return generated_text.strip()
65
+
66
+ # Create Gradio interface
67
+ iface = gr.Interface(
68
+ fn=chat_with_model,
69
+ inputs="text",
70
+ outputs="text",
71
+ title="Med42 Medical Assistant",
72
+ description="Ask anything about medicine!",
73
+ )
74
+
75
+ # Launch the app
76
+ iface.launch()