ayyuce commited on
Commit
aad1f20
·
verified ·
1 Parent(s): 0950411

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -27
app.py CHANGED
@@ -80,31 +80,30 @@ def generate_report(frontal_path, lateral_path, indication, technique, compariso
80
  if not MODEL_STATE["authenticated"]:
81
  return "⚠️ Please authenticate with your Hugging Face token first!"
82
 
 
 
 
83
  try:
84
- current_frontal = Image.open(frontal_path) if frontal_path else None
85
- current_lateral = Image.open(lateral_path) if lateral_path else None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
 
87
- kwargs = {
88
- "current_frontal": current_frontal,
89
- "current_lateral": current_lateral,
90
- "indication": indication,
91
- "technique": technique,
92
- "comparison": comparison,
93
- "prior_report": prior_report or None,
94
- "return_tensors": "pt",
95
- "get_grounding": grounding
96
- }
97
- if prior_frontal_path:
98
- kwargs["prior_frontal"] = Image.open(prior_frontal_path)
99
- if prior_lateral_path:
100
- kwargs["prior_lateral"] = Image.open(prior_lateral_path)
101
-
102
- processed = MODEL_STATE["processor"].format_and_preprocess_reporting_input(**kwargs).to("cpu")
103
-
104
- # Remove any unsupported keys (e.g. image_sizes) if present
105
- if "image_sizes" in processed:
106
- processed.pop("image_sizes")
107
-
108
  outputs = MODEL_STATE["model"].generate(
109
  **processed,
110
  max_new_tokens=450 if grounding else 300,
@@ -123,17 +122,20 @@ def ground_phrase(frontal_path, phrase):
123
  if not MODEL_STATE["authenticated"]:
124
  return "⚠️ Please authenticate with your Hugging Face token first!"
125
 
 
 
 
 
126
  try:
127
- frontal = Image.open(frontal_path) if frontal_path else None
128
  processed = MODEL_STATE["processor"].format_and_preprocess_phrase_grounding_input(
129
  frontal_image=frontal,
130
  phrase=phrase,
131
  return_tensors="pt"
132
  ).to("cpu")
133
 
134
- # Remove unsupported keyword if present
135
- if "image_sizes" in processed:
136
- processed.pop("image_sizes")
137
 
138
  outputs = MODEL_STATE["model"].generate(
139
  **processed,
 
80
  if not MODEL_STATE["authenticated"]:
81
  return "⚠️ Please authenticate with your Hugging Face token first!"
82
 
83
+ if not frontal_path or not lateral_path:
84
+ return "❌ Please upload both the frontal and lateral images for the current study."
85
+
86
  try:
87
+ current_frontal = Image.open(frontal_path)
88
+ current_lateral = Image.open(lateral_path)
89
+ prior_frontal = Image.open(prior_frontal_path) if prior_frontal_path else None
90
+ prior_lateral = Image.open(prior_lateral_path) if prior_lateral_path else None
91
+
92
+ processed = MODEL_STATE["processor"].format_and_preprocess_reporting_input(
93
+ current_frontal=current_frontal,
94
+ current_lateral=current_lateral,
95
+ prior_frontal=prior_frontal,
96
+ prior_lateral=prior_lateral,
97
+ indication=indication,
98
+ technique=technique,
99
+ comparison=comparison,
100
+ prior_report=prior_report or None,
101
+ return_tensors="pt",
102
+ get_grounding=grounding
103
+ ).to("cpu")
104
+
105
+ processed.pop("image_sizes", None)
106
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
  outputs = MODEL_STATE["model"].generate(
108
  **processed,
109
  max_new_tokens=450 if grounding else 300,
 
122
  if not MODEL_STATE["authenticated"]:
123
  return "⚠️ Please authenticate with your Hugging Face token first!"
124
 
125
+ # Check that the required image is provided.
126
+ if not frontal_path:
127
+ return "❌ Please upload the frontal image for phrase grounding."
128
+
129
  try:
130
+ frontal = Image.open(frontal_path)
131
  processed = MODEL_STATE["processor"].format_and_preprocess_phrase_grounding_input(
132
  frontal_image=frontal,
133
  phrase=phrase,
134
  return_tensors="pt"
135
  ).to("cpu")
136
 
137
+ # Remove the unexpected key if present.
138
+ processed.pop("image_sizes", None)
 
139
 
140
  outputs = MODEL_STATE["model"].generate(
141
  **processed,