Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
!nvidia-smi
|
3 |
+
|
4 |
+
"""Install the libraries"""
|
5 |
+
|
6 |
+
!pip install -q langchain transformers accelerate bitsandbytes
|
7 |
+
|
8 |
+
"""Load the libraries"""
|
9 |
+
|
10 |
+
from langchain.chains import LLMChain, SequentialChain
|
11 |
+
from langchain.memory import ConversationBufferMemory
|
12 |
+
from langchain import HuggingFacePipeline
|
13 |
+
from langchain import PromptTemplate, LLMChain
|
14 |
+
|
15 |
+
|
16 |
+
from transformers import AutoModel
|
17 |
+
import torch
|
18 |
+
import transformers
|
19 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
20 |
+
|
21 |
+
import json
|
22 |
+
import textwrap
|
23 |
+
|
24 |
+
"""Download the Model - We are using NousResearch's Llama2 which is the same as Meta AI's Llama 2, the only difference being "**Not requiring authentication to download**"
|
25 |
+
"""
|
26 |
+
|
27 |
+
tokenizer = AutoTokenizer.from_pretrained("NousResearch/Llama-2-7b-chat-hf")
|
28 |
+
|
29 |
+
model = AutoModelForCausalLM.from_pretrained("NousResearch/Llama-2-7b-chat-hf",
|
30 |
+
device_map='auto',
|
31 |
+
torch_dtype=torch.float16,
|
32 |
+
load_in_4bit=True,
|
33 |
+
bnb_4bit_quant_type="nf4",
|
34 |
+
bnb_4bit_compute_dtype=torch.float16)
|
35 |
+
|
36 |
+
"""Define Transformers Pipeline which will be fed into Langchain"""
|
37 |
+
|
38 |
+
from transformers import pipeline
|
39 |
+
|
40 |
+
pipe = pipeline("text-generation",
|
41 |
+
model=model,
|
42 |
+
tokenizer= tokenizer,
|
43 |
+
torch_dtype=torch.float16,
|
44 |
+
device_map="auto",
|
45 |
+
max_new_tokens = 4956,
|
46 |
+
do_sample=True,
|
47 |
+
top_k=30,
|
48 |
+
num_return_sequences=1,
|
49 |
+
eos_token_id=tokenizer.eos_token_id
|
50 |
+
)
|
51 |
+
|
52 |
+
"""Define the Prompt format for Llama 2 - This might change if you have a different model"""
|
53 |
+
|
54 |
+
B_INST, E_INST = "[INST]", "[/INST]"
|
55 |
+
B_SYS, E_SYS = "<>\n", "\n<>\n\n"
|
56 |
+
DEFAULT_SYSTEM_PROMPT = """\
|
57 |
+
As the leader of a sizable team in a dynamic business, I'm tasked with improving our supply chain management process. Recently, we've been facing issues like increased costs, longer lead times, and decreased customer satisfaction, all of which we believe are interconnected. To address these challenges, I need your assistance in optimizing our supply chain management. Please provide insights, strategies, and best practices that can help us streamline our operations, reduce costs, improve efficiency, and ultimately enhance customer satisfaction. Additionally, consider the latest technologies and innovations that could be integrated into our supply chain to make it more agile and responsive to market demands.If you don't know the answer to a question, please don't share false information.Just say you don't know and you are sorry!"""
|
58 |
+
|
59 |
+
"""All the helper fucntions to generate prompt, prompt template, clean up output text"""
|
60 |
+
|
61 |
+
def get_prompt(instruction, new_system_prompt=DEFAULT_SYSTEM_PROMPT, citation=None):
|
62 |
+
SYSTEM_PROMPT = B_SYS + new_system_prompt + E_SYS
|
63 |
+
prompt_template = B_INST + SYSTEM_PROMPT + instruction + E_INST
|
64 |
+
|
65 |
+
if citation:
|
66 |
+
prompt_template += f"\n\nCitation: {citation}" # Insert citation here
|
67 |
+
|
68 |
+
return prompt_template
|
69 |
+
|
70 |
+
def cut_off_text(text, prompt):
|
71 |
+
cutoff_phrase = prompt
|
72 |
+
index = text.find(cutoff_phrase)
|
73 |
+
if index != -1:
|
74 |
+
return text[:index]
|
75 |
+
else:
|
76 |
+
return text
|
77 |
+
|
78 |
+
def remove_substring(string, substring):
|
79 |
+
return string.replace(substring, "")
|
80 |
+
|
81 |
+
def generate(text, citation=None):
|
82 |
+
prompt = get_prompt(text, citation=citation)
|
83 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
84 |
+
with torch.no_grad():
|
85 |
+
outputs = model.generate(**inputs,
|
86 |
+
max_length=4956,
|
87 |
+
eos_token_id=tokenizer.eos_token_id,
|
88 |
+
pad_token_id=tokenizer.eos_token_id,
|
89 |
+
)
|
90 |
+
final_outputs = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
|
91 |
+
final_outputs = cut_off_text(final_outputs, '')
|
92 |
+
final_outputs = remove_substring(final_outputs, prompt)
|
93 |
+
|
94 |
+
return final_outputs
|
95 |
+
|
96 |
+
def parse_text(text):
|
97 |
+
wrapped_text = textwrap.fill(text, width=100)
|
98 |
+
print(wrapped_text + '\n\n')
|
99 |
+
|
100 |
+
"""Defining Langchain LLM"""
|
101 |
+
|
102 |
+
llm = HuggingFacePipeline(pipeline = pipe, model_kwargs = {'temperature':0.3,'max_length': 4956, 'top_k' :50})
|
103 |
+
|
104 |
+
system_prompt = "You are an advanced supply chain optimization expert"
|
105 |
+
instruction = "Use the data provided to you to optimize the supply chain:\n\n {text}"
|
106 |
+
template = get_prompt(instruction, system_prompt)
|
107 |
+
print(template)
|
108 |
+
|
109 |
+
prompt = PromptTemplate(template=template, input_variables=["text"])
|
110 |
+
|
111 |
+
llm_chain = LLMChain(prompt=prompt, llm=llm, verbose = False)
|
112 |
+
|
113 |
+
import pandas as pd
|
114 |
+
|
115 |
+
df_supplier = pd.read_csv('merged_data.csv')
|
116 |
+
|
117 |
+
print(df_supplier.head())
|
118 |
+
|
119 |
+
text = f"Based on the data provided how can you optimize my supply chain by prorviding me with the optmized solution as well as the techniques used. {df_supplier}"
|
120 |
+
|
121 |
+
response = llm_chain.run(text)
|
122 |
+
print(response)
|
123 |
+
|