Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,39 @@
|
|
| 1 |
import torch
|
| 2 |
-
|
| 3 |
-
from
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
# Load tokenizer
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained("VanguardAI/BhashiniLLaMa3-8B_LoRA_Adapters")
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
# Apply LoRA adapters
|
| 13 |
-
|
| 14 |
r=16,
|
| 15 |
lora_alpha=16,
|
| 16 |
-
target_modules
|
| 17 |
lora_dropout=0,
|
| 18 |
bias="none",
|
| 19 |
task_type="CAUSAL_LM"
|
| 20 |
)
|
| 21 |
-
model = PeftModel.from_pretrained(base_model, "VanguardAI/BhashiniLLaMa3-8B_LoRA_Adapters", config=lora_config)
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
ALWAYS provide output in a JSON format.
|
| 25 |
'''
|
| 26 |
alpaca_prompt = """Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
|
|
@@ -34,6 +47,7 @@ alpaca_prompt = """Below is an instruction that describes a task, paired with an
|
|
| 34 |
### Response:
|
| 35 |
{}"""
|
| 36 |
|
|
|
|
| 37 |
@spaces.GPU(duration=300)
|
| 38 |
def chunk_it(inventory_list, user_input_text):
|
| 39 |
inputs = tokenizer(
|
|
@@ -41,7 +55,7 @@ def chunk_it(inventory_list, user_input_text):
|
|
| 41 |
alpaca_prompt.format(
|
| 42 |
'''
|
| 43 |
You will receive text input that you need to analyze to perform the following tasks:
|
| 44 |
-
|
| 45 |
transaction: Record the details of an item transaction.
|
| 46 |
last n days transactions: Retrieve transaction records for a specified time period.
|
| 47 |
view risk inventory: View inventory items based on a risk category.
|
|
@@ -49,33 +63,33 @@ def chunk_it(inventory_list, user_input_text):
|
|
| 49 |
new items: Add new items to the inventory.
|
| 50 |
report generation: Generate various inventory reports.
|
| 51 |
delete item: Delete an existing Item.
|
| 52 |
-
|
| 53 |
Required Parameters:
|
| 54 |
Each task requires specific parameters to execute correctly:
|
| 55 |
-
|
| 56 |
transaction:
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
last n days transactions:
|
| 62 |
-
|
| 63 |
-
|
| 64 |
view risk inventory:
|
| 65 |
-
|
| 66 |
view inventory:
|
| 67 |
-
|
| 68 |
new items:
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
report generation:
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
The ItemName must always be matched from the below list of names, EXCEPT for when the Function is "new items".
|
| 78 |
-
''' + inventory_list +
|
| 79 |
'''
|
| 80 |
ALWAYS provide output in a JSON format.
|
| 81 |
''', # instruction
|
|
@@ -83,16 +97,14 @@ def chunk_it(inventory_list, user_input_text):
|
|
| 83 |
"", # output - leave this blank for generation!
|
| 84 |
)
|
| 85 |
], return_tensors="pt").to("cuda")
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
| 87 |
content = tokenizer.batch_decode(outputs, skip_special_tokens=True)
|
| 88 |
return content[0]
|
| 89 |
|
| 90 |
-
|
| 91 |
-
iface=gr.Interface(fn=chunk_it,
|
| 92 |
-
inputs="text",
|
| 93 |
-
outputs="text",
|
| 94 |
-
title="Bhashini_LLaMa_LoRA",
|
| 95 |
-
)
|
| 96 |
iface = gr.Interface(
|
| 97 |
fn=chunk_it,
|
| 98 |
inputs=[
|
|
@@ -102,4 +114,5 @@ iface = gr.Interface(
|
|
| 102 |
outputs="text",
|
| 103 |
title="Formatter Pro",
|
| 104 |
)
|
|
|
|
| 105 |
iface.launch(inline=False)
|
|
|
|
| 1 |
import torch
|
| 2 |
+
import spaces
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
| 4 |
+
from peft import LoraConfig, PeftModel, get_peft_model
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
# Load tokenizer
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained("VanguardAI/BhashiniLLaMa3-8B_LoRA_Adapters")
|
| 9 |
|
| 10 |
+
# Configuration for 4-bit quantization
|
| 11 |
+
bnb_config = BitsAndBytesConfig(
|
| 12 |
+
load_in_4bit=True,
|
| 13 |
+
bnb_4bit_use_double_quant=True,
|
| 14 |
+
bnb_4bit_quant_type="nf4",
|
| 15 |
+
bnb_4bit_compute_dtype=torch.bfloat16
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
# Load base model with quantization (replace 'your-username' if needed)
|
| 19 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
| 20 |
+
"meta-llama/Meta-Llama-3-8B-Instruct", # Replace with actual base model
|
| 21 |
+
quantization_config=bnb_config,
|
| 22 |
+
)
|
| 23 |
|
| 24 |
+
# Apply LoRA adapters
|
| 25 |
+
peft_config = LoraConfig(
|
| 26 |
r=16,
|
| 27 |
lora_alpha=16,
|
| 28 |
+
target_modules=["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"],
|
| 29 |
lora_dropout=0,
|
| 30 |
bias="none",
|
| 31 |
task_type="CAUSAL_LM"
|
| 32 |
)
|
|
|
|
| 33 |
|
| 34 |
+
model = PeftModel.from_pretrained(base_model, "VanguardAI/BhashiniLLaMa3-8B_LoRA_Adapters", config=peft_config)
|
| 35 |
+
|
| 36 |
+
condition = '''
|
| 37 |
ALWAYS provide output in a JSON format.
|
| 38 |
'''
|
| 39 |
alpaca_prompt = """Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
|
|
|
|
| 47 |
### Response:
|
| 48 |
{}"""
|
| 49 |
|
| 50 |
+
|
| 51 |
@spaces.GPU(duration=300)
|
| 52 |
def chunk_it(inventory_list, user_input_text):
|
| 53 |
inputs = tokenizer(
|
|
|
|
| 55 |
alpaca_prompt.format(
|
| 56 |
'''
|
| 57 |
You will receive text input that you need to analyze to perform the following tasks:
|
| 58 |
+
|
| 59 |
transaction: Record the details of an item transaction.
|
| 60 |
last n days transactions: Retrieve transaction records for a specified time period.
|
| 61 |
view risk inventory: View inventory items based on a risk category.
|
|
|
|
| 63 |
new items: Add new items to the inventory.
|
| 64 |
report generation: Generate various inventory reports.
|
| 65 |
delete item: Delete an existing Item.
|
| 66 |
+
|
| 67 |
Required Parameters:
|
| 68 |
Each task requires specific parameters to execute correctly:
|
| 69 |
+
|
| 70 |
transaction:
|
| 71 |
+
ItemName (string)
|
| 72 |
+
ItemQt (quantity - integer)
|
| 73 |
+
Type (string: "sale" or "purchase" or "return")
|
| 74 |
+
ReorderPoint (integer)
|
| 75 |
last n days transactions:
|
| 76 |
+
ItemName (string)
|
| 77 |
+
Duration (integer: number of days, if user input is in weeks, months or years then convert to days)
|
| 78 |
view risk inventory:
|
| 79 |
+
RiskType (string: "overstock", "understock", or "Null" for all risk types)
|
| 80 |
view inventory:
|
| 81 |
+
ItemName (string)
|
| 82 |
new items:
|
| 83 |
+
ItemName (string)
|
| 84 |
+
SellingPrice (number)
|
| 85 |
+
CostPrice (number)
|
| 86 |
report generation:
|
| 87 |
+
ItemName (string)
|
| 88 |
+
Duration (integer: number of days, if user input is in weeks, months or years then convert to days)
|
| 89 |
+
ReportType (string: "profit", "revenue", "inventory", or "Null" for all reports)
|
| 90 |
+
|
| 91 |
The ItemName must always be matched from the below list of names, EXCEPT for when the Function is "new items".
|
| 92 |
+
''' + inventory_list +
|
| 93 |
'''
|
| 94 |
ALWAYS provide output in a JSON format.
|
| 95 |
''', # instruction
|
|
|
|
| 97 |
"", # output - leave this blank for generation!
|
| 98 |
)
|
| 99 |
], return_tensors="pt").to("cuda")
|
| 100 |
+
|
| 101 |
+
# Generation with a longer max_length and better sampling
|
| 102 |
+
outputs = model.generate(**inputs, max_new_tokens=216, use_cache=True)
|
| 103 |
+
|
| 104 |
content = tokenizer.batch_decode(outputs, skip_special_tokens=True)
|
| 105 |
return content[0]
|
| 106 |
|
| 107 |
+
# Interface for inputs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
iface = gr.Interface(
|
| 109 |
fn=chunk_it,
|
| 110 |
inputs=[
|
|
|
|
| 114 |
outputs="text",
|
| 115 |
title="Formatter Pro",
|
| 116 |
)
|
| 117 |
+
|
| 118 |
iface.launch(inline=False)
|