Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,60 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
-
def
|
| 8 |
-
|
| 9 |
-
|
| 10 |
|
| 11 |
-
def
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
with gr.Blocks() as demo:
|
| 17 |
-
chatbot = gr.Chatbot(
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
| 33 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
demo.launch()
|
|
|
|
| 1 |
+
from transformers import AutoModel, AutoTokenizer
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
+
tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True)
|
| 5 |
+
model = AutoModel.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True)
|
| 6 |
+
#.half().cuda()
|
| 7 |
+
model = model.eval()
|
| 8 |
|
| 9 |
+
# def add_text(history, text):
|
| 10 |
+
# history = history + [(text, None)]
|
| 11 |
+
# return history, ""
|
| 12 |
|
| 13 |
+
# def add_file(history, file):
|
| 14 |
+
# history = history + [((file.name,), None)]
|
| 15 |
+
# return history
|
| 16 |
+
|
| 17 |
+
# def bot(history):
|
| 18 |
+
# response = "**That's cool!**"
|
| 19 |
+
# history[-1][1] = response
|
| 20 |
+
# return history
|
| 21 |
+
|
| 22 |
+
def predict(input, history=None):
|
| 23 |
+
if history is None:
|
| 24 |
+
history = []
|
| 25 |
+
response, history = model.chat(tokenizer, input, history)
|
| 26 |
+
return history, history
|
| 27 |
|
| 28 |
with gr.Blocks() as demo:
|
| 29 |
+
chatbot = gr.Chatbot()
|
| 30 |
+
msg = gr.Textbox()
|
| 31 |
+
clear = gr.Button("Clear")
|
| 32 |
+
|
| 33 |
+
def user(user_message, history):
|
| 34 |
+
return "", history + [[user_message, None]]
|
| 35 |
+
|
| 36 |
+
def bot(msg, history):
|
| 37 |
+
# bot_message = random.choice(["How are you?", "I love you", "I'm very hungry"])
|
| 38 |
+
bot_message = predict(msg, history)
|
| 39 |
+
history[-1][1] = ""
|
| 40 |
+
for character in bot_message:
|
| 41 |
+
history[-1][1] += character
|
| 42 |
+
time.sleep(0.05)
|
| 43 |
+
yield history
|
| 44 |
+
|
| 45 |
+
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 46 |
+
bot, [msg, chatbot], [msg, chatbot]
|
| 47 |
)
|
| 48 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 49 |
+
|
| 50 |
+
demo.queue()
|
| 51 |
+
demo.launch()
|
| 52 |
+
|
| 53 |
+
# txt.submit(predict, [txt, state], [chatbot, state])
|
| 54 |
+
# button.click(predict, [txt, state], [chatbot, state])
|
| 55 |
+
|
| 56 |
+
# btn.upload(add_file, [chatbot, btn], [chatbot]).then(
|
| 57 |
+
# bot, chatbot, chatbot
|
| 58 |
+
# )
|
| 59 |
|
| 60 |
demo.launch()
|