Juho Inkinen commited on
Commit
5c3f18e
·
1 Parent(s): dea4bf5

Improved error handling and deduplication of image display etc.

Browse files
Files changed (1) hide show
  1. app.py +9 -8
app.py CHANGED
@@ -6,6 +6,8 @@ import os
6
 
7
  # Get VLM API base URL and API key from environment variables
8
  VLM_API_BASE_URL = os.getenv("VLM_API_BASE_URL")
 
 
9
  VLM_API_KEY = os.getenv("VLM_API_KEY", "")
10
  VLM_API_ENDPOINT = f"{VLM_API_BASE_URL}/v1/chat/completions"
11
 
@@ -48,7 +50,8 @@ def get_caption(image):
48
  # Assume caption is in data['choices'][0]['message']['content']
49
  caption = data["choices"][0]["message"]["content"]
50
  except Exception as e:
51
- caption = f"Error: {e}"
 
52
  return caption
53
 
54
 
@@ -58,14 +61,13 @@ PROJECT_ID = "yso-en" # Placeholder, update as needed
58
  def get_subjects(caption):
59
  try:
60
  results = annif.suggest(project_id=PROJECT_ID, text=caption)
61
- # Return a dict: {label: score, ...}
62
  label_scores = {result["label"]: result["score"] for result in results}
63
- # If no results, return a string error
64
  if not label_scores:
65
- return "No subjects found."
66
  return label_scores
67
  except Exception as e:
68
- return f"Error: {e}"
 
69
 
70
 
71
  def process_image(image):
@@ -75,12 +77,11 @@ def process_image(image):
75
 
76
 
77
  demo = gr.Interface(
78
- fn=process_image,
79
  inputs=gr.Image(type="pil", label="Upload or take a photo"),
80
  outputs=[
81
- gr.Image(type="pil", label="Input Image"),
82
  gr.Textbox(label="Caption"),
83
- gr.Label(label="Subject Suggestions"),
84
  ],
85
  title="VLM Caption & Annif Subject Demo",
86
  description="Upload or take a photo. The app generates a caption using a Visual Language Model and suggests subjects using Annif.",
 
6
 
7
  # Get VLM API base URL and API key from environment variables
8
  VLM_API_BASE_URL = os.getenv("VLM_API_BASE_URL")
9
+ if not VLM_API_BASE_URL:
10
+ raise RuntimeError("VLM_API_BASE_URL environment variable must be set.")
11
  VLM_API_KEY = os.getenv("VLM_API_KEY", "")
12
  VLM_API_ENDPOINT = f"{VLM_API_BASE_URL}/v1/chat/completions"
13
 
 
50
  # Assume caption is in data['choices'][0]['message']['content']
51
  caption = data["choices"][0]["message"]["content"]
52
  except Exception as e:
53
+ print(f"VLM API error: {e}") # Detailed error for admin
54
+ raise gr.Error("Sorry, there was a problem generating a caption.")
55
  return caption
56
 
57
 
 
61
  def get_subjects(caption):
62
  try:
63
  results = annif.suggest(project_id=PROJECT_ID, text=caption)
 
64
  label_scores = {result["label"]: result["score"] for result in results}
 
65
  if not label_scores:
66
+ return {}
67
  return label_scores
68
  except Exception as e:
69
+ print(f"Annif API error: {e}") # Detailed error for admin
70
+ raise gr.Error("Sorry, there was a problem getting subject suggestions.")
71
 
72
 
73
  def process_image(image):
 
77
 
78
 
79
  demo = gr.Interface(
80
+ fn=lambda image: process_image(image)[1:], # Only return caption and subjects
81
  inputs=gr.Image(type="pil", label="Upload or take a photo"),
82
  outputs=[
 
83
  gr.Textbox(label="Caption"),
84
+ gr.Label(label="Subject Suggestions", show_heading=False),
85
  ],
86
  title="VLM Caption & Annif Subject Demo",
87
  description="Upload or take a photo. The app generates a caption using a Visual Language Model and suggests subjects using Annif.",