leoxiaobin haipingwu commited on
Commit
e4e3ff8
1 Parent(s): 5117f5a

update_readme_example (#3)

Browse files

- update readme example (5ce8030ab7f9b5c0d7c8b28f65ec68aafc818235)


Co-authored-by: Haiping Wu <haipingwu@users.noreply.huggingface.co>

Files changed (1) hide show
  1. README.md +15 -15
README.md CHANGED
@@ -85,11 +85,11 @@ processor = AutoProcessor.from_pretrained("microsoft/Florence-2-base-ft", trust_
85
  url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg?download=true"
86
  image = Image.open(requests.get(url, stream=True).raw)
87
 
88
- def run_example(prompt, text_input=None):
89
-
90
- if text_input is not None:
91
- prompt = prompt + text_input
92
-
93
  inputs = processor(text=prompt, images=image, return_tensors="pt")
94
  generated_ids = model.generate(
95
  input_ids=inputs["input_ids"],
@@ -99,7 +99,7 @@ def run_example(prompt, text_input=None):
99
  )
100
  generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
101
 
102
- parsed_answer = processor.post_process_generation(generated_text, task="<OD>", image_size=(image.width, image.height))
103
 
104
  print(parsed_answer)
105
  ```
@@ -113,7 +113,7 @@ Here are the tasks `Florence-2` could perform:
113
  ### OCR
114
 
115
  ```python
116
- prompt = <OCR>
117
  run_example(prompt)
118
  ```
119
 
@@ -121,25 +121,25 @@ run_example(prompt)
121
  OCR with region output format:
122
  {'\<OCR_WITH_REGION>': {'quad_boxes': [[x1, y1, x2, y2, x3, y3, x4, y4], ...], 'labels': ['text1', ...]}}
123
  ```python
124
- prompt = <OCR_WITH_REGION>
125
  run_example(prompt)
126
  ```
127
 
128
  ### Caption
129
  ```python
130
- prompt = <CAPTION>
131
  run_example(prompt)
132
  ```
133
 
134
  ### Detailed Caption
135
  ```python
136
- prompt = <DETAILED_CAPTION>
137
  run_example(prompt)
138
  ```
139
 
140
  ### More Detailed Caption
141
  ```python
142
- prompt = <MORE_DETAILED_CAPTION>
143
  run_example(prompt)
144
  ```
145
 
@@ -150,7 +150,7 @@ OD results format:
150
  'labels': ['label1', 'label2', ...]} }
151
 
152
  ```python
153
- prompt = <OD>
154
  run_example(prompt)
155
  ```
156
 
@@ -159,7 +159,7 @@ Dense region caption results format:
159
  {'\<DENSE_REGION_CAPTION>' : {'bboxes': [[x1, y1, x2, y2], ...],
160
  'labels': ['label1', 'label2', ...]} }
161
  ```python
162
- prompt = <DENSE_REGION_CAPTION>
163
  run_example(prompt)
164
  ```
165
 
@@ -168,7 +168,7 @@ Dense region caption results format:
168
  {'\<REGION_PROPOSAL>': {'bboxes': [[x1, y1, x2, y2], ...],
169
  'labels': ['', '', ...]}}
170
  ```python
171
- prompt = <REGION_PROPOSAL>
172
  run_example(prompt)
173
  ```
174
 
@@ -178,7 +178,7 @@ caption to phrase grounding task requires additional text input, i.e. caption.
178
  Caption to phrase grounding results format:
179
  {'\<CAPTION_TO_PHRASE_GROUNDING>': {'bboxes': [[x1, y1, x2, y2], ...], 'labels': ['', '', ...]}}
180
  ```python
181
- task_prompt = '<CAPTION_TO_PHRASE_GROUNDING>'
182
  results = run_example(task_prompt, text_input="A green car parked in front of a yellow building.")
183
  ```
184
 
 
85
  url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg?download=true"
86
  image = Image.open(requests.get(url, stream=True).raw)
87
 
88
+ def run_example(task_prompt, text_input=None):
89
+ if text_input is None:
90
+ prompt = task_prompt
91
+ else:
92
+ prompt = task_prompt + text_input
93
  inputs = processor(text=prompt, images=image, return_tensors="pt")
94
  generated_ids = model.generate(
95
  input_ids=inputs["input_ids"],
 
99
  )
100
  generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
101
 
102
+ parsed_answer = processor.post_process_generation(generated_text, task=task_prompt, image_size=(image.width, image.height))
103
 
104
  print(parsed_answer)
105
  ```
 
113
  ### OCR
114
 
115
  ```python
116
+ prompt = "<OCR>"
117
  run_example(prompt)
118
  ```
119
 
 
121
  OCR with region output format:
122
  {'\<OCR_WITH_REGION>': {'quad_boxes': [[x1, y1, x2, y2, x3, y3, x4, y4], ...], 'labels': ['text1', ...]}}
123
  ```python
124
+ prompt = "<OCR_WITH_REGION>"
125
  run_example(prompt)
126
  ```
127
 
128
  ### Caption
129
  ```python
130
+ prompt = "<CAPTION>"
131
  run_example(prompt)
132
  ```
133
 
134
  ### Detailed Caption
135
  ```python
136
+ prompt = "<DETAILED_CAPTION>"
137
  run_example(prompt)
138
  ```
139
 
140
  ### More Detailed Caption
141
  ```python
142
+ prompt = "<MORE_DETAILED_CAPTION>"
143
  run_example(prompt)
144
  ```
145
 
 
150
  'labels': ['label1', 'label2', ...]} }
151
 
152
  ```python
153
+ prompt = "<OD>"
154
  run_example(prompt)
155
  ```
156
 
 
159
  {'\<DENSE_REGION_CAPTION>' : {'bboxes': [[x1, y1, x2, y2], ...],
160
  'labels': ['label1', 'label2', ...]} }
161
  ```python
162
+ prompt = "<DENSE_REGION_CAPTION>"
163
  run_example(prompt)
164
  ```
165
 
 
168
  {'\<REGION_PROPOSAL>': {'bboxes': [[x1, y1, x2, y2], ...],
169
  'labels': ['', '', ...]}}
170
  ```python
171
+ prompt = "<REGION_PROPOSAL>"
172
  run_example(prompt)
173
  ```
174
 
 
178
  Caption to phrase grounding results format:
179
  {'\<CAPTION_TO_PHRASE_GROUNDING>': {'bboxes': [[x1, y1, x2, y2], ...], 'labels': ['', '', ...]}}
180
  ```python
181
+ task_prompt = '<CAPTION_TO_PHRASE_GROUNDING>"
182
  results = run_example(task_prompt, text_input="A green car parked in front of a yellow building.")
183
  ```
184