dominguezdaniel commited on
Commit
8fac661
·
verified ·
1 Parent(s): f564393

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -19
app.py CHANGED
@@ -1,27 +1,20 @@
1
  import gradio as gr
2
- from transformers import AutoModelForCausalLM, AutoTokenizer, AutoFeatureExtractor
3
- import torch
4
- from PIL import Image
5
- import requests
6
 
7
- # Load the tokenizer, model, and feature extractor
8
- model_name = "Salesforce/BLIP-image-captioning-base"
9
- tokenizer = AutoTokenizer.from_pretrained(model_name)
10
- model = AutoModelForCausalLM.from_pretrained(model_name)
11
- feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)
12
 
13
- def generate_caption(image):
14
- inputs = feature_extractor(images=image, return_tensors="pt")
15
- outputs = model.generate(**inputs, max_length=128, num_beams=4, return_dict_in_generate=True)
16
- caption = tokenizer.decode(outputs.sequences[0], skip_special_tokens=True)
17
- return caption
18
 
19
- # Create the Gradio interface
20
- interface = gr.Interface(fn=generate_caption,
21
- inputs=gr.inputs.Image(type="pil"),
22
  outputs="text",
23
- title="Image Captioning with BLIP",
24
- description="Upload an image to generate a caption.")
25
 
26
  if __name__ == "__main__":
27
  interface.launch()
 
1
  import gradio as gr
2
+ from transformers import BartTokenizer, BartForConditionalGeneration
 
 
 
3
 
4
+ model_name = "facebook/bart-large-cnn" # Example BART model
5
+ tokenizer = BartTokenizer.from_pretrained(model_name)
6
+ model = BartForConditionalGeneration.from_pretrained(model_name)
 
 
7
 
8
+ def generate_text(prompt):
9
+ inputs = tokenizer.encode("summarize: " + prompt, return_tensors="pt", max_length=1024, truncation=True)
10
+ summary_ids = model.generate(inputs, max_length=150, min_length=40, length_penalty=2.0, num_beams=4, early_stopping=True)
11
+ return tokenizer.decode(summary_ids[0], skip_special_tokens=True)
 
12
 
13
+ interface = gr.Interface(fn=generate_text,
14
+ inputs=gr.inputs.Textbox(lines=5, placeholder="Enter Text Here..."),
 
15
  outputs="text",
16
+ title="Text Generation with BART",
17
+ description="Enter text to generate a summary.")
18
 
19
  if __name__ == "__main__":
20
  interface.launch()