| import torch | |
| from transformers import AutoProcessor,AutoModelForCausalLM | |
| import gradio as gr | |
| device = 'cuda' if torch.cuda.is_available() else 'cpu' | |
| processor=AutoProcessor.from_pretrained("alibidaran/General_image_captioning") | |
| model=AutoModelForCausalLM.from_pretrained("alibidaran/General_image_captioning").to(device) | |
| def generate_caption(image,length): | |
| encoded=processor(images=image, return_tensors="pt").to(device) | |
| pixels=encoded['pixel_values'].to(device) | |
| with torch.no_grad(): | |
| generated_ids=model.generate(pixel_values=pixels,max_length=length) | |
| generated_caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0] | |
| return generated_caption | |
| demo=gr.Interface( | |
| generate_caption, | |
| [ | |
| gr.Image(type='pil',flagging_options=["blurry", "incorrect", "other"]), | |
| gr.Slider(10,50,value=10) | |
| ], | |
| 'label', | |
| theme=gr.themes.Soft(primary_hue='purple',secondary_hue=gr.themes.colors.gray) | |
| ) | |
| demo.launch(show_error=True) |