Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,84 +1,143 @@
|
|
1 |
import streamlit as st
|
|
|
2 |
from PIL import Image
|
3 |
-
import cv2
|
4 |
-
import numpy as np
|
5 |
-
import pytesseract
|
6 |
import torch
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
#
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import AutoModel
|
3 |
from PIL import Image
|
|
|
|
|
|
|
4 |
import torch
|
5 |
+
import numpy as np
|
6 |
+
import urllib.request
|
7 |
+
|
8 |
+
# Initialize session state for memory if not already
|
9 |
+
if "memory" not in st.session_state:
|
10 |
+
st.session_state.memory = {"characters": {}, "transcript": ""}
|
11 |
+
|
12 |
+
@st.cache_resource
|
13 |
+
def load_model():
|
14 |
+
model = AutoModel.from_pretrained("ragavsachdeva/magi", trust_remote_code=True)
|
15 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
16 |
+
model.to(device)
|
17 |
+
return model
|
18 |
+
|
19 |
+
@st.cache_data
|
20 |
+
def read_image_as_np_array(image_path):
|
21 |
+
if "http" in image_path:
|
22 |
+
image = Image.open(urllib.request.urlopen(image_path)).convert("L").convert("RGB")
|
23 |
+
else:
|
24 |
+
image = Image.open(image_path).convert("L").convert("RGB")
|
25 |
+
image = np.array(image)
|
26 |
+
return image
|
27 |
+
|
28 |
+
@st.cache_data
|
29 |
+
def predict_detections_and_associations(
|
30 |
+
image_path,
|
31 |
+
character_detection_threshold,
|
32 |
+
panel_detection_threshold,
|
33 |
+
text_detection_threshold,
|
34 |
+
character_character_matching_threshold,
|
35 |
+
text_character_matching_threshold,
|
36 |
+
):
|
37 |
+
image = read_image_as_np_array(image_path)
|
38 |
+
with torch.no_grad():
|
39 |
+
result = model.predict_detections_and_associations(
|
40 |
+
[image],
|
41 |
+
character_detection_threshold=character_detection_threshold,
|
42 |
+
panel_detection_threshold=panel_detection_threshold,
|
43 |
+
text_detection_threshold=text_detection_threshold,
|
44 |
+
character_character_matching_threshold=character_character_matching_threshold,
|
45 |
+
text_character_matching_threshold=text_character_matching_threshold,
|
46 |
+
)[0]
|
47 |
+
return result
|
48 |
+
|
49 |
+
@st.cache_data
|
50 |
+
def predict_ocr(
|
51 |
+
image_path,
|
52 |
+
character_detection_threshold,
|
53 |
+
panel_detection_threshold,
|
54 |
+
text_detection_threshold,
|
55 |
+
character_character_matching_threshold,
|
56 |
+
text_character_matching_threshold,
|
57 |
+
):
|
58 |
+
if not generate_transcript:
|
59 |
+
return
|
60 |
+
image = read_image_as_np_array(image_path)
|
61 |
+
result = predict_detections_and_associations(
|
62 |
+
image_path,
|
63 |
+
character_detection_threshold,
|
64 |
+
panel_detection_threshold,
|
65 |
+
text_detection_threshold,
|
66 |
+
character_character_matching_threshold,
|
67 |
+
text_character_matching_threshold,
|
68 |
+
)
|
69 |
+
text_bboxes_for_all_images = [result["texts"]]
|
70 |
+
with torch.no_grad():
|
71 |
+
ocr_results = model.predict_ocr([image], text_bboxes_for_all_images)
|
72 |
+
return ocr_results
|
73 |
+
|
74 |
+
def clear_memory():
|
75 |
+
st.session_state.memory = {"characters": {}, "transcript": ""}
|
76 |
+
st.write("Memory cleared.")
|
77 |
+
|
78 |
+
model = load_model()
|
79 |
+
|
80 |
+
# Display header and UI components
|
81 |
+
st.markdown(""" <style> ... styles here ... </style> """, unsafe_allow_html=True)
|
82 |
+
path_to_image = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
|
83 |
+
|
84 |
+
# Memory control button
|
85 |
+
st.button("Clear Memory", on_click=clear_memory)
|
86 |
+
|
87 |
+
st.sidebar.markdown("**Mode**")
|
88 |
+
generate_detections_and_associations = st.sidebar.toggle("Generate detections and associations", True)
|
89 |
+
generate_transcript = st.sidebar.toggle("Generate transcript (slower)", False)
|
90 |
+
|
91 |
+
st.sidebar.markdown("**Hyperparameters**")
|
92 |
+
input_character_detection_threshold = st.sidebar.slider('Character detection threshold', 0.0, 1.0, 0.30, step=0.01)
|
93 |
+
input_panel_detection_threshold = st.sidebar.slider('Panel detection threshold', 0.0, 1.0, 0.2, step=0.01)
|
94 |
+
input_text_detection_threshold = st.sidebar.slider('Text detection threshold', 0.0, 1.0, 0.25, step=0.01)
|
95 |
+
input_character_character_matching_threshold = st.sidebar.slider('Character-character matching threshold', 0.0, 1.0, 0.7, step=0.01)
|
96 |
+
input_text_character_matching_threshold = st.sidebar.slider('Text-character matching threshold', 0.0, 1.0, 0.4, step=0.01)
|
97 |
+
|
98 |
+
if path_to_image is not None:
|
99 |
+
image = read_image_as_np_array(path_to_image)
|
100 |
+
st.markdown("**Prediction**")
|
101 |
+
|
102 |
+
if generate_detections_and_associations or generate_transcript:
|
103 |
+
result = predict_detections_and_associations(
|
104 |
+
path_to_image,
|
105 |
+
input_character_detection_threshold,
|
106 |
+
input_panel_detection_threshold,
|
107 |
+
input_text_detection_threshold,
|
108 |
+
input_character_character_matching_threshold,
|
109 |
+
input_text_character_matching_threshold,
|
110 |
+
)
|
111 |
+
|
112 |
+
if generate_transcript:
|
113 |
+
ocr_results = predict_ocr(
|
114 |
+
path_to_image,
|
115 |
+
input_character_detection_threshold,
|
116 |
+
input_panel_detection_threshold,
|
117 |
+
input_text_detection_threshold,
|
118 |
+
input_character_character_matching_threshold,
|
119 |
+
input_text_character_matching_threshold,
|
120 |
+
)
|
121 |
+
|
122 |
+
# Append new characters and transcript to memory
|
123 |
+
if generate_detections_and_associations:
|
124 |
+
output = model.visualise_single_image_prediction(image, result)
|
125 |
+
st.image(output)
|
126 |
+
# Update character memory based on detected characters
|
127 |
+
detected_characters = result.get("characters", {})
|
128 |
+
st.session_state.memory["characters"].update(detected_characters)
|
129 |
+
|
130 |
+
# Append the current transcript to the ongoing transcript in memory
|
131 |
+
transcript = model.generate_transcript_for_single_image(result, ocr_results[0])
|
132 |
+
st.session_state.memory["transcript"] += transcript + "\n"
|
133 |
+
|
134 |
+
# Display the cumulative transcript from memory
|
135 |
+
st.text(st.session_state.memory["transcript"])
|
136 |
+
|
137 |
+
elif generate_detections_and_associations:
|
138 |
+
output = model.visualise_single_image_prediction(image, result)
|
139 |
+
st.image(output)
|
140 |
+
|
141 |
+
elif generate_transcript:
|
142 |
+
# Display the cumulative transcript
|
143 |
+
st.text(st.session_state.memory["transcript"])
|