Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,22 @@
|
|
| 1 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 2 |
import gradio as gr
|
| 3 |
-
import
|
| 4 |
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
| 13 |
|
|
|
|
| 14 |
|
| 15 |
-
def predict(input, history=[]):
|
| 16 |
-
# tokenize the new input sentence
|
| 17 |
-
new_user_input_ids = tokenizer.encode(
|
| 18 |
-
input + tokenizer.eos_token, return_tensors="pt"
|
| 19 |
-
)
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
# generate a response
|
| 25 |
-
history = model.generate(
|
| 26 |
-
bot_input_ids, max_length=4000, pad_token_id=tokenizer.eos_token_id
|
| 27 |
-
).tolist()
|
| 28 |
-
|
| 29 |
-
# convert the tokens to text, and then split the responses into lines
|
| 30 |
-
response = tokenizer.decode(history[0]).split("<|endoftext|>")
|
| 31 |
-
# print('decoded_response-->>'+str(response))
|
| 32 |
-
response = [
|
| 33 |
-
(response[i], response[i + 1]) for i in range(0, len(response) - 1, 2)
|
| 34 |
-
] # convert to tuples of list
|
| 35 |
-
# print('response-->>'+str(response))
|
| 36 |
-
return response, history
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
gr.Interface(
|
| 40 |
-
fn=predict,
|
| 41 |
-
title=title,
|
| 42 |
-
description=description,
|
| 43 |
-
examples=examples,
|
| 44 |
-
inputs=["text", "state"],
|
| 45 |
-
outputs=["chatbot", "state"]
|
| 46 |
-
).launch()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import Conversation, pipeline
|
| 3 |
|
| 4 |
+
message_list = []
|
| 5 |
+
response_list = []
|
| 6 |
|
| 7 |
+
# Initialize the chatbot pipeline
|
| 8 |
+
chatbot = pipeline(model="facebook/blenderbot-400M-distill",
|
| 9 |
+
task="conversational")
|
| 10 |
|
| 11 |
|
| 12 |
+
def chat_with_bot(message, history):
|
| 13 |
+
conversation = Conversation(
|
| 14 |
+
text=message, past_user_inputs=message_list, generated_responses=response_list)
|
| 15 |
+
conversation = chatbot(conversation)
|
| 16 |
|
| 17 |
+
return conversation.generated_responses[-1]
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
iface = gr.ChatInterface(
|
| 21 |
+
chat_with_bot, title="My Conversational AI", description="A chatbot powered by Hugging Face Transformers!")
|
| 22 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|