Frontend_test / app.py
Th3r0's picture
added more info page
8487cf1 verified
import json
import gradio as gr
from transformers import pipeline, AutoTokenizer, AutoModel, BertForSequenceClassification, AlbertForSequenceClassification, DebertaForSequenceClassification, AutoModelForSequenceClassification, RobertaForSequenceClassification
from peft.auto import AutoPeftModelForSequenceClassification
from tensorboard.backend.event_processing import event_accumulator
from peft import PeftModel
from huggingface_hub import hf_hub_download
import plotly.express as px
import pandas as pd
# Parse sentiment analysis pipeline results
def parse_pipe_sa(pipe_out_text: str):
output_list = list(pipe_out_text)
pipe_label = output_list[0]['label']
pipe_score = float(output_list[0]['score'])*100
parsed_prediction = 'NULL'
if pipe_label == 'NEGATIVE' or pipe_label == 'LABEL_0':
parsed_prediction = f'This model thinks the sentiment is NEGATIVE. \nConfidence score of {pipe_score:.3f}%'
elif pipe_label == 'POSITIVE' or pipe_label == 'LABEL_1':
parsed_prediction = f'This model thinks the sentiment is POSITIVE. \nConfidence score of {pipe_score:.3f}%'
return parsed_prediction
# Parse sentiment NLI pipeline results
def parse_pipe_nli(pipe_out_text: str):
output_list = pipe_out_text
pipe_label = output_list['label']
pipe_score = float(output_list['score'])*100
parsed_prediction = 'NULL'
if pipe_label == 'NEGATIVE' or pipe_label == 'LABEL_0':
parsed_prediction = f'This model thinks the clauses CONFIRM each other. \nConfidence score of {pipe_score:.3f}'
elif pipe_label == 'POSITIVE' or pipe_label == 'LABEL_1':
parsed_prediction = f'This model thinks the clauses are Neutral. \nConfidence score of {pipe_score:.3f}'
elif pipe_label == 'POSITIVE' or pipe_label == 'LABEL_2':
parsed_prediction = f'This model thinks the clauses CONTRADICT each other. \nConfidence score of {pipe_score:.3f}'
return parsed_prediction
# Parse sentiment STS pipeline results
def parse_pipe_sts(pipe_out_text: str):
output_list = pipe_out_text
pipe_label = output_list['label']
pipe_score = float(output_list['score'])*100
parsed_prediction = 'NULL'
if pipe_label == 'NO SIMILARITY' or pipe_label == 'LABEL_0':
parsed_prediction = f'This model thinks the clauses have NO similarity. \nConfidence score of {pipe_score:.3f}%'
elif pipe_label == 'LITTLE SIMILARITY' or pipe_label == 'LABEL_1':
parsed_prediction = f'This model thinks the clauses have LITTLE similarity. \nConfidence score of {pipe_score:.3f}%'
elif pipe_label == 'MEDIUM OR HIGHER SIMILARITY' or pipe_label == 'LABEL_2':
parsed_prediction = f'This model thinks the clauses have MEDIUM to HIGH similarity. \nConfidence score of {pipe_score:.3f}%'
return parsed_prediction
#pretty sure this can be removed
loraModel = AutoPeftModelForSequenceClassification.from_pretrained("Intradiction/text_classification_WithLORA")
#tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased")
tokenizer1 = AutoTokenizer.from_pretrained("albert-base-v2")
tokenizer2 = AutoTokenizer.from_pretrained("microsoft/deberta-v3-xsmall")
# Handle calls to DistilBERT------------------------------------------
base_model = BertForSequenceClassification.from_pretrained("bert-base-uncased")
peft_model_id = "Intradiction/BERT-SA-LORA"
model = PeftModel.from_pretrained(model=base_model, model_id=peft_model_id)
sa_merged_model = model.merge_and_unload()
bbu_tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
distilBERTUntrained_pipe = pipeline("sentiment-analysis", model="bert-base-uncased")
distilBERTnoLORA_pipe = pipeline(model="Intradiction/text_classification_NoLORA")
SentimentAnalysis_LORA_pipe = pipeline("sentiment-analysis", model=sa_merged_model, tokenizer=bbu_tokenizer)
#text class models
def distilBERTnoLORA_fn(text):
return parse_pipe_sa(distilBERTnoLORA_pipe(text))
def distilBERTwithLORA_fn(text):
return parse_pipe_sa(SentimentAnalysis_LORA_pipe(text))
def distilBERTUntrained_fn(text):
return parse_pipe_sa(distilBERTUntrained_pipe(text))
# Handle calls to ALBERT---------------------------------------------
base_model1 = AlbertForSequenceClassification.from_pretrained("Alireza1044/albert-base-v2-mnli")
peft_model_id1 = "m4faisal/NLI-Lora-Fine-Tuning-10K-ALBERT"
model1 = PeftModel.from_pretrained(model=base_model1, model_id=peft_model_id1)
sa_merged_model1 = model1.merge_and_unload()
bbu_tokenizer1 = AutoTokenizer.from_pretrained("Alireza1044/albert-base-v2-mnli")
ALbertUntrained_pipe = pipeline("text-classification", model="Alireza1044/albert-base-v2-mnli")
AlbertnoLORA_pipe = pipeline(model="m4faisal/NLI-Conventional-Fine-Tuning")
AlbertwithLORA_pipe = pipeline("text-classification",model=sa_merged_model1, tokenizer=bbu_tokenizer1)
#NLI models
def AlbertnoLORA_fn(text1, text2):
return parse_pipe_nli(AlbertnoLORA_pipe({'text': text1, 'text_pair': text2}))
def AlbertwithLORA_fn(text1, text2):
return parse_pipe_nli(AlbertwithLORA_pipe({'text': text1, 'text_pair': text2}))
def AlbertUntrained_fn(text1, text2):
return parse_pipe_nli(ALbertUntrained_pipe({'text': text1, 'text_pair': text2}))
# Handle calls to Deberta--------------------------------------------
base_model2 = RobertaForSequenceClassification.from_pretrained("FacebookAI/roberta-base", num_labels=3)
peft_model_id2 = "rajevan123/STS-Lora-Fine-Tuning-Capstone-roberta-base-filtered-137-with-higher-r-mid"
model2 = PeftModel.from_pretrained(model=base_model2, model_id=peft_model_id2)
sa_merged_model2 = model2.merge_and_unload()
bbu_tokenizer2 = AutoTokenizer.from_pretrained("FacebookAI/roberta-base")
DebertaUntrained_pipe = pipeline("text-classification", model="FacebookAI/roberta-base")
DebertanoLORA_pipe = pipeline(model="rajevan123/STS-conventional-Fine-Tuning-Capstone-roberta-base-filtered-137")
DebertawithLORA_pipe = pipeline("text-classification",model=sa_merged_model2, tokenizer=bbu_tokenizer2)
#STS models
def DebertanoLORA_fn(text1, text2):
return parse_pipe_sts(DebertanoLORA_pipe({'text': text1, 'text_pair': text2}))
def DebertawithLORA_fn(text1, text2):
return parse_pipe_sts(DebertawithLORA_pipe({'text': text1, 'text_pair': text2}))
#return ("working2")
def DebertaUntrained_fn(text1, text2):
return parse_pipe_sts(DebertaUntrained_pipe({'text': text1, 'text_pair': text2}))
#helper functions ------------------------------------------------------
#Text metrics for Untrained models
def displayMetricStatsUntrained():
return "No statistics to display for untrained models"
def displayMetricStatsText():
#file_name = 'events.out.tfevents.distilbertSA-conventional.0'
file_name = hf_hub_download(repo_id="Intradiction/text_classification_NoLORA", filename="runs/Nov28_21-52-51_81dc5cd53c46/events.out.tfevents.1701208378.81dc5cd53c46.1934.0")
event_acc = event_accumulator.EventAccumulator(file_name,
size_guidance={
event_accumulator.COMPRESSED_HISTOGRAMS: 500,
event_accumulator.IMAGES: 4,
event_accumulator.AUDIO: 4,
event_accumulator.SCALARS: 0,
event_accumulator.HISTOGRAMS: 1,
})
event_acc.Reload()
accuracy_data = event_acc.Scalars('eval/accuracy')
loss_data = event_acc.Scalars('eval/loss')
#code to pull time data (very inaccurate)
# time_data = event_acc.Scalars('eval/runtime')
# Ttime = 0
# for time in time_data:
# Ttime+=time.value
# Ttime = str(round(Ttime/60,2))
# print(Ttime)
metrics = ("Active Training Time: 27.95 mins \n\n")
for i in range(0, len(loss_data)):
metrics = metrics + 'Epoch Number: ' + str(i) + '\n'
metrics = metrics + 'Accuracy (%): ' + str(round(accuracy_data[i].value * 100, 3)) + '\n'
metrics = metrics + 'Loss (%): ' + str(round(loss_data[i].value * 100, 3)) + '\n\n'
return metrics
def displayMetricStatsTextTCLora():
#file_name = 'events.out.tfevents.distilbertSA-LORA.0'
file_name = hf_hub_download(repo_id="Intradiction/BERT-SA-LORA", filename="runs/Mar16_18-10-29_INTRADICTION/events.out.tfevents.1710627034.INTRADICTION.31644.0")
event_acc = event_accumulator.EventAccumulator(file_name,
size_guidance={
event_accumulator.COMPRESSED_HISTOGRAMS: 500,
event_accumulator.IMAGES: 4,
event_accumulator.AUDIO: 4,
event_accumulator.SCALARS: 0,
event_accumulator.HISTOGRAMS: 1,
})
event_acc.Reload()
accuracy_data = event_acc.Scalars('eval/accuracy')
loss_data = event_acc.Scalars('eval/loss')
#code to pull time data (very inaccurate)
# time_data = event_acc.Scalars('eval/runtime')
# Ttime = 0
# for time in time_data:
# Ttime+=time.value
# Ttime = str(round(Ttime/60,2))
# print(event_acc.Tags())
metrics = ("Active Training Time: 15.58 mins \n\n")
for i in range(0, len(loss_data)):
metrics = metrics + 'Epoch Number: ' + str(i) + '\n'
metrics = metrics + 'Accuracy (%): ' + str(round(accuracy_data[i].value * 100, 3)) + '\n'
metrics = metrics + 'Loss (%): ' + str(round(loss_data[i].value * 100, 3)) + '\n\n'
return metrics
def displayMetricStatsTextNLINoLora():
#file_name = 'events.out.tfevents.NLI-Conventional.1'
file_name = hf_hub_download(repo_id="m4faisal/NLI-Conventional-Fine-Tuning", filename="runs/Mar20_23-18-22_a7cbf6b28344/events.out.tfevents.1710976706.a7cbf6b28344.5071.0")
event_acc = event_accumulator.EventAccumulator(file_name,
size_guidance={
event_accumulator.COMPRESSED_HISTOGRAMS: 500,
event_accumulator.IMAGES: 4,
event_accumulator.AUDIO: 4,
event_accumulator.SCALARS: 0,
event_accumulator.HISTOGRAMS: 1,
})
event_acc.Reload()
accuracy_data = event_acc.Scalars('eval/accuracy')
loss_data = event_acc.Scalars('eval/loss')
metrics = "Active Training Time: 6.74 mins \n\n"
for i in range(0, len(loss_data)):
metrics = metrics + 'Epoch Number: ' + str(i) + '\n'
metrics = metrics + 'Accuracy (%): ' + str(round(accuracy_data[i].value * 100, 3)) + '\n'
metrics = metrics + 'Loss (%): ' + str(round(loss_data[i].value * 100, 3)) + '\n\n'
return metrics
def displayMetricStatsTextNLILora():
#file_name = 'events.out.tfevents.NLI-Lora.0'
file_name = hf_hub_download(repo_id="m4faisal/NLI-Lora-Fine-Tuning-10K", filename="runs/Mar20_18-07-52_87caf1b1d04f/events.out.tfevents.1710958080.87caf1b1d04f.7531.0")
event_acc = event_accumulator.EventAccumulator(file_name,
size_guidance={
event_accumulator.COMPRESSED_HISTOGRAMS: 500,
event_accumulator.IMAGES: 4,
event_accumulator.AUDIO: 4,
event_accumulator.SCALARS: 0,
event_accumulator.HISTOGRAMS: 1,
})
event_acc.Reload()
accuracy_data = event_acc.Scalars('eval/accuracy')
loss_data = event_acc.Scalars('eval/loss')
metrics = "Active Training Time: 15.04 mins \n\n"
for i in range(0, len(loss_data)):
metrics = metrics + 'Epoch Number: ' + str(i) + '\n'
metrics = metrics + 'Accuracy (%): ' + str(round(accuracy_data[i].value * 100, 3)) + '\n'
metrics = metrics + 'Loss (%): ' + str(round(loss_data[i].value * 100, 3)) + '\n\n'
return metrics
def displayMetricStatsTextSTSLora():
#file_name = 'events.out.tfevents.STS-Lora.2'
file_name = hf_hub_download(repo_id="rajevan123/STS-Lora-Fine-Tuning-Capstone-roberta-base-filtered-137-with-higher-r-mid", filename="runs/Mar28_19-51-13_fcdc58e67935/events.out.tfevents.1711655476.fcdc58e67935.625.0")
event_acc = event_accumulator.EventAccumulator(file_name,
size_guidance={
event_accumulator.COMPRESSED_HISTOGRAMS: 500,
event_accumulator.IMAGES: 4,
event_accumulator.AUDIO: 4,
event_accumulator.SCALARS: 0,
event_accumulator.HISTOGRAMS: 1,
})
event_acc.Reload()
accuracy_data = event_acc.Scalars('eval/accuracy')
loss_data = event_acc.Scalars('eval/loss')
metrics = "Active Training Time: 41.07 mins \n\n"
for i in range(0, len(loss_data)):
metrics = metrics + 'Epoch Number: ' + str(i) + '\n'
metrics = metrics + 'Accuracy (%): ' + str(round(accuracy_data[i].value * 100, 3)) + '\n'
metrics = metrics + 'Loss (%): ' + str(round(loss_data[i].value * 100, 3)) + '\n\n'
return metrics
def displayMetricStatsTextSTSNoLora():
#file_name = 'events.out.tfevents.STS-Conventional.0'
file_name = hf_hub_download(repo_id="rajevan123/STS-conventional-Fine-Tuning-Capstone-roberta-base-filtered-137", filename="runs/Mar31_15-13-28_585e70ba99a4/events.out.tfevents.1711898010.585e70ba99a4.247.0")
event_acc = event_accumulator.EventAccumulator(file_name,
size_guidance={
event_accumulator.COMPRESSED_HISTOGRAMS: 500,
event_accumulator.IMAGES: 4,
event_accumulator.AUDIO: 4,
event_accumulator.SCALARS: 0,
event_accumulator.HISTOGRAMS: 1,
})
event_acc.Reload()
accuracy_data = event_acc.Scalars('eval/accuracy')
loss_data = event_acc.Scalars('eval/loss')
metrics = "Active Training Time: 23.96 mins \n\n"
for i in range(0, len(loss_data)):
metrics = metrics + 'Epoch Number: ' + str(i) + '\n'
metrics = metrics + 'Accuracy (%): ' + str(round(accuracy_data[i].value * 100, 3)) + '\n'
metrics = metrics + 'Loss (%): ' + str(round(loss_data[i].value * 100, 3)) + '\n\n'
return metrics
def displayMetricStatsGraph():
file_name = 'events.out.tfevents.1701212945.784ae33ab242.985.0'
event_acc = event_accumulator.EventAccumulator(file_name,
size_guidance={
event_accumulator.COMPRESSED_HISTOGRAMS: 500,
event_accumulator.IMAGES: 4,
event_accumulator.AUDIO: 4,
event_accumulator.SCALARS: 0,
event_accumulator.HISTOGRAMS: 1,
})
event_acc.Reload()
accuracy_data = event_acc.Scalars('eval/accuracy')
loss_data = event_acc.Scalars("eval/loss")
epoch = []
metric = []
group = []
for i in range(0, len(accuracy_data)):
epoch.append(str(i))
metric.append(accuracy_data[i].value)
group.append('G1')
for j in range(0, len(loss_data)):
epoch.append(str(j))
metric.append(loss_data[j].value)
group.append('G2')
data = pd.DataFrame()
data['Epoch'] = epoch
data['Metric'] = metric
data['Group'] = group
#generate the actual plot
return px.line(data, x = 'Epoch', y = 'Metric', color=group, markers = True)
# #placeholder
# def chat1(message,history):
# history = history or []
# message = message.lower()
# if message.startswith("how many"):
# response = ("1 to 10")
# else:
# response = ("whatever man whatever manwhatever manwhatever manwhatever manwhatever manwhatever manwhatever manwhatever manwhatever manwhatever manwhatever man")
# history.append((message, response))
# return history, history
with gr.Blocks(
title="",
) as demo:
gr.Markdown("""
<div style="overflow: hidden;color:#fff;display: flex;flex-direction: column;align-items: center; position: relative; width: 100%; height: 180px;background-size: cover; background-image: url(https://www.grssigns.co.uk/wp-content/uploads/web-Header-Background.jpg);">
<img style="width: 130px;height: 60px;position: absolute;top:10px;left:10px" src="https://www.torontomu.ca/content/dam/tmumobile/images/TMU-Mobile-AppIcon.png"/>
<span style="margin-top: 40px;font-size: 36px ;font-family:fantasy;">Efficient Fine Tuning Of Large Language Models</span>
<span style="margin-top: 10px;font-size: 14px;">By: Rahul Adams, Greylyn Gao, Rajevan Logarajah & Mahir Faisal</span>
<span style="margin-top: 5px;font-size: 14px;">Group Id: AR06 FLC: Alice Reuda</span>
</div>
""")
with gr.Tab("Text Classification"):
with gr.Row():
gr.Markdown("<h1>Efficient Fine Tuning for Text Classification</h1>")
with gr.Row():
with gr.Column(variant="panel"):
gr.Markdown("""
<h2>Specifications</h2>
<p><b>Model:</b> Bert Base Uncased <br>
<b>Number of Parameters:</b> 110 Million <br>
<b>Dataset:</b> IMDB Movie review dataset <br>
<b>NLP Task:</b> Text Classification</p>
<p>Text classification is an NLP task that focuses on automatically ascribing a predefined category or labels to an input prompt. In this demonstration the Tiny Bert model has been used to classify the text on the basis of sentiment analysis, where the labels (negative and positive) will indicate the emotional state expressed by the input prompt.<br><br>The models were trained on the IMDB dataset which includes over 100k sentiment pairs pulled from IMDB movie reviews.<br><br><b>Results:</b><br> It can be seen that the LoRA fine tuned model performs comparably to the conventionally trained model. The difference arises in the training time where the conventional model takes almost 30 mins to train through 2 epochs the LoRA model takes half the time to train through 4 epochs.</p>
""")
with gr.Column(variant="panel"):
inp = gr.Textbox(placeholder="Prompt",label= "Enter Query")
btn = gr.Button("Run")
btnTextClassStats = gr.Button("Display Training Metrics")
btnTensorLinkTCNoLora = gr.Button(value="View Conventional Training Graphs", link="https://huggingface.co/Intradiction/text_classification_NoLORA/tensorboard")
btnTensorLinkTCLora = gr.Button(value="View LoRA Training Graphs", link="https://huggingface.co/Intradiction/BERT-SA-LORA/tensorboard")
gr.Examples(
[
"I thought this was a bit contrived",
"You would need to be a child to enjoy this",
"Drive more like Drive away",
],
inp,
label="Try asking",
)
with gr.Column(scale=3):
with gr.Row(variant="panel"):
TextClassOut = gr.Textbox(label= "Untrained Base Model")
TextClassUntrained = gr.Textbox(label = "Training Informaiton")
with gr.Row(variant="panel"):
TextClassOut1 = gr.Textbox(label="Conventionaly Trained Model")
TextClassNoLoraStats = gr.Textbox(label = "Training Informaiton - Active Training Time: 27.95 mins")
with gr.Row(variant="panel"):
TextClassOut2 = gr.Textbox(label= "LoRA Fine Tuned Model")
TextClassLoraStats = gr.Textbox(label = "Training Informaiton - Active Training Time: 15.58 mins")
btn.click(fn=distilBERTUntrained_fn, inputs=inp, outputs=TextClassOut)
btn.click(fn=distilBERTnoLORA_fn, inputs=inp, outputs=TextClassOut1)
btn.click(fn=distilBERTwithLORA_fn, inputs=inp, outputs=TextClassOut2)
btnTextClassStats.click(fn=displayMetricStatsUntrained, outputs=TextClassUntrained)
btnTextClassStats.click(fn=displayMetricStatsText, outputs=TextClassNoLoraStats)
btnTextClassStats.click(fn=displayMetricStatsTextTCLora, outputs=TextClassLoraStats)
with gr.Tab("Natural Language Inferencing"):
with gr.Row():
gr.Markdown("<h1>Efficient Fine Tuning for Natural Language Inferencing</h1>")
with gr.Row():
with gr.Column(variant="panel"):
gr.Markdown("""
<h2>Specifications</h2>
<p><b>Model:</b> Albert <br>
<b>Number of Parameters:</b> 11 Million <br>
<b>Dataset:</b> Stanford Natural Language Inference Dataset <br>
<b>NLP Task:</b> Natural Language Inferencing</p>
<p>Natural Language Inference (NLI) which can also be referred to as Textual Entailment is an NLP task with the objective of determining the relationship between two pieces of text. Ideally to determine logical inference (i.e. If the pairs contradict or confirm one another).<br><br>The models were trained on the Stanford Natural Language Inference Dataset which is a collection of 570k human-written English sentence pairs manually labeled for balanced classification, listed as positive, negative or neutral. <br><br><b>Results</b><br>While the time to train for the conventional model may be lower if we look closer at the number of epochs the models we trained over the LoRA model has a time per epoch of 1.5 mins vs the conventional's 3mins per epoch, showing significant improvement. </p>
""")
with gr.Column(variant="panel"):
nli_p1 = gr.Textbox(placeholder="Prompt One",label= "Enter Query")
nli_p2 = gr.Textbox(placeholder="Prompt Two",label= "Enter Query")
nli_btn = gr.Button("Run")
btnNLIStats = gr.Button("Display Training Metrics")
btnTensorLinkNLICon = gr.Button(value="View Conventional Training Graphs", link="https://huggingface.co/m4faisal/NLI-Conventional-Fine-Tuning/tensorboard")
btnTensorLinkNLILora = gr.Button(value="View LoRA Training Graphs", link="https://huggingface.co/m4faisal/NLI-Lora-Fine-Tuning-10K/tensorboard")
gr.Examples(
[
"A man is awake",
"People like apples",
"A game with mutiple people playing",
],
nli_p1,
label="Try asking",
)
gr.Examples(
[
"A man is sleeping",
"Apples are good",
"Some people are playing a game",
],
nli_p2,
label="Try asking",
)
with gr.Column(scale=3):
with gr.Row(variant="panel"):
NLIOut = gr.Textbox(label= "Untrained Base Model")
NLIUntrained = gr.Textbox(label = "Training Informaiton")
with gr.Row(variant="panel"):
NLIOut1 = gr.Textbox(label= "Conventionaly Trained Model")
NLINoLoraStats = gr.Textbox(label = "Training Informaiton - Active Training Time: 6.74 mins")
with gr.Row(variant="panel"):
NLIOut2 = gr.Textbox(label= "LoRA Fine Tuned Model")
NLILoraStats = gr.Textbox(label = "Training Informaiton - Active Training Time: 15.04 mins")
nli_btn.click(fn=AlbertUntrained_fn, inputs=[nli_p1,nli_p2], outputs=NLIOut)
nli_btn.click(fn=AlbertnoLORA_fn, inputs=[nli_p1,nli_p2], outputs=NLIOut1)
nli_btn.click(fn=AlbertwithLORA_fn, inputs=[nli_p1,nli_p2], outputs=NLIOut2)
btnNLIStats.click(fn=displayMetricStatsUntrained, outputs=NLIUntrained)
btnNLIStats.click(fn=displayMetricStatsTextNLINoLora, outputs=NLINoLoraStats)
btnNLIStats.click(fn=displayMetricStatsTextNLILora, outputs=NLILoraStats)
with gr.Tab("Semantic Text Similarity"):
with gr.Row():
gr.Markdown("<h1>Efficient Fine Tuning for Semantic Text Similarity</h1>")
with gr.Row():
with gr.Column(variant="panel"):
gr.Markdown("""
<h2>Specifications</h2>
<p><b>Model:</b> Roberta Base <br>
<b>Number of Parameters:</b> 125 Million <br>
<b>Dataset:</b> Semantic Text Similarity Benchmark <br>
<b>NLP Task:</b> Semantic Text Similarity</p>
<p>Semantic text similarity measures the closeness in meaning of two pieces of text despite differences in their wording or structure. This task involves two input prompts which can be sentences, phrases or entire documents and assessing them for similarity. <br><br>This implementation uses the Roberta base model and training was performed on the semantic text similarity benchmark dataset which contains over 86k semantic pairs and their scores.<br><br><b>Results</b><br> We can see that for a comparable result the LoRA trained model manages to train for 30 epochs in 14.5 mins vs the conventional models 24 mins displaying a 60% increase in efficiency. </p>
""")
with gr.Column(variant="panel"):
sts_p1 = gr.Textbox(placeholder="Prompt One",label= "Enter Query")
sts_p2 = gr.Textbox(placeholder="Prompt Two",label= "Enter Query")
sts_btn = gr.Button("Run")
btnSTSStats = gr.Button("Display Training Metrics")
btnTensorLinkSTSCon = gr.Button(value="View Conventional Training Graphs", link="https://huggingface.co/rajevan123/STS-Conventional-Fine-Tuning/tensorboard")
btnTensorLinkSTSLora = gr.Button(value="View Lora Training Graphs", link="https://huggingface.co/rajevan123/STS-Lora-Fine-Tuning-Capstone-roberta-base-filtered-137-with-higher-r-mid/tensorboard")
gr.Examples(
[
"the ball is green",
"i dont like apples",
"our air is clean becase of trees",
],
sts_p1,
label="Try asking",
)
gr.Examples(
[
"the green ball",
"apples are great",
"trees produce oxygen",
],
sts_p2,
label="Try asking",
)
with gr.Column(scale=3):
with gr.Row(variant="panel"):
sts_out = gr.Textbox(label= "Untrained Base Model")
STSUntrained = gr.Textbox(label = "Training Informaiton")
with gr.Row(variant="panel"):
sts_out1 = gr.Textbox(label= "Conventionally Trained Model")
STSNoLoraStats = gr.Textbox(label = "Training Informaiton - Active Training Time: 23.96 mins")
with gr.Row(variant="panel"):
sts_out2 = gr.Textbox(label= "LoRA Fine Tuned Model")
STSLoraStats = gr.Textbox(label = "Training Informaiton - Active Training Time: 14.62 mins")
sts_btn.click(fn=DebertaUntrained_fn, inputs=[sts_p1,sts_p2], outputs=sts_out)
sts_btn.click(fn=DebertanoLORA_fn, inputs=[sts_p1,sts_p2], outputs=sts_out1)
sts_btn.click(fn=DebertawithLORA_fn, inputs=[sts_p1,sts_p2], outputs=sts_out2)
btnSTSStats.click(fn=displayMetricStatsUntrained, outputs=STSUntrained)
btnSTSStats.click(fn=displayMetricStatsTextSTSNoLora, outputs=STSNoLoraStats)
btnSTSStats.click(fn=displayMetricStatsTextSTSLora, outputs=STSLoraStats)
with gr.Tab("More information"):
with gr.Row():
gr.Markdown("<h1>More Information on the Project</h1>")
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("""
<img style="width: 320px;height: 180px;position:center;" src="https://assets-global.website-files.com/601be0f0f62d8b2e2a92b830/647617bf24dd28930978918d_fine-tuning-ai.png"/>
""")
with gr.Column(scale=3):
gr.Markdown("""<h2>Objective</h2>
<p> Large Language Models (LLM) are complex natural language processing algorithms which can perform a multitude of tasks. These models outperform the average individual in many standardized tests, but they lack the ability to provide accurate results in specialized tasks. In the past, general purpose models have been fine tuned to provide domain-specific knowledge. However, as models become more complex, the number of parameters increases; just this year, we have witnessed an over 500x increase between GPT-3 and GPT-4, as such the computational cost of conventional fine tuning in some cases makes it prohibitive to do so. In this Engineering design project, we have been tasked to investigate fine tuning strategies to provide a more efficient solution to train LLMs on a singular GPU.</p>
""")
with gr.Row():
with gr.Column(scale=2):
gr.Markdown("""<h2>Theory of LoRA</h2>
<p> In the world of deep learning, Low-Rank adaptation (LoRA) stands out as an efficient strategy for fine tuning Large Language models. Being a subset of parameter efficient fine tuning (PEFT), LoRA is a targeted fine tuning process, which minimizes the necessity for retraining the entire model and substantially reduces computational costs.</p>
<p> The fundamental property that LoRA is built on is the assumption that the large matrices which compose the layers of modern LLM models have an intrinsically low rank. The rank of a matrix refers to the number of linearly independent variables, and this is important because this means that we can decompose layers into much smaller matrices reducing the number of training parameters without losing information. A very simple example of this would be to imagine a 100 x 100 matrix with a rank of 2 which would have a 10 '000 trainable parameters. Should we decompose this matrix following the theorem we can create two constituent matrixes which can be multiplied together to restore the original. The decomposition matrices would be of sizes 100 x 2 and 2 x 100 resulting in only 400 trainable parameters, a 96% decrease. As the number of parameters are directly correlated to the amount of time required for training this is a massive difference. </p>
""")
with gr.Column(scale=1):
gr.Markdown("""<br><br><br><br>
<img style="width: 644px;height: 180px;" src="https://docs.h2o.ai/h2o/latest-stable/h2o-docs/_images/glrm_matrix_decomposition.png"/>
""")
if __name__ == "__main__":
demo.launch()