Spaces:
Sleeping
Sleeping
Chnaged requirement text
Browse files- app/routes.py +34 -29
app/routes.py
CHANGED
@@ -4,6 +4,8 @@ import os
|
|
4 |
import easyocr
|
5 |
import pytesseract # Ensure this is imported
|
6 |
from PIL import Image
|
|
|
|
|
7 |
from app.models import audio_model, sentiment_pipeline, emotion_pipeline
|
8 |
from app.services import extract_tasks
|
9 |
from app.utils import generate_tags, error_response
|
@@ -13,11 +15,14 @@ bp = Blueprint('main', __name__)
|
|
13 |
|
14 |
model_dir = os.getenv('EASYOCR_MODEL_STORAGE', None)
|
15 |
|
16 |
-
#
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
21 |
|
22 |
EMOTION_SCORE_THRESHOLD = 0.15 # Adjust based on your testing
|
23 |
MIN_SENTIMENT_CONFIDENCE = 0.4 # Below this becomes "neutral"
|
@@ -64,33 +69,33 @@ def analyze_image():
|
|
64 |
return error_response("No image file provided", 400)
|
65 |
|
66 |
file = request.files['file']
|
67 |
-
|
68 |
-
file_path = os.path.join("/tmp", filename)
|
69 |
-
file.save(file_path)
|
70 |
|
71 |
try:
|
72 |
-
#
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
|
|
|
|
88 |
return jsonify({
|
89 |
-
"extracted_text":
|
90 |
-
"sentiment":
|
91 |
-
"emotion":
|
92 |
-
"confidence":
|
93 |
-
"tags":
|
94 |
})
|
95 |
except Exception as e:
|
96 |
return error_response(str(e), 500)
|
|
|
4 |
import easyocr
|
5 |
import pytesseract # Ensure this is imported
|
6 |
from PIL import Image
|
7 |
+
|
8 |
+
from app.config import Config
|
9 |
from app.models import audio_model, sentiment_pipeline, emotion_pipeline
|
10 |
from app.services import extract_tasks
|
11 |
from app.utils import generate_tags, error_response
|
|
|
15 |
|
16 |
model_dir = os.getenv('EASYOCR_MODEL_STORAGE', None)
|
17 |
|
18 |
+
# ββ OCR via HF Inference API βββββββββββββββββββββββββββββββββββββββββββββββββ
|
19 |
+
# We're using Microsoft's TrOCR for printed text:
|
20 |
+
HF_API_TOKEN = Config.FIREWORKS_API_KEY
|
21 |
+
ocr_api = InferenceApi(
|
22 |
+
repo_id="microsoft/trocr-base-printed",
|
23 |
+
token=HF_API_TOKEN,
|
24 |
+
inference_type="text-generation" # TroCR is a seq2seq model
|
25 |
+
)
|
26 |
|
27 |
EMOTION_SCORE_THRESHOLD = 0.15 # Adjust based on your testing
|
28 |
MIN_SENTIMENT_CONFIDENCE = 0.4 # Below this becomes "neutral"
|
|
|
69 |
return error_response("No image file provided", 400)
|
70 |
|
71 |
file = request.files['file']
|
72 |
+
image_bytes = file.read()
|
|
|
|
|
73 |
|
74 |
try:
|
75 |
+
# send raw bytes to HF inference
|
76 |
+
result = ocr_api(image_bytes)
|
77 |
+
# TroCR returns a single string of text
|
78 |
+
extracted = ""
|
79 |
+
if isinstance(result, str):
|
80 |
+
extracted = result
|
81 |
+
elif isinstance(result, dict) and "generated_text" in result:
|
82 |
+
extracted = result["generated_text"]
|
83 |
+
else:
|
84 |
+
# fallback to printing whatever we got
|
85 |
+
extracted = str(result)
|
86 |
+
|
87 |
+
extracted = extracted.strip()
|
88 |
+
if not extracted:
|
89 |
+
return error_response("No text extracted from image", 400)
|
90 |
+
|
91 |
+
analysis = analyze_text_internal(extracted)
|
92 |
+
tags = generate_tags(extracted)
|
93 |
return jsonify({
|
94 |
+
"extracted_text": extracted,
|
95 |
+
"sentiment": analysis["sentiment"],
|
96 |
+
"emotion": analysis["emotion"],
|
97 |
+
"confidence": analysis["confidence"],
|
98 |
+
"tags": tags
|
99 |
})
|
100 |
except Exception as e:
|
101 |
return error_response(str(e), 500)
|