# mohtasham9/medical_prescription_parser architecture

> Architecture diagram of the GitHub repository mohtasham9/medical_prescription_parser, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mohtasham9/medical_prescription_parser
- Repository: https://github.com/mohtasham9/medical_prescription_parser

## Overview

##### Main Components
- User Interface (UI): This is the Streamlit web interface where users upload prescription images. Although the file tree does not explicitly list an “app.py,” the README instructs you to run the interface via Streamlit.
- Image Processing and Parsing Engine: Likely encapsulated in files such as prescription.py (and possibly parts of app.py). This component handles loading images, calling the GPT-4o API, and parsing the response into structured data.
- External API Service: The GPT-4o model provided by OpenAI. This service is called during the transcription process and is critical for extracting prescription details.
- Environment and Configuration Management: Represented by keys.py and environment setups, where the OpenAI API key is configured.
- Data Models & Validation: Handled via Pydantic models (mentioned in the README’s “Code Explanation” section) to ensure that the output data (medications, patient info, etc.) is properly structured.
- Styling Component: The styles.css file contains custom CSS that is applied to the Streamlit application for a better UI experience.

##### Relationships and Interactions
- The User interacts with the Streamlit UI by uploading images.
- The UI component then calls the backend processing logic (in prescription.py) to load and encode images.
- The processing logic triggers a call to the external GPT-4o API (utilizing the API key set up in keys.py) and passes a detailed prompt extracted from the prescription.
- The API returns a raw response which is then processed, with data validated and structured using the Pydantic models.
- The results are sent back to the UI layer where the Streamlit application displays the structured prescription information.
- Additionally, the UI applies custom styles from styles.css to ensure consistency and a polished presentation.

##### Architectural Patterns or Design Principles
- Layered Architecture: Clearly separate UI (presentation layer), business logic (processing, image parsing, API calls), and configuration services (environment keys, external API).
- API Integration: The prescription processing layer acts as an integration point between the application and the OpenAI GPT-4o service.
- Component Modularity: Each file (keys.py for config, prescription.py for processing, styles.css for styling) encapsulates a specific set of concerns, enabling easier maintenance and potential extensibility.
- Data Validation: The use of Pydantic models emphasizes the importance of ensuring that extracted data meets the required schema and quality.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Interaction
    subgraph "User Interaction"
        U["User"]:::user
    end

    %% Frontend Layer
    subgraph "Frontend Layer"
        UI["Streamlit Frontend"]:::frontend
        CSS["Custom CSS"]:::css
    end

    %% Backend Processing Layer
    subgraph "Backend Processing Layer"
        ENG["Prescription Parsing Engine"]:::processing
        DM["Data Models & Validation"]:::processing
        CONF["Configuration (keys.py)"]:::config
    end

    %% External APIs
    subgraph "External Service"
        API["OpenAI GPT-4o API"]:::external
    end

    %% Data Flow Connections
    U -->|"uploadsImage"| UI
    UI -->|"sendImageForProcessing"| ENG
    ENG -->|"callsTranscription"| API
    ENG -->|"processesResponse"| DM
    DM -->|"structuredData"| ENG
    ENG -->|"returnsData"| UI
    CSS -->|"appliesToUI"| UI
    CONF -->|"providesConfig"| ENG
    CONF -->|"providesAPIKey"| API

    %% Click Events
    click UI "https://github.com/mohtasham9/medical_prescription_parser/blob/main/README.md"
    click ENG "https://github.com/mohtasham9/medical_prescription_parser/blob/main/prescription.py"
    click CONF "https://github.com/mohtasham9/medical_prescription_parser/blob/main/keys.py"
    click CSS "https://github.com/mohtasham9/medical_prescription_parser/blob/main/styles.css"

    %% Styles
    classDef user fill:#FFD700,stroke:#B8860B,stroke-width:2px;
    classDef frontend fill:#ADD8E6,stroke:#00008B,stroke-width:2px;
    classDef processing fill:#F4A460,stroke:#8B4513,stroke-width:2px;
    classDef config fill:#90EE90,stroke:#006400,stroke-width:2px;
    classDef external fill:#FFDD57,stroke:#B8860B,stroke-width:2px;
    classDef css fill:#FFC0CB,stroke:#FF69B4,stroke-width:2px;
```
