bixentemal commited on
Commit
8352cd8
1 Parent(s): e4c96d3
Files changed (1) hide show
  1. app.py +14 -57
app.py CHANGED
@@ -1,60 +1,17 @@
1
- from transformers import AutoModelForCausalLM, AutoTokenizer, BlenderbotForConditionalGeneration
2
- import torch
3
-
4
- chat_tkn = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
5
- mdl = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
6
-
7
-
8
- # chat_tkn = AutoTokenizer.from_pretrained("facebook/blenderbot-400M-distill")
9
- # mdl = BlenderbotForConditionalGeneration.from_pretrained("facebook/blenderbot-400M-distill")
10
-
11
- def converse(user_input, chat_history=[]):
12
- user_input_ids = chat_tkn(user_input + chat_tkn.eos_token, return_tensors='pt').input_ids
13
-
14
- # keep history in the tensor
15
- bot_input_ids = torch.cat([torch.LongTensor(chat_history), user_input_ids], dim=-1)
16
-
17
- # get response
18
- chat_history = mdl.generate(bot_input_ids, max_length=1000, pad_token_id=chat_tkn.eos_token_id).tolist()
19
- print(chat_history)
20
-
21
- response = chat_tkn.decode(chat_history[0]).split("<|endoftext|>")
22
-
23
- print("starting to print response")
24
- print(response)
25
-
26
- # html for display
27
- html = "<div class='mybot'>"
28
- for x, mesg in enumerate(response):
29
- if x % 2 != 0:
30
- mesg = "Alicia:" + mesg
31
- clazz = "alicia"
32
- else:
33
- clazz = "user"
34
-
35
- print("value of x")
36
- print(x)
37
- print("message")
38
- print(mesg)
39
-
40
- html += "<div class='mesg {}'> {}</div>".format(clazz, mesg)
41
- html += "</div>"
42
- print(html)
43
- return html, chat_history
44
 
 
 
 
 
45
 
46
- import gradio as grad
 
 
47
 
48
- css = """
49
- .mychat {display:flex;flex-direction:column}
50
- .mesg {padding:5px;margin-bottom:5px;border-radius:5px;width:75%}
51
- .mesg.user {background-color:lightblue;color:white}
52
- .mesg.alicia {background-color:orange;color:white,align-self:self-end}
53
- .footer {display:none !important}
54
- """
55
- text = grad.inputs.Textbox(placeholder="Lets chat")
56
- grad.Interface(fn=converse,
57
- theme="default",
58
- inputs=[text, "state"],
59
- outputs=["html", "state"],
60
- css=css).launch()
 
1
+ from transformers import AutoTokenizer, AutoModelForCausalLM
2
+ import gradio as grad
3
+ codegen_tkn = AutoTokenizer.from_pretrained("Salesforce/codegen-350M-mono")
4
+ mdl = AutoModelForCausalLM.from_pretrained("Salesforce/codegen-350M-mono")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
+ def codegen(intent):
7
+ # give input as text which reflects intent of the program.
8
+ #text = " write a function which takes 2 numbers as input and returns the larger of the two"
9
+ input_ids = codegen_tkn(intent, return_tensors="pt").input_ids
10
 
11
+ gen_ids = mdl.generate(input_ids, max_length=128)
12
+ response = codegen_tkn.decode(gen_ids[0], skip_special_tokens=True)
13
+ return response
14
 
15
+ output=grad.Textbox(lines=1, label="Generated Python Code", placeholder="")
16
+ inp=grad.Textbox(lines=1, label="Place your intent here")
17
+ grad.Interface(codegen, inputs=inp, outputs=output).launch()