Categories
AI

GPU Cost Comparison

#VRAMFloat16 TFLOPsFloat8 TFLOPsBand WidthW Per CardPricePer GPUPer fp16 GFLOPPer fp8 GFLOPBF16Supply
KaggleTesla T41166532070000.000N5
LambdaA6000 Ada448364.25728.59603003.20.82.1961.098Y1
LambdaH100 PCIe180989197920003502.492.492.5181.258Y1
RunpodA6000 Ada848364.25728.59603009.121.143.1301.565Y2
ColabTesla T411665320700.2050.2053.154N5
LambdaH100 SXM8809891979335070027.923.493.5291.764Y0
RunpodL4824121242.5300723.520.443.6361.814Y0
RunpodRTX A450042094.66402001.440.363.805Y2
RunpodH100 PCIe8809891979335070031.123.893.9331.966Y3
RunpodRTX A5000824111.17682303.520.443.960Y3
RunpodL40S848362.0573386435011.921.494.1152.033Y2
LambdaA100 PCIe44031215552505.161.294.135Y0
ColabA10014031215552501.3081.3084.192Y2
RunpodRTX A400081676.74481402.720.344.433Y2
RunpodRTX 4090824165.2330.310084505.920.744.4792.240Y3
RunpodA40848149.76963005.520.694.609Y3
RunpodH100 SXM8809891979335070037.524.694.7422.370Y3
RunpodA4000 Ada42082163.83601301.560.394.7562.381Y2
ColabV1001161129003000.5450.5454.866N5
LambdaV1008161129003004.40.554.911N1
RunpodRTX A6000848154.857683006.320.795.102Y3
GoogleTesla T441665320701.40.355.385N5
LambdaA100 SXM880312203940014.321.795.737Y0
LambdaA101241252506001500.750.756.0003.000Y3
RunpodA100 PCIe880312155525015.121.896.058Y3
RunpodRTX 3090824719363503.520.446.197Y1
RunpodL40848181.053628643009.121.146.2973.149Y2
RunpodA100 SXM880312203940018.322.297.340Y2
GoogleL4824121242.5300728.0033311.0004163758.2684.125Y4
GoogleA100 PCIe840312155525029.3878163.67347711.774Y4
AWSA100 PCIe840312155525032.774.0962513.129Y4
AWSTesla T481665320707.8240.97815.046N5
GoogleA100 SXM880312203940040.55045.068816.246Y4
AWSA100 SXM880312203940040.965.1216.410Y4
GoogleV10081611290030019.842.4822.143N5
AWSV10081611290030024.483.0627.321N5

Source: https://docs.google.com/spreadsheets/d/1tRbUj8qjsnZdUJOEXJFRLFSOwOWKFaA3hgRC1XWje-w/edit#gid=0

Categories
Finetuning

Fine Tuning Pokemon Cards on idefics-9b Vision Model

!pip install -q datasets
!pip install -q git+https://github.com/huggingface/transformers
!pip install -q bitsandbytes sentencepiece accelerate loralib
!pip install -q -U git+https://github.com/huggingface/peft.git
import torch
from datasets import load_dataset
from peft import LoraConfig, get_peft_model
from PIL import Image
from transformers import IdeficsForVisionText2Text, AutoProcessor, Trainer, TrainingArguments, BitsAndBytesConfig
device = "cuda" if torch.cuda.is_available() else "cpu"
checkpoint = "HuggingFaceM4/idefics-9b"
bnb_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_use_double_quant=True,
    bnb_4bit_quant_type="nf4",
    bnb_4bit_compute_dtype=torch.float16,
    llm_int8_skip_modules=["lm_head", "embed_tokens"]
)
processor = AutoProcessor.from_pretrained(checkpoint)
model = IdeficsForVisionText2Text.from_pretrained(checkpoint, quantization_config=bnb_config, device_map="auto")
model
# Inference
def do_inference(model, processor, prompts, max_new_tokens=50):
    tokenizer = processor.tokenizer
    bad_words = ["<image>", "<fake_token_around_image>"]
    if len(bad_words) > 0:
        bad_words_ids = tokenizer(bad_words, add_special_tokens=False).input_ids
    eos_token = "</s>"
    eos_token_id = tokenizer.convert_tokens_to_ids(eos_token)

    inputs = processor(prompts, return_tensors="pt").to(device)
    generated_ids = model.generate(
        **inputs,
        eos_token_id=[eos_token_id],
        bad_words_ids=bad_words_ids,
        max_new_tokens=max_new_tokens,
        early_stopping=True
    )
    generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
    print(generated_text)
import torchvision.transforms as transforms
url = "https://hips.hearstapps.com/hmg-prod/images/cute-photos-of-cats-in-grass-1593184777.jpg"
prompts = [
    url,
    "Question: What's on the picture? Answer:",
]
##preprocessing
def convert_to_rgb(image):
  if image.mode == "RGB":
    return image

  image_rgba = image.convert("RGBA")
  background = Image.new("RGBA", image_rgba.size, (255,255,255))
  alpha_composite = Image.alpha_composite(background, image_rgba)
  alpha_composite = alpha_composite.convert("RGB")
  return alpha_composite

def ds_transforms(example_batch):
  image_size = processor.image_processor.image_size
  image_mean = processor.image_processor.image_mean
  image_std = processor.image_processor.image_std

  image_transform = transforms.Compose([
      convert_to_rgb,
      transforms.RandomResizedCrop((image_size, image_size), scale=(0.9, 1.0), interpolation=transforms.InterpolationMode.BICUBIC),
      transforms.ToTensor(),
      transforms.Normalize(mean=image_mean, std=image_std)
  ])

  prompts = []
  for i in range(len(example_batch['caption'])):
    caption = example_batch['caption'][i].split(".")[0]
    prompts.append(
        [
            example_batch['image_url'][i],
            f"Question: What's on the picture? Answer: This is {example_batch['name']}. {caption}</s>",
        ],
    )
  inputs = processor(prompts, transform=image_transform, return_tensors="pt").to(device)
  inputs["labels"] = inputs["input_ids"]
  return inputs
