jaothan commited on
Commit
cb16ad0
·
verified ·
1 Parent(s): 7e01dbe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -25
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
- # Define the function for generating captions
10
- def generate_caption(article):
11
- inputs = tokenizer(article, return_tensors="pt", max_length=128, truncation=True)
12
- outputs = model.generate(inputs["input_ids"], max_length=20, num_beams=5, early_stopping=True)
13
- return tokenizer.decode(outputs[0], skip_special_tokens=True)
14
-
15
- # Create the Gradio interface
16
- interface = gr.Interface(
17
- fn=generate_caption,
18
- inputs="text",
19
- outputs="text",
20
- title="Headline Generator",
21
- description="Enter an article to generate a headline.",
22
- )
23
-
24
- # Launch the app
25
- interface.launch()
 
 
 
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()