Abigail
commited on
Commit
·
863d316
1
Parent(s):
5817c5e
llama2 with a little bit of context
Browse files
llama2.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""llama2
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colaboratory.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/15UK6iHd1y0pMMQc-DbZIYhSteoTCUsMH
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
#install and then restart execution
|
| 11 |
+
!pip install accelerate
|
| 12 |
+
!pip install bitsandbytes
|
| 13 |
+
!pip install optimum
|
| 14 |
+
!pip install auto-gptq
|
| 15 |
+
|
| 16 |
+
!pip install transformers
|
| 17 |
+
from transformers import AutoModelForCausalLM,AutoTokenizer
|
| 18 |
+
import torch
|
| 19 |
+
!pip install transformers huggingface_hub
|
| 20 |
+
from huggingface_hub import notebook_login
|
| 21 |
+
|
| 22 |
+
notebook_login()
|
| 23 |
+
|
| 24 |
+
mn = 'stabilityai/StableBeluga-7B'
|
| 25 |
+
#mn = "TheBloke/Llama-2-7b-Chat-GPTQ"
|
| 26 |
+
|
| 27 |
+
model = AutoModelForCausalLM.from_pretrained(mn, device_map=0, load_in_8bit=True)
|
| 28 |
+
|
| 29 |
+
#model = AutoModelForCausalLM.from_pretrained(mn, device_map=0, torch_dtype=torch.float16)
|
| 30 |
+
|
| 31 |
+
sb_sys = "### System:\nYou are a AI driving assistant in my car, that follows instructions extremely well. Help as much as you can.\n\n"
|
| 32 |
+
|
| 33 |
+
def gen(p, maxlen=15, sample=True):
|
| 34 |
+
toks = tokr(p, return_tensors="pt")
|
| 35 |
+
res = model.generate(**toks.to("cuda"), max_new_tokens=maxlen, do_sample=sample).to('cpu')
|
| 36 |
+
return tokr.batch_decode(res)
|
| 37 |
+
|
| 38 |
+
tokr = AutoTokenizer.from_pretrained(mn)
|
| 39 |
+
|
| 40 |
+
#to have a prompt corresponding to the specific format required by the fine-tuned model Stable Beluga
|
| 41 |
+
def mk_prompt(user, syst=sb_sys): return f"{syst}### User: {user}\n\n### Assistant:\n"
|
| 42 |
+
|
| 43 |
+
complete_answer= ''
|
| 44 |
+
|
| 45 |
+
#attempt to get user location
|
| 46 |
+
|
| 47 |
+
import requests
|
| 48 |
+
|
| 49 |
+
response = requests.get("http://ip-api.com/json/")
|
| 50 |
+
data = response.json()
|
| 51 |
+
print(data['city'], data['lat'], data['lon'])
|
| 52 |
+
city= data['city']
|
| 53 |
+
lat = data['lat']
|
| 54 |
+
lon = data['lon']
|
| 55 |
+
|
| 56 |
+
import re
|
| 57 |
+
model_answer= ''
|
| 58 |
+
general_context= f'I am in my car in {city}, latitude {lat}, longitude {lon}, I can move with my car to reach a destination'
|
| 59 |
+
pattern = r"Assistant:\\n(.*?)</s>"
|
| 60 |
+
|
| 61 |
+
ques = "I hate pizzas"
|
| 62 |
+
|
| 63 |
+
ques_ctx = f"""Answer the question with the help of the provided context.
|
| 64 |
+
|
| 65 |
+
## Context
|
| 66 |
+
|
| 67 |
+
{general_context} .
|
| 68 |
+
|
| 69 |
+
## Question
|
| 70 |
+
|
| 71 |
+
{ques}"""
|
| 72 |
+
|
| 73 |
+
complete_answer = str(gen(mk_prompt(ques_ctx), 150))
|
| 74 |
+
|
| 75 |
+
match = re.search(pattern, complete_answer, re.DOTALL)
|
| 76 |
+
|
| 77 |
+
if match:
|
| 78 |
+
# Extracting the text
|
| 79 |
+
model_answer = match.group(1)
|
| 80 |
+
else:
|
| 81 |
+
model_answer = "There has been an error with the generated response."
|
| 82 |
+
|
| 83 |
+
general_context += model_answer
|
| 84 |
+
print(model_answer)
|
| 85 |
+
|
| 86 |
+
print(complete_answer)
|