#Load and prepare the data
ds = load_dataset("TheFusion21/PokemonCards")
ds = ds["train"].train_test_split(test_size=0.002)
train_ds = ds["train"]
eval_ds = ds["test"]
train_ds.set_transform(ds_transforms)
eval_ds.set_transform(ds_transforms)
model_name = checkpoint.split("/")[1]
config = LoraConfig(
    r = 16,
    lora_alpha = 32,
    target_modules = ["q_proj", "k_proj", "v_proj"],
    lora_dropout = 0.05,
    bias="none"
)
model = get_peft_model(model, config)
model.print_trainable_parameters()
training_args = TrainingArguments(
    output_dir = f"{model_name}-PokemonCards",
    learning_rate = 2e-4,
    fp16 = True,
    per_device_train_batch_size = 2,
    per_device_eval_batch_size = 2,
    gradient_accumulation_steps = 8,
    dataloader_pin_memory = False,
    save_total_limit = 3,
    evaluation_strategy ="steps",
    save_strategy = "steps",
    eval_steps = 10,
    save_steps = 25,
    max_steps = 25,
    logging_steps = 5,
    remove_unused_columns = False,
    push_to_hub=False,
    label_names = ["labels"],
    load_best_model_at_end = False,
    report_to = "none",
    optim = "paged_adamw_8bit",
)
trainer = Trainer(
    model = model,
    args = training_args,
    train_dataset = train_ds,
    eval_dataset = eval_ds
)
trainer.train()
url = "https://images.pokemontcg.io/pop6/2_hires.png"
prompts = [
    url,
    "Question: What's on the picture? Answer:",
]
do_inference(model, processor, prompts, max_new_tokens=100)
import locale
locale.getpreferredencoding = lambda: "UTF-8"
## Push to hub
model = model.merge_and_unload()
model.push_to_hub(f"{model_name}-PokemonCards", use_temp_dir=False, private=False, token="API_TOKEN")
Categories
Finetuning

Merge Base Model and PEFT Adapter and Push it to HF Hub

# Example usage:
# python merge_peft.py --base_model=meta-llama/Llama-2-7b-hf --peft_model=./qlora-out --hub_id=alpaca-qlora

from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel
import torch

import argparse

def get_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("--base_model", type=str)
    parser.add_argument("--peft_model", type=str)
    parser.add_argument("--hub_id", type=str)

    return parser.parse_args()

def main():
    args = get_args()

    print(f"[1/5] Loading base model: {args.base_model}")
    base_model = AutoModelForCausalLM.from_pretrained(
        args.base_model,
        return_dict=True,
        torch_dtype=torch.float16,
        device_map="auto",
    )
    tokenizer = AutoTokenizer.from_pretrained(args.base_model)

    print(f"[2/5] Loading adapter: {args.peft_model}")
    model = PeftModel.from_pretrained(base_model, args.peft_model, device_map="auto")
    
    print("[3/5] Merge base model and adapter")
    model = model.merge_and_unload()
    
    print(f"[4/5] Saving model and tokenizer in {args.hub_id}")
    model.save_pretrained(f"{args.hub_id}")
    tokenizer.save_pretrained(f"{args.hub_id}")

    print(f"[5/5] Uploading to Hugging Face Hub: {args.hub_id}")
    model.push_to_hub(f"{args.hub_id}", use_temp_dir=False)
    tokenizer.push_to_hub(f"{args.hub_id}", use_temp_dir=False)
    
    print("Merged model uploaded to Hugging Face Hub!")

if __name__ == "__main__" :
    main()
Categories
Machine Learning

Machine Learning Concepts and Techniques

TermDefinitionExample OutputsInput DataGoalModel Complexity
Large Language ModelsModels trained on large text datasets for language tasks.Textual output, translations.TextUnderstanding/generating languageVery high
RegressionPredicting numerical values.House prices, temperatures.Numeric, categoricalQuantitative predictionsVaries
ClassificationPredicting categories or classes.Spam or not spam, image categories.Numeric, categoricalCategorical predictionsVaries
ClusteringGrouping data points based on similarity.Customer segments, data categorizationNumeric, categoricalData segmentationModerate to high
Dimensionality ReductionReducing the number of random variables under consideration.Reduced feature set for visualizationNumeric, categoricalSimplify data, improve efficiencyModerate
Reinforcement LearningLearning to make decisions through rewards.Game playing, autonomous vehicles.Environment statesOptimize decision-makingHigh
Anomaly DetectionIdentifying unusual data points.Fraud detection, system health monitoringNumeric, categoricalIdentify outliersModerate to high
Recommendation SystemsSuggesting products or services.Movie/music recommendations, product suggestionsUser-item interactionsPersonalize suggestionsHigh

Embeddings and re-ranking typically fall under multiple categories, depending on their application:

  • Embeddings: Dimensionality Reduction, sometimes Clustering.
    • Purpose: Convert high-dimensional data (like text) into lower-dimensional vectors to capture semantic meaning or relationships.
    • Applications: Used in various ML tasks, including classification, recommendation systems, and natural language processing.
  • Re-ranking: Often associated with Recommendation Systems and Classification.
    • Purpose: Adjust or refine the ordering of a list of items based on certain criteria or additional models.
    • Applications: Improving the relevance of search engine results, enhancing recommendation systems.

Others

  1. Feature Engineering: The process of using domain knowledge to extract features from raw data that make machine learning algorithms work.
  2. Hyperparameter Tuning: The process of optimizing the parameters that govern the training process of a machine learning model.
  3. Transfer Learning: Leveraging a pre-trained model on a new, related problem.
  4. Ensemble Methods: Techniques that combine the predictions from multiple machine learning algorithms to make more accurate predictions than any individual model.
  5. Generative Models: Models that can generate new data instances that resemble your training data, such as GANs (Generative Adversarial Networks).
  6. Sequence Modeling: Techniques used for predicting the next item in a sequence, applicable in time series analysis, natural language processing, etc.
  7. Attention Mechanisms: Components that allow models to focus on specific parts of the input for generating a particular output, crucial in advanced language models.
  8. Graph Neural Networks (GNNs): Neural networks designed to directly work with data that is structured as graphs, useful in social network analysis, recommendation systems, etc.
  9. Self-supervised Learning: A type of learning where the data itself provides supervision, often used in situations where labeled data is scarce.
  10. Multi-task Learning: An approach where a single model is trained on multiple related tasks, which can improve the model’s performance on individual tasks.
  11. Few-shot Learning: Techniques that aim to train models on a very small amount of data.
  12. Reinforcement Learning: Learning to make decisions by taking actions in an environment to maximize some notion of cumulative reward.
  13. Natural Language Understanding (NLU): Techniques for machines to understand and interpret human language, beyond just generating or processing text.
  14. Speech Recognition: Converting spoken words into text, a specific application of machine learning models tailored for audio data.
  15. Computer Vision: Techniques for machines to interpret and understand the visual world, involving tasks like image recognition, object detection, and more.
  16. Bias and Fairness in AI: Approaches to ensure machine learning models do not perpetuate or amplify bias, and are fair in their decisions.
