File size: 2,180 Bytes
3e530ed
 
 
 
 
 
 
 
 
bfeccc6
 
3e530ed
e4993f9
 
 
3e530ed
 
 
bfeccc6
3e530ed
bfeccc6
 
 
 
 
 
 
 
 
3e530ed
f9135e7
 
097e32d
8234400
 
 
 
bfeccc6
 
e7fe6f7
 
 
 
 
 
 
bfeccc6
 
 
 
 
 
 
e7fe6f7
 
 
 
 
 
 
 
bfeccc6
e7fe6f7
 
 
 
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
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python
# coding: utf-8

# In[ ]:


import os
import openai
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch

tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
model = AutoModelForCausalLM.from_pretrained("text-davinci-003")

openai.organization = "org-orRhfBkKOfOuNACbjPyWKbUt"
openai.api_key = "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"

def predict(input, history=[]):

    new_user_input_ids = tokenizer.encode(input + tokenizer.eos_token, return_tensors='pt')
    
    # tokenize the new input sentence
    new_user_input_ids = tokenizer.encode(input + tokenizer.eos_token, return_tensors='pt')

    # append the new user input tokens to the chat history
    bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)

    # generate a response 
    response = openai.Completion.create(
    model="text-davinci-003",
    #model="davinci:ft-placeholder:ai-dhd-2022-12-07-10-09-37",
    prompt= input,
    temperature=0.09,
    max_tokens=608,
    top_p=1,
    frequency_penalty=0,
    presence_penalty=0).tolist()

    # write some HTML
    html = "<div class='chatbot'>"
    for m, msg in enumerate(response):
        cls = "user" if m%2 == 0 else "bot"
        html += "<div class='msg {}'> {}</div>".format(cls, msg)
    html += "</div>"

    history = response[Completion]
    
    # convert the tokens to text, and then split the responses into lines
    response = tokenizer.decode(history[0]).split("<|endoftext|>")
    response = [(response[i], response[i+1]) for i in range(0, len(response)-1, 2)]  # convert to tuples of list
    return response, history

css = """
.chatbox {display:flex;flex-direction:column}
.msg {padding:4px;margin-bottom:4px;border-radius:4px;width:80%}
.msg.user {background-color:cornflowerblue;color:white}
.msg.bot {background-color:lightgray;align-self:self-end}
.footer {display:none !important}
"""

gr.Interface(fn=predict,
             theme="default",
             inputs=[gr.inputs.Textbox(placeholder="I'm AI-DHD - ask me anything!"), "state"],
             outputs=["html", "state"],
             css=css).launch()