I am building a chatbot on huggingface:"import subprocess import os import gradio as gr…
- User
I am building a chatbot on huggingface:"import subprocess import os import gradio as gr import torch from transformers import AutoModelForCausalLM, AutoTokenizer
Define constants and environment settings.
MAX_MAX_NEW_TOKENS = 2048 DEFAULT_MAX_NEW_TOKENS = 1024 MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "4096"))
Loading model and tokenizer for Llama-2
if torch.cuda.is_available(): model_id = "meta-llama/Llama-2-7b-chat-hf" model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map="auto") tokenizer = AutoTokenizer.from_pretrained(model_id) tokenizer.use_default_system_prompt = False
def generate( message: str, chat_history: list[tuple[str, str]], system_prompt: str, max_new_tokens: int = DEFAULT_MAX_NEW_TOKENS, temperature: float = 0.6, top_p: float = 0.9, top_k: int = 50, repetition_penalty: float = 1.2, ): prompt = "I am a powerful and optimized chat model designed to help with a wide range of topics and questions. Feel free to ask me anything!" chat_history = [(prompt, '')] + chat_history
codeconversation = [] for user, assistant in chat_history: conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}]) conversation.append({"role": "user", "content": message}) input_ids = tokenizer(conversation, return_tensors="pt", padding=True, truncation=True) input_ids = input_ids.to(model.device) # Generate the model’s response outputs = model.generate( input_ids=input_ids['input_ids'], max_new_tokens=max_new_tokens, do_sample=True, temperature=temperature, top_k=top_k, top_p=top_p, repetition_penalty=repetition_penalty, ) reply = tokenizer.decode(outputs[:, input_ids['input_ids'].shape[-1]:][0], skip_special_tokens=True) return replySet up the Gradio interface
title = "ChatSherman-2.0" description = "This chatbot is powered by Llama-2 7B and optimized to provide helpful responses across a wide range of topics." examples = [ ["What is ChatSherman, and how does it work?", []], ["Is my personal information and data safe when I use the ChatSherman chatbot?", []], ["What are some common applications of deep learning in engineering?", []] ]
iface = gr.Interface( fn=generate, inputs=[ gr.inputs.Textbox(lines=5, placeholder="Enter your message here…"), gr.inputs.JSON(label="Previous conversation history"), gr.inputs.Textbox(label="Custom System Prompt", default="I am a powerful and optimized chat model designed to help with a wide range of topics and questions. Feel free to ask me anything!"), ], outputs='text', title=title, description=description, examples=examples, )
- Assistant
It looks like you're encountering an error because the way Gradio components are imported has changed in more recent updates. This error message indicates that does not have an attribute named .
- User
- Assistant
- User
Show full code that should work
- Assistant
- User
- Assistant
- User
- Assistant