Categories
Embedding

Cohere RAG with Reranking

pip install cohere wikipedia langchain-text-splitters numpy
import cohere, os
import wikipedia

co = cohere.Client(os.environ['COHERE_API_KEY'])

# let's get the wikipedia article about Dune Part Two
article = wikipedia.page('Dune Part Two')
text = article.content
print(f"The text has roughly {len(text.split())} words.")

# 1. Indexing and given a user query, retrieve the relevant chunks from the index
from langchain_text_splitters import RecursiveCharacterTextSplitter

text_splitter = RecursiveCharacterTextSplitter(
    chunk_size=512,
    chunk_overlap=50,
    length_function=len,
    is_separator_regex=False,
)

chunks_ = text_splitter.create_documents([text])
chunks = [c.page_content for c in chunks_]
print(f"The text has been broken down in {len(chunks)} chunks.")

# Because the texts being embedded are the chunks we are searching over, we set the input type as search_doc
model="embed-english-v3.0"
response = co.embed(
    texts= chunks,
    model=model,
    input_type="search_document",
    embedding_types=['float']
)
embeddings = response.embeddings.float
print(f"We just computed {len(embeddings)} embeddings.")

# ### Store the embeddings in a vector database
# We use the simplest vector database ever: a python dictionary using `np.array()`.
import numpy as np
vector_database = {i: np.array(embedding) for i, embedding in enumerate(embeddings)}

query = "Name everyone involved in writing the script, directing, and producing 'Dune: Part Two'?"

# ### Embed the user question
# Because the text being embedded is the search query, we set the input type as search_query
response = co.embed(
    texts=[query],
    model=model,
    input_type="search_query",
    embedding_types=['float']
)
query_embedding = response.embeddings.float[0]
print("query_embedding: ", query_embedding)

# ### Retrieve the most relevant chunks from the vector database (cosine similarity)
def cosine_similarity(a, b):
    return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))

# Calculate similarity between the user question & each chunk
similarities = [cosine_similarity(query_embedding, chunk) for chunk in embeddings]
print("similarity scores: ", similarities)

# Get indices of the top 10 most similar chunks
sorted_indices = np.argsort(similarities)[::-1]

# Keep only the top 10 indices
top_indices = sorted_indices[:10]
print("Here are the indices of the top 10 chunks after retrieval: ", top_indices)

# Retrieve the top 10 most similar chunks
top_chunks_after_retrieval = [chunks[i] for i in top_indices]
print("Here are the top 10 chunks after retrieval: ")
for t in top_chunks_after_retrieval:
    print("== " + t)

# 2. Rerank the chunks retrieved from the vector database
# We rerank the 10 chunks retrieved from the vector database. 
# Reranking boosts retrieval accuracy.
# Reranking lets us go from 10 chunks retrieved from the vector database, to the 3 most relevant chunks.

response = co.rerank(
    query=query,
    documents=top_chunks_after_retrieval,
    top_n=3,
    model="rerank-english-v2.0",
)

top_chunks_after_rerank = [result.document['text'] for result in response]
print("Here are the top 3 chunks after rerank: ")
for t in top_chunks_after_rerank:
    print("== " + t)

# 3. Generate the model final answer, given the retrieved and reranked chunks

# preamble containing instructions about the task and the desired style for the output.
preamble = """
## Task & Context
You help people answer their questions and other requests interactively. You will be asked a very wide array of requests on all kinds of topics. You will be equipped with a wide range of search engines or similar tools to help you, which you use to research your answer. You should focus on serving the user's needs as best you can, which will be wide-ranging.

## Style Guide
Unless the user asks for a different style of answer, you should answer in full sentences, using proper grammar and spelling.
"""

# retrieved documents
documents = [
    {"title": "chunk 0", "snippet": top_chunks_after_rerank[0]},
    {"title": "chunk 1", "snippet": top_chunks_after_rerank[1]},
    {"title": "chunk 2", "snippet": top_chunks_after_rerank[2]},
  ]

# get model response
response = co.chat(
  message=query,
  documents=documents,
  preamble=preamble,
  model="command-r",
  temperature=0.3
)

print("Final answer:")
print(response.text)

# 4. Citations
print("Citations that support the final answer:")
for cite in response.citations:
  print(cite)

def insert_citations_in_order(text, citations):
    """
    A helper function to pretty print citations.
    """
    offset = 0
    document_id_to_number = {}
    citation_number = 0
    modified_citations = []

    # Process citations, assigning numbers based on unique document_ids
    for citation in citations:
        citation_numbers = []
        for document_id in sorted(citation["document_ids"]):
            if document_id not in document_id_to_number:
                citation_number += 1  # Increment for a new document_id
                document_id_to_number[document_id] = citation_number
            citation_numbers.append(document_id_to_number[document_id])

        # Adjust start/end with offset
        start, end = citation['start'] + offset, citation['end'] + offset
        placeholder = ''.join([f'[{number}]' for number in citation_numbers])
        # Bold the cited text and append the placeholder
        modification = f'**{text[start:end]}**{placeholder}'
        # Replace the cited text with its bolded version + placeholder
        text = text[:start] + modification + text[end:]
        # Update the offset for subsequent replacements
        offset += len(modification) - (end - start)

    # Prepare citations for listing at the bottom, ensuring unique document_ids are listed once
    unique_citations = {number: doc_id for doc_id, number in document_id_to_number.items()}
    citation_list = '\n'.join([f'[{doc_id}] source: "{documents[doc_id - 1]["snippet"]}"' for doc_id, number in sorted(unique_citations.items(), key=lambda item: item[1])])
    text_with_citations = f'{text}\n\n{citation_list}'
    return text_with_citations

print(insert_citations_in_order(response.text, response.citations))
Categories
Tools

Cohere Tools / Function Calling

pip install cohere rich
export COHERE_API_KEY=xxxxxxx

https://dashboard.cohere.com/

Code

# 1. Configuration and Creating Tools
import cohere, json, os, sqlite3
from rich import print
co = cohere.Client(os.environ["COHERE_API_KEY"])
conn = sqlite3.connect('mydatabase.db')
c = conn.cursor()

def query_daily_sales_report(day: str) -> dict:
    """
    Function to retrieve the sales report for the given day from the SQLite database
    """
    c.execute('SELECT total_sales_amount, total_units_sold FROM sales_data WHERE date = ?', (day,))
    result = c.fetchone()
    if result:
        return {
            'date': day,
            'summary': f"Total Sales Amount: {result[0]}, Total Units Sold: {result[1]}"
        }
    else:
        return {'date': day, 'summary': 'No sales data available for this day.'}

