Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
from fastapi import FastAPI, UploadFile, File, HTTPException, Form
|
| 3 |
from fastapi.staticfiles import StaticFiles
|
| 4 |
-
from fastapi.responses import RedirectResponse
|
| 5 |
from transformers import pipeline
|
| 6 |
import logging
|
| 7 |
from PIL import Image
|
|
@@ -206,24 +206,39 @@ plt.show()
|
|
| 206 |
|
| 207 |
return {"code": code, "filename": code_filename}
|
| 208 |
except Exception as e:
|
| 209 |
-
logger.error(f"Error during visualization
|
| 210 |
raise HTTPException(status_code=500, detail=str(e))
|
| 211 |
|
| 212 |
async def extract_text_from_file(file: UploadFile):
|
| 213 |
try:
|
| 214 |
file_content = await file.read()
|
|
|
|
|
|
|
|
|
|
| 215 |
if file.filename.endswith(".pdf"):
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
elif file.filename.endswith(".docx"):
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
elif file.filename.endswith(".txt"):
|
| 226 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
else:
|
| 228 |
raise ValueError("Unsupported file format. Please upload a PDF, DOCX, or TXT file.")
|
| 229 |
except Exception as e:
|
|
|
|
| 1 |
import os
|
| 2 |
from fastapi import FastAPI, UploadFile, File, HTTPException, Form
|
| 3 |
from fastapi.staticfiles import StaticFiles
|
| 4 |
+
from fastapi.responses import RedirectResponse, JSONResponse
|
| 5 |
from transformers import pipeline
|
| 6 |
import logging
|
| 7 |
from PIL import Image
|
|
|
|
| 206 |
|
| 207 |
return {"code": code, "filename": code_filename}
|
| 208 |
except Exception as e:
|
| 209 |
+
logger.error(f"Error during visualization code generation: {e}")
|
| 210 |
raise HTTPException(status_code=500, detail=str(e))
|
| 211 |
|
| 212 |
async def extract_text_from_file(file: UploadFile):
|
| 213 |
try:
|
| 214 |
file_content = await file.read()
|
| 215 |
+
if not file_content:
|
| 216 |
+
raise ValueError("Uploaded file is empty.")
|
| 217 |
+
|
| 218 |
if file.filename.endswith(".pdf"):
|
| 219 |
+
try:
|
| 220 |
+
doc = fitz.open(stream=file_content, filetype="pdf")
|
| 221 |
+
text = ""
|
| 222 |
+
for page in doc:
|
| 223 |
+
text += page.get_text()
|
| 224 |
+
return text
|
| 225 |
+
except Exception as e:
|
| 226 |
+
logger.error(f"Error reading PDF file: {e}")
|
| 227 |
+
raise ValueError("Failed to read PDF file. It might be corrupted or not a valid PDF.")
|
| 228 |
elif file.filename.endswith(".docx"):
|
| 229 |
+
try:
|
| 230 |
+
doc = Document(io.BytesIO(file_content))
|
| 231 |
+
text = "\n".join([para.text for para in doc.paragraphs])
|
| 232 |
+
return text
|
| 233 |
+
except Exception as e:
|
| 234 |
+
logger.error(f"Error reading DOCX file: {e}")
|
| 235 |
+
raise ValueError("Failed to read DOCX file. It might be corrupted or not a valid DOCX.")
|
| 236 |
elif file.filename.endswith(".txt"):
|
| 237 |
+
try:
|
| 238 |
+
return file_content.decode("utf-8")
|
| 239 |
+
except Exception as e:
|
| 240 |
+
logger.error(f"Error reading TXT file: {e}")
|
| 241 |
+
raise ValueError("Failed to read TXT file. It might be corrupted or not a valid TXT.")
|
| 242 |
else:
|
| 243 |
raise ValueError("Unsupported file format. Please upload a PDF, DOCX, or TXT file.")
|
| 244 |
except Exception as e:
|