Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,31 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
-
import
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
def classify_image(image):
|
| 11 |
-
image = Image.fromarray(image).convert("RGB")
|
| 12 |
-
inputs = feature_extractor(images=image, return_tensors="pt")
|
| 13 |
-
outputs = model(**inputs)
|
| 14 |
-
predicted_class = outputs.logits.argmax(-1).item()
|
| 15 |
-
return model.config.id2label[predicted_class]
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
inputs=gr.Image(type="numpy"),
|
| 21 |
-
outputs="text",
|
| 22 |
-
title="Image Classifier"
|
| 23 |
-
)
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
from PIL import Image
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
+
# Set up the OCR pipeline
|
| 7 |
+
@st.cache_resource
|
| 8 |
+
def load_model():
|
| 9 |
+
return pipeline("image-to-text", model="microsoft/trocr-base-stage1")
|
| 10 |
|
| 11 |
+
ocr_model = load_model()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# Streamlit UI
|
| 14 |
+
st.title("Image Data Extractor using Hugging Face")
|
| 15 |
+
st.write("Upload an image, and this app will extract text from it using a Hugging Face model.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
# Upload an image
|
| 18 |
+
uploaded_image = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
|
| 19 |
+
|
| 20 |
+
if uploaded_image:
|
| 21 |
+
# Display the image
|
| 22 |
+
image = Image.open(uploaded_image)
|
| 23 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 24 |
+
|
| 25 |
+
# Run OCR on the image
|
| 26 |
+
with st.spinner("Extracting text..."):
|
| 27 |
+
text = ocr_model(image.convert("RGB"))
|
| 28 |
+
|
| 29 |
+
# Display the extracted text
|
| 30 |
+
st.subheader("Extracted Text:")
|
| 31 |
+
st.write(text[0]['generated_text'])
|