Spaces:
Sleeping
Sleeping
Create caption_refiner.py
Browse files- caption_refiner.py +18 -0
caption_refiner.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
| 2 |
+
|
| 3 |
+
# Load the T5 model and tokenizer
|
| 4 |
+
tokenizer = T5Tokenizer.from_pretrained("t5-base")
|
| 5 |
+
model = T5ForConditionalGeneration.from_pretrained("t5-base")
|
| 6 |
+
|
| 7 |
+
def refine_caption(initial_caption):
|
| 8 |
+
# Prepare the input for the T5 model
|
| 9 |
+
input_text = f"refine caption: {initial_caption}"
|
| 10 |
+
input_ids = tokenizer(input_text, return_tensors="pt").input_ids
|
| 11 |
+
|
| 12 |
+
# Generate the refined caption
|
| 13 |
+
outputs = model.generate(input_ids, max_new_tokens=100, num_return_sequences=1)
|
| 14 |
+
|
| 15 |
+
# Decode the refined caption
|
| 16 |
+
refined_caption = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 17 |
+
|
| 18 |
+
return refined_caption
|