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