Spaces:
Runtime error
Runtime error
File size: 1,137 Bytes
1352a28 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
import os
import requests
from dotenv import load_dotenv
load_dotenv()
def caption_from_url(image_url):
"""
Generates a caption for an image using the Azure Computer Vision API.
Parameters:
image_url (str): The URL of the image for which a caption should be generated.
Returns:
str: The generated caption for the image.
Raises:
requests.exceptions.HTTPError: If the request to the Azure API fails.
"""
subscription_key = os.getenv('AZURE_SUBSCRIPTION_KEY')
endpoint = 'https://icmvp.cognitiveservices.azure.com/'
analyze_url = endpoint + "computervision/imageanalysis:analyze?api-version=2023-10-01"
headers = {
"Content-Type": "application/json",
'Ocp-Apim-Subscription-Key': subscription_key
}
params = {
'features': 'caption'
}
data = {'url': image_url}
response = requests.post(analyze_url, headers=headers, params=params, json=data)
response.raise_for_status()
analysis = response.json()
# Extract the description from the returned JSON
description = analysis['captionResult']['text']
return description
|