Update app.py
Browse files
app.py
CHANGED
|
@@ -1,90 +1,39 @@
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
| 3 |
-
import
|
| 4 |
-
import docx2txt
|
| 5 |
-
from PIL import Image
|
| 6 |
-
import torch
|
| 7 |
-
from transformers import AutoProcessor, AutoModelForImageClassification
|
| 8 |
|
| 9 |
-
st.set_page_config(page_title="
|
| 10 |
-
st.title("π Type of Text Input")
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
for page in reader.pages:
|
| 16 |
-
page_text = page.extract_text()
|
| 17 |
-
if page_text:
|
| 18 |
-
text += page_text + "\n"
|
| 19 |
-
return text
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
@st.cache_resource
|
| 33 |
-
def load_ocr_model():
|
| 34 |
-
processor = TrOCRProcessor.from_pretrained(OCR_MODEL)
|
| 35 |
-
model = VisionEncoderDecoderModel.from_pretrained(OCR_MODEL)
|
| 36 |
-
return processor, model
|
| 37 |
-
|
| 38 |
-
ocr_processor, ocr_model = load_ocr_model()
|
| 39 |
-
# --- Main UI ---
|
| 40 |
-
input_type = st.selectbox(
|
| 41 |
-
"Select the type of input:",
|
| 42 |
-
["Select...", "PDF", "Word Document", "Text", "Image"]
|
| 43 |
-
)
|
| 44 |
-
|
| 45 |
-
if input_type == "PDF":
|
| 46 |
-
uploaded_file = st.file_uploader("Upload a PDF file", type=["pdf"])
|
| 47 |
-
if uploaded_file is not None:
|
| 48 |
-
raw_text = extract_pdf_text(uploaded_file)
|
| 49 |
-
single_line = format_single_line(raw_text)
|
| 50 |
-
st.subheader("π PDF Text as One Line")
|
| 51 |
-
st.text_area("All content in one line", single_line, height=150)
|
| 52 |
-
|
| 53 |
-
elif input_type == "Word Document":
|
| 54 |
-
uploaded_file = st.file_uploader("Upload a DOCX file", type=["docx"])
|
| 55 |
-
if uploaded_file is not None:
|
| 56 |
-
extracted_text = extract_docx_text(uploaded_file)
|
| 57 |
-
st.subheader("π Extracted DOCX Text")
|
| 58 |
-
st.text_area("Content", extracted_text, height=300)
|
| 59 |
-
|
| 60 |
-
elif input_type == "Text":
|
| 61 |
-
notes = st.text_area(
|
| 62 |
-
"Paste your class notes here",
|
| 63 |
-
height=300,
|
| 64 |
-
placeholder="e.g., AI notes ..."
|
| 65 |
-
)
|
| 66 |
-
if notes:
|
| 67 |
-
st.subheader("π Your Input Text")
|
| 68 |
-
st.text_area("Content", notes, height=300)
|
| 69 |
-
|
| 70 |
-
elif input_type == "Image":
|
| 71 |
-
uploaded_img = st.file_uploader("Upload a PNG/JPG image", type=["png", "jpg", "jpeg"])
|
| 72 |
-
if uploaded_img is not None:
|
| 73 |
-
img = Image.open(uploaded_img).convert("RGB")
|
| 74 |
-
st.image(img, caption="πΌοΈ Uploaded Image", use_column_width=True)
|
| 75 |
-
|
| 76 |
-
# 1. Preprocess for OCR
|
| 77 |
-
pixel_values = ocr_processor(images=img, return_tensors="pt").pixel_values
|
| 78 |
-
|
| 79 |
-
# 2. Generate and decode
|
| 80 |
-
with torch.no_grad():
|
| 81 |
-
generated_ids = ocr_model.generate(pixel_values)
|
| 82 |
-
extracted_text = ocr_processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# quicknote_app.py
|
| 2 |
import streamlit as st
|
| 3 |
+
import os
|
| 4 |
+
import json
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
st.set_page_config(page_title="QuickNote Clone", layout="wide")
|
|
|
|
| 7 |
|
| 8 |
+
# Load note ID from query parameter (optional)
|
| 9 |
+
query_params = st.experimental_get_query_params()
|
| 10 |
+
note_id = query_params.get("note", ["default"])[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
NOTE_DIR = "notes"
|
| 13 |
+
os.makedirs(NOTE_DIR, exist_ok=True)
|
| 14 |
+
note_file_path = os.path.join(NOTE_DIR, f"{note_id}.json")
|
| 15 |
|
| 16 |
+
# Load existing note if available
|
| 17 |
+
if os.path.exists(note_file_path):
|
| 18 |
+
with open(note_file_path, "r", encoding="utf-8") as f:
|
| 19 |
+
note_data = json.load(f)
|
| 20 |
+
note_text = note_data.get("text", "")
|
| 21 |
+
else:
|
| 22 |
+
note_text = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
st.title("π QuickNote Clone")
|
| 25 |
+
st.caption("Clean text interface built with Streamlit.")
|
| 26 |
|
| 27 |
+
# Text editor
|
| 28 |
+
text_input = st.text_area("Your Note", value=note_text, height=500, label_visibility="collapsed")
|
| 29 |
|
| 30 |
+
# Save note manually
|
| 31 |
+
if st.button("πΎ Save Note"):
|
| 32 |
+
with open(note_file_path, "w", encoding="utf-8") as f:
|
| 33 |
+
json.dump({"text": text_input}, f)
|
| 34 |
+
st.success("Note saved!")
|
| 35 |
|
| 36 |
+
# Autosave every time user types (optional)
|
| 37 |
+
if text_input != note_text:
|
| 38 |
+
with open(note_file_path, "w", encoding="utf-8") as f:
|
| 39 |
+
json.dump({"text": text_input}, f)
|