Abubakar Abid commited on
Commit
ba5d287
1 Parent(s): 9b0a211

Create app.py

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