import gradio as gr from transformers import AutoTokenizer, AutoModelForCausalLM import torch # Load the model and tokenizer outside the function to avoid # reloading it on every call. model_name = "gustavokuklinski/aeon-360m" tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForCausalLM.from_pretrained(model_name) def respond( message, history: list[dict[str, str]], system_message, max_tokens, temperature, top_p, ): messages = [{"role": "system", "content": system_message}] messages.extend(history) messages.append({"role": "user", "content": message}) # Apply the chat template to format the messages # The jinja template from the model card can be used input_ids = tokenizer.apply_chat_template( messages, add_generation_prompt=True, return_tensors="pt" ) # Generate the response outputs = model.generate( input_ids, max_new_tokens=max_tokens, temperature=temperature, top_p=top_p, ) # Decode the generated text and yield the response response = tokenizer.decode(outputs[0][input_ids.shape[-1]:], skip_special_tokens=True) yield response # The rest of your Gradio ChatInterface code remains the same chatbot = gr.ChatInterface( respond, type="messages", additional_inputs=[ gr.Textbox(value="Your name is Aeon. Answer the user's question concisely using **only** the provided CONTEXT. If the CONTEXT doesn't contain the answer, state: 'I don't know, can we search?'. Do not add any information not present in the CONTEXT. Your responses must be in plain, natural language. **Factual questions:** Use the provided CONTEXT to form a single, comprehensive answer. Do not use information outside the CONTEXT. If the CONTEXT does not contain the answer, state: 'I don't know about it. Can we search?'. **Conversational questions:** Respond naturally and conversationally, without using the CONTEXT. Never echo the user's question or the CONTEXT.", label="System message"), gr.Slider(minimum=1, maximum=2048, value=2048, step=1, label="Max new tokens"), gr.Slider(minimum=0.1, maximum=1.0, value=0.5, step=0.1, label="Temperature"), gr.Slider( minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)", ), ], ) if __name__ == "__main__": chatbot.launch()