prasanna2003 commited on
Commit
e8d0976
·
1 Parent(s): 205e913

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -0
app.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import AutoTokenizer
4
+
5
+ class Pipline:
6
+ def __init__(self, model, tokenizer, device='cpu'):
7
+ self.device = device
8
+ self.model = model.to(self.device)
9
+ self.tokenizer = tokenizer
10
+ self.pre_prompt = "\n\nYou are a AI assistant who helps the user to solve their issue\n\n"
11
+
12
+ @torch.no_grad()
13
+ def respond(self, Instruction=None, input=None, temperature=0.8, max_length=200, do_sample=True, top_k=0, top_p=0.9, repetition_penalty=1.0, num_return_sequences=1, num_beams=1, early_stopping=False, use_cache=True, **generate_kwargs):
14
+ if not Instruction and not input:
15
+ raise ValueError("Either Instruction or input must be passed.")
16
+ query = f"""{self.pre_prompt}
17
+ Instruction: {Instruction if Instruction else ""}
18
+ Input: {input if input else ""}
19
+ Output:"""
20
+ inp_tokens_l = self.tokenizer(query, return_tensors='pt').input_ids
21
+ inp_tokens = inp_tokens_l.to(self.device)
22
+ out_tokens = self.model.generate(inp_tokens, max_length=max_length, temperature=temperature, do_sample=do_sample, top_k=top_k, top_p=top_p, repetition_penalty=repetition_penalty, num_return_sequences=num_return_sequences, num_beams=num_beams, early_stopping=early_stopping, use_cache=use_cache, **generate_kwargs)
23
+ out_text = self.tokenizer.batch_decode(out_tokens, skip_special_tokens=True)
24
+ # self.pre_prompt = out_text[0].split("<|endoftext|>")[0]
25
+ return out_text
26
+
27
+ tokenizer = AutoTokenizer.from_pretrained("facebook/opt-125M")
28
+ model = torch.load('model-cpu.pkl')
29
+
30
+ pipe = Pipline(model=model, tokenizer=tokenizer, device='cpu')
31
+
32
+ input_components = [
33
+ gr.inputs.Textbox(label='Instruction', placeholder='Enter instruction...'),
34
+ gr.inputs.Textbox(label='Input', placeholder='Enter input...'),
35
+ ]
36
+
37
+ output_components = [
38
+ gr.outputs.Textbox(label='Output'),
39
+ ]
40
+
41
+ def chatbot_response(Instruction, input, max_length, temperature):
42
+ output = pipe.respond(
43
+ Instruction=Instruction,
44
+ input=input,
45
+ max_length=int(max_length),
46
+ temperature=float(temperature),
47
+ )
48
+ return output[0]
49
+
50
+ interface = gr.Interface(
51
+ fn=chatbot_response,
52
+ inputs=input_components + [
53
+ gr.inputs.Slider(
54
+ label='Max Length',
55
+ minimum=10,
56
+ maximum=500,
57
+ step=10,
58
+ default=200,
59
+ ),
60
+ gr.inputs.Slider(
61
+ label='Temperature',
62
+ minimum=0.1,
63
+ maximum=1.0,
64
+ step=0.1,
65
+ default=0.8,
66
+ ),
67
+ ],
68
+ outputs=output_components,
69
+ title='AI Assistant Chatbot',
70
+ description='Type in an instruction and input, and get a response from the AI assistant!',
71
+ )
72
+
73
+ interface.launch()