winamnd commited on
Commit
f9afb31
·
verified ·
1 Parent(s): cf84747

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -25
app.py CHANGED
@@ -5,12 +5,12 @@ import csv
5
  import os
6
  import cv2
7
  import numpy as np
8
- import pandas as pd
9
  import easyocr
10
  import keras_ocr
11
  from paddleocr import PaddleOCR
12
  from transformers import DistilBertTokenizer, DistilBertForSequenceClassification
13
  import torch.nn.functional as F # Added for softmax
 
14
 
15
  # Paths
16
  MODEL_PATH = "./distilbert_spam_model"
@@ -74,33 +74,11 @@ def generate_ocr(method, img):
74
  label_map = {0: "Not Spam", 1: "Spam"}
75
  label = label_map[prediction]
76
 
77
- # Save results
78
- save_results(text_output, label)
79
 
80
  return text_output, label
81
 
82
- # Save extracted text to JSON & CSV
83
- def save_results(text, label):
84
- data = {"text": text, "label": label}
85
-
86
- # Save to JSON
87
- if not os.path.exists(RESULTS_JSON):
88
- with open(RESULTS_JSON, "w") as f:
89
- json.dump([], f)
90
- with open(RESULTS_JSON, "r+") as f:
91
- content = json.load(f)
92
- content.append(data)
93
- f.seek(0)
94
- json.dump(content, f, indent=4)
95
-
96
- # Save to CSV
97
- file_exists = os.path.exists(RESULTS_CSV)
98
- with open(RESULTS_CSV, "a", newline="") as f:
99
- writer = csv.DictWriter(f, fieldnames=["text", "label"])
100
- if not file_exists:
101
- writer.writeheader()
102
- writer.writerow(data)
103
-
104
  # Gradio Interface
105
  image_input = gr.Image()
106
  method_input = gr.Radio(["PaddleOCR", "EasyOCR", "KerasOCR"], value="PaddleOCR")
 
5
  import os
6
  import cv2
7
  import numpy as np
 
8
  import easyocr
9
  import keras_ocr
10
  from paddleocr import PaddleOCR
11
  from transformers import DistilBertTokenizer, DistilBertForSequenceClassification
12
  import torch.nn.functional as F # Added for softmax
13
+ from save_results import save_results_to_repo # Import the new save function
14
 
15
  # Paths
16
  MODEL_PATH = "./distilbert_spam_model"
 
74
  label_map = {0: "Not Spam", 1: "Spam"}
75
  label = label_map[prediction]
76
 
77
+ # Save results using the external save function
78
+ save_results_to_repo(text_output, label)
79
 
80
  return text_output, label
81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  # Gradio Interface
83
  image_input = gr.Image()
84
  method_input = gr.Radio(["PaddleOCR", "EasyOCR", "KerasOCR"], value="PaddleOCR")