Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
from PIL import Image
|
3 |
+
import streamlit as st
|
4 |
+
from streamlit_drawable_canvas import st_canvas
|
5 |
+
|
6 |
+
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
7 |
+
processor = TrOCRProcessor.from_pretrained("UBC-NLP/ArOCR-Fonts-v2")
|
8 |
+
model = VisionEncoderDecoderModel.from_pretrained("UBC-NLP/ArOCR-Fonts-v2")
|
9 |
+
def predict(img):
|
10 |
+
# if img is None:
|
11 |
+
# _,generated_text=main(image)
|
12 |
+
# return generated_text
|
13 |
+
# else:
|
14 |
+
|
15 |
+
images = Image.open(img).convert("RGB")
|
16 |
+
pixel_values = processor(images, return_tensors="pt").pixel_values
|
17 |
+
# PIL_image = Image.fromarray(np.uint8(pixel_values.squeeze()[0,:,:])).convert('RGB')
|
18 |
+
generated_ids = model.generate(pixel_values,max_length=512)
|
19 |
+
generated_text = processor.batch_decode(
|
20 |
+
generated_ids, skip_special_tokens=True)[0]
|
21 |
+
return generated_text
|
22 |
+
|
23 |
+
# Specify canvas parameters in application
|
24 |
+
stroke_width = st.sidebar.slider("Stroke width: ", 1, 25, 3)
|
25 |
+
stroke_color = st.sidebar.color_picker("Stroke color hex: ")
|
26 |
+
bg_color = st.sidebar.color_picker("Background color hex: ", "#eee")
|
27 |
+
bg_image = st.sidebar.file_uploader("Background image:", type=["png", "jpg"])
|
28 |
+
|
29 |
+
realtime_update = st.sidebar.checkbox("Update in realtime", True)
|
30 |
+
|
31 |
+
|
32 |
+
# Create a canvas component
|
33 |
+
canvas_result = st_canvas(
|
34 |
+
fill_color="rgba(255, 165, 0, 0.3)", # Fixed fill color with some opacity
|
35 |
+
stroke_width=stroke_width,
|
36 |
+
stroke_color=stroke_color,
|
37 |
+
background_color=bg_color,
|
38 |
+
background_image=Image.open(bg_image) if bg_image else None,
|
39 |
+
update_streamlit=realtime_update,
|
40 |
+
height=200,
|
41 |
+
drawing_mode="freedraw",
|
42 |
+
display_toolbar=st.sidebar.checkbox("Display toolbar", True),
|
43 |
+
key="full_app",
|
44 |
+
)
|
45 |
+
|
46 |
+
# Do something interesting with the image data and paths
|
47 |
+
if canvas_result.image_data is not None:
|
48 |
+
st.image(canvas_result.image_data)
|
49 |
+
st.text(predict(canvas_result.image_data))
|