Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,45 +1,105 @@
|
|
1 |
import streamlit as st
|
2 |
from transformers import pipeline
|
|
|
|
|
3 |
import logging
|
|
|
4 |
|
5 |
# Setup logging
|
6 |
def setup_logging():
|
7 |
logging.basicConfig(
|
8 |
level=logging.INFO,
|
9 |
-
format=
|
10 |
-
handlers=[
|
11 |
-
logging.StreamHandler()
|
12 |
-
]
|
13 |
)
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
def main():
|
16 |
setup_logging()
|
17 |
-
|
|
|
18 |
|
19 |
-
#
|
20 |
-
|
|
|
|
|
21 |
|
22 |
-
#
|
23 |
-
st.
|
24 |
-
st.
|
25 |
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
|
|
|
|
|
|
|
|
|
29 |
try:
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
st.text_area("Hinglish Translation:", translation, height=200)
|
35 |
-
logging.info("Translation completed successfully.")
|
36 |
-
else:
|
37 |
-
st.warning("Please enter text to translate.")
|
38 |
except Exception as e:
|
39 |
-
logging.error(f"Error during
|
40 |
-
st.error("An error occurred during
|
41 |
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
if __name__ == "__main__":
|
45 |
main()
|
|
|
1 |
import streamlit as st
|
2 |
from transformers import pipeline
|
3 |
+
from PIL import Image
|
4 |
+
import pytesseract
|
5 |
import logging
|
6 |
+
import PyPDF2
|
7 |
|
8 |
# Setup logging
|
9 |
def setup_logging():
|
10 |
logging.basicConfig(
|
11 |
level=logging.INFO,
|
12 |
+
format="%(asctime)s - %(levelname)s - %(message)s",
|
13 |
+
handlers=[logging.StreamHandler()],
|
|
|
|
|
14 |
)
|
15 |
|
16 |
+
# Text extraction from image
|
17 |
+
def extract_text_from_image(image):
|
18 |
+
try:
|
19 |
+
text = pytesseract.image_to_string(image)
|
20 |
+
return text
|
21 |
+
except Exception as e:
|
22 |
+
logging.error(f"Error during OCR: {e}")
|
23 |
+
return "Error occurred during text extraction."
|
24 |
+
|
25 |
+
# Text extraction from PDF
|
26 |
+
def extract_text_from_pdf(file):
|
27 |
+
try:
|
28 |
+
pdf_reader = PyPDF2.PdfReader(file)
|
29 |
+
text = ""
|
30 |
+
for page in pdf_reader.pages:
|
31 |
+
text += page.extract_text()
|
32 |
+
return text
|
33 |
+
except Exception as e:
|
34 |
+
logging.error(f"Error during PDF text extraction: {e}")
|
35 |
+
return "Error occurred during text extraction."
|
36 |
+
|
37 |
+
# Main function
|
38 |
def main():
|
39 |
setup_logging()
|
40 |
+
st.title("Lab Report Analyzer")
|
41 |
+
st.write("Analyze lab reports from images, PDFs, or text and get summaries in English, Hindi, and Urdu.")
|
42 |
|
43 |
+
# Hugging Face pipelines
|
44 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn") # Summarization model
|
45 |
+
translator_hi = pipeline("translation", model="Helsinki-NLP/opus-mt-en-hi") # English to Hindi
|
46 |
+
translator_ur = pipeline("translation", model="Helsinki-NLP/opus-mt-en-ur") # English to Urdu
|
47 |
|
48 |
+
# File upload section
|
49 |
+
uploaded_file = st.file_uploader("Upload a file (Image or PDF):", type=["png", "jpg", "jpeg", "pdf"])
|
50 |
+
text_input = st.text_area("Or paste your text here:")
|
51 |
|
52 |
+
if st.button("Analyze"):
|
53 |
+
extracted_text = ""
|
54 |
+
|
55 |
+
# Extract text based on file type
|
56 |
+
if uploaded_file:
|
57 |
+
if uploaded_file.name.endswith(".pdf"):
|
58 |
+
st.info("Extracting text from PDF...")
|
59 |
+
extracted_text = extract_text_from_pdf(uploaded_file)
|
60 |
+
else:
|
61 |
+
st.info("Extracting text from image...")
|
62 |
+
image = Image.open(uploaded_file)
|
63 |
+
extracted_text = extract_text_from_image(image)
|
64 |
+
elif text_input:
|
65 |
+
extracted_text = text_input
|
66 |
+
else:
|
67 |
+
st.warning("Please upload a file or enter text.")
|
68 |
+
return
|
69 |
|
70 |
+
# Display extracted text
|
71 |
+
st.subheader("Extracted Text")
|
72 |
+
st.text_area("Extracted Text:", extracted_text, height=200)
|
73 |
+
|
74 |
+
# Summarize the text
|
75 |
try:
|
76 |
+
st.info("Summarizing text...")
|
77 |
+
summary = summarizer(extracted_text, max_length=150, min_length=30, do_sample=False)[0]['summary_text']
|
78 |
+
st.subheader("Summary (English)")
|
79 |
+
st.write(summary)
|
|
|
|
|
|
|
|
|
80 |
except Exception as e:
|
81 |
+
logging.error(f"Error during summarization: {e}")
|
82 |
+
st.error("An error occurred during summarization.")
|
83 |
|
84 |
+
# Translate summary to Hindi
|
85 |
+
try:
|
86 |
+
st.info("Translating summary to Hindi...")
|
87 |
+
summary_hi = translator_hi(summary)[0]['translation_text']
|
88 |
+
st.subheader("Summary (Hindi)")
|
89 |
+
st.write(summary_hi)
|
90 |
+
except Exception as e:
|
91 |
+
logging.error(f"Error during Hindi translation: {e}")
|
92 |
+
st.error("An error occurred during Hindi translation.")
|
93 |
+
|
94 |
+
# Translate summary to Urdu
|
95 |
+
try:
|
96 |
+
st.info("Translating summary to Urdu...")
|
97 |
+
summary_ur = translator_ur(summary)[0]['translation_text']
|
98 |
+
st.subheader("Summary (Urdu)")
|
99 |
+
st.write(summary_ur)
|
100 |
+
except Exception as e:
|
101 |
+
logging.error(f"Error during Urdu translation: {e}")
|
102 |
+
st.error("An error occurred during Urdu translation.")
|
103 |
|
104 |
if __name__ == "__main__":
|
105 |
main()
|