Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
def generate_caption(image):
|
| 2 |
+
if not isinstance(image, Image.Image):
|
| 3 |
+
image = Image.fromarray(image)
|
| 4 |
+
|
| 5 |
+
task = "<CAPTION>" # เปลี่ยน task เป็น caption ธรรมดา
|
| 6 |
+
|
| 7 |
+
inputs = florence_processor(text=task, images=image, return_tensors="pt").to(device)
|
| 8 |
+
generated_ids = florence_model.generate(
|
| 9 |
+
input_ids=inputs["input_ids"],
|
| 10 |
+
pixel_values=inputs["pixel_values"],
|
| 11 |
+
max_new_tokens=1024,
|
| 12 |
+
early_stopping=False,
|
| 13 |
+
do_sample=False,
|
| 14 |
+
num_beams=3,
|
| 15 |
+
)
|
| 16 |
+
generated_text = florence_processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
|
| 17 |
+
parsed_answer = florence_processor.post_process_generation(
|
| 18 |
+
generated_text,
|
| 19 |
+
task=task,
|
| 20 |
+
image_size=(image.width, image.height)
|
| 21 |
+
)
|
| 22 |
+
prompt = parsed_answer[task]
|
| 23 |
+
print("\n\nGeneration completed!:" + prompt)
|
| 24 |
+
return prompt
|