def query_product_catalog(category: str) -> dict:
    """
    Function to retrieve products for the given category from the SQLite database
    """
    c.execute('SELECT product_id, name, price, stock_level FROM product_catalog WHERE category = ?', (category,))
    products = [{'product_id': row[0], 'name': row[1], 'price': row[2], 'stock_level': row[3]} for row in c.fetchall()]
    return {
        'category': category,
        'products': products
    }

functions_map = {
    "query_daily_sales_report": query_daily_sales_report,
    "query_product_catalog": query_product_catalog
}

tools = [
    {
        "name": "query_daily_sales_report",
        "description": "Connects to a database to retrieve overall sales volumes and sales information for a given day.",
        "parameter_definitions": {
            "day": {
                "description": "Retrieves sales data for this day, formatted as YYYY-MM-DD.",
                "type": "str",
                "required": True
            }
        }
    },
    {
        "name": "query_product_catalog",
        "description": "Connects to a a product catalog with information about all the products being sold, including categories, prices, and stock levels.",
        "parameter_definitions": {
            "category": {
                "description": "Retrieves product information data for all products in this category.",
                "type": "str",
                "required": True
            }
        }
    }
]

# preamble containing instructions about the task and the desired style for the output.
preamble = """
## Task & Context
You help people answer their questions and other requests interactively. You will be asked a very wide array of requests on all kinds of topics. You will be equipped with a wide range of search engines or similar tools to help you, which you use to research your answer. You should focus on serving the user's needs as best you can, which will be wide-ranging.

## Style Guide
Unless the user asks for a different style of answer, you should answer in full sentences, using proper grammar and spelling.
"""

# user request
message = """Can you provide a sales summary for 29th September 2023, 
and also give me some details about the products in the 'Electronics' category, 
for example their prices and stock levels?"""

# 2. The model smartly decides which tool(s) to use and how
response = co.chat(
    message=message,
    tools=tools,
    preamble=preamble,
    model="command-r"
)
print("The model recommends doing the following tool calls:")
print("\n".join(str(tool_call) for tool_call in response.tool_calls))

# 3. the tool calls are Cxecuted
tool_results = []
for tool_call in response.tool_calls:
    # here is where you would call the tool recommended by the model, using the parameters recommended by the model
    print(f"= running tool {tool_call.name}, with parameters: {tool_call.parameters}")
    output = functions_map[tool_call.name](**tool_call.parameters)
    # store the output in a list
    outputs = [output]
    print(f"== tool results: {outputs}")
    # store your tool results in this format
    tool_results.append({
        "call": tool_call,
        "outputs": outputs
    })

print("Tool results that will be fed back to the model in step 4:")
print(json.dumps(tool_results, indent=4))

# 4. The model generates a final answer based on the tool results 
response = co.chat(
    message=message,
    tools=tools,
    tool_results=tool_results,
    preamble=preamble,
    model="command-r",
    temperature=0.3
)

print("Final answer:")
print(response.text)

# Bonus: Citations come for free with Cohere! 
print("Citations that support the final answer:")
for cite in response.citations:
  print(cite)

def insert_citations_in_order(text, citations):
    """
    A helper function to pretty print citations.
    """
    offset = 0
    document_id_to_number = {}
    citation_number = 0
    modified_citations = []

    # Process citations, assigning numbers based on unique document_ids
    for citation in citations:
        citation_numbers = []
        for document_id in sorted(citation["document_ids"]):
            if document_id not in document_id_to_number:
                citation_number += 1  # Increment for a new document_id
                document_id_to_number[document_id] = citation_number
            citation_numbers.append(document_id_to_number[document_id])

        # Adjust start/end with offset
        start, end = citation['start'] + offset, citation['end'] + offset
        placeholder = ''.join([f'[{number}]' for number in citation_numbers])
        # Bold the cited text and append the placeholder
        modification = f'**{text[start:end]}**{placeholder}'
        # Replace the cited text with its bolded version + placeholder
        text = text[:start] + modification + text[end:]
        # Update the offset for subsequent replacements
        offset += len(modification) - (end - start)

    # Prepare citations for listing at the bottom, ensuring unique document_ids are listed once
    unique_citations = {number: doc_id for doc_id, number in document_id_to_number.items()}
    citation_list = '\n'.join([f'[{doc_id}] source: {tool_results[doc_id - 1]["outputs"]} \n    based on tool call: {dict(tool_results[doc_id - 1]["call"])}' for doc_id, number in sorted(unique_citations.items(), key=lambda item: item[1])])
    text_with_citations = f'{text}\n\n{citation_list}'
    return text_with_citations

print(insert_citations_in_order(response.text, response.citations))

Create DB

sqlite3 mydatabase.db

Press Ctrl + D to exit

import sqlite3

# Connect to SQLite database (or create it if it doesn't exist)
conn = sqlite3.connect('mydatabase.db')
c = conn.cursor()

# Create tables
c.execute('''CREATE TABLE IF NOT EXISTS sales_data
             (date TEXT PRIMARY KEY, total_sales_amount INTEGER, total_units_sold INTEGER)''')
c.execute('''CREATE TABLE IF NOT EXISTS product_catalog
             (product_id TEXT PRIMARY KEY, category TEXT, name TEXT, price INTEGER, stock_level INTEGER)''')

# Insert sales data
sales_data = [
    ('2023-09-28', 5000, 100),
    ('2023-09-29', 10000, 250),
    ('2023-09-30', 8000, 200)
]

c.executemany('INSERT INTO sales_data VALUES (?,?,?)', sales_data)

# Insert product catalog data
product_catalog = [
    ('E1001', 'Electronics', 'Smartphone', 500, 20),
    ('E1002', 'Electronics', 'Laptop', 1000, 15),
    ('E1003', 'Electronics', 'Tablet', 300, 25),
    ('C1001', 'Clothing', 'T-Shirt', 20, 100),
    ('C1002', 'Clothing', 'Jeans', 50, 80),
    ('C1003', 'Clothing', 'Jacket', 100, 40)
]

c.executemany('INSERT INTO product_catalog VALUES (?,?,?,?,?)', product_catalog)

# Commit changes and close connection
conn.commit()
conn.close()

# Reconnect to SQLite database to retrieve data
conn = sqlite3.connect('mydatabase.db')
c = conn.cursor()

