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