artintel235 commited on
Commit
455762b
β€’
1 Parent(s): 627141f

added modified app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -0
app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from peft import PeftModel, PeftConfig
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
+
5
+ device_map = {
6
+ "transformer.word_embeddings": 0,
7
+ "transformer.word_embeddings_layernorm": 0,
8
+ "lm_head": "cpu",
9
+ "transformer.h": 0,
10
+ "transformer.ln_f": 0,
11
+ }
12
+
13
+ # config = PeftConfig.from_pretrained("/content/llama-2-7b-medichat")
14
+ model = AutoModelForCausalLM.from_pretrained("NousResearch/Llama-2-7b-chat-hf", return_dict=True, load_in_8bit=True, device_map=device_map)
15
+ tokenizer = AutoTokenizer.from_pretrained("NousResearch/Llama-2-7b-chat-hf")
16
+ model = PeftModel.from_pretrained(model, "maxspin/medichat")
17
+
18
+ import gradio as gr
19
+
20
+
21
+ iface.launch()
22
+
23
+ def query_handling(query, conversation):
24
+ if "thanks" in query.lower() or "thank you" in query.lower() or "thank you very much" in query.lower():
25
+ conversation=""
26
+ return conversation
27
+
28
+ def process_response(input_string):
29
+ # Find the indices of the first [INST] and last [/INST]
30
+ start_index = input_string.find("[INST]")
31
+ end_index = input_string.rfind("[/INST]")
32
+
33
+ # If both [INST] and [/INST] are found
34
+ if start_index != -1 and end_index != -1:
35
+ # Extract the substring between [INST] and [/INST]
36
+ inst_substring = input_string[start_index:end_index + len("[/INST]")]
37
+ # Remove the extracted substring from the original string
38
+ cleaned_string = input_string.replace(inst_substring, "")
39
+ else:
40
+ # If [INST] or [/INST] is not found, keep the original string
41
+ cleaned_string = input_string
42
+
43
+ # Remove the special characters <s> and </s>
44
+ cleaned_string = cleaned_string.replace("<s>", "").replace("</s>", "").replace("[INST]","").replace("[/INST]","")
45
+
46
+ return cleaned_string
47
+
48
+
49
+ conversation=""
50
+ def predict(prompt):
51
+ global conversation
52
+ conversation = conversation+f"[INST]{prompt}[/INST]"
53
+ input_sequense = "<s>"+conversation
54
+ batch = tokenizer(f"{input_sequense}", return_tensors='pt')
55
+ batch = batch.to('cuda')
56
+ with torch.cuda.amp.autocast():
57
+ output_tokens = model.generate(**batch, max_new_tokens=4000)
58
+ response = tokenizer.decode(output_tokens[0], skip_special_tokens=True)
59
+ print('\n\n', tokenizer.decode(output_tokens[0], skip_special_tokens=True))
60
+ response = process_response(response)
61
+ conversation+=response
62
+ conversation = query_handling(prompt,conversation)
63
+ print(conversation)
64
+ return response
65
+
66
+ iface = gr.Interface(
67
+ fn=predict,
68
+ inputs="text", # Accepts a single text input
69
+ outputs="text" # Outputs a single text response
70
+ )