OrcaMistral7B / app.py
Tonic's picture
Update app.py
9b8ebfb
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()