Update app.py
Browse files
app.py
CHANGED
|
@@ -19,18 +19,38 @@ import shutil
|
|
| 19 |
from pathlib import Path
|
| 20 |
import py_text_scan
|
| 21 |
from sqlalchemy import create_engine, Column, Integer, String, Boolean, Text, DateTime
|
|
|
|
|
|
|
| 22 |
from sqlalchemy.orm import sessionmaker, Session
|
| 23 |
from passlib.context import CryptContext
|
| 24 |
import datetime
|
| 25 |
|
| 26 |
-
# --- Database
|
| 27 |
DATABASE_URL = "sqlite:///./test.db"
|
| 28 |
engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False})
|
| 29 |
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
|
|
| 30 |
Base = declarative_base()
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
class OCRResponse(BaseModel):
|
| 36 |
sakshi_output: str
|
|
@@ -110,7 +130,7 @@ def process_image(image_array, use_keras: bool, use_py_text_scan: bool):
|
|
| 110 |
cv2.imwrite(word_detection_path, word_detected_img)
|
| 111 |
session_files['word_detection'] = word_detection_path
|
| 112 |
|
| 113 |
-
# ---
|
| 114 |
pred_label = "Keras model disabled by user"
|
| 115 |
if use_keras:
|
| 116 |
try:
|
|
@@ -126,7 +146,7 @@ def process_image(image_array, use_keras: bool, use_py_text_scan: bool):
|
|
| 126 |
except Exception as e:
|
| 127 |
pred_label = f"Keras Error: {str(e)}"
|
| 128 |
|
| 129 |
-
# ---
|
| 130 |
sakshi_output = "py_text_scan disabled by user"
|
| 131 |
if use_py_text_scan:
|
| 132 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp_file:
|
|
@@ -141,7 +161,6 @@ def process_image(image_array, use_keras: bool, use_py_text_scan: bool):
|
|
| 141 |
}
|
| 142 |
|
| 143 |
# --- API Endpoints ---
|
| 144 |
-
# MODIFIED: Endpoint now takes query parameters to control models
|
| 145 |
@app.post("/process/", response_model=OCRResponse)
|
| 146 |
async def process(
|
| 147 |
file: UploadFile = File(...),
|
|
@@ -185,7 +204,5 @@ async def process(
|
|
| 185 |
if os.path.exists(temp_file_path):
|
| 186 |
os.unlink(temp_file_path)
|
| 187 |
|
| 188 |
-
# ... (other endpoints like /word-detection/ can remain as they are or be removed if not needed) ...
|
| 189 |
-
|
| 190 |
if __name__ == "__main__":
|
| 191 |
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
|
| 19 |
from pathlib import Path
|
| 20 |
import py_text_scan
|
| 21 |
from sqlalchemy import create_engine, Column, Integer, String, Boolean, Text, DateTime
|
| 22 |
+
# --- FIX: Added the missing import below ---
|
| 23 |
+
from sqlalchemy.ext.declarative import declarative_base
|
| 24 |
from sqlalchemy.orm import sessionmaker, Session
|
| 25 |
from passlib.context import CryptContext
|
| 26 |
import datetime
|
| 27 |
|
| 28 |
+
# --- Database Setup (SQLite) ---
|
| 29 |
DATABASE_URL = "sqlite:///./test.db"
|
| 30 |
engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False})
|
| 31 |
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
| 32 |
+
# This line will now work correctly
|
| 33 |
Base = declarative_base()
|
| 34 |
|
| 35 |
+
# --- Database Models ---
|
| 36 |
+
class UserModel(Base):
|
| 37 |
+
__tablename__ = "users"
|
| 38 |
+
id = Column(Integer, primary_key=True, index=True)
|
| 39 |
+
username = Column(String, unique=True, index=True)
|
| 40 |
+
email = Column(String, unique=True, index=True)
|
| 41 |
+
hashed_password = Column(String)
|
| 42 |
+
is_active = Column(Boolean, default=True)
|
| 43 |
+
is_admin = Column(Boolean, default=False)
|
| 44 |
+
|
| 45 |
+
class FeedbackModel(Base):
|
| 46 |
+
__tablename__ = "feedback"
|
| 47 |
+
id = Column(Integer, primary_key=True, index=True)
|
| 48 |
+
username = Column(String)
|
| 49 |
+
comment = Column(Text)
|
| 50 |
+
created_at = Column(DateTime, default=datetime.datetime.utcnow)
|
| 51 |
+
|
| 52 |
+
Base.metadata.create_all(bind=engine)
|
| 53 |
+
|
| 54 |
|
| 55 |
class OCRResponse(BaseModel):
|
| 56 |
sakshi_output: str
|
|
|
|
| 130 |
cv2.imwrite(word_detection_path, word_detected_img)
|
| 131 |
session_files['word_detection'] = word_detection_path
|
| 132 |
|
| 133 |
+
# --- Conditional Keras Model Prediction ---
|
| 134 |
pred_label = "Keras model disabled by user"
|
| 135 |
if use_keras:
|
| 136 |
try:
|
|
|
|
| 146 |
except Exception as e:
|
| 147 |
pred_label = f"Keras Error: {str(e)}"
|
| 148 |
|
| 149 |
+
# --- Conditional py_text_scan Execution ---
|
| 150 |
sakshi_output = "py_text_scan disabled by user"
|
| 151 |
if use_py_text_scan:
|
| 152 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp_file:
|
|
|
|
| 161 |
}
|
| 162 |
|
| 163 |
# --- API Endpoints ---
|
|
|
|
| 164 |
@app.post("/process/", response_model=OCRResponse)
|
| 165 |
async def process(
|
| 166 |
file: UploadFile = File(...),
|
|
|
|
| 204 |
if os.path.exists(temp_file_path):
|
| 205 |
os.unlink(temp_file_path)
|
| 206 |
|
|
|
|
|
|
|
| 207 |
if __name__ == "__main__":
|
| 208 |
uvicorn.run(app, host="0.0.0.0", port=8000)
|