Spaces:
Runtime error
Runtime error
File size: 2,232 Bytes
5acdf7e 0cc628a 700caa0 5acdf7e 700caa0 7339879 700caa0 7339879 700caa0 |
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 |
from transformers import AutoModelForCausalLM, AutoTokenizer
import gradio as gr
import torch
# Create the ChatBot class
class ChatBot:
def __init__(self):
# Initialize the history
self.history = []
# Initialize the tokenizer with SentencePiece support
self.tokenizer = AutoTokenizer.from_pretrained("Open-Orca/Mistral-7B-OpenOrca", use_fast=True)
# Initialize the model
self.model = AutoModelForCausalLM.from_pretrained("Open-Orca/Mistral-7B-OpenOrca")
def predict(self, input):
# Encode user input
new_user_input_ids = self.tokenizer.encode(input + self.tokenizer.eos_token, return_tensors="pt")
# Flatten the conversation history
flat_history = [item for sublist in self.history for item in sublist]
flat_history_tensor = torch.tensor(flat_history).unsqueeze(dim=0)
# Create the input tensor for the model
bot_input_ids = torch.cat([flat_history_tensor, new_user_input_ids], dim=-1) if self.history else new_user_input_ids
# Generate a response from the model
chat_history_ids = self.model.generate(bot_input_ids, max_length=2000, pad_token_id=self.tokenizer.eos_token_id)
# Update the history with the generated response
self.history.append(chat_history_ids[:, bot_input_ids.shape[-1]:].tolist()[0])
# Decode the response
response = self.tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
return response
# Example usage
chatbot = ChatBot()
user_input = "Hello, chatbot!"
response = chatbot.predict(user_input)
print(response)
title = "👋🏻Welcome to Tonic's EZ Chat🚀"
description = "You can use this Space to test out the current model (DialoGPT-medium) or duplicate this Space and use it for any other model on 🤗HuggingFace. Join me on [Discord](https://discord.gg/fpEPNZGsbt) to build together."
examples = [["How are you?"]]
iface = gr.Interface(
fn=bot.predict,
title=title,
description=description,
examples=examples,
inputs="text",
outputs="text",
theme="ParityError/Anime"
)
iface.launch()
|