acecalisto3 commited on
Commit
208c5b6
1 Parent(s): 8058749

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -0
app.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import torchvision.transforms as T
4
+ from torchvision.models.detection import maskrcnn_resnet50_fpn
5
+ from transformers import RagTokenizer, RagRetriever, RagSequenceForGeneration
6
+ from google_drive_downloader import GoogleDriveDownloader as gdd
7
+
8
+ # Download and load the RAG model and tokenizer
9
+ gdd.download_file_from_google_drive(file_id='your_model_file_id', dest_path='./model.pt')
10
+ gdd.download_file_from_google_drive(file_id='your_tokenizer_file_id', dest_path='./tokenizer')
11
+
12
+ tokenizer = RagTokenizer.from_pretrained('./tokenizer')
13
+ retriever = RagRetriever.from_pretrained('./model.pt')
14
+ model = RagSequenceForGeneration.from_pretrained('./model.pt')
15
+
16
+ # Load the Mask R-CNN model
17
+ model_rcnn = maskrcnn_resnet50_fpn(pretrained=True)
18
+ model_rcnn.eval()
19
+
20
+ # Define the class labels for COCO dataset
21
+ class_labels = [
22
+ '__background__', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus',
23
+ 'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'N/A', 'stop sign',
24
+ 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow',
25
+ 'elephant', 'bear', 'zebra', 'giraffe', 'N/A', 'backpack', 'umbrella', 'N/A',
26
+ 'N/A', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball',
27
+ 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket',
28
+ 'bottle', 'N/A', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl',
29
+ 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza',
30
+ 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'N/A', 'dining table',
31
+ 'N/A', 'N/A', 'toilet', 'N/A', 'tv', 'laptop', 'mouse', 'remote', 'keyboard',
32
+ 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'N/A', 'book',
33
+ 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush'
34
+ ]
35
+
36
+ # Define the image-to-text object segmentation function
37
+ def image_to_text_segmentation(image):
38
+ # Convert the image to the expected format (RGB and tensor)
39
+ image = T.ToTensor()(image)
40
+ image = image.unsqueeze(0)
41
+
42
+ # Run the image through the Mask R-CNN model
43
+ with torch.no_grad():
44
+ predictions = model_rcnn(image)
45
+
46
+ # Extract the bounding boxes, labels, and masks from the predictions
47
+ boxes = predictions[0]['boxes'].tolist()
48
+ labels = [class_labels[i] for i in predictions[0]['labels'].tolist()]
49
+ masks = predictions[0]['masks'].squeeze().detach().cpu().numpy()
50
+
51
+ # Generate the segmented text for each object
52
+ segmented_text = []
53
+ for i in range(len(boxes)):
54
+ mask = masks[i]
55
+ object_text = ""
56
+ for j in range(mask.shape[0]):
57
+ for k in range(mask.shape[1]):
58
+ if mask[j][k]:
59
+ object_text += labels[i] + " "
60
+ segmented_text.append(object_text.strip())
61
+
62
+ return segmented_text
63
+
64
+ # Define the Gradio interface
65
+ input_image = gr.inputs.Image(label="Input Image")
66
+ input_text = gr.inputs.Textbox(label="Question")
67
+ output_text = gr.outputs.Textbox(label="Generated Text")
68
+
69
+ title = "RAG Text Generation and Object Segmentation"
70
+ description = "Generate text based on the given question using RAG model and perform object segmentation on the input image."
71
+
72
+ gr.Interface(
73
+ fn=generate_text,
74
+ inputs=input_text,
75
+ outputs=output_text,
76
+ title=title,
77
+ description=description,
78
+ examples=[
79
+ ["What is the capital of France?"],
80
+ ["Who is the president of the United States?"],
81
+ ]
82
+ ).launch()
83
+
84
+ gr.Interface(
85
+ fn=image_to_text_segmentation,
86
+ inputs=input_image,
87
+ outputs=output_text,
88
+ title="Image-to-Text Object Segmentation",
89
+ description="Segment objects in the image and generate corresponding text.",
90
+ ).launch()