Downloads · 30 days
3
1% of all-time downloads
skush1/gemma_song_finder
gemma_song_finder is a machine learning model from skush1. Use it for the machine learning task on the model card, and read the license before you ship it in a product. It is set up for keras.
Downloads · 30 days
3
1% of all-time downloads
All-time downloads
321
Public
Repo size
12.8 GB
Likes
2
Trending 1
Click a slice to open those files.
.keras10.5 GB · 82%
From the Hugging Face model README
This project demonstrates how to fine-tune and use the Gemma-2B model for Question-Answering (QA) tasks. The fine-tuning process integrates LoRA (Low-Rank Adaptation) to optimize memory usage. The dataset used is a custom-generated dataset consisting of question-answer pairs related to song lyrics, where the original CSV format was converted into JSON format for fine-tuning. The fine-tuned model is then capable of generating answers based on new questions provided in a similar format.
The final set of {'question', 'answer'} pairs was stored in a JSON file, with the following structure:
...
{
"q_script": ...,
"a_script": ...
}
{
"q_script": "What is the name of the song that was released in 2022 with the lyrics 'You got me for days'?",
"a_script": "The song is 'You Got Me' by Alan Walker, released in 2022."
}
{
"q_script": ...,
"a_script": ...
}
...
This dataset was originally collected in CSV format but was converted into JSON to facilitate easier fine-tuning with the Gemma-2B model.
Ensure you have the required libraries installed:
!pip install transformers keras-nlp pandas datasets huggingface_hub
The original dataset was in CSV format but was converted into JSON for fine-tuning. The following code demonstrates how to load the CSV file and convert it to a JSON file:
import pandas as pd
import json
import random
# Load the CSV dataset
csv_file = "qa_dataset.csv"
df = pd.read_csv(csv_file)
# Convert CSV data to JSON format
json_file = "qa_dataset.json"
data = df.to_dict(orient="records")
# Save the data as a JSON file
with open(json_file, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=4)
print(f"JSON file saved: {json_file}")
Use the following code to fine-tune the Gemma-2B model with LoRA:
import keras
from transformers import AutoTokenizer, AutoModelForCausalLM
from keras_nlp.models import GemmaCausalLM
# Load the fine-tuning dataset from the JSON file
with open(json_file, 'r', encoding='utf-8') as f:
data = json.load(f)
# Format the data for fine-tuning
formatted_data = [f"Question:
{item['q_script']}
Answer:
{item['a_script']}" for item in data]
# Load the Gemma-2B model
gemma_model_id = "gemma2_instruct_2b_en"
gemma_lm = GemmaCausalLM.from_preset(gemma_model_id)
# Enable LoRA for the model
gemma_lm.backbone.enable_lora(rank=4)
# Set sequence length and compile the model
gemma_lm.preprocessor.sequence_length = 128
optimizer = keras.optimizers.AdamW(learning_rate=5e-5, weight_decay=0.01)
gemma_lm.compile(
loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
optimizer=optimizer,
weighted_metrics=[keras.metrics.SparseCategoricalAccuracy()],
)
# Fine-tune the model with the formatted data
gemma_lm.fit(formatted_data, epochs=1, batch_size=1)
Once the model is fine-tuned, you can query it with new questions:
# Define a function to format a question into a prompt template
def ask_question(query: str) -> str:
template = "Question:
{question}
Answer:
{answer}"
prompt = template.format(question=query, answer="")
return prompt
# Ask a question and generate an answer
prompt = ask_question("How can I install Python 3 on an AWS EC2 instance?")
print(gemma_lm.generate(prompt, max_length=512))
LoRA (Low-Rank Adaptation) is used in this project to optimize the fine-tuning process. By reducing the number of trainable parameters, LoRA allows for efficient fine-tuning of large models like Gemma-2B, even on devices with limited memory.
Once the model is fine-tuned, you can upload it to the Hugging Face Hub using the following steps:
!pip install huggingface_hub
!huggingface-cli login
from huggingface_hub import HfApi
# Save and push model to Hugging Face
model_name = "my-fine-tuned-gemma"
gemma_lm.save_pretrained(model_name)
tokenizer.save_pretrained(model_name)
# Push to Hugging Face Hub
api = HfApi()
api.upload_folder(
folder_path=model_name,
repo_id="username/my-fine-tuned-gemma",
repo_type="model"
)
Here’s an example of querying the fine-tuned model:
# Define a prompt with a specific query
prompt = ask_question("노래 제목이 도대체 뭐였는지 기억 안 나서 답답해요.
2016년경에 발표된 곡 같아요.
대중 음악 스타일 맞는 것 같아.
아마도 The Chainsmokers의 곡일 거야.")
# Generate the response
print(gemma_lm.generate(prompt, max_length=1012))
Output:
질문하신 노래는 ‘The Chainsmokers’의 ‘Closer (Feat. Halsey)’입니다.
해당 노래는 ‘일렉트로니카’ 장르의 노래입니다.
‘2016.11.05’ 발매되었습니다.
‘Collage EP’ 앨범에 수록되었습니다.
We would like to thank:
This project was developed by: