Update app.py
Browse files
app.py
CHANGED
@@ -1,62 +1,38 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
import os
|
4 |
-
import pickle
|
5 |
from PIL import Image
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
def
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
image = Image.fromarray(image.astype('uint8'), 'RGB')
|
26 |
-
prediction = classifier(image)
|
27 |
-
return {pred["label"]: pred["score"] for pred in prediction}
|
28 |
-
|
29 |
-
def provide_feedback(image, label):
|
30 |
-
global feedback_data
|
31 |
-
feedback_data["images"].append(image)
|
32 |
-
feedback_data["labels"].append(label)
|
33 |
-
save_feedback()
|
34 |
-
|
35 |
-
if len(feedback_data["images"]) % 5 == 0:
|
36 |
-
retrain_model()
|
37 |
-
return "Feedback saved. Thank you!"
|
38 |
-
|
39 |
-
def retrain_model():
|
40 |
-
global classifier
|
41 |
-
# Here, include the retraining logic using the feedback_data
|
42 |
-
# This is a placeholder for actual retraining logic
|
43 |
-
print("Retraining the model with new data...")
|
44 |
-
|
45 |
-
# Load existing feedback data
|
46 |
-
load_feedback()
|
47 |
|
48 |
with gr.Blocks() as demo:
|
49 |
-
with gr.Tab("
|
50 |
-
image_input = gr.
|
51 |
-
output = gr.
|
52 |
-
|
|
|
53 |
|
54 |
with gr.Tab("Provide Feedback"):
|
55 |
-
image_feedback = gr.
|
56 |
-
|
57 |
feedback_button = gr.Button("Submit Feedback")
|
58 |
feedback_output = gr.Textbox()
|
59 |
-
|
60 |
-
feedback_button.click(fn=provide_feedback, inputs=[image_feedback, label_feedback], outputs=feedback_output)
|
61 |
|
62 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
|
|
|
|
3 |
from PIL import Image
|
4 |
+
import requests
|
5 |
+
import torch
|
6 |
+
|
7 |
+
# Load the pre-trained model and processor
|
8 |
+
processor = TrOCRProcessor.from_pretrained('microsoft/trocr-base-handwritten')
|
9 |
+
model = VisionEncoderDecoderModel.from_pretrained('microsoft/trocr-base-handwritten')
|
10 |
+
|
11 |
+
# Define the prediction function
|
12 |
+
def recognize_handwriting(image):
|
13 |
+
pixel_values = processor(images=image, return_tensors="pt").pixel_values
|
14 |
+
generated_ids = model.generate(pixel_values)
|
15 |
+
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
16 |
+
return generated_text
|
17 |
+
|
18 |
+
def provide_feedback(image, correct_text):
|
19 |
+
# Save the feedback to a file or database for later retraining
|
20 |
+
with open("feedback.txt", "a") as f:
|
21 |
+
f.write(f"{correct_text}\n")
|
22 |
+
return "Feedback received. Thank you!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
with gr.Blocks() as demo:
|
25 |
+
with gr.Tab("Recognize Handwriting"):
|
26 |
+
image_input = gr.Image(type="pil")
|
27 |
+
output = gr.Textbox(label="Recognized Text")
|
28 |
+
recognize_button = gr.Button("Recognize")
|
29 |
+
recognize_button.click(fn=recognize_handwriting, inputs=image_input, outputs=output)
|
30 |
|
31 |
with gr.Tab("Provide Feedback"):
|
32 |
+
image_feedback = gr.Image(type="pil")
|
33 |
+
correct_text = gr.Textbox(label="Correct Text")
|
34 |
feedback_button = gr.Button("Submit Feedback")
|
35 |
feedback_output = gr.Textbox()
|
36 |
+
feedback_button.click(fn=provide_feedback, inputs=[image_feedback, correct_text], outputs=feedback_output)
|
|
|
37 |
|
38 |
demo.launch()
|