File size: 1,009 Bytes
cb16ad0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import BartTokenizer, BartForConditionalGeneration

# Load your model and tokenizer from the Hugging Face Hub
model_name = "bart_headline_model"  # Replace with your uploaded model name
tokenizer = BartTokenizer.from_pretrained(model_name)
#model = BartForConditionalGeneration.from_pretrained(model_name)
model = BartForConditionalGeneration.from_pretrained(model_name, device_map="cpu")


# Define the function for generating captions
def generate_caption(article):
    inputs = tokenizer(article, return_tensors="pt", max_length=128, truncation=True)
    outputs = model.generate(inputs["input_ids"], max_length=20, num_beams=5, early_stopping=True)
    return tokenizer.decode(outputs[0], skip_special_tokens=True)

# Create the Gradio interface
interface = gr.Interface(
    fn=generate_caption,
    inputs="text",
    outputs="text",
    title="Headline Generator",
    description="Enter an article to generate a headline.",
)

# Launch the app
interface.launch()