Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -40,6 +40,21 @@ model.eval()
|
|
| 40 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 41 |
model.to(device)
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
# FastAPI endpoint to handle image upload and forward it to Hugging Face API for caption generation
|
| 44 |
HUGGING_FACE_ENDPOINT = 'https://huggingface.co/spaces/Rammohan0504/DPR-4/predict'
|
| 45 |
|
|
@@ -262,8 +277,6 @@ iface = gr.Interface(
|
|
| 262 |
allow_flagging="never"
|
| 263 |
)
|
| 264 |
|
| 265 |
-
|
| 266 |
-
|
| 267 |
if __name__ == "__main__":
|
| 268 |
iface.launch()
|
| 269 |
|
|
|
|
| 40 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 41 |
model.to(device)
|
| 42 |
|
| 43 |
+
# Function to generate captions dynamically based on image content
|
| 44 |
+
def generate_captions_from_image(image):
|
| 45 |
+
if image.mode != "RGB":
|
| 46 |
+
image = image.convert("RGB")
|
| 47 |
+
|
| 48 |
+
# Resize image for faster processing
|
| 49 |
+
image = image.resize((640, 640))
|
| 50 |
+
|
| 51 |
+
# Preprocess the image and generate a caption
|
| 52 |
+
inputs = processor(image, return_tensors="pt").to(device, torch.float16)
|
| 53 |
+
output = model.generate(**inputs, max_new_tokens=50)
|
| 54 |
+
caption = processor.decode(output[0], skip_special_tokens=True)
|
| 55 |
+
|
| 56 |
+
return caption
|
| 57 |
+
|
| 58 |
# FastAPI endpoint to handle image upload and forward it to Hugging Face API for caption generation
|
| 59 |
HUGGING_FACE_ENDPOINT = 'https://huggingface.co/spaces/Rammohan0504/DPR-4/predict'
|
| 60 |
|
|
|
|
| 277 |
allow_flagging="never"
|
| 278 |
)
|
| 279 |
|
|
|
|
|
|
|
| 280 |
if __name__ == "__main__":
|
| 281 |
iface.launch()
|
| 282 |
|