Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pytesseract
|
3 |
+
import torch
|
4 |
+
from PIL import Image
|
5 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
6 |
+
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained("usvsnsp/code-vs-nl")
|
8 |
+
model = AutoModelForSequenceClassification.from_pretrained("usvsnsp/code-vs-nl")
|
9 |
+
|
10 |
+
def classify_text(text):
|
11 |
+
input_ids = tokenizer(text, return_tensors="pt")
|
12 |
+
with torch.no_grad():
|
13 |
+
logits = model(**input_ids).logits
|
14 |
+
|
15 |
+
predicted_class_id = logits.argmax().item()
|
16 |
+
|
17 |
+
return model.config.id2label[predicted_class_id]
|
18 |
+
|
19 |
+
uploaded_file = st.file_uploader("Upload Image", type= ['png', 'jpg'])
|
20 |
+
|
21 |
+
if uploaded_file is not None:
|
22 |
+
ocr_list = [x for x in pytesseract.image_to_string(uploaded_file).split("\n") if x != '']
|
23 |
+
ocr_class = [classify_text(x) for x in ocr_list]
|
24 |
+
idx = []
|
25 |
+
for i in range(len(ocr_class)):
|
26 |
+
if ocr_class[i] == 'Code':
|
27 |
+
idx.append(ocr_list[i])
|
28 |
+
|
29 |
+
|
30 |
+
st.text(("\n").join(idx))
|