Nymbo commited on
Commit
2a6ff70
·
verified ·
1 Parent(s): c2115a0

python 3.12 bookworm base, gradio 6.29 pinned, path-string file inputs, fix server_name typo, slimmer apt set

Browse files
Files changed (4) hide show
  1. Dockerfile +9 -10
  2. app.py +13 -6
  3. packages.txt +3 -4
  4. requirements.txt +2 -4
Dockerfile CHANGED
@@ -1,15 +1,14 @@
1
- FROM python:3.9
2
 
3
  WORKDIR /app
4
 
5
- COPY . .
 
 
 
6
 
7
- RUN --mount=target=/tmp/packages.txt,source=packages.txt apt-get update && xargs -r -a /tmp/packages.txt apt-get install -y && apt-get install -y curl && curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && apt-get install -y nodejs && rm -rf /var/lib/apt/lists/* && apt-get clean
8
- RUN ln -s /usr/lib/python3/dist-packages/uno.py /usr/local/lib/python3.9/site-packages/
9
- RUN ln -s /usr/lib/python3/dist-packages/unohelper.py /usr/local/lib/python3.9/site-packages/
10
-
11
-
12
- RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt
13
 
14
  # Set up a new user named "user" with user ID 1000
15
  RUN useradd -m -u 1000 user
@@ -25,5 +24,5 @@ WORKDIR $HOME/app
25
  # Copy the current directory contents into the container at $HOME/app setting the owner to the user
26
  COPY --chown=user . $HOME/app
27
 
28
-
29
- CMD ["python","app.py"]
 
1
+ FROM python:3.12-slim-bookworm
2
 
3
  WORKDIR /app
4
 
5
+ COPY packages.txt /tmp/packages.txt
6
+ RUN apt-get update && \
7
+ xargs -r -a /tmp/packages.txt apt-get install -y --no-install-recommends && \
8
+ rm -rf /var/lib/apt/lists/* && apt-get clean
9
 
10
+ COPY requirements.txt /tmp/requirements.txt
11
+ RUN pip install --no-cache-dir --upgrade -r /tmp/requirements.txt
 
 
 
 
12
 
13
  # Set up a new user named "user" with user ID 1000
14
  RUN useradd -m -u 1000 user
 
24
  # Copy the current directory contents into the container at $HOME/app setting the owner to the user
25
  COPY --chown=user . $HOME/app
26
 
27
+ EXPOSE 7860
28
+ CMD ["python","app.py"]
app.py CHANGED
@@ -52,6 +52,7 @@ def extract_text_from_ppt(file_path):
52
 
53
 
54
  def extract_text_from_ppt_or_pptx(file_path):
 
55
  if file_path.endswith(".pptx"):
56
  return extract_text_from_pptx(file_path)
57
  elif file_path.endswith(".ppt"):
@@ -60,6 +61,11 @@ def extract_text_from_ppt_or_pptx(file_path):
60
  return "Unsupported file type. Please provide a .ppt or .pptx file."
61
 
62
 
 
 
 
 
 
63
  def convert_pdf_to_image(file):
64
  images = convert_from_path(file)
65
  return images
@@ -69,13 +75,13 @@ def extract_text_from_pdf(file):
69
  text = ""
70
  with pdfplumber.open(file) as pdf:
71
  for page in pdf.pages:
72
- text += page.extract_text() + "\n"
73
  return text
74
 
75
 
76
  def extract_text_from_docx(file):
77
  text = ""
78
- doc = Document(file.name)
79
  for paragraph in doc.paragraphs:
80
  text += paragraph.text + "\n"
81
  return text
@@ -101,10 +107,11 @@ def convert_doc_to_text(doc_path):
101
 
102
 
103
  def extract_text_from_doc_or_docx(file):
104
- if file.name.endswith(".docx"):
 
105
  return extract_text_from_docx(file)
106
- elif file.name.endswith(".doc"):
107
- return convert_doc_to_text(file.name)
108
  else:
109
  return "Unsupported file type. Please upload a .doc or .docx file."
110
 
@@ -234,4 +241,4 @@ demo = gr.TabbedInterface(
234
  ],
235
  )
236
 
237
- demo.launch(server_name="0.0.0.0.", server_port=7860, debug=True)
 
52
 
53
 
54
  def extract_text_from_ppt_or_pptx(file_path):
55
+ file_path = _path(file_path)
56
  if file_path.endswith(".pptx"):
57
  return extract_text_from_pptx(file_path)
58
  elif file_path.endswith(".ppt"):
 
61
  return "Unsupported file type. Please provide a .ppt or .pptx file."
62
 
63
 
64
+ def _path(file):
65
+ """Gradio 6 passes uploaded files as path strings; older code passed tempfile objects."""
66
+ return file if isinstance(file, str) else getattr(file, "name", str(file))
67
+
68
+
69
  def convert_pdf_to_image(file):
70
  images = convert_from_path(file)
71
  return images
 
75
  text = ""
76
  with pdfplumber.open(file) as pdf:
77
  for page in pdf.pages:
78
+ text += (page.extract_text() or "") + "\n"
79
  return text
80
 
81
 
82
  def extract_text_from_docx(file):
83
  text = ""
84
+ doc = Document(_path(file))
85
  for paragraph in doc.paragraphs:
86
  text += paragraph.text + "\n"
87
  return text
 
107
 
108
 
109
  def extract_text_from_doc_or_docx(file):
110
+ path = _path(file)
111
+ if path.lower().endswith(".docx"):
112
  return extract_text_from_docx(file)
113
+ elif path.lower().endswith(".doc"):
114
+ return convert_doc_to_text(path)
115
  else:
116
  return "Unsupported file type. Please upload a .doc or .docx file."
117
 
 
241
  ],
242
  )
243
 
244
+ demo.launch(server_name="0.0.0.0", server_port=7860)
packages.txt CHANGED
@@ -1,7 +1,6 @@
1
  poppler-utils
2
- libreoffice-script-provider-python
3
  antiword
4
  ghostscript
5
- default-jre
6
- python3-uno
7
- unoconv
 
1
  poppler-utils
 
2
  antiword
3
  ghostscript
4
+ unoconv
5
+ libreoffice-writer
6
+ libreoffice-impress
requirements.txt CHANGED
@@ -1,7 +1,5 @@
1
- typing
2
  pdf2image
3
- gradio
4
  pdfplumber
5
  python-docx
6
- gradio
7
- python-pptx
 
1
+ gradio==6.29.0
2
  pdf2image
 
3
  pdfplumber
4
  python-docx
5
+ python-pptx