malavika-2016 commited on
Commit
e28e542
·
verified ·
1 Parent(s): ee58f76

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -9
app.py CHANGED
@@ -13,25 +13,26 @@ device = "cuda" if torch.cuda.is_available() else "cpu"
13
  model.to(device)
14
 
15
  # Define function for image captioning
16
- def analyze_scene(image):
17
  try:
18
  # Preprocess the image
19
  processed_image = processor(images=image, return_tensors="pt").pixel_values.to(device)
20
  with torch.no_grad():
21
  output = model.generate(processed_image)
22
  caption = processor.decode(output[0], skip_special_tokens=True)
23
- return caption
24
  except Exception as e:
25
- return f"Error: {str(e)}"
26
 
27
- # Create Gradio Interface
28
  interface = gr.Interface(
29
- fn=analyze_scene,
30
  inputs=gr.Image(type="pil"),
31
- outputs="text",
32
- title="BLIP Image Captioning",
33
- description="Upload an image to get a caption generated by BLIP."
34
  )
35
 
36
  if __name__ == "__main__":
37
- interface.launch()
 
 
13
  model.to(device)
14
 
15
  # Define function for image captioning
16
+ def analyze_scene_api(image):
17
  try:
18
  # Preprocess the image
19
  processed_image = processor(images=image, return_tensors="pt").pixel_values.to(device)
20
  with torch.no_grad():
21
  output = model.generate(processed_image)
22
  caption = processor.decode(output[0], skip_special_tokens=True)
23
+ return {"caption": caption}
24
  except Exception as e:
25
+ return {"error": str(e)}
26
 
27
+ # Create Gradio Interface with API mode
28
  interface = gr.Interface(
29
+ fn=analyze_scene_api,
30
  inputs=gr.Image(type="pil"),
31
+ outputs="json",
32
+ title="BLIP API for Image Captioning",
33
+ description="Send an image to get a caption response in JSON format."
34
  )
35
 
36
  if __name__ == "__main__":
37
+ # Launch Gradio interface in API mode
38
+ interface.launch(server_name="0.0.0.0", server_port=7860, enable_queue=True)