import gradio as gr from transformers import GPT2Tokenizer, GPT2LMHeadModel # Load the model and tokenizer model_name = "migueldeguzmandev/GPT2XL_RLLMv3-PTT-10" tokenizer = GPT2Tokenizer.from_pretrained(model_name) model = GPT2LMHeadModel.from_pretrained(model_name) # Set the pad token ID to the EOS token ID model.config.pad_token_id = model.config.eos_token_id # Define the inference function def generate_response(input_text, temperature): # Tokenize the input text inputs = tokenizer(input_text, return_tensors="pt") input_ids = inputs["input_ids"] attention_mask = inputs["attention_mask"] # Generate the model's response output = model.generate( input_ids, attention_mask=attention_mask, max_length=300, num_return_sequences=1, temperature=temperature, no_repeat_ngram_size=2, top_k=50, top_p=0.95, do_sample=True, # Set do_sample to True when using temperature ) response = tokenizer.decode(output[0], skip_special_tokens=True) start_index = len(input_text) answer_text = response[start_index:] stopping_phrase = "Thank you for your question, Glad to be of service." stop_index = answer_text.find(stopping_phrase) if stop_index != -1: # Include the stopping phrase in the final answer stop_index += len(stopping_phrase) answer_text = answer_text[:stop_index] return answer_text # Create the Gradio interface interface = gr.Interface( fn=generate_response, inputs=[ gr.Textbox(label="User Input"), gr.Slider(minimum=0.000000000000000000000000000000000001, maximum=1.0, value=0.7, step=0.1, label="Temperature"), ], outputs=gr.Textbox(label="Model Response"), title="Hello, I'm Aligned AI! - PPT-RLLMv3 ", description=( """ For testing purposes only, please note that there is a stop index/ text strip here, where I remove any token/response beyond the phrase "Thank you for your question, Glad to be of service." """ ), ) # Launch the interface without the share option interface.launch()