KarthiEz commited on
Commit
9651557
·
verified ·
1 Parent(s): 2e0534e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -7
app.py CHANGED
@@ -62,25 +62,53 @@ def infer(image: Image.Image, question: str) -> str:
62
 
63
  # ---- Robust readout (handles nested lists/dicts/strings) ----
64
  def extract_text(obj):
 
 
 
 
 
 
 
65
  if obj is None:
66
  return ""
 
 
67
  if isinstance(obj, str):
68
  return obj
 
 
69
  if isinstance(obj, dict):
70
- # Most common contract
71
- if "generated_text" in obj and isinstance(obj["generated_text"], str):
72
- return obj["generated_text"]
73
- # Some models return {"text": "..."}
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  if "text" in obj and isinstance(obj["text"], str):
75
  return obj["text"]
76
- # Fallback: stringify dict
 
77
  return str(obj)
 
 
78
  if isinstance(obj, (list, tuple)) and obj:
79
- # Flatten by drilling down to the first non-list item
80
  return extract_text(obj[0])
81
- # Last resort
 
82
  return str(obj)
83
 
 
84
  return extract_text(out).strip()
85
 
86
 
 
62
 
63
  # ---- Robust readout (handles nested lists/dicts/strings) ----
64
  def extract_text(obj):
65
+ """
66
+ Normalizes pipeline outputs to a plain string.
67
+ Handles:
68
+ - {'generated_text': '...'}
69
+ - {'generated_text': [{'role': 'assistant', 'content': '...'}, ...]}
70
+ - [{'generated_text': '...'}], nested lists, or raw strings
71
+ """
72
  if obj is None:
73
  return ""
74
+
75
+ # If it's already a string
76
  if isinstance(obj, str):
77
  return obj
78
+
79
+ # If it's a dict
80
  if isinstance(obj, dict):
81
+ gen = obj.get("generated_text")
82
+ # Case A: direct string
83
+ if isinstance(gen, str):
84
+ return gen
85
+ # Case B: list of chat turns -> find the assistant message
86
+ if isinstance(gen, (list, tuple)) and gen:
87
+ for turn in reversed(gen):
88
+ if isinstance(turn, dict) and turn.get("role") == "assistant":
89
+ content = turn.get("content")
90
+ # Some models return list[str] or str here; normalize
91
+ if isinstance(content, list):
92
+ return " ".join(map(str, content))
93
+ return str(content) if content is not None else ""
94
+ # Fallback: stringify first element
95
+ return extract_text(gen[0])
96
+
97
+ # Other keys sometimes used by models
98
  if "text" in obj and isinstance(obj["text"], str):
99
  return obj["text"]
100
+
101
+ # Last resort for dicts
102
  return str(obj)
103
+
104
+ # If it's a list/tuple, drill down
105
  if isinstance(obj, (list, tuple)) and obj:
 
106
  return extract_text(obj[0])
107
+
108
+ # Fallback
109
  return str(obj)
110
 
111
+
112
  return extract_text(out).strip()
113
 
114