RP-Azul commited on
Commit
e8951e6
Β·
verified Β·
1 Parent(s): 7d0ed4f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -1
app.py CHANGED
@@ -2,6 +2,9 @@ import streamlit as st
2
  from io import BytesIO
3
  import PyPDF2
4
  import docx2txt
 
 
 
5
 
6
  st.set_page_config(page_title="πŸ“ Note Input", layout="centered")
7
  st.title("πŸ“„ Type of Text Input")
@@ -22,10 +25,21 @@ def format_single_line(text: str) -> str:
22
  def extract_docx_text(uploaded_file):
23
  # docx2txt.process accepts a path or a file-like object
24
  return docx2txt.process(uploaded_file)
 
 
25
 
 
 
 
 
 
 
 
 
 
26
  input_type = st.selectbox(
27
  "Select the type of input:",
28
- ["Select...", "PDF", "Word Document", "Text"]
29
  )
30
 
31
  if input_type == "PDF":
@@ -53,5 +67,24 @@ elif input_type == "Text":
53
  st.subheader("πŸ“ Your Input Text")
54
  st.text_area("Content", notes, height=300)
55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  else:
57
  st.info("Please select an input type to get started.")
 
 
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")
 
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
+ MODEL_NAME = "google/vit-base-patch16-224"
30
 
31
+ @st.cache_resource
32
+ def load_image_model():
33
+ proc = AutoProcessor.from_pretrained(MODEL_NAME)
34
+ mdl = AutoModelForImageClassification.from_pretrained(MODEL_NAME)
35
+ return proc, mdl
36
+
37
+ processor, model = load_image_model()
38
+
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":
 
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 image", type=["png"])
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
+ # preprocess & inference
77
+ inputs = processor(images=img, return_tensors="pt")
78
+ with torch.no_grad():
79
+ outputs = model(**inputs)
80
+ probs = torch.softmax(outputs.logits, dim=-1)[0]
81
+ top5 = torch.topk(probs, k=5)
82
+
83
+ st.subheader("πŸ” Top 5 Predictions")
84
+ for idx, score in zip(top5.indices.tolist(), top5.values.tolist()):
85
+ label = model.config.id2label[idx]
86
+ st.write(f"- **{label}**: {score*100:.1f}%")
87
+
88
  else:
89
  st.info("Please select an input type to get started.")
90
+