Absolutely! Here’s a simple proof-of-concept web app using FastAPI (Python backend), HTML/CSS, and JavaScript. The predictive text is shown inline (Gmail-style) in the textbox, and the user can press TAB to accept the suggestion.
Assumptions:
- The image is a static file in your project directory.
- The predictive words are provided by the backend (e.g., from OCR output).
- No actual OCR is performed here; you can later replace the static list with real OCR output.
1. Project Structure
your_project/
│
├── main.py
├── static/
│ ├── document.jpg
│ └── style.css
└── templates/
└── index.html
2. main.py (FastAPI backend)
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="templates")
# Example OCR output (replace with real OCR later)
PREDICTIVE_WORDS = [
"This is a sample document.",
"The quick brown fox jumps over the lazy dog.",
"Document processing is important.",
"FastAPI is a modern web framework.",
"Predictive text helps users type faster."
]
@app.get("/", response_class=HTMLResponse)
async def read_root(request: Request):
return templates.TemplateResponse("index.html", {"request": request})
@app.get("/predict")
async def predict(prefix: str):
# Find the first suggestion that starts with the prefix
for sentence in PREDICTIVE_WORDS:
if sentence.lower().startswith(prefix.lower()):
return {"suggestion": sentence}
return {"suggestion": ""}
3. templates/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document Predictive Text POC</title>
<link rel="stylesheet" href="/static/style.css">
</head>
<body>
<div class="container">
<div class="left">
<img src="/static/document.jpg" alt="Document" class="doc-image">
</div>
<div class="right">
<div class="predictive-input-wrapper">
<input id="userInput" type="text" autocomplete="off" spellcheck="false" />
<div id="suggestion" class="suggestion"></div>
</div>
</div>
</div>
<script>
const input = document.getElementById('userInput');
const suggestionDiv = document.getElementById('suggestion');
let currentSuggestion = "";
input.addEventListener('input', async function() {
const value = input.value;
if (value.length === 0) {
suggestionDiv.textContent = "";
return;
}
const res = await fetch(`/predict?prefix=${encodeURIComponent(value)}`);
const data = await res.json();
if (data.suggestion && data.suggestion.toLowerCase().startsWith(value.toLowerCase())) {
const rest = data.suggestion.slice(value.length);
suggestionDiv.textContent = value + rest;
suggestionDiv.innerHTML = `<span style="color: black;">${value}</span><span style="color: #aaa;">${rest}</span>`;
currentSuggestion = data.suggestion;
} else {
suggestionDiv.textContent = "";
currentSuggestion = "";
}
});
input.addEventListener('keydown', function(e) {
if (e.key === "Tab" && currentSuggestion) {
e.preventDefault();
input.value = currentSuggestion;
suggestionDiv.textContent = "";
currentSuggestion = "";
}
});
// Keep suggestionDiv aligned with input
input.addEventListener('scroll', function() {
suggestionDiv.scrollLeft = input.scrollLeft;
});
</script>
</body>
</html>
4. static/style.css
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
.container {
display: flex;
height: 100vh;
}
.left, .right {
flex: 1;
display: flex;
align-items: center;
justify-content: center;
}
.doc-image {
max-width: 90%;
max-height: 90%;
border: 1px solid #ccc;
box-shadow: 2px 2px 8px #aaa;
}
.predictive-input-wrapper {
position: relative;
width: 400px;
}
#userInput {
width: 100%;
font-size: 1.2em;
padding: 8px;
box-sizing: border-box;
position: relative;
background: transparent;
color: black;
z-index: 2;
}
.suggestion {
position: absolute;
top: 0;
left: 0;
width: 100%;
font-size: 1.2em;
padding: 8px;
pointer-events: none;
color: #aaa;
z-index: 1;
white-space: pre;
}
5. How it works
- The user types in the input box.
- The frontend fetches a prediction from
/predict?prefix=....
- If a suggestion is found, it is shown inline in gray.
- Pressing TAB accepts the suggestion.
To run:
- Place your image in
static/document.jpg.
- Install FastAPI and Uvicorn:
pip install fastapi uvicorn jinja2
- Run:
uvicorn main:app --reload
- Open http://localhost:8000
Let me know if you want to extend this to use real OCR, or have any other requirements!