File size: 1,263 Bytes
1c1883c
 
 
 
f02cd6e
1c1883c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f02cd6e
1c1883c
 
f02cd6e
1c1883c
f02cd6e
1c1883c
 
 
 
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
import gradio as gr 
from transformers import Conversation, pipeline
from typing import Union

chatbot = pipeline("conversational", model="facebook/blenderbot-400M-distill")

def process(prompt:Union[str,None]=None,history:Union[str,None]=None):
    """" process prompt
    history has the following format 
    ```
    ****user \t hello
    ****assistant \t hi
    ****user \t how are you
    ****assistant \t good
    """
    conv = Conversation()
    if history != None : 
        for hist in history.split("****") : 
            if len(hist.strip()) == 0 : continue
            entry = hist.split("\t")
            conv.add_message({"role": entry[0].strip(), "content": entry[1].strip()})
    if prompt is not None or prompt != "":
        conv.add_message({"role": "user", "content": prompt})
    return chatbot(conv).messages[-1]["content"]


prompt = gr.Textbox(label="Prompt")
history = gr.Textbox(label="History")
out = gr.Textbox(label="Response")
examples = [
    ["",""" ****user \t hello \n ***assistant \t hi \n ****user \t how are you"""],
    ["what's 2+10",""" ****user \t hi \n ***assistant \t hello \n"""]
    ]

iface = gr.Interface(fn=process,inputs=[prompt,history],outputs=[out],examples=examples,cache_examples=True)
iface.launch()