# Retrieve and print sales data
c.execute('SELECT * FROM sales_data')
print("Sales Data:")
for row in c.fetchall():
    print(row)

# Retrieve and print product catalog data
c.execute('SELECT * FROM product_catalog')
print("\nProduct Catalog:")
for row in c.fetchall():
    print(row)

conn.close()

Basic Code with Integrated DB

# 1. Configuration and Creating Tools
import cohere, json, os
co = cohere.Client(os.environ["COHERE_API_KEY"])

sales_database = {
    '2023-09-28': {
        'total_sales_amount': 5000,
        'total_units_sold': 100,
    },
    '2023-09-29': {
        'total_sales_amount': 10000,
        'total_units_sold': 250,
    },
    '2023-09-30': {
        'total_sales_amount': 8000,
        'total_units_sold': 200,
    }
}

product_catalog = {
    'Electronics': [
        {'product_id': 'E1001', 'name': 'Smartphone', 'price': 500, 'stock_level': 20},
        {'product_id': 'E1002', 'name': 'Laptop', 'price': 1000, 'stock_level': 15},
        {'product_id': 'E1003', 'name': 'Tablet', 'price': 300, 'stock_level': 25},
    ],
    'Clothing': [
        {'product_id': 'C1001', 'name': 'T-Shirt', 'price': 20, 'stock_level': 100},
        {'product_id': 'C1002', 'name': 'Jeans', 'price': 50, 'stock_level': 80},
        {'product_id': 'C1003', 'name': 'Jacket', 'price': 100, 'stock_level': 40},
    ]
}

def query_daily_sales_report(day: str) -> dict:
    """
    Function to retrieve the sales report for the given day
    """
    report = sales_database.get(day, {})
    if report:
        return {
            'date': day,
            'summary': f"Total Sales Amount: {report['total_sales_amount']}, Total Units Sold: {report['total_units_sold']}"
        }
    else:
        return {'date': day, 'summary': 'No sales data available for this day.'}


def query_product_catalog(category: str) -> dict:
    """
    Function to retrieve products for the given category
    """
    products = product_catalog.get(category, [])
    return {
        'category': category,
        'products': products
    }


functions_map = {
    "query_daily_sales_report": query_daily_sales_report,
    "query_product_catalog": query_product_catalog
}

tools = [
    {
        "name": "query_daily_sales_report",
        "description": "Connects to a database to retrieve overall sales volumes and sales information for a given day.",
        "parameter_definitions": {
            "day": {
                "description": "Retrieves sales data for this day, formatted as YYYY-MM-DD.",
                "type": "str",
                "required": True
            }
        }
    },
    {
        "name": "query_product_catalog",
        "description": "Connects to a a product catalog with information about all the products being sold, including categories, prices, and stock levels.",
        "parameter_definitions": {
            "category": {
                "description": "Retrieves product information data for all products in this category.",
                "type": "str",
                "required": True
            }
        }
    }
]

# preamble containing instructions about the task and the desired style for the output.
preamble = """
## Task & Context
You help people answer their questions and other requests interactively. You will be asked a very wide array of requests on all kinds of topics. You will be equipped with a wide range of search engines or similar tools to help you, which you use to research your answer. You should focus on serving the user's needs as best you can, which will be wide-ranging.

## Style Guide
Unless the user asks for a different style of answer, you should answer in full sentences, using proper grammar and spelling.
"""

# user request
message = "Can you provide a sales summary for 29th September 2023, and also give me some details about the products in the 'Electronics' category, for example their prices and stock levels?"

# 2. The model smartly decides which tool(s) to use and how
response = co.chat(
    message=message,
    tools=tools,
    preamble=preamble,
    model="command-r"
)
print("The model recommends doing the following tool calls:")
print("\n".join(str(tool_call) for tool_call in response.tool_calls))

# 3. the tool calls are Cxecuted
tool_results = []
for tool_call in response.tool_calls:
    # here is where you would call the tool recommended by the model, using the parameters recommended by the model
    print(f"= running tool {tool_call.name}, with parameters: {tool_call.parameters}")
    output = functions_map[tool_call.name](**tool_call.parameters)
    # store the output in a list
    outputs = [output]
    print(f"== tool results: {outputs}")
    # store your tool results in this format
    tool_results.append({
        "call": tool_call,
        "outputs": outputs
    })

print("Tool results that will be fed back to the model in step 4:")
print(json.dumps(tool_results, indent=4))

# 4. The model generates a final answer based on the tool results 
response = co.chat(
    message=message,
    tools=tools,
    tool_results=tool_results,
    preamble=preamble,
    model="command-r",
    temperature=0.3
)

print("Final answer:")
print(response.text)

# Bonus: Citations come for free with Cohere!
print("Citations that support the final answer:")
for cite in response.citations:
  print(cite)

def insert_citations_in_order(text, citations):
    """
    A helper function to pretty print citations.
    """
    offset = 0
    document_id_to_number = {}
    citation_number = 0
    modified_citations = []

    # Process citations, assigning numbers based on unique document_ids
    for citation in citations:
        citation_numbers = []
        for document_id in sorted(citation["document_ids"]):
            if document_id not in document_id_to_number:
                citation_number += 1  # Increment for a new document_id
                document_id_to_number[document_id] = citation_number
            citation_numbers.append(document_id_to_number[document_id])

        # Adjust start/end with offset
        start, end = citation['start'] + offset, citation['end'] + offset
        placeholder = ''.join([f'[{number}]' for number in citation_numbers])
        # Bold the cited text and append the placeholder
        modification = f'**{text[start:end]}**{placeholder}'
        # Replace the cited text with its bolded version + placeholder
        text = text[:start] + modification + text[end:]
        # Update the offset for subsequent replacements
        offset += len(modification) - (end - start)

    # Prepare citations for listing at the bottom, ensuring unique document_ids are listed once
    unique_citations = {number: doc_id for doc_id, number in document_id_to_number.items()}
    citation_list = '\n'.join([f'[{doc_id}] source: {tool_results[doc_id - 1]["outputs"]} \n    based on tool call: {dict(tool_results[doc_id - 1]["call"])}' for doc_id, number in sorted(unique_citations.items(), key=lambda item: item[1])])
    text_with_citations = f'{text}\n\n{citation_list}'
    return text_with_citations

print(insert_citations_in_order(response.text, response.citations))

