gulabpatel commited on
Commit
57c760a
1 Parent(s): 5eeb191

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import GPTNeoForCausalLM, GPT2Tokenizer
2
+ import gradio as gr
3
+ model = GPTNeoForCausalLM.from_pretrained("EleutherAI/gpt-neo-125M")
4
+ tokenizer = GPT2Tokenizer.from_pretrained("EleutherAI/gpt-neo-125M")
5
+ prompt = """This is a discussion between a person and an entrepreneur.
6
+ person: What is your name?
7
+ entrepreneur: Mr. Patel
8
+ person: Where are you working?
9
+ entrepreneur: It's like one of these fancy adjustable height desk
10
+ person: What will you work on?
11
+ entrepreneur: The international development hackathon
12
+ person: What are you working on?
13
+ entrepreneur: Developping an classification web app
14
+ person: """
15
+ def my_split(s, seps):
16
+ res = [s]
17
+ for sep in seps:
18
+ s, res = res, []
19
+ for seq in s:
20
+ res += seq.split(sep)
21
+ return res
22
+ # input = "Who are you?"
23
+ def chat_base(input):
24
+ p = prompt + input
25
+ input_ids = tokenizer(p, return_tensors="pt").input_ids
26
+ gen_tokens = model.generate(input_ids, do_sample=True, temperature=0.7, max_length=150,)
27
+ gen_text = tokenizer.batch_decode(gen_tokens)[0]
28
+ # print(gen_text)
29
+ result = gen_text[len(p):]
30
+ # print(">", result)
31
+ result = my_split(result, [']', '\n'])[1]
32
+ # print(">>", result)
33
+ result = result[14:]
34
+ # print(">>>", result)
35
+ return result
36
+
37
+ import gradio as gr
38
+ def chat(message):
39
+ history = gr.get_state() or []
40
+ print(history)
41
+ response = chat_base(message)
42
+ history.append((message, response))
43
+ gr.set_state(history)
44
+ html = "<div class='chatbot'>"
45
+ for user_msg, resp_msg in history:
46
+ html += f"<div class='user_msg'>{user_msg}</div>"
47
+ html += f"<div class='resp_msg'>{resp_msg}</div>"
48
+ html += "</div>"
49
+ return html
50
+ iface = gr.Interface(chat, gr.inputs.Textbox(label="Ask Hassan a Question"), "html", css="""
51
+ .chatbox {display:flex;flex-direction:column}
52
+ .user_msg, .resp_msg {padding:4px;margin-bottom:4px;border-radius:4px;width:80%}
53
+ .user_msg {background-color:cornflowerblue;color:white;align-self:start}
54
+ .resp_msg {background-color:lightgray;align-self:self-end}
55
+ """, allow_screenshot=False, allow_flagging=False)
56
+ iface.launch()