Spaces:
Sleeping
Sleeping
import gradio as gr | |
import torch | |
from transformers import AutoModelForCausalLM, AutoTokenizer | |
# Load the tokenizer and model | |
model_id = "meta-llama/Llama-3.2-3B-Instruct" | |
tokenizer = AutoTokenizer.from_pretrained(model_id) | |
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.bfloat16, device_map="auto") | |
# Define the prediction function | |
def generate_text(prompt): | |
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=131072) | |
with torch.no_grad(): | |
outputs = model.generate(**inputs, max_length=131072) | |
return tokenizer.decode(outputs[0], skip_special_tokens=True) | |
# Create the Gradio interface | |
interface = gr.Interface( | |
fn=generate_text, | |
inputs=gr.Textbox(lines=10, label="Input Prompt"), | |
outputs=gr.Textbox(lines=10, label="Generated Text"), | |
title="Meta Llama 3.2 3B Instruct Model", | |
description="Generate text using the Meta Llama 3.2 3B Instruct model with a context length of up to 128,000 tokens." | |
) | |
if __name__ == "__main__": | |
interface.launch() | |