# - **Function calling**: It's now possible to ask the model to output a JSON object with specific function parameters.
# For instance, this allows your chatbot to interact with your CRM to change the status of a deal, or to engage with a Python interpreter to conduct data science analyses.
# 
# - **Query transformation**: You can transform a user message into a search query for a vector database or any search engine.
# For instance, this enables your work assistant to automatically retrieve the appropriate data from your company's documentation by creating the right query for your vector database.
# 
# - **Advanced searches**: You can transform a user message into one-or-many queries, to do multiple subtasks based on the content of the message.
# For instance, this allows your chatbot to search across different databases and platforms to retrieve relevant information or to conduct comparative analysis.

Output

The model recommends doing the following tool calls:
cohere.ToolCall {
        name: query_daily_sales_report
        parameters: {'day': '2023-09-29'}
        generation_id: None
}
cohere.ToolCall {
        name: query_product_catalog
        parameters: {'category': 'Electronics'}
        generation_id: None
}
= running tool query_daily_sales_report, with parameters: {'day': '2023-09-29'}
== tool results: [{'date': '2023-09-29', 'summary': 'Total Sales Amount: 10000, Total Units Sold: 250'}]
= running tool query_product_catalog, with parameters: {'category': 'Electronics'}
== tool results: [{'category': 'Electronics', 'products': [{'product_id': 'E1001', 'name': 'Smartphone', 'price': 500, 
'stock_level': 20}, {'product_id': 'E1002', 'name': 'Laptop', 'price': 1000, 'stock_level': 15}, {'product_id': 'E1003',
'name': 'Tablet', 'price': 300, 'stock_level': 25}]}]
Tool results that will be fed back to the model in step 4:
[
    {
        "call": {
            "name": "query_daily_sales_report",
            "parameters": {
                "day": "2023-09-29"
            },
            "generation_id": null
        },
        "outputs": [
            {
                "date": "2023-09-29",
                "summary": "Total Sales Amount: 10000, Total Units Sold: 250"
            }
        ]
    },
    {
        "call": {
            "name": "query_product_catalog",
            "parameters": {
                "category": "Electronics"
            },
            "generation_id": null
        },
        "outputs": [
            {
                "category": "Electronics",
                "products": [
                    {
                        "product_id": "E1001",
                        "name": "Smartphone",
                        "price": 500,
                        "stock_level": 20
                    },
                    {
                        "product_id": "E1002",
                        "name": "Laptop",
                        "price": 1000,
                        "stock_level": 15
                    },
                    {
                        "product_id": "E1003",
                        "name": "Tablet",
                        "price": 300,
                        "stock_level": 25
                    }
                ]
            }
        ]
    }
]
Final answer:
On September 29th 2023, the total sales amount was 10,000 and the total units sold were 250.

Here are some details about products in the Electronics category:

| Product Name | Price | Stock Level |
|---|---|---|
| Smartphone | 500 | 20 |
| Laptop | 1000 | 15 |
| Tablet | 300 | 25 |
Citations that support the final answer:
{'start': 3, 'end': 22, 'text': 'September 29th 2023', 'document_ids': ['query_daily_sales_report:0:0']}
{'start': 28, 'end': 57, 'text': 'total sales amount was 10,000', 'document_ids': ['query_daily_sales_report:0:0']}
{'start': 66, 'end': 92, 'text': 'total units sold were 250.', 'document_ids': ['query_daily_sales_report:0:0']}
{'start': 216, 'end': 226, 'text': 'Smartphone', 'document_ids': ['query_product_catalog:1:0']}
{'start': 229, 'end': 232, 'text': '500', 'document_ids': ['query_product_catalog:1:0']}
{'start': 235, 'end': 237, 'text': '20', 'document_ids': ['query_product_catalog:1:0']}
{'start': 242, 'end': 248, 'text': 'Laptop', 'document_ids': ['query_product_catalog:1:0']}
{'start': 251, 'end': 255, 'text': '1000', 'document_ids': ['query_product_catalog:1:0']}
{'start': 258, 'end': 260, 'text': '15', 'document_ids': ['query_product_catalog:1:0']}
{'start': 265, 'end': 271, 'text': 'Tablet', 'document_ids': ['query_product_catalog:1:0']}
{'start': 274, 'end': 277, 'text': '300', 'document_ids': ['query_product_catalog:1:0']}
{'start': 280, 'end': 282, 'text': '25', 'document_ids': ['query_product_catalog:1:0']}
On **September 29th 2023**[1], the **total sales amount was 10,000**[1] and the **total units sold were 250.**[1]

Here are some details about products in the Electronics category:

| Product Name | Price | Stock Level |
|---|---|---|
| **Smartphone**[2] | **500**[2] | **20**[2] |
| **Laptop**[2] | **1000**[2] | **15**[2] |
| **Tablet**[2] | **300**[2] | **25**[2] |

[1] source: [{'date': '2023-09-29', 'summary': 'Total Sales Amount: 10000, Total Units Sold: 250'}] 
    based on tool call: {'name': 'query_daily_sales_report', 'parameters': {'day': '2023-09-29'}, 'generation_id': None}
[2] source: [{'category': 'Electronics', 'products': [{'product_id': 'E1001', 'name': 'Smartphone', 'price': 500, 
'stock_level': 20}, {'product_id': 'E1002', 'name': 'Laptop', 'price': 1000, 'stock_level': 15}, {'product_id': 'E1003',
'name': 'Tablet', 'price': 300, 'stock_level': 25}]}] 
    based on tool call: {'name': 'query_product_catalog', 'parameters': {'category': 'Electronics'}, 'generation_id': 
None}
Categories
Tools

Groq Function Calling

https://github.com/unclecode/funckycall

git clone https://github.com/unclecode/funckycall
cd funkycall
export GROQ_API_KEY=xxxxxxxxxxxxxxx
mkdir .logs
uvicorn app.main:app --reload

Note: Keep the terminal running

Next, run the code only in the new terminal.

http://127.0.0.1:8000/proxy/groq/v1

Phidata

from phi.llm.openai.like import OpenAILike
from phi.assistant import Assistant
from phi.tools.duckduckgo import DuckDuckGo
import os, json

my_groq = OpenAILike(
        model="mixtral-8x7b-32768",
        api_key=os.environ["GROQ_API_KEY"],
        base_url="http://127.0.0.1:8000/proxy/groq/v1"
    )
assistant = Assistant(
    llm=my_groq,
    tools=[DuckDuckGo()], show_tool_calls=True, markdown=True
)
assistant.print_response("Whats happening in France? Summarize top stories with sources, very short and concise.", stream=False)

# Ollama
my_ollama = OpenAILike(
        model="mistral",
        api_key="",
        base_url="http://localhost:11235/proxy/ollama/v1"
    )
