crystal-technologies's picture
Upload 303 files
de4ade4
from transformers import LlamaTokenizer, LlamaForCausalLM, GenerationConfig, BitsAndBytesConfig
from Perceptrix.streamer import TextStreamer
from utils import setup_device
import torch
import tqdm
import sys
import os
model_name = os.environ.get('LLM_MODEL')
model_id = "models/CRYSTAL-instruct" if model_name == None else model_name
device = setup_device()
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_use_double_quant=True,
bnb_4bit_compute_dtype=torch.bfloat16
)
tokenizer = LlamaTokenizer.from_pretrained(
model_id,
use_fast=True)
model = LlamaForCausalLM.from_pretrained(
model_id,
load_in_8bit=False,
device_map="auto",
torch_dtype=torch.float16,
low_cpu_mem_usage=True,
offload_folder="offload",
quantization_config=bnb_config,
)
streamer = TextStreamer(tokenizer, skip_prompt=True,
skip_special_tokens=True, save_file="reply.txt")
PROMPT = '''### Instruction:
{}
### Input:
{}
### Response:'''
model.config.pad_token_id = tokenizer.pad_token_id = 0
model.config.bos_token_id = 1
model.config.eos_token_id = 2
model.eval()
if torch.__version__ >= "2" and sys.platform != "win32":
model = torch.compile(model)
def evaluate(
prompt='',
temperature=0.4,
top_p=0.65,
top_k=35,
repetition_penalty=1.1,
max_new_tokens=512,
**kwargs,
):
inputs = tokenizer(prompt, return_tensors="pt")
input_ids = inputs["input_ids"].to(device)
generation_config = GenerationConfig(
temperature=temperature,
top_p=top_p,
top_k=top_k,
repetition_penalty=repetition_penalty,
**kwargs,
)
with torch.no_grad():
generation_output = model.generate(
input_ids=input_ids,
generation_config=generation_config,
return_dict_in_generate=True,
output_scores=True,
max_new_tokens=max_new_tokens,
streamer=streamer,
)
s = generation_output.sequences[0]
output = tokenizer.decode(s)
yield output.split("### Response:")[-1].strip()
def run_instruction(
instruction,
inputs,
temperature=0.4,
top_p=0.65,
top_k=35,
repetition_penalty=1.1,
max_new_tokens=512,
stop_tokens=None,
):
now_prompt = PROMPT.format(instruction+'\n', inputs)
response = evaluate(
now_prompt, temperature, top_p, top_k, repetition_penalty, max_new_tokens, stop_tokens=stop_tokens, do_sample=True
)
for i in response:
yield i
def search_keyword(prompt):
instructions = """Prompt:Time: Fri, 23 August 2023 2:30PM\nWeather: 73F\nHow many friends have I told you about?
Search Keyword:Friends
Prompt:Time: Thu, 27 September 2023 3:41PM\nWeather: 62F\nWhat was our very first conversation
Chat Index:0
Prompt:Time: Tue, 21 September 2023 2:30PM\nWeather: 67F\nWhat was the last thing I said to you
Chat Index:-1
Prompt:Time: Sun, 31 October 2023 7:33AM\nWeather: 59F\nWhat was the last thing I said to you before that
Chat Index:-2
Prompt:Time: Sat, 30 October 2023 8:21PM\nWeather: 65F\nDid I ever tell you about my math class?
Search Keyword:math
Prompt:Time: Mon, 13 November 2023 4:52PM\nWeather: 55F\nWhat was my 7th grade English teacher's name?
Search Keyword:English
Prompt:Time: Wed, 15 May 2023 6:19PM\nWeather: 80F\nWhere did I say my wallet was?
Search Keyword:Wallet
Prompt:Time: Fri, 24 June 2023 1:52PM\nWeather: 92F\nWhat did Alex tell you?
Search Keyword:Alex
Prompt:Time: Sat, 19 July 2023 2:44PM\nWeather: 91F\nWhat was my first conversation today
Search Keyword:24 June"""
answer = ''.join(run_instruction(
instructions,
"Prompt:"+prompt+"\n",
temperature=0.5,
top_p=0.5,
top_k=200,
repetition_penalty=1.1,
max_new_tokens=256,
))
return answer
def identify_objects_from_text(prompt):
instructions = """Input:The object that flies in the air from this picture is a toy helicopter
Output:Toy helicopter
Input:For the robot to be able to achieve the task, the robot needs to look for a white shirt
Output:White shirt
Input:To complete the task, the robot needs to remove the fruits from the wooden basket.
Output:fruits, wooden basket
Input:To clean up your desk, you need to gather and organize the various items scattered around it. This includes the laptop, cell phone, scissors, pens, and other objects. By putting these items back in their designated spaces or containers, you can create a more organized and clutter-free workspace.
Output:Laptop, cell phone, scissors, pens, containers
Input:The tree with a colorful sky background is the one to be looking for.
Output:Tree"""
answer = ''.join(run_instruction(
instructions,
prompt,
temperature=0.5,
top_p=0.5,
top_k=200,
repetition_penalty=1.1,
max_new_tokens=256,
))
return answer
def robotix(prompt, stop=None):
instructions = """#Get me some water
objects = [['water: 57%', (781, 592)]]
robot.target((781, 592))
object_distance = distance()
if object_distance > 10:
robot.go("forward", object_distance, track="water")
robot.grab()
if object_distance > 10:
robot.go("back", object_distance)
robot.release("here")
#Stand by the table
objects = [['table: 81%', (1489, 1173)], ['table: 75%', (1971, 1293)]]
robot.target((1489, 1173))
if distance() > 10:
robot.go(forward, distance())
#Put the apples in the basket
objects = [['basket: 77%', (89, 112)], ['apples: 72%', (222, 182)]]
robot.target((281, 189))
if distance() > 10:
robot.go("forward", distance(), track="apples")
robot.grab()
robot.target(robot.find("basket"))
robot.release(distance())
#Go to the sofa
objects=[['sofa: 81%', (1060, 931)]]
robot.target((1060, 931))
if distance() > 10:
robot.go("forward", distance())
#Go to that person over there and then come back
objects=[['person: 85%', (331, 354)]]
robot.target((331, 354))
object_distance = distance()
if object_distance > 10:
robot.go("forward", object_distance)
robot.go("backward", object_distance)
"""
answer = ''.join(run_instruction(
instructions,
prompt,
temperature=0.2,
top_p=0.5,
top_k=300,
repetition_penalty=1.1,
max_new_tokens=256,
stop_tokens=stop,
))
return answer
if __name__ == "__main__":
print(robotix("#Get me a glass of water\nobjects = [['water: 65%', (695, 234)]]"))