Spaces:
Running
Running
UniquePratham
commited on
Commit
β’
a0652de
1
Parent(s):
aba3166
Update app.py
Browse files
app.py
CHANGED
@@ -1,80 +1,73 @@
|
|
1 |
-
# app.py
|
2 |
-
|
3 |
import streamlit as st
|
4 |
-
from ocr_cpu import extract_text_got,
|
5 |
import json
|
6 |
|
7 |
-
#
|
8 |
-
st.set_page_config(page_title="
|
9 |
|
10 |
st.markdown(
|
11 |
"""
|
12 |
<style>
|
13 |
-
.reportview-container {
|
14 |
-
|
15 |
-
}
|
16 |
-
.
|
17 |
-
background: #e0e0e0;
|
18 |
-
}
|
19 |
-
h1 {
|
20 |
-
color: #007BFF;
|
21 |
-
}
|
22 |
-
.upload-btn {
|
23 |
-
background-color: #007BFF;
|
24 |
-
color: white;
|
25 |
-
padding: 10px;
|
26 |
-
border-radius: 5px;
|
27 |
-
text-align: center;
|
28 |
-
}
|
29 |
</style>
|
30 |
-
""",
|
31 |
-
unsafe_allow_html=True
|
32 |
)
|
33 |
|
34 |
-
# --- Title ---
|
35 |
-
st.title("
|
36 |
-
st.write("Upload an image
|
37 |
|
38 |
# --- Image Upload Section ---
|
39 |
-
uploaded_file = st.file_uploader("
|
|
|
|
|
|
|
|
|
40 |
|
41 |
if uploaded_file is not None:
|
42 |
st.image(uploaded_file, caption='Uploaded Image', use_column_width=True)
|
43 |
|
44 |
-
# Extract text from the image
|
45 |
-
with st.spinner("Extracting text
|
46 |
try:
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
if not extracted_text.strip():
|
49 |
-
st.warning("No text extracted
|
50 |
-
cleaned_text = ""
|
51 |
else:
|
52 |
-
# Clean the extracted text
|
53 |
-
|
54 |
-
cleaned_text = clean_text_with_ai(extracted_text)
|
55 |
-
if cleaned_text.startswith("Error"):
|
56 |
-
st.error(cleaned_text)
|
57 |
-
else:
|
58 |
-
st.success("Text extraction and cleaning successful.")
|
59 |
except Exception as e:
|
60 |
st.error(f"Error during text extraction: {str(e)}")
|
61 |
-
extracted_text
|
62 |
|
63 |
-
# Display
|
64 |
-
|
65 |
-
|
66 |
-
st.text_area("Cleaned Text", cleaned_text, height=250)
|
67 |
|
68 |
-
|
|
|
|
|
|
|
|
|
69 |
with open("extracted_text.json", "w") as json_file:
|
70 |
-
json.dump({"text":
|
71 |
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from ocr_cpu import extract_text_got, extract_text_qwen, extract_text_llama, clean_extracted_text
|
3 |
import json
|
4 |
|
5 |
+
# Set up page layout and styling
|
6 |
+
st.set_page_config(page_title="MultiModel OCR Fusion", layout="centered", page_icon="π")
|
7 |
|
8 |
st.markdown(
|
9 |
"""
|
10 |
<style>
|
11 |
+
.reportview-container { background: #f4f4f4; }
|
12 |
+
.sidebar .sidebar-content { background: #e0e0e0; }
|
13 |
+
h1 { color: #007BFF; }
|
14 |
+
.upload-btn { background-color: #007BFF; color: white; padding: 10px; border-radius: 5px; text-align: center; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
</style>
|
16 |
+
""", unsafe_allow_html=True
|
|
|
17 |
)
|
18 |
|
19 |
+
# --- Title Section ---
|
20 |
+
st.title("π MultiModel OCR Fusion")
|
21 |
+
st.write("Upload an image to extract and clean text using multiple OCR models (GOT, Qwen, LLaMA).")
|
22 |
|
23 |
# --- Image Upload Section ---
|
24 |
+
uploaded_file = st.file_uploader("Upload an image file", type=["jpg", "jpeg", "png"])
|
25 |
+
|
26 |
+
# Model selection
|
27 |
+
st.sidebar.title("Model Selection")
|
28 |
+
model_choice = st.sidebar.selectbox("Choose OCR Model", ("GOT", "Qwen", "LLaMA"))
|
29 |
|
30 |
if uploaded_file is not None:
|
31 |
st.image(uploaded_file, caption='Uploaded Image', use_column_width=True)
|
32 |
|
33 |
+
# Extract text from the image based on selected model
|
34 |
+
with st.spinner(f"Extracting text using the {model_choice} model..."):
|
35 |
try:
|
36 |
+
if model_choice == "GOT":
|
37 |
+
extracted_text = extract_text_got(uploaded_file)
|
38 |
+
elif model_choice == "Qwen":
|
39 |
+
extracted_text = extract_text_qwen(uploaded_file)
|
40 |
+
elif model_choice == "LLaMA":
|
41 |
+
extracted_text = extract_text_llama(uploaded_file)
|
42 |
+
|
43 |
+
# If no text extracted
|
44 |
if not extracted_text.strip():
|
45 |
+
st.warning(f"No text extracted using {model_choice}.")
|
|
|
46 |
else:
|
47 |
+
# Clean the extracted text
|
48 |
+
cleaned_text = clean_extracted_text(extracted_text)
|
|
|
|
|
|
|
|
|
|
|
49 |
except Exception as e:
|
50 |
st.error(f"Error during text extraction: {str(e)}")
|
51 |
+
extracted_text, cleaned_text = "", ""
|
52 |
|
53 |
+
# --- Display Extracted and Cleaned Text ---
|
54 |
+
st.subheader(f"Extracted Text using {model_choice}")
|
55 |
+
st.text_area(f"Raw Text ({model_choice})", extracted_text, height=200)
|
|
|
56 |
|
57 |
+
st.subheader("Cleaned Text (AI-processed)")
|
58 |
+
st.text_area("Cleaned Text", cleaned_text, height=200)
|
59 |
+
|
60 |
+
# Save extracted text for further use
|
61 |
+
if extracted_text:
|
62 |
with open("extracted_text.json", "w") as json_file:
|
63 |
+
json.dump({"text": extracted_text}, json_file)
|
64 |
|
65 |
+
# --- Keyword Search ---
|
66 |
+
st.subheader("Search for Keywords")
|
67 |
+
keyword = st.text_input("Enter a keyword to search in the extracted text")
|
68 |
|
69 |
+
if keyword:
|
70 |
+
if keyword.lower() in cleaned_text.lower():
|
71 |
+
st.success(f"Keyword **'{keyword}'** found in the cleaned text!")
|
72 |
+
else:
|
73 |
+
st.error(f"Keyword **'{keyword}'** not found.")
|