| import os |
| from datetime import datetime |
|
|
| from dotenv import load_dotenv |
| from sqlalchemy import create_engine, Column, Integer, String, Float, DateTime, Text |
|
|
| load_dotenv() |
| from sqlalchemy.ext.declarative import declarative_base |
| from sqlalchemy.orm import sessionmaker |
|
|
| Base = declarative_base() |
|
|
|
|
| class Prediction(Base): |
| __tablename__ = "predictions" |
|
|
| id = Column(Integer, primary_key=True, autoincrement=True) |
| image_data = Column(Text, nullable=True) |
| image_path = Column(String, nullable=False) |
| disease = Column(String, nullable=False) |
| confidence = Column(Float, nullable=False) |
| cnn_disease = Column(String, nullable=True) |
| cnn_confidence = Column(Float, nullable=True) |
| source = Column(String, nullable=True) |
| autoderm_predictions = Column(Text, nullable=True) |
| severity = Column(String, nullable=True) |
| severity_confidence = Column(String, nullable=True) |
| reasoning = Column(String, nullable=True) |
| timestamp = Column(DateTime, default=datetime.utcnow) |
|
|
|
|
| DATABASE_URL = os.getenv("DATABASE_URL") |
| if not DATABASE_URL: |
| raise ValueError("DATABASE_URL environment variable is not set") |
|
|
| engine = create_engine( |
| DATABASE_URL, |
| connect_args={"sslmode": "require"}, |
| pool_pre_ping=True, |
| ) |
| SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) |
|
|
|
|
| def init_db(): |
| Base.metadata.create_all(bind=engine) |
|
|
|
|
| def get_db(): |
| db = SessionLocal() |
| try: |
| yield db |
| finally: |
| db.close() |