hideosnes commited on
Commit
8ef2c76
·
verified ·
1 Parent(s): 9192e24

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -9
app.py CHANGED
@@ -27,15 +27,13 @@ tokenizer, model = load_model()
27
  def summarize(file, text, style, length):
28
  text_input = ""
29
  if file is not None:
30
- if file.name.endswith(".pdf"):
31
- # Only PDFs have .read()
32
- with fitz.open(stream=file.read(), filetype="pdf") as doc:
33
- text_input = " ".join([page.get_text() for page in doc])
34
- elif file.name.endswith(".txt"):
35
- # TXT files are passed as NamedString -> use str(file)
36
  text_input = str(file)
37
- else:
38
- text_input = ""
39
  elif text:
40
  text_input = text
41
  # If the input text is empty or contains only whitespace,
@@ -45,7 +43,7 @@ def summarize(file, text, style, length):
45
  # so we return a message for the first output (the summary box) and None for the rest.
46
  # This ensures the UI remains consistent and doesn't break if the input is empty.
47
  return "Maybe try uploading a file or typing some text?", None, None, None, None, None
48
-
49
  # Language detection
50
  try:
51
  lang_code = detect(text_input)
 
27
  def summarize(file, text, style, length):
28
  text_input = ""
29
  if file is not None:
30
+ if file.name.endswith(".pdf") and hasattr(file, 'read'):
31
+ with fitz.open(stream=file.read(), filetype="pdf") as doc:
32
+ text_input = " ".join([page.get_text() for page in doc])
33
+ else:
34
+ # if file doesn't end with .pdf AND hasn't attribute 'read',
35
+ # then handle all other cases (TXT files, PDFs without .read(), etc.)
36
  text_input = str(file)
 
 
37
  elif text:
38
  text_input = text
39
  # If the input text is empty or contains only whitespace,
 
43
  # so we return a message for the first output (the summary box) and None for the rest.
44
  # This ensures the UI remains consistent and doesn't break if the input is empty.
45
  return "Maybe try uploading a file or typing some text?", None, None, None, None, None
46
+
47
  # Language detection
48
  try:
49
  lang_code = detect(text_input)