first commit
Browse files- app.py +42 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import Conversation, pipeline
|
3 |
+
from typing import Union
|
4 |
+
|
5 |
+
chatbot = pipeline(model="facebook/blenderbot-400M-distill")
|
6 |
+
|
7 |
+
def process(prompt:Union[str,None]=None,history:Union[str,None]=None):
|
8 |
+
"""" process prompt
|
9 |
+
history has the following format
|
10 |
+
```
|
11 |
+
****user \t hello
|
12 |
+
****assistant \t hi
|
13 |
+
****user \t how are you
|
14 |
+
****assistant \t good
|
15 |
+
"""
|
16 |
+
conv = Conversation()
|
17 |
+
if history != None :
|
18 |
+
for hist in history.split("****") :
|
19 |
+
if len(hist.strip()) == 0 : continue
|
20 |
+
entry = hist.split("\t")
|
21 |
+
conv.add_message({"role": entry[0].strip(), "content": entry[1].strip()})
|
22 |
+
if prompt is not None or prompt != "":
|
23 |
+
conv.add_message({"role": "user", "content": prompt})
|
24 |
+
return chatbot(conv).messages[-1]["content"]
|
25 |
+
|
26 |
+
|
27 |
+
prompt = gr.Textbox(label="Prompt")
|
28 |
+
history = gr.Textbox(label="History")
|
29 |
+
out = gr.Textbox(label="Response")
|
30 |
+
examples = gr.Examples([
|
31 |
+
["",""" ****user \t hello \n ***assistant \t hi \n ****user \t how are you"""],
|
32 |
+
["what's 2+10",""" ****user \t hi \n ***assistant \t hello \n"""]
|
33 |
+
],
|
34 |
+
inputs=[prompt,history],
|
35 |
+
outputs=[out],
|
36 |
+
cache_examples=True)
|
37 |
+
|
38 |
+
iface = gr.Interface(fn=process,inputs=[prompt,history],outputs=[out])
|
39 |
+
iface.launch()
|
40 |
+
|
41 |
+
|
42 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
transformers
|