andrew3279 commited on
Commit
c4f4968
·
1 Parent(s): 29781dd

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import transformers
2
+ from transformers import BloomForCausalLM
3
+ from transformers import BloomTokenizerFast
4
+ import torch
5
+ import gradio as gr
6
+
7
+ # setting device on GPU if available, else CPU
8
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
9
+ print(device)
10
+
11
+ model_name = "bigscience/bloom-1b"
12
+
13
+ model = BloomForCausalLM.from_pretrained(model_name,device_map="auto", load_in_8bit=True)
14
+ tokenizer = BloomTokenizerFast.from_pretrained(model_name)
15
+
16
+ # Define the pipeline for Gradio purpose
17
+
18
+ def beam_gradio_pipeline(prompt,length=500):
19
+
20
+ result_length = length
21
+
22
+ inputs = tokenizer(prompt, return_tensors="pt").to('cuda')
23
+
24
+ return tokenizer.decode(model.generate(inputs["input_ids"],
25
+ max_length=result_length,
26
+ num_beams=2,
27
+ no_repeat_ngram_size=2,
28
+ early_stopping=True
29
+ )[0])
30
+
31
+ with gr.Blocks() as demo:
32
+ gr.Markdown("<h1><center>Andrew Lim Bloom LLM </center></h1>")
33
+ gr.Markdown("""<h2><center>Generate your story with a sentence or ask a question:<br><br>
34
+ <img src=https://aeiljuispo.cloudimg.io/v7/https://s3.amazonaws.com/moonup/production/uploads/1634806038075-5df7e9e5da6d0311fd3d53f9.png?w=200&h=200&f=face width=200px></center></h2>""")
35
+ gr.Markdown("""<center>******</center>""")
36
+
37
+
38
+ input_text = gr.Textbox(label="Prompt", lines=6)
39
+ buton = gr.Button("Submit ")
40
+ output_text = gr.Textbox(lines=6, label="The story start with :")
41
+ buton.click(beam_gradio_pipeline, inputs=[input_text], outputs=output_text)
42
+
43
+ gr.HTML("""
44
+ <div style="border-top: 1px solid #303030;">
45
+ <br>
46
+ <p>Help me pay for GPU hours so I can publish faster models!</p>
47
+ <a href="https://www.buymeacoffee.com/" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy Me A Coffee" style="height: 45px !important;width: 162px !important;" ></a><br><br>
48
+ </div>
49
+ """)
50
+ demo.launch(enable_queue=True, debug=True)