ollama_assistant = Assistant(
    llm=my_ollama,
    tools=[DuckDuckGo()], show_tool_calls=True, markdown=True
)
ollama_assistant.print_response("Whats happening in France? Summarize top stories with sources, very short and concise.", stream=False)

Function Definition

from duckduckgo_search import DDGS
import requests, os
api_key=os.environ["GROQ_API_KEY"]
import json
header = {
    "Authorization": f"Bearer {api_key}",
    "Content-Type": "application/json"
}
proxy_url = "http://127.0.0.1:8000/proxy/groq/v1/chat/completions"


def duckduckgo_search(query, max_results=None):
    """
    Use this function to search DuckDuckGo for a query.
    """
    with DDGS() as ddgs:
        return [r for r in ddgs.text(query, safesearch='off', max_results=max_results)]
    
def duckduckgo_news(query, max_results=None):
    """
    Use this function to get the latest news from DuckDuckGo.
    """    
    with DDGS() as ddgs:
        return [r for r in ddgs.news(query, safesearch='off', max_results=max_results)]

function_map = {
    "duckduckgo_search": duckduckgo_search,
    "duckduckgo_news": duckduckgo_news,
}

request = {
    "messages": [
        {
            "role": "system",
            "content": "YOU MUST FOLLOW THESE INSTRUCTIONS CAREFULLY.\n<instructions>\n1. Use markdown to format your answers.\n</instructions>"
        },
        {
            "role": "user",
            "content": "Whats happening in France? Summarize top stories with sources, very short and concise."
        }
    ],
    "model": "mixtral-8x7b-32768",
    "tool_choice": "auto",
    "tools": [
        {
            "type": "function",
            "function": {
                "name": "duckduckgo_search",
                "description": "Use this function to search DuckDuckGo for a query.\n\nArgs:\n    query(str): The query to search for.\n    max_results (optional, default=5): The maximum number of results to return.\n\nReturns:\n    The result from DuckDuckGo.",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "query": {
                            "type": "string"
                        },
                        "max_results": {
                            "type": [
                                "number",
                                "null"
                            ]
                        }
                    }
                }
            }
        },
        {
            "type": "function",
            "function": {
                "name": "duckduckgo_news",
                "description": "Use this function to get the latest news from DuckDuckGo.\n\nArgs:\n    query(str): The query to search for.\n    max_results (optional, default=5): The maximum number of results to return.\n\nReturns:\n    The latest news from DuckDuckGo.",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "query": {
                            "type": "string"
                        },
                        "max_results": {
                            "type": [
                                "number",
                                "null"
                            ]
                        }
                    }
                }
            }
        }
    ]
}

response = requests.post(
    proxy_url,
    headers=header,
    json=request
)
# Check if the request was successful
if response.status_code == 200:
    # Process the response data (if needed)
    res = response.json()
    message = res['choices'][0]['message']
    tools_response_messages = []
    if not message['content'] and 'tool_calls' in message:
        for tool_call in message['tool_calls']:
            tool_name = tool_call['function']['name']
            tool_args = tool_call['function']['arguments']
            tool_args = json.loads(tool_args)
            if tool_name not in function_map:
                print(f"Error: {tool_name} is not a valid function name.")
                continue
            tool_func = function_map[tool_name]
            tool_response = tool_func(**tool_args)
            tools_response_messages.append({
                "role": "tool", "content": json.dumps(tool_response)
            })
    
        if tools_response_messages:
            request['messages'] += tools_response_messages
            response = requests.post(
                proxy_url,
                headers=header,
                json=request
            )
            if response.status_code == 200:
                res = response.json()
                print(res['choices'][0]['message']['content'])
            else:
                print("Error:", response.status_code, response.text)
    else:
        print(message['content'])
else:
    print("Error:", response.status_code, response.text)

Tools

import requests, os
api_key = os.environ["GROQ_API_KEY"]
header = {
    "Authorization": f"Bearer {api_key}",
    "Content-Type": "application/json"
}

proxy_url = "http://127.0.0.1:8000/proxy/groq/v1/chat/completions"


request = {
    "messages": [
        {
            "role": "system",
            "content": "YOU MUST FOLLOW THESE INSTRUCTIONS CAREFULLY.\n<instructions>\n1. Use markdown to format your answers.\n</instructions>",
        },
        {
            "role": "user",
            "content": "Whats happening in France? Summarize top stories with sources, very short and concise. Also please search about the histoy of france as well.",
        },
    ],
    "model": "mixtral-8x7b-32768",
    "tool_choice": "auto",
    "tools": [
        {
            "type": "function",
            "function": {
                "name": "duckduck.search",
            },
        },
        {
            "type": "function",
            "function": {
                "name": "duckduck.news",
            },
        },
    ],
}

response = requests.post(
    proxy_url,
    headers=header,
    json=request,
)

if response.status_code == 200:
    res = response.json()
    print(res["choices"][0]["message"]["content"])
else:
    print("Error:", response.status_code, response.text)
Categories
Tools

AI Real-Time Code Iteration

Repo: https://github.com/freuk/iter

  1. Download Nix https://nixos.org/download
  2. Mac
