Mohuu0601 commited on
Commit
c240e04
1 Parent(s): b569a13

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -20
app.py CHANGED
@@ -1,25 +1,31 @@
1
- from transformers import AutoFeatureExtractor, AutoModelForImageClassification
 
2
  from PIL import Image
3
- import gradio as gr
4
 
5
- # Load the model and feature extractor
6
- feature_extractor = AutoFeatureExtractor.from_pretrained("google/vit-base-patch16-224")
7
- model = AutoModelForImageClassification.from_pretrained("google/vit-base-patch16-224")
 
8
 
9
- # Define prediction function
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
- # Create a Gradio app
18
- app = gr.Interface(
19
- fn=classify_image,
20
- inputs=gr.Image(type="numpy"),
21
- outputs="text",
22
- title="Image Classifier"
23
- )
24
 
25
- app.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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'])