Spaces:
Sleeping
Sleeping
Update api.py
Browse files
api.py
CHANGED
@@ -1,25 +1,100 @@
|
|
1 |
-
from fastapi import FastAPI
|
2 |
-
from pydantic import BaseModel
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
from model_loader import ModelLoader
|
4 |
from services.text_filter import TextFilterService
|
5 |
from services.image_ocr import ImageOCRService
|
6 |
-
from typing import Optional
|
7 |
-
from fastapi.responses import JSONResponse
|
8 |
import logging
|
|
|
|
|
|
|
|
|
9 |
|
|
|
10 |
logging.basicConfig(
|
11 |
level=logging.INFO,
|
12 |
-
format="%(asctime)s [%(levelname)s] %(message)s"
|
13 |
-
handlers=[
|
14 |
-
logging.StreamHandler()
|
15 |
-
]
|
16 |
)
|
17 |
-
|
18 |
logger = logging.getLogger(__name__)
|
19 |
|
20 |
-
app =
|
21 |
-
logger.info("Starting
|
22 |
|
|
|
23 |
model_loader = ModelLoader()
|
24 |
logger.info("ModelLoader initialized.")
|
25 |
|
@@ -27,49 +102,41 @@ text_filter_service = TextFilterService(model_loader)
|
|
27 |
logger.info("TextFilterService initialized.")
|
28 |
|
29 |
image_ocr_service = ImageOCRService()
|
30 |
-
logger.info("
|
31 |
|
|
|
|
|
|
|
|
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
image_url: Optional[str] = None
|
36 |
-
|
37 |
-
@app.post("/filtercomment")
|
38 |
-
async def filter_comment(input_data: InputData):
|
39 |
-
logger.info("Received request: %s", input_data)
|
40 |
final_text = ""
|
41 |
-
|
42 |
-
if
|
43 |
-
logger.info("Image URL provided: %s",
|
44 |
try:
|
45 |
-
|
46 |
-
final_text = image_ocr_service.extract_text(input_data.image_url)
|
47 |
logger.info("Generated text: %s", final_text)
|
48 |
-
|
49 |
except Exception as e:
|
50 |
logger.error("Image processing failed: %s", str(e))
|
51 |
-
return
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
logger.info("Text input provided.")
|
56 |
-
final_text = input_data.text
|
57 |
else:
|
58 |
logger.warning("No input provided.")
|
59 |
-
return
|
60 |
|
61 |
try:
|
62 |
-
logger.info("Processing text through TextFilterService...")
|
63 |
results = text_filter_service.process_text(final_text)
|
64 |
results["extracted_text"] = final_text
|
65 |
logger.info("Text filtering complete. Results: %s", results)
|
66 |
-
return results
|
67 |
-
|
68 |
except Exception as e:
|
69 |
logger.exception("Text filtering failed.")
|
70 |
-
return
|
71 |
|
72 |
if __name__ == "__main__":
|
73 |
-
|
74 |
-
|
75 |
-
uvicorn.run(app, host="0.0.0.0", port=3000)
|
|
|
1 |
+
# from fastapi import FastAPI
|
2 |
+
# from pydantic import BaseModel
|
3 |
+
# from model_loader import ModelLoader
|
4 |
+
# from services.text_filter import TextFilterService
|
5 |
+
# from services.image_ocr import ImageOCRService
|
6 |
+
# from typing import Optional
|
7 |
+
# from fastapi.responses import JSONResponse
|
8 |
+
# import logging
|
9 |
+
|
10 |
+
# logging.basicConfig(
|
11 |
+
# level=logging.INFO,
|
12 |
+
# format="%(asctime)s [%(levelname)s] %(message)s",
|
13 |
+
# handlers=[
|
14 |
+
# logging.StreamHandler()
|
15 |
+
# ]
|
16 |
+
# )
|
17 |
+
|
18 |
+
# logger = logging.getLogger(__name__)
|
19 |
+
|
20 |
+
# app = FastAPI()
|
21 |
+
# logger.info("Starting FastAPI app...")
|
22 |
+
|
23 |
+
# model_loader = ModelLoader()
|
24 |
+
# logger.info("ModelLoader initialized.")
|
25 |
+
|
26 |
+
# text_filter_service = TextFilterService(model_loader)
|
27 |
+
# logger.info("TextFilterService initialized.")
|
28 |
+
|
29 |
+
# image_ocr_service = ImageOCRService()
|
30 |
+
# logger.info("Image OCR image initialized")
|
31 |
+
|
32 |
+
|
33 |
+
# class InputData(BaseModel):
|
34 |
+
# text: Optional[str] = None
|
35 |
+
# image_url: Optional[str] = None
|
36 |
+
|
37 |
+
# @app.post("/filtercomment")
|
38 |
+
# async def filter_comment(input_data: InputData):
|
39 |
+
# logger.info("Received request: %s", input_data)
|
40 |
+
# final_text = ""
|
41 |
+
# # Case 1: Extract text from image
|
42 |
+
# if input_data.image_url:
|
43 |
+
# logger.info("Image URL provided: %s", input_data.image_url)
|
44 |
+
# try:
|
45 |
+
# logger.info("Fetching image from URL...")
|
46 |
+
# final_text = image_ocr_service.extract_text(input_data.image_url)
|
47 |
+
# logger.info("Generated text: %s", final_text)
|
48 |
+
|
49 |
+
# except Exception as e:
|
50 |
+
# logger.error("Image processing failed: %s", str(e))
|
51 |
+
# return JSONResponse(status_code=400, content={"error": f"Image processing failed: {str(e)}"})
|
52 |
+
|
53 |
+
# # Case 2: Use provided text
|
54 |
+
# elif input_data.text:
|
55 |
+
# logger.info("Text input provided.")
|
56 |
+
# final_text = input_data.text
|
57 |
+
# else:
|
58 |
+
# logger.warning("No input provided.")
|
59 |
+
# return JSONResponse(status_code=400, content={"error": "Either 'text' or 'image_url' must be provided."})
|
60 |
+
|
61 |
+
# try:
|
62 |
+
# logger.info("Processing text through TextFilterService...")
|
63 |
+
# results = text_filter_service.process_text(final_text)
|
64 |
+
# results["extracted_text"] = final_text
|
65 |
+
# logger.info("Text filtering complete. Results: %s", results)
|
66 |
+
# return results
|
67 |
+
|
68 |
+
# except Exception as e:
|
69 |
+
# logger.exception("Text filtering failed.")
|
70 |
+
# return JSONResponse(status_code=500, content={"error": f"Text filtering failed: {str(e)}"})
|
71 |
+
|
72 |
+
# if __name__ == "__main__":
|
73 |
+
# import uvicorn
|
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.image_ocr import ImageOCRService
|
|
|
|
|
81 |
import logging
|
82 |
+
import os
|
83 |
+
|
84 |
+
# Set Hugging Face cache directory
|
85 |
+
os.environ["HF_HOME"] = "/app/cache"
|
86 |
|
87 |
+
# Logging setup
|
88 |
logging.basicConfig(
|
89 |
level=logging.INFO,
|
90 |
+
format="%(asctime)s [%(levelname)s] %(message)s"
|
|
|
|
|
|
|
91 |
)
|
|
|
92 |
logger = logging.getLogger(__name__)
|
93 |
|
94 |
+
app = Flask(__name__)
|
95 |
+
logger.info("Starting Flask app...")
|
96 |
|
97 |
+
# Load model and services
|
98 |
model_loader = ModelLoader()
|
99 |
logger.info("ModelLoader initialized.")
|
100 |
|
|
|
102 |
logger.info("TextFilterService initialized.")
|
103 |
|
104 |
image_ocr_service = ImageOCRService()
|
105 |
+
logger.info("ImageOCRService initialized.")
|
106 |
|
107 |
+
@app.route("/filtercomment", methods=["POST"])
|
108 |
+
def filter_comment():
|
109 |
+
data = request.get_json()
|
110 |
+
logger.info("Received request: %s", data)
|
111 |
|
112 |
+
text = data.get("text")
|
113 |
+
image_url = data.get("image_url")
|
|
|
|
|
|
|
|
|
|
|
114 |
final_text = ""
|
115 |
+
|
116 |
+
if image_url:
|
117 |
+
logger.info("Image URL provided: %s", image_url)
|
118 |
try:
|
119 |
+
final_text = image_ocr_service.extract_text(image_url)
|
|
|
120 |
logger.info("Generated text: %s", final_text)
|
|
|
121 |
except Exception as e:
|
122 |
logger.error("Image processing failed: %s", str(e))
|
123 |
+
return jsonify({"error": f"Image processing failed: {str(e)}"}), 400
|
124 |
|
125 |
+
elif text:
|
126 |
+
final_text = text
|
|
|
|
|
127 |
else:
|
128 |
logger.warning("No input provided.")
|
129 |
+
return jsonify({"error": "Either 'text' or 'image_url' must be provided."}), 400
|
130 |
|
131 |
try:
|
|
|
132 |
results = text_filter_service.process_text(final_text)
|
133 |
results["extracted_text"] = final_text
|
134 |
logger.info("Text filtering complete. Results: %s", results)
|
135 |
+
return jsonify(results)
|
|
|
136 |
except Exception as e:
|
137 |
logger.exception("Text filtering failed.")
|
138 |
+
return jsonify({"error": f"Text filtering failed: {str(e)}"}), 500
|
139 |
|
140 |
if __name__ == "__main__":
|
141 |
+
app.run(host="0.0.0.0", port=7860)
|
142 |
+
|
|