Spaces:
Runtime error
Runtime error
wddw12332e
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
|
4 |
+
def correct_spell(inputs):
|
5 |
+
return "res"
|
6 |
+
|
7 |
+
def process_text_in_chunks(text, process_function, max_chunk_size=256):
|
8 |
+
# Split text into sentences
|
9 |
+
sentences = re.split(r'(?<=[.!?])\s+', text)
|
10 |
+
processed_text = ""
|
11 |
+
|
12 |
+
for sentence in sentences:
|
13 |
+
# Further split long sentences into smaller chunks
|
14 |
+
chunks = [sentence[i:i + max_chunk_size] for i in range(0, len(sentence), max_chunk_size)]
|
15 |
+
for chunk in chunks:
|
16 |
+
processed_text += process_function(chunk)
|
17 |
+
processed_text += " " # Add space after each processed sentence
|
18 |
+
|
19 |
+
return processed_text.strip()
|
20 |
+
|
21 |
+
def greet(img, apply_grammar_correction, apply_spell_check,lang_of_input):
|
22 |
+
|
23 |
+
if (lang_of_input=="Hindi"):
|
24 |
+
res = pt.image_to_string(img,lang='hin')
|
25 |
+
_output_name = "RESULT_OCR.txt"
|
26 |
+
open(_output_name, 'w').write(res)
|
27 |
+
return res, _output_name
|
28 |
+
|
29 |
+
if (lang_of_input=="Punjabi"):
|
30 |
+
res = pt.image_to_string(img,lang='pan')
|
31 |
+
_output_name = "RESULT_OCR.txt"
|
32 |
+
open(_output_name, 'w').write(res)
|
33 |
+
return res, _output_name
|
34 |
+
|
35 |
+
|
36 |
+
img.save("out.jpg")
|
37 |
+
doc = DocumentFile.from_images("out.jpg")
|
38 |
+
output = OCRpredictor(doc)
|
39 |
+
|
40 |
+
res = ""
|
41 |
+
for obj in output.pages:
|
42 |
+
for obj1 in obj.blocks:
|
43 |
+
for obj2 in obj1.lines:
|
44 |
+
for obj3 in obj2.words:
|
45 |
+
res += " " + obj3.value
|
46 |
+
res += "\n"
|
47 |
+
res += "\n"
|
48 |
+
|
49 |
+
# Process in chunks for grammar correction
|
50 |
+
if apply_grammar_correction:
|
51 |
+
res = process_text_in_chunks(res, lambda x: happy_tt.generate_text("grammar: " + x, args=grammar_args).text)
|
52 |
+
|
53 |
+
# Process in chunks for spell check
|
54 |
+
if apply_spell_check:
|
55 |
+
res = process_text_in_chunks(res, correct_spell)
|
56 |
+
|
57 |
+
_output_name = "RESULT_OCR.txt"
|
58 |
+
open(_output_name, 'w').write(res)
|
59 |
+
return res, _output_name
|
60 |
+
|
61 |
+
# Gradio Interface for OCR
|
62 |
+
demo_ocr = gr.Interface(
|
63 |
+
fn=greet,
|
64 |
+
inputs=[
|
65 |
+
gr.Image(type="pil"),
|
66 |
+
gr.Checkbox(label="Apply Grammar Correction"),
|
67 |
+
gr.Checkbox(label="Apply Spell Check"),
|
68 |
+
gr.Dropdown(["English","Hindi","Punjabi"],label="Select Language")
|
69 |
+
],
|
70 |
+
outputs=["text", "file"],
|
71 |
+
title="DocTR OCR with Grammar and Spell Check",
|
72 |
+
description="Upload an image to get the OCR results. Optionally, apply grammar and spell check."
|
73 |
+
)
|
74 |
+
|
75 |
+
|
76 |
+
# demo_ocr.launch(debug=True)
|
77 |
+
|
78 |
+
def split_text_into_batches(text, max_tokens_per_batch):
|
79 |
+
sentences = text # Tokenize text into sentences
|
80 |
+
batches = []
|
81 |
+
current_batch = ""
|
82 |
+
for sentence in sentences:
|
83 |
+
if len(current_batch) + len(sentence) + 1 <= max_tokens_per_batch: # Add 1 for space
|
84 |
+
current_batch += sentence + " " # Add sentence to current batch
|
85 |
+
else:
|
86 |
+
batches.append(current_batch.strip()) # Add current batch to batches list
|
87 |
+
current_batch = sentence + " " # Start a new batch with the current sentence
|
88 |
+
if current_batch:
|
89 |
+
batches.append(current_batch.strip()) # Add the last batch
|
90 |
+
return batches
|
91 |
+
|
92 |
+
|
93 |
+
def run_t2tt(file_uploader , input_text: str, source_language: str, target_language: str) -> (str, bytes):
|
94 |
+
if file_uploader is not None:
|
95 |
+
with open(file_uploader, 'r') as file:
|
96 |
+
input_text=file.read()
|
97 |
+
source_language_code = []
|
98 |
+
target_language_code = []
|
99 |
+
max_tokens_per_batch= 256
|
100 |
+
batches = split_text_into_batches(input_text, max_tokens_per_batch)
|
101 |
+
translated_text = ""
|
102 |
+
return "hello"
|
103 |
+
|
104 |
+
with gr.Blocks() as demo_t2tt:
|
105 |
+
with gr.Row():
|
106 |
+
with gr.Column():
|
107 |
+
with gr.Group():
|
108 |
+
file_uploader = gr.File(label="Upload a text file (Optional)")
|
109 |
+
input_text = gr.Textbox(label="Input text")
|
110 |
+
with gr.Row():
|
111 |
+
source_language = gr.Dropdown(
|
112 |
+
label="Source language",
|
113 |
+
choices=[],
|
114 |
+
value="Punjabi",
|
115 |
+
)
|
116 |
+
target_language = gr.Dropdown(
|
117 |
+
label="Target language",
|
118 |
+
choices=[],
|
119 |
+
value=[],
|
120 |
+
)
|
121 |
+
btn = gr.Button("Translate")
|
122 |
+
with gr.Column():
|
123 |
+
output_text = gr.Textbox(label="Translated text")
|
124 |
+
output_file = gr.File(label="Translated text file")
|
125 |
+
|
126 |
+
gr.on(
|
127 |
+
triggers=[input_text.submit, btn.click],
|
128 |
+
fn=run_t2tt,
|
129 |
+
inputs=[file_uploader, input_text, source_language, target_language],
|
130 |
+
outputs=[output_text, output_file],
|
131 |
+
api_name="t2tt",
|
132 |
+
)
|
133 |
+
|
134 |
+
with gr.Blocks() as demo:
|
135 |
+
with gr.Tabs():
|
136 |
+
with gr.Tab(label="OCR"):
|
137 |
+
demo_ocr.render()
|
138 |
+
with gr.Tab(label="Translate"):
|
139 |
+
demo_t2tt.render()
|
140 |
+
|
141 |
+
if __name__ == "__main__":
|
142 |
+
demo.launch()
|