omkar56 commited on
Commit
551b199
·
1 Parent(s): 91a3d66

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +6 -0
main.py CHANGED
@@ -12,6 +12,7 @@ from nltk.tokenize import sent_tokenize
12
  from transformers import MarianMTModel, MarianTokenizer
13
 
14
  API_KEY = os.environ.get("API_KEY")
 
15
 
16
  app = FastAPI()
17
  # CORS issue write below code
@@ -41,6 +42,11 @@ async def ocr(
41
  # languages: list = Body(["eng"])
42
  ):
43
  try:
 
 
 
 
 
44
  content = await image.read()
45
  image = Image.open(BytesIO(content))
46
  text = pytesseract.image_to_string(image, lang = 'eng')
 
12
  from transformers import MarianMTModel, MarianTokenizer
13
 
14
  API_KEY = os.environ.get("API_KEY")
15
+ VALID_IMAGE_EXTENSIONS = {".jpg", ".jpeg", ".png"}
16
 
17
  app = FastAPI()
18
  # CORS issue write below code
 
42
  # languages: list = Body(["eng"])
43
  ):
44
  try:
45
+ # Check if the file format is allowed
46
+ file_extension = image.filename.split(".")[-1].lower()
47
+ if file_extension not in VALID_IMAGE_EXTENSIONS:
48
+ raise HTTPException(status_code=400, detail="Invalid file format. Only .jpg, .jpeg, and .png are allowed.")
49
+
50
  content = await image.read()
51
  image = Image.open(BytesIO(content))
52
  text = pytesseract.image_to_string(image, lang = 'eng')