Spaces:
Running
Running
Update api.py
Browse files
api.py
CHANGED
@@ -74,79 +74,99 @@
|
|
74 |
# logger.info("Starting Uvicorn server...")
|
75 |
# uvicorn.run(app, host="0.0.0.0", port=3000)
|
76 |
|
77 |
-
from flask import Flask, request, jsonify
|
78 |
-
from flask_cors import CORS
|
79 |
from model_loader import ModelLoader
|
80 |
from services.text_filter import TextFilterService
|
81 |
-
from services.
|
|
|
|
|
|
|
82 |
import logging
|
83 |
-
import
|
84 |
-
import json
|
85 |
-
# Set Hugging Face cache directory
|
86 |
-
os.environ["HF_HOME"] = "/app/cache"
|
87 |
|
88 |
-
# Logging setup
|
89 |
logging.basicConfig(
|
90 |
level=logging.INFO,
|
91 |
-
format="%(asctime)s [%(levelname)s] %(message)s"
|
|
|
|
|
|
|
92 |
)
|
|
|
93 |
logger = logging.getLogger(__name__)
|
94 |
|
95 |
app = Flask(__name__)
|
96 |
logger.info("Starting Flask app...")
|
97 |
-
|
98 |
-
# Load model and services
|
99 |
model_loader = ModelLoader()
|
100 |
logger.info("ModelLoader initialized.")
|
101 |
|
102 |
text_filter_service = TextFilterService(model_loader)
|
103 |
logger.info("TextFilterService initialized.")
|
104 |
|
105 |
-
|
106 |
-
logger.info("
|
107 |
|
108 |
@app.route("/filtercomment", methods=["POST"])
|
109 |
def filter_comment():
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
image_url
|
115 |
final_text = ""
|
116 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
if image_url:
|
118 |
logger.info("Image URL provided: %s", image_url)
|
119 |
try:
|
120 |
-
|
|
|
|
|
121 |
logger.info("Generated text: %s", final_text)
|
122 |
except Exception as e:
|
123 |
logger.error("Image processing failed: %s", str(e))
|
124 |
return jsonify({"error": f"Image processing failed: {str(e)}"}), 400
|
125 |
|
|
|
126 |
elif text:
|
|
|
127 |
final_text = text
|
128 |
else:
|
129 |
logger.warning("No input provided.")
|
130 |
-
return jsonify({"error": "Either 'text' or '
|
131 |
|
132 |
try:
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
|
146 |
except Exception as e:
|
147 |
logger.exception("Text filtering failed.")
|
148 |
return jsonify({"error": f"Text filtering failed: {str(e)}"}), 500
|
149 |
|
150 |
if __name__ == "__main__":
|
|
|
151 |
app.run(host="0.0.0.0", port=7860)
|
152 |
|
|
|
74 |
# logger.info("Starting Uvicorn server...")
|
75 |
# uvicorn.run(app, host="0.0.0.0", port=3000)
|
76 |
|
77 |
+
from flask import Flask, request, jsonify
|
|
|
78 |
from model_loader import ModelLoader
|
79 |
from services.text_filter import TextFilterService
|
80 |
+
from services.llm_text_filter import ArticleClassifier
|
81 |
+
from services.image_ocr import ImageClassifier
|
82 |
+
from typing import Optional
|
83 |
+
from PIL import Image
|
84 |
import logging
|
85 |
+
import io
|
|
|
|
|
|
|
86 |
|
|
|
87 |
logging.basicConfig(
|
88 |
level=logging.INFO,
|
89 |
+
format="%(asctime)s [%(levelname)s] %(message)s",
|
90 |
+
handlers=[
|
91 |
+
logging.StreamHandler()
|
92 |
+
]
|
93 |
)
|
94 |
+
|
95 |
logger = logging.getLogger(__name__)
|
96 |
|
97 |
app = Flask(__name__)
|
98 |
logger.info("Starting Flask app...")
|
99 |
+
|
|
|
100 |
model_loader = ModelLoader()
|
101 |
logger.info("ModelLoader initialized.")
|
102 |
|
103 |
text_filter_service = TextFilterService(model_loader)
|
104 |
logger.info("TextFilterService initialized.")
|
105 |
|
106 |
+
image_classifier = ImageClassifier()
|
107 |
+
logger.info("ImageClassifier initialized.")
|
108 |
|
109 |
@app.route("/filtercomment", methods=["POST"])
|
110 |
def filter_comment():
|
111 |
+
text = request.form.get("text")
|
112 |
+
image_url = request.form.get("image_url")
|
113 |
+
article = request.form.get("article", "false").lower() == "true"
|
114 |
+
image_file = request.files.get("image_file")
|
115 |
+
logger.info("Received request: text=%s, image_url=%s, article=%s, image_file=%s", text, image_url, article, image_file.filename if image_file else None)
|
116 |
final_text = ""
|
117 |
|
118 |
+
# Case 1: Image file upload
|
119 |
+
if image_file:
|
120 |
+
try:
|
121 |
+
logger.info("Processing uploaded image file: %s", image_file.filename)
|
122 |
+
image_bytes = image_file.read()
|
123 |
+
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
124 |
+
result = image_classifier.classify(image)
|
125 |
+
logger.info("Image classification result: %s", result)
|
126 |
+
return jsonify({"image_classification": result})
|
127 |
+
except Exception as e:
|
128 |
+
logger.error("Image file processing failed: %s", str(e))
|
129 |
+
return jsonify({"error": f"Image file processing failed: {str(e)}"}), 400
|
130 |
+
|
131 |
+
# Case 2: Extract text from image URL
|
132 |
if image_url:
|
133 |
logger.info("Image URL provided: %s", image_url)
|
134 |
try:
|
135 |
+
logger.info("Fetching image from URL...")
|
136 |
+
# You need to implement image_ocr_service.extract_text or use image_classifier if that's your OCR
|
137 |
+
final_text = image_classifier.extract_text(image_url)
|
138 |
logger.info("Generated text: %s", final_text)
|
139 |
except Exception as e:
|
140 |
logger.error("Image processing failed: %s", str(e))
|
141 |
return jsonify({"error": f"Image processing failed: {str(e)}"}), 400
|
142 |
|
143 |
+
# Case 3: Use provided text
|
144 |
elif text:
|
145 |
+
logger.info("Text input provided.")
|
146 |
final_text = text
|
147 |
else:
|
148 |
logger.warning("No input provided.")
|
149 |
+
return jsonify({"error": "Either 'text', 'image_url', or 'image_file' must be provided."}), 400
|
150 |
|
151 |
try:
|
152 |
+
logger.info("Processing text through TextFilterService...")
|
153 |
+
if article:
|
154 |
+
logger.info("Classifying article...")
|
155 |
+
classifier = ArticleClassifier(final_text)
|
156 |
+
result = classifier.classify()
|
157 |
+
logger.info("Article classification complete: %s", final_text)
|
158 |
+
return jsonify(result)
|
159 |
+
else:
|
160 |
+
results = text_filter_service.process_text(final_text)
|
161 |
+
results["extracted_text"] = final_text
|
162 |
+
logger.info("Text filtering complete. Results: %s", results)
|
163 |
+
return jsonify(results)
|
164 |
|
165 |
except Exception as e:
|
166 |
logger.exception("Text filtering failed.")
|
167 |
return jsonify({"error": f"Text filtering failed: {str(e)}"}), 500
|
168 |
|
169 |
if __name__ == "__main__":
|
170 |
+
logger.info("Starting Flask server...")
|
171 |
app.run(host="0.0.0.0", port=7860)
|
172 |
|