import gradio as gr from transformers import AutoTokenizer, AutoModelForCausalLM from huggingface_hub import login import os # Retrieve the Hugging Face token from the Space secrets token = os.getenv("HF_TOKEN") # Log in using the token login(token=token) # Load model and tokenizer model_name = "openai-community/gpt2" #"meta-llama/Llama-3.2-3B" # Replace with the correct model name if necessary tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForCausalLM.from_pretrained(model_name) # Define inference function def generate_text(input_text): inputs = tokenizer(input_text, return_tensors="pt") outputs = model.generate(inputs["input_ids"], max_length=50, num_return_sequences=1) response = tokenizer.decode(outputs[0], skip_special_tokens=True) return response # Create Gradio interface iface = gr.Interface(fn=generate_text, inputs="text", outputs="text") # Launch the interface iface.launch()