Update app.py
Browse files
app.py
CHANGED
@@ -1,42 +1,24 @@
|
|
1 |
-
import os
|
2 |
-
os.environ['USE_TORCH'] = '1'
|
3 |
-
|
4 |
-
from doctr.io import DocumentFile
|
5 |
-
from doctr.models import ocr_predictor
|
6 |
import gradio as gr
|
7 |
from PIL import Image
|
8 |
-
import base64
|
9 |
-
from utils import HocrParser
|
10 |
import google.generativeai as genai
|
|
|
11 |
|
12 |
api_key = 'AIzaSyB7-RzBwTAfVA-7ZGk2mEOQwOxshpwzhpM' # put your API key here
|
13 |
genai.configure(api_key=api_key)
|
14 |
-
geminiModel = genai.GenerativeModel(model_name='gemini-pro')
|
15 |
-
predictor = ocr_predictor(det_arch='db_mobilenet_v3_large', reco_arch='crnn_vgg16_bn',pretrained=True)
|
16 |
-
predictor.reco_predictor.model.cfg['vocab']='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!”#$%&’()*+,-./:;<=>?@[]^_`{|}~°£€¥¢฿áàảạãăắằẳẵặâấầẩẫậéèẻẽẹêếềểễệóòỏõọôốồổộỗơớờởợỡúùủũụưứừửữựiíìỉĩịýỳỷỹỵÁÀẢẠÃĂẮẰẲẴẶÂẤẦẨẪẬÉÈẺẼẸÊẾỀỂỄỆÓÒỎÕỌÔỐỒỔỘỖƠỚỜỞỢỠÚÙỦŨỤƯỨỪỬỮỰIÍÌỈĨỊÝỲỶỸỴ'
|
17 |
title="DocTR OCR (PDL Demo)"
|
18 |
description="Upload an image to get the OCR results !"
|
19 |
|
|
|
|
|
|
|
|
|
20 |
def greet(img):
|
21 |
-
img.save("out.jpg")
|
22 |
-
doc = DocumentFile.from_images("out.jpg")
|
23 |
-
output=predictor(doc)
|
24 |
-
|
25 |
-
xml_outputs = output.export_as_xml()
|
26 |
-
parser = HocrParser()
|
27 |
-
|
28 |
-
res=""
|
29 |
-
for obj in output.pages:
|
30 |
-
for obj1 in obj.blocks:
|
31 |
-
for obj2 in obj1.lines:
|
32 |
-
for obj3 in obj2.words:
|
33 |
-
res=res + " " + obj3.value
|
34 |
-
res=res + "\n"
|
35 |
-
res=res + "\n"
|
36 |
# generate text
|
37 |
prompt = "the input text in vietnamese, please add accend and take this peace of information and give all the information in point wise better format also give some recomendation related to them: " + res
|
38 |
# print(prompt)
|
39 |
-
response = geminiModel.generate_content(prompt)
|
|
|
40 |
print(response)
|
41 |
res = response.text
|
42 |
_output_name = "RESULT_OCR.txt"
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from PIL import Image
|
|
|
|
|
3 |
import google.generativeai as genai
|
4 |
+
from IPython.display import Markdown
|
5 |
|
6 |
api_key = 'AIzaSyB7-RzBwTAfVA-7ZGk2mEOQwOxshpwzhpM' # put your API key here
|
7 |
genai.configure(api_key=api_key)
|
8 |
+
geminiModel = genai.GenerativeModel(model_name='gemini-pro-vision')
|
|
|
|
|
9 |
title="DocTR OCR (PDL Demo)"
|
10 |
description="Upload an image to get the OCR results !"
|
11 |
|
12 |
+
def to_markdown(text):
|
13 |
+
text = text.replace('•', ' *')
|
14 |
+
return Markdown(textwrap.indent(text, '> ', predicate=lambda _: True))
|
15 |
+
|
16 |
def greet(img):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
# generate text
|
18 |
prompt = "the input text in vietnamese, please add accend and take this peace of information and give all the information in point wise better format also give some recomendation related to them: " + res
|
19 |
# print(prompt)
|
20 |
+
response = geminiModel.generate_content([prompt,img], stream=True)
|
21 |
+
response.resolve()
|
22 |
print(response)
|
23 |
res = response.text
|
24 |
_output_name = "RESULT_OCR.txt"
|