jmartin233 commited on
Commit
c78ba93
1 Parent(s): c111383

adapt to my model

Browse files
Files changed (1) hide show
  1. app.py +66 -5
app.py CHANGED
@@ -1,7 +1,68 @@
1
- import gradio as gr
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
 
 
 
 
 
 
5
 
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from peft import PeftModel, PeftConfig
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
 
5
+ peft_model_id = f"jmartin233/bloom-1b7-lora-reading-comprehension"
6
+ config = PeftConfig.from_pretrained(peft_model_id)
7
+ model = AutoModelForCausalLM.from_pretrained(
8
+ config.base_model_name_or_path,
9
+ return_dict=True,
10
+ load_in_8bit=True,
11
+ device_map="auto",
12
+ )
13
+ tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)
14
 
15
+ # Load the Lora model
16
+ model = PeftModel.from_pretrained(model, peft_model_id)
17
+
18
+
19
+ def make_inference(product_name, product_description):
20
+ batch = tokenizer(
21
+ f"### Product and Description:\n{product_name}: {product_description}\n\n### Ad:",
22
+ return_tensors="pt",
23
+ )
24
+
25
+ with torch.cuda.amp.autocast():
26
+ output_tokens = model.generate(**batch, max_new_tokens=50)
27
+
28
+
29
+
30
+ def make_inference(person, location, grammer, level):
31
+
32
+ batch = tokenizer(f"""
33
+ Below is a set of requirements for a short passage of English. Please write a passage that meets these requirements:
34
+
35
+ ### Requirements:
36
+ person: {person}
37
+ location: {location}.
38
+ grammar: {grammar}
39
+ level: {level}
40
+
41
+ ### Passage:
42
+ Passage:""",
43
+ return_tensors='pt')
44
+
45
+ with torch.cuda.amp.autocast():
46
+ output_tokens = model.generate(**batch, max_new_tokens=50)
47
+
48
+ return tokenizer.decode(output_tokens[0], skip_special_tokens=True)
49
+
50
+
51
+
52
+ if __name__ == "__main__":
53
+ # make a gradio interface
54
+ import gradio as gr
55
+
56
+ gr.Interface(
57
+ make_inference,
58
+ [
59
+ gr.inputs.Textbox(lines=2, label="Someone's name"),
60
+ gr.inputs.Textbox(lines=2, label="A location they might visit"),
61
+ gr.inputs.Textbox(lines=2, label="A type of grammar to use"),
62
+ gr.inputs.Textbox(lines=2, label="The leve of English to use (beginner, intermediate, advanced))"),
63
+
64
+ ],
65
+ gr.outputs.Textbox(label="Passage"),
66
+ title="Reading Comprehension",
67
+ description="A generative model that generates simple texts for testing reading comprehension.",
68
+ ).launch()