adamtappis commited on
Commit
631e32b
β€’
1 Parent(s): eba1212

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -21
app.py CHANGED
@@ -1,25 +1,23 @@
1
- from langchain.embeddings.openai import OpenAIEmbeddings
2
- from langchain.vectorstores import Chroma
3
- from langchain.text_splitter import CharacterTextSplitter
4
- from langchain.chains.question_answering import load_qa_chain
5
- from langchain.llms import OpenAI
6
- import os
7
 
8
- with open("guide1.txt") as f:
9
- hitchhikersguide = f.read()
 
 
10
 
11
- text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0, separator = "\n")
12
- texts = text_splitter.split_text(hitchhikersguide)
13
 
14
- embeddings = OpenAIEmbeddings()
 
15
 
16
- docsearch = Chroma.from_texts(texts, embeddings, metadatas=[{"source": str(i)} for i in range(len(texts))]).as_retriever()
 
17
 
18
- chain = load_qa_chain(OpenAI(temperature=0), chain_type="stuff")
19
-
20
- def make_inference(query):
21
- docs = docsearch.get_relevant_documents(query)
22
- return(chain.run(input_documents=docs, question=query))
23
 
24
  if __name__ == "__main__":
25
  # make a gradio interface
@@ -28,9 +26,10 @@ if __name__ == "__main__":
28
  gr.Interface(
29
  make_inference,
30
  [
31
- gr.inputs.Textbox(lines=2, label="Query"),
 
32
  ],
33
- gr.outputs.Textbox(label="Response"),
34
- title="πŸ—£οΈTalkToMyDocπŸ“„",
35
- description="πŸ—£οΈTalkToMyDocπŸ“„ is a tool that allows you to ask questions about a document. In this case - Hitch Hitchhiker's Guide to the Galaxy.",
36
  ).launch()
 
1
+ import torch
2
+ from peft import PeftModel, PeftConfig
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
+ from IPython.display import display, Markdown
 
 
5
 
6
+ peft_model_id = f"adamtappis/marketing_emails_model"
7
+ config = PeftConfig.from_pretrained(peft_model_id)
8
+ model = AutoModelForCausalLM.from_pretrained(config.base_model_name_or_path, return_dict=True, load_in_8bit=False)
9
+ tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)
10
 
11
+ # Load the Lora model
12
+ model = PeftModel.from_pretrained(model, peft_model_id)
13
 
14
+ def make_inference(product, description):
15
+ batch = tokenizer(f"### INSTRUCTION\nBelow is a product and description, please write a marketing email for this product.\n\n### Product:\n{product}\n### Description:\n{description}\n\n### Marketing Email:\n", return_tensors='pt')
16
 
17
+ with torch.cuda.amp.autocast():
18
+ output_tokens = model.generate(**batch, max_new_tokens=200)
19
 
20
+ display(Markdown((tokenizer.decode(output_tokens[0], skip_special_tokens=True))))
 
 
 
 
21
 
22
  if __name__ == "__main__":
23
  # make a gradio interface
 
26
  gr.Interface(
27
  make_inference,
28
  [
29
+ gr.inputs.Textbox(lines=1, label="Product Name"),
30
+ gr.inputs.Textbox(lines=1, label="Product Description"),
31
  ],
32
+ gr.outputs.Textbox(label="Email"),
33
+ title="πŸ—£οΈMarketing Email GeneratorπŸ“„",
34
+ description="πŸ—£οΈMarketing Email GeneratorπŸ“„ is a tool that allows you to generate marketing emails for different products",
35
  ).launch()