File size: 945 Bytes
b3fecc1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()