RP-Azul commited on
Commit
7ac4bca
Β·
verified Β·
1 Parent(s): 9eca730

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -81
app.py CHANGED
@@ -1,90 +1,39 @@
 
1
  import streamlit as st
2
- from io import BytesIO
3
- import PyPDF2
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="πŸ“ Note Input", layout="centered")
10
- st.title("πŸ“„ Type of Text Input")
11
 
12
- def extract_pdf_text(uploaded_file):
13
- reader = PyPDF2.PdfReader(uploaded_file)
14
- text = ""
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
- def format_single_line(text: str) -> str:
22
- # split on any whitespace and re-join with single spaces
23
- return " ".join(text.split())
24
 
25
- def extract_docx_text(uploaded_file):
26
- # docx2txt.process accepts a path or a file-like object
27
- return docx2txt.process(uploaded_file)
28
- # --- Image model setup ---
29
- OCR_MODEL = "microsoft/trocr-base-printed"
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
- st.subheader("πŸ–‹οΈ Extracted Text from Image")
85
- st.text_area("OCR Result", extracted_text, height=300)
86
 
87
- else:
88
- st.info("Please select an input type to get started.")
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)