Commit
·
2ee994f
1
Parent(s):
dbe0dd0
update_new_new_new_new
Browse files- HTR/strike.py +13 -8
- HTR/word.py +8 -6
- main.py +31 -27
HTR/strike.py
CHANGED
@@ -5,24 +5,29 @@ import os
|
|
5 |
import cv2
|
6 |
from transformers import AutoModelForImageClassification
|
7 |
import logging
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
# Initialize model globally
|
11 |
model = None
|
12 |
-
|
13 |
-
# Set up logging
|
14 |
-
logging.basicConfig(level=logging.INFO)
|
15 |
-
logger = logging.getLogger(__name__)
|
16 |
|
17 |
def initialize_model():
|
18 |
global model
|
19 |
if model is None:
|
20 |
try:
|
|
|
21 |
model = AutoModelForImageClassification.from_pretrained("models/vit-base-beans")
|
22 |
if torch.cuda.is_available():
|
23 |
model = model.to('cuda')
|
|
|
|
|
24 |
except Exception as e:
|
25 |
-
|
26 |
raise
|
27 |
|
28 |
def image_preprocessing(image):
|
@@ -37,7 +42,7 @@ def image_preprocessing(image):
|
|
37 |
images.append(binary_image)
|
38 |
return images
|
39 |
except Exception as e:
|
40 |
-
|
41 |
return []
|
42 |
|
43 |
def predict_image(image_path, model):
|
@@ -56,7 +61,7 @@ def predict_image(image_path, model):
|
|
56 |
predictions = model(images).logits.detach().cpu().numpy()
|
57 |
return predictions
|
58 |
except Exception as e:
|
59 |
-
|
60 |
return None
|
61 |
|
62 |
def struck_images(image_paths):
|
|
|
5 |
import cv2
|
6 |
from transformers import AutoModelForImageClassification
|
7 |
import logging
|
8 |
+
|
9 |
+
logging.basicConfig(
|
10 |
+
level=logging.INFO,
|
11 |
+
format='%(asctime)s - %(levelname)s - %(message)s'
|
12 |
+
)
|
13 |
+
logger = logging.getLogger(__name__)
|
14 |
|
15 |
# Initialize model globally
|
16 |
model = None
|
17 |
+
TEMP_IMAGES_DIR = None
|
|
|
|
|
|
|
18 |
|
19 |
def initialize_model():
|
20 |
global model
|
21 |
if model is None:
|
22 |
try:
|
23 |
+
logger.info("Initializing model...")
|
24 |
model = AutoModelForImageClassification.from_pretrained("models/vit-base-beans")
|
25 |
if torch.cuda.is_available():
|
26 |
model = model.to('cuda')
|
27 |
+
logger.info("Model moved to CUDA")
|
28 |
+
logger.info("Model initialized successfully")
|
29 |
except Exception as e:
|
30 |
+
logger.error(f"Error initializing model: {str(e)}")
|
31 |
raise
|
32 |
|
33 |
def image_preprocessing(image):
|
|
|
42 |
images.append(binary_image)
|
43 |
return images
|
44 |
except Exception as e:
|
45 |
+
logger.error(f"Error in image_preprocessing: {str(e)}")
|
46 |
return []
|
47 |
|
48 |
def predict_image(image_path, model):
|
|
|
61 |
predictions = model(images).logits.detach().cpu().numpy()
|
62 |
return predictions
|
63 |
except Exception as e:
|
64 |
+
logger.error(f"Error in predict_image: {str(e)}")
|
65 |
return None
|
66 |
|
67 |
def struck_images(image_paths):
|
HTR/word.py
CHANGED
@@ -6,14 +6,16 @@ import os
|
|
6 |
import tempfile
|
7 |
import logging
|
8 |
|
9 |
-
|
10 |
-
|
|
|
|
|
11 |
logger = logging.getLogger(__name__)
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
def four_point_transform(image, pts):
|
18 |
try:
|
19 |
rect = pts
|
|
|
6 |
import tempfile
|
7 |
import logging
|
8 |
|
9 |
+
logging.basicConfig(
|
10 |
+
level=logging.INFO,
|
11 |
+
format='%(asctime)s - %(levelname)s - %(message)s'
|
12 |
+
)
|
13 |
logger = logging.getLogger(__name__)
|
14 |
|
15 |
+
# Global variables
|
16 |
+
TEMP_IMAGES_DIR = None
|
17 |
+
cordinates = []
|
18 |
+
|
19 |
def four_point_transform(image, pts):
|
20 |
try:
|
21 |
rect = pts
|
main.py
CHANGED
@@ -6,6 +6,8 @@ import sys
|
|
6 |
import builtins
|
7 |
from datetime import datetime
|
8 |
from flask_cors import CORS
|
|
|
|
|
9 |
from dotenv import load_dotenv
|
10 |
|
11 |
# Load environment variables
|
@@ -44,14 +46,14 @@ except Exception as e:
|
|
44 |
|
45 |
# Set up logging to both file and console
|
46 |
try:
|
47 |
-
logging.basicConfig(
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
)
|
55 |
except Exception as e:
|
56 |
print(f"Warning: Could not set up file logging: {e}")
|
57 |
# Fallback to console-only logging
|
@@ -107,7 +109,7 @@ for data in required_nltk_data:
|
|
107 |
try:
|
108 |
log_print(f"Downloading NLTK data: {data}")
|
109 |
nltk.download(data, download_dir=nltk_data_dir)
|
110 |
-
except Exception as e:
|
111 |
log_print(f"Error downloading NLTK data {data}: {e}", "ERROR")
|
112 |
raise
|
113 |
|
@@ -124,8 +126,10 @@ from similarity_check.llm_based_scoring.llm import llm_score
|
|
124 |
|
125 |
app = Flask(__name__)
|
126 |
|
127 |
-
#
|
128 |
-
UPLOAD_FOLDER =
|
|
|
|
|
129 |
|
130 |
# Configure CORS for Hugging Face
|
131 |
CORS(app, resources={
|
@@ -155,11 +159,11 @@ def compute_answers():
|
|
155 |
return jsonify({"error": "Missing query file"}), 400
|
156 |
|
157 |
try:
|
158 |
-
|
159 |
if not queries:
|
160 |
log_print("No queries found in file", "ERROR")
|
161 |
return jsonify({"error": "No queries found in file"}), 400
|
162 |
-
|
163 |
except UnicodeDecodeError:
|
164 |
log_print("Invalid file encoding", "ERROR")
|
165 |
return jsonify({"error": "Invalid file encoding"}), 400
|
@@ -177,12 +181,12 @@ def compute_answers():
|
|
177 |
return jsonify({"error": "Missing answer CSV file"}), 400
|
178 |
|
179 |
try:
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
except UnicodeDecodeError:
|
187 |
log_print("Invalid CSV file encoding", "ERROR")
|
188 |
return jsonify({"error": "Invalid CSV file encoding"}), 400
|
@@ -253,14 +257,14 @@ def compute_marks():
|
|
253 |
return jsonify({"error": "No answers provided"}), 400
|
254 |
|
255 |
try:
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
except json.JSONDecodeError:
|
265 |
log_print("Invalid JSON format in answers", "ERROR")
|
266 |
return jsonify({"error": "Invalid JSON format in answers"}), 400
|
|
|
6 |
import builtins
|
7 |
from datetime import datetime
|
8 |
from flask_cors import CORS
|
9 |
+
from flask import Flask, request, jsonify, render_template
|
10 |
+
from werkzeug.utils import secure_filename
|
11 |
from dotenv import load_dotenv
|
12 |
|
13 |
# Load environment variables
|
|
|
46 |
|
47 |
# Set up logging to both file and console
|
48 |
try:
|
49 |
+
logging.basicConfig(
|
50 |
+
level=logging.INFO,
|
51 |
+
format='%(asctime)s - %(levelname)s - %(message)s',
|
52 |
+
handlers=[
|
53 |
+
logging.FileHandler(log_file),
|
54 |
+
logging.StreamHandler(sys.stdout)
|
55 |
+
]
|
56 |
+
)
|
57 |
except Exception as e:
|
58 |
print(f"Warning: Could not set up file logging: {e}")
|
59 |
# Fallback to console-only logging
|
|
|
109 |
try:
|
110 |
log_print(f"Downloading NLTK data: {data}")
|
111 |
nltk.download(data, download_dir=nltk_data_dir)
|
112 |
+
except Exception as e:
|
113 |
log_print(f"Error downloading NLTK data {data}: {e}", "ERROR")
|
114 |
raise
|
115 |
|
|
|
126 |
|
127 |
app = Flask(__name__)
|
128 |
|
129 |
+
# Create a temporary directory for file uploads
|
130 |
+
UPLOAD_FOLDER = tempfile.mkdtemp()
|
131 |
+
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
132 |
+
logger.info(f"Using temporary upload folder: {UPLOAD_FOLDER}")
|
133 |
|
134 |
# Configure CORS for Hugging Face
|
135 |
CORS(app, resources={
|
|
|
159 |
return jsonify({"error": "Missing query file"}), 400
|
160 |
|
161 |
try:
|
162 |
+
queries = query_file.read().decode('utf-8').splitlines()
|
163 |
if not queries:
|
164 |
log_print("No queries found in file", "ERROR")
|
165 |
return jsonify({"error": "No queries found in file"}), 400
|
166 |
+
log_print(f"Received queries: {queries}")
|
167 |
except UnicodeDecodeError:
|
168 |
log_print("Invalid file encoding", "ERROR")
|
169 |
return jsonify({"error": "Invalid file encoding"}), 400
|
|
|
181 |
return jsonify({"error": "Missing answer CSV file"}), 400
|
182 |
|
183 |
try:
|
184 |
+
ans_csv_file = ans_csv_file.read().decode('utf-8').splitlines()
|
185 |
+
c_answers = []
|
186 |
+
for i in ans_csv_file:
|
187 |
+
c_answers.append(i.split('\\n'))
|
188 |
+
log_print(f"Processed CSV answers: {c_answers}")
|
189 |
+
return jsonify({"answers": c_answers}), 200
|
190 |
except UnicodeDecodeError:
|
191 |
log_print("Invalid CSV file encoding", "ERROR")
|
192 |
return jsonify({"error": "Invalid CSV file encoding"}), 400
|
|
|
257 |
return jsonify({"error": "No answers provided"}), 400
|
258 |
|
259 |
try:
|
260 |
+
log_print("=== Processing Answers ===")
|
261 |
+
log_print(f"Received answers: {a}")
|
262 |
+
a = json.loads(a)
|
263 |
+
answers = []
|
264 |
+
for i in a:
|
265 |
+
ans = i.split('\n\n')
|
266 |
+
answers.append(ans)
|
267 |
+
log_print(f"Processed answers structure: {answers}")
|
268 |
except json.JSONDecodeError:
|
269 |
log_print("Invalid JSON format in answers", "ERROR")
|
270 |
return jsonify({"error": "Invalid JSON format in answers"}), 400
|