Spaces:
Sleeping
Sleeping
File size: 1,531 Bytes
ade33fd 0e7a53c ade33fd 509ba0d ade33fd 4d25f4e f2d06de 4d25f4e f2d06de a0dcbc6 4d25f4e a0dcbc6 4d25f4e ade33fd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
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 = "nikunjcepatel/gpt_finetune_test" #"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=256, num_return_sequences=1)
outputs = model.generate(
inputs["input_ids"],
max_length=256, # Set max length for output
num_return_sequences=1,
temperature=0.2, # Control randomness (higher is more random)
top_k=50, # Top-k sampling to limit vocabulary to top 50 choices
top_p=0.8, # Nucleus sampling to choose tokens with 95% cumulative probability
repetition_penalty=1.2, # Penalize repetition; increase if repetitions persist
do_sample=True # Enable sampling for non-deterministic output
)
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()
|