sh <(curl -L https://nixos.org/nix/install)

3. Nix Profile

nix profile install github:freuk/iter#iter --extra-experimental-features nix-command --extra-experimental-features flakes

Example 1

app.yml

model: "mixtral-8x7b-32768"
feedback:
  execute: ["python", "{file}"]
  pylint: ["pylint", "{file}"]
legacy-api: false

app.py

# Empty

Example 2

demo.yml

model: "mixtral-8x7b-32768"
# model: "llama2-70b-4096" <- somewhat better at Haskell
# model: "gemma-7b-it" <- doesn't follow the hardcoded prompts very well

# these available templates get replaced in strings: {file}, {basename}
feedback:
  # [<cmd>, <arg1>, <arg2>, ..]
  execute: ["python", "{file}"]
  country: ["python", "{file}", "--country=USA"]
  pylint: ["pylint", "{file}"]

# set to true if you have an old-style 'request_manager' token.
legacy-api: false

# define this if you'd like to log the full LLM interaction sequence to a
# file.
# log-llm: "/tmp/iter.log"

demo.py

import argparse
import json

# List of customer dictionaries
customers = [
    {"name": "John Doe", "country": "USA" },
    {"name": "Jane Smith", "country": "Canada"},
    {"name": "Alice Johnson", "country": "UK"},
    {"name": "Bob Brown", "country": "Australia"}
]

def filter_customers_by_country(country):
    """
    Filter customers based on the given country.

    :param country: The target country for filtering.
    :return: A list of customers who are from the given country.
    """
    return [customer for customer in customers if customer["country"] == country]

def load_customers_from_file(file_path):
    """
    Load customers from a JSON file.

    :param file_path: The path to the JSON file containing customers.
    """
    with open(file_path, 'r') as file:
        global customers
        customers = json.load(file)

def main(country=None, file_path=None):
    """
    The main function to filter and print customers.

    :param country: Filter customers by country.
    :param file_path: Path to the file containing customers.
    """
    if file_path:
        load_customers_from_file(file_path)
    if country:
        filtered_customers = filter_customers_by_country(country)
    else:
        filtered_customers = customers
    for customer in filtered_customers:
        print(customer)

if __name__ == "__main__":
    # Initialize the argument parser
    parser = argparse.ArgumentParser()

    # Add arguments
    parser.add_argument("--country", help="Filter customers by country")
    parser.add_argument("--file", help="Path to the file containing customers")

    # Parse arguments
    args = parser.parse_args()

    # Call the main function
    main(args.country, args.file)


  • Remove the Ability to load customers from a file
  • :f execute
  • :f pylint
  • Improve this file for SWE Best Practices. Don’t make it longer
  • reflection

Note: Examples are in the demo folder of the main repo

Categories
Tools

MusicLang AI Music Generator

conda create -n musiclang python=3.10 -y
conda activate musiclang
pip install musiclang_predict
import time
from musiclang_predict import MusicLangPredictor, corpus

# Initialize MusicLangPredictor
ml = MusicLangPredictor('musiclang/musiclang-v2')

# 1. Generating a free music idea
# 1024 tokens ~ 25s of music (depending on the number of instruments)
nb_tokens_free = 1024 # {type:"slider", min:32, max:1024, step:32}
temperature_free = 0.9 # {type:"slider", min:0.0, max:1, step:0.05}
top_p_free = 1.0 # {type:"slider", min:0.0, max:1.0, step:0.05}
seed_free = 16 # {type: "integer"} Set to 0 to unset seed

start = time.time()
score_free = ml.predict(
    nb_tokens=nb_tokens_free,
    temperature=temperature_free,
    topp=top_p_free,
    rng_seed=seed_free
)
end = time.time()
print(f"Free music idea generated in {end - start} seconds")
score_free.to_midi('music.mid')

# 2. Controlling the chord progression
# Chord qualities: M, m, 7, m7b5, sus2, sus4, m7, M7, dim, dim7.
chord_progression = "Am CM Dm E7 Am" # {type: "string"}
nb_tokens_chords = 1024 # {type:"slider", min:32, max:1024, step:32}
temperature_chords = 0.8 # {type:"slider", min:0.0, max:1, step:0.05}
top_p_chords = 1.0 # {type:"slider", min:0.0, max:1.0, step:0.05}
seed_chords = 42 # {type: "integer"}

start = time.time()
score_chords = ml.predict_chords(
    chord_progression,
    time_signature=(4, 4),
    temperature=temperature_chords,
    topp=top_p_chords,
    rng_seed=seed_chords
)
end = time.time()
print(f"Music with chord progression generated in {end - start} seconds")
score_chords.to_midi('chord.mid', tempo=110, time_signature=(4, 4))

# 3. Using a prompt to continue your own music
song_name_continue = 'boney_m_ma_baker' # ['bach_847', 'bob_marley_jammin', 'boney_m_ma_baker', 'mozart_alla_turca', 'white_stripes_seven_nation_army']
nb_tokens_continue = 1504 # {type:"slider", min:32, max:2048, step:32}
temperature_continue = 0.85 # {type:"slider", min:0.0, max:1, step:0.05}
top_p_continue = 1.0 # {type:"slider", min:0.0, max:1.0, step:0.05}
seed_continue = 1000 # {type: "integer"}

start = time.time()
score_continue = ml.predict(
    score=corpus.get_midi_path_from_corpus(song_name_continue),
    nb_tokens=nb_tokens_continue,
    prompt_chord_range=(0,4),
    temperature=temperature_continue,
    topp=top_p_continue,
    rng_seed=seed_continue
)
end = time.time()
print(f"Continued music generated in {end - start} seconds")
score_continue.to_midi('continue.mid', tempo=120, time_signature=(4, 4))

# 4. Combining both prompt and chord progression
song_name_with_chords = 'bach_847' # ['bach_847', 'bob_marley_jammin', 'boney_m_ma_baker', 'mozart_alla_turca', 'white_stripes_seven_nation_army']
chord_progression_with_prompt = "Cm C7/E Fm F#dim G7 Cm" # {type: "string"}
nb_tokens_with_chords = 1024 # {type:"slider", min:32, max:1024, step:32}
temperature_with_chords = 0.8 # {type:"slider", min:0.0, max:1, step:0.05}
top_p_with_chords = 1.0 # {type:"slider", min:0.0, max:1.0, step:0.05}
seed_with_chords = 3666 # {type: "integer"}

start = time.time()
score_combined = ml.predict_chords(
    chord_progression_with_prompt,
    score=corpus.get_midi_path_from_corpus(song_name_with_chords),
    time_signature=(4, 4),
    nb_tokens=nb_tokens_with_chords,
    prompt_chord_range=(0,4),
    temperature=temperature_with_chords,
    topp=top_p_with_chords,
    rng_seed=seed_with_chords
)
end = time.time()
print(f"Combined music generated in {end - start} seconds")
score_combined.to_midi('songchord.mid', tempo=110, time_signature=(4, 4))

https://github.com/MusicLang/musiclang_predict

Categories
LLM

Tamil LLM

Tamil Large Language Model

from transformers import AutoTokenizer, AutoModelForCausalLM

tokenizer = AutoTokenizer.from_pretrained("mervinpraison/tamil-large-language-model-7b-v1.0")
model = AutoModelForCausalLM.from_pretrained("mervinpraison/tamil-large-language-model-7b-v1.0")

query_to_llm = "ஆரோக்கியமாக இருப்பதற்கான இரண்டு வழிகள்"
inputs = tokenizer.encode(query_to_llm, return_tensors="pt")
outputs = model.generate(inputs, max_length=200)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(response)

Output

ஆரோக்கியமாக இருப்பதற்கான இரண்டு வழிகள்:

1. உடல் நலம்
2. மன நலம்
pip install huggingface_hub[hf_transfer]
export HF_HUB_ENABLE_HF_TRANSFER=1