File size: 1,023 Bytes
043c5c1 8c07f2f 043c5c1 b6f27cc 043c5c1 8c07f2f 043c5c1 8c07f2f |
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 |
import os
from transformers import AutoModelForCausalLM, AutoTokenizer
import gradio as gr
# Get the Hugging Face token from the environment variable
hf_token = os.getenv("HUGGINGFACE_HUB_TOKEN")
# Model name
model_name = "meta-llama/Llama-3.2-3B-Instruct"
# Load the model and tokenizer with the token
tokenizer = AutoTokenizer.from_pretrained(model_name, use_auth_token=hf_token)
model = AutoModelForCausalLM.from_pretrained(model_name, use_auth_token=hf_token)
def predict(input_text):
# Tokenize input and generate text
inputs = tokenizer(input_text, return_tensors="pt")
outputs = model.generate(**inputs)
return tokenizer.decode(outputs[0], skip_special_tokens=True)
# Create a Gradio interface
interface = gr.Interface(
fn=predict,
inputs=gr.Textbox(label="Input Text"),
outputs=gr.Textbox(label="Generated Output"),
title="Meta-LLaMA-3.1-8B-Instruct",
description="Generate text using the meta-llama/Llama-3.1-8B-Instruct model."
)
# Launch the interface
interface.launch() |