Model: TheBloke/llama2-7b-chat-codeCherryPop-qLoRA-AWQ Source: Original Platform
5.3 KiB
5.3 KiB
In [1]:
from huggingface_hub import notebook_login
notebook_login()VBox(children=(HTML(value='<center> <img\nsrc=https://huggingface.co/front/assets/huggingface_logo-noborder.sv…
In [8]:
from transformers import AutoTokenizer
import transformers
import torch
model = "TokenBender/llama2-7b-chat-hf-codeCherryPop-qLoRA-merged"
tokenizer = AutoTokenizer.from_pretrained(model)
pipeline = transformers.pipeline(
"text-generation",
model=model,
torch_dtype=torch.float16,
device_map="auto",
)Loading checkpoint shards: 0%| | 0/2 [00:00<?, ?it/s]
In [9]:
conversation_text = '''[INST]<<SYS>>
You are a helpful coding assistant that provides code based on the given query in context.
<</SYS>>
Write a python program to perform binary search in a given list.[/INST]'''
sequences = pipeline(
conversation_text,
do_sample=True,
top_k=10,
num_return_sequences=1,
eos_token_id=tokenizer.eos_token_id,
max_length=2048,
)
for seq in sequences:
print(f"Result: {seq['generated_text']}")Result: [INST]<<SYS>>
You are a helpful coding assistant that provides code based on the given query in context.
<</SYS>>
Write a python program to perform binary search in a given list.[/INST] Of course! Here is a basic implementation of a binary search algorithm in Python:
```
def binary_search(list, target):
# Initialize the indices of the left and right halves of the list
low = 0
high = len(list) - 1
# While the list has not been searched thoroughly
while low < high:
# Calculate the index of the middle element of the list
mid = (low + high) // 2
# Check whether the target is equal to the middle element
if list[mid] == target:
# Return the index of the target
return mid
# If the target is less than the middle element, move to the right
elif list[mid] < target:
high = mid - 1
# If the target is greater than the middle element, move to the left
else:
low = mid + 1
# If we reach this point, the target was not found in the list
return -1
# Example usage
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
target = 5
print(binary_search(list, target))
```
This code uses a simple loop to perform the binary search. It iterates through the list, comparing each element to the target, and adjusting the position of the current element based on the result of the comparison. At the end of the loop, it returns the index of the target element if it is found, or -1 if it is not found.
You can test the code with your own list by replacing the `list` variable with a list of your own, and calling the `binary_search` function with the target element you want to search for. For example, if your list is `['apple', 'banana', 'orange', 'banana', 'orange', 'apple']`, and you want to search for the element 'banana', you can call the function like this: `print(binary_search(list, 'banana')`.
In [ ]: