Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,27 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from transformers import BartTokenizer, BartForConditionalGeneration
|
| 3 |
-
|
| 4 |
-
# Load your model and tokenizer from the Hugging Face Hub
|
| 5 |
-
model_name = "bart_headline_model" # Replace with your uploaded model name
|
| 6 |
-
tokenizer = BartTokenizer.from_pretrained(model_name)
|
| 7 |
-
model = BartForConditionalGeneration.from_pretrained(model_name)
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import BartTokenizer, BartForConditionalGeneration
|
| 3 |
+
|
| 4 |
+
# Load your model and tokenizer from the Hugging Face Hub
|
| 5 |
+
model_name = "bart_headline_model" # Replace with your uploaded model name
|
| 6 |
+
tokenizer = BartTokenizer.from_pretrained(model_name)
|
| 7 |
+
#model = BartForConditionalGeneration.from_pretrained(model_name)
|
| 8 |
+
model = BartForConditionalGeneration.from_pretrained(model_name, device_map="cpu")
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
# Define the function for generating captions
|
| 12 |
+
def generate_caption(article):
|
| 13 |
+
inputs = tokenizer(article, return_tensors="pt", max_length=128, truncation=True)
|
| 14 |
+
outputs = model.generate(inputs["input_ids"], max_length=20, num_beams=5, early_stopping=True)
|
| 15 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 16 |
+
|
| 17 |
+
# Create the Gradio interface
|
| 18 |
+
interface = gr.Interface(
|
| 19 |
+
fn=generate_caption,
|
| 20 |
+
inputs="text",
|
| 21 |
+
outputs="text",
|
| 22 |
+
title="Headline Generator",
|
| 23 |
+
description="Enter an article to generate a headline.",
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# Launch the app
|
| 27 |
+
interface.launch()
|