dlaima commited on
Commit
46f4a73
·
verified ·
1 Parent(s): 800fd2d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -19
app.py CHANGED
@@ -7,26 +7,27 @@ import os
7
  INFERENCE_ENDPOINT = "https://your-endpoint-url" # Replace with your endpoint URL
8
  API_TOKEN = "your-api-token" # Replace with your Hugging Face API token
9
 
10
- def generate_caption(image):
11
- """
12
- Sends an image to the Hugging Face Inference Endpoint for caption generation.
13
- :param image: An image in PIL format.
14
- :return: Generated caption or error message.
15
- """
16
- headers = {"Authorization": f"Bearer {API_TOKEN}"}
17
- files = {"inputs": image}
18
- response = requests.post(INFERENCE_ENDPOINT, headers=headers, files=files)
19
-
20
- if response.status_code == 200:
21
- return response.json().get("generated_text", "No caption generated.")
22
- else:
23
- return f"Error: {response.status_code} - {response.text}"
 
 
24
 
25
  def load_image(img_url):
26
  image = Image.open(requests.get(img_url, stream=True).raw).convert('RGB')
27
  return image
28
 
29
-
30
  #Open the images
31
  #Image1=Image.open('dlaima/Multiple_Image_captioning/main/image1.jpg')
32
  #Image2=Image.open('https://huggingface.co/spaces/dlaima/Multiple_Image_captioning/resolve/main/image2.jpeg')
@@ -35,14 +36,27 @@ def load_image(img_url):
35
  image_url = 'https://free-images.com/lg/9e46/white_bengal_tiger_tiger_0.jpg'
36
  image = load_image(image_url)
37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  # Gradio interface
39
 
40
  demo = gr.Interface(
41
- fn=generate_caption,
42
- inputs=gr.Image(type="pil", label="Upload Image"),
43
- outputs=gr.Textbox(label="Generated Caption"),
44
  #examples=[Image1, Image2, Image3],
45
- examples=[image],
46
  title="Image Captioning App",
47
  description=(
48
  "Upload an image or use one of the predefined samples to generate a caption. "
 
7
  INFERENCE_ENDPOINT = "https://your-endpoint-url" # Replace with your endpoint URL
8
  API_TOKEN = "your-api-token" # Replace with your Hugging Face API token
9
 
10
+ #Image-to-text endpoint
11
+ def get_completion(inputs, parameters=None, endpoint_url=endpoint_url):
12
+ headers = {
13
+ "Authorization": f"Bearer {API_TOKEN}",
14
+ "Content-Type": "application/json"
15
+ }
16
+ data = {"inputs": inputs}
17
+ if parameters is not None:
18
+ data.update({"parameters": parameters})
19
+ response = requests.post(INFERENCE_ENDPOINT, headers=headers, data=json.dumps(data))
20
+ return json.loads(response.content.decode("utf-8"))
21
+
22
+ def get_generation(model, processor, image, dtype):
23
+ inputs = processor(image, return_tensors="pt").to(dtype)
24
+ out = model.generate(**inputs)
25
+ return processor.decode(out[0], skip_special_tokens=True)
26
 
27
  def load_image(img_url):
28
  image = Image.open(requests.get(img_url, stream=True).raw).convert('RGB')
29
  return image
30
 
 
31
  #Open the images
32
  #Image1=Image.open('dlaima/Multiple_Image_captioning/main/image1.jpg')
33
  #Image2=Image.open('https://huggingface.co/spaces/dlaima/Multiple_Image_captioning/resolve/main/image2.jpeg')
 
36
  image_url = 'https://free-images.com/lg/9e46/white_bengal_tiger_tiger_0.jpg'
37
  image = load_image(image_url)
38
 
39
+
40
+ def caption_image(image_url):
41
+ # Download the image from the URL
42
+ response = requests.get(image_url)
43
+ response.raise_for_status() # Ensure the request was successful
44
+ image = Image.open(BytesIO(response.content)) # Load image with PIL
45
+
46
+ # Call your captioning function here (replace `get_completion` with the actual implementation)
47
+ #caption = get_completion(image)
48
+ caption = get_completion(image_url)
49
+ return caption
50
+
51
+
52
  # Gradio interface
53
 
54
  demo = gr.Interface(
55
+ fn=caption_image,
56
+ inputs=gr.Textbox(label="Image URL"), # Input as a URL
57
+ outputs="text",
58
  #examples=[Image1, Image2, Image3],
59
+ #examples=[image],
60
  title="Image Captioning App",
61
  description=(
62
  "Upload an image or use one of the predefined samples to generate a caption. "