Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- app/services/vision_service.py +40 -11
app/services/vision_service.py
CHANGED
|
@@ -1,36 +1,65 @@
|
|
| 1 |
import logging
|
|
|
|
|
|
|
| 2 |
from gradio_client import Client, handle_file
|
| 3 |
from app.core.config import settings
|
| 4 |
|
| 5 |
class VisionService:
|
| 6 |
def __init__(self):
|
| 7 |
self.client = None
|
|
|
|
| 8 |
try:
|
| 9 |
-
|
|
|
|
|
|
|
| 10 |
except Exception as e:
|
| 11 |
-
logging.error(f"Failed to initialize
|
|
|
|
| 12 |
|
| 13 |
async def describe_screenshot(self, image_path: str):
|
| 14 |
if not self.client:
|
|
|
|
| 15 |
return "Vision service not available."
|
| 16 |
|
|
|
|
|
|
|
|
|
|
| 17 |
try:
|
| 18 |
-
logging.info(f"
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
result = self.client.predict(
|
| 22 |
image=handle_file(image_path),
|
| 23 |
-
task_prompt=
|
| 24 |
text_input=None,
|
| 25 |
-
model_id=
|
| 26 |
api_name="/process_image"
|
| 27 |
)
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
if isinstance(result, (list, tuple)) and len(result) > 0:
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
except Exception as e:
|
| 33 |
-
logging.error(f"Vision API call failed: {e}")
|
|
|
|
| 34 |
return f"Error describing screenshot: {str(e)}"
|
| 35 |
|
| 36 |
vision_service = VisionService()
|
|
|
|
| 1 |
import logging
|
| 2 |
+
import time
|
| 3 |
+
import traceback
|
| 4 |
from gradio_client import Client, handle_file
|
| 5 |
from app.core.config import settings
|
| 6 |
|
| 7 |
class VisionService:
|
| 8 |
def __init__(self):
|
| 9 |
self.client = None
|
| 10 |
+
self.endpoint = "MinhDS/Florence-2-Demo"
|
| 11 |
try:
|
| 12 |
+
logging.info(f"[VisionService] Initializing client for {self.endpoint}...")
|
| 13 |
+
self.client = Client(self.endpoint)
|
| 14 |
+
logging.info(f"[VisionService] Client initialized successfully.")
|
| 15 |
except Exception as e:
|
| 16 |
+
logging.error(f"[VisionService] Failed to initialize client: {e}")
|
| 17 |
+
logging.error(traceback.format_exc())
|
| 18 |
|
| 19 |
async def describe_screenshot(self, image_path: str):
|
| 20 |
if not self.client:
|
| 21 |
+
logging.warning("[VisionService] describe_screenshot called but client is not available.")
|
| 22 |
return "Vision service not available."
|
| 23 |
|
| 24 |
+
task_prompt = "More Detailed Caption"
|
| 25 |
+
model_id = "microsoft/Florence-2-base"
|
| 26 |
+
|
| 27 |
try:
|
| 28 |
+
logging.info(f"[VisionService] Starting description for {image_path}...")
|
| 29 |
+
logging.info(f"[VisionService] Task: {task_prompt}, Model: {model_id}")
|
| 30 |
+
|
| 31 |
+
start_time = time.time()
|
| 32 |
+
|
| 33 |
+
# Note: client.predict is synchronous in gradio_client,
|
| 34 |
+
# but we are in an async function. For now keeping it simple as it was.
|
| 35 |
result = self.client.predict(
|
| 36 |
image=handle_file(image_path),
|
| 37 |
+
task_prompt=task_prompt,
|
| 38 |
text_input=None,
|
| 39 |
+
model_id=model_id,
|
| 40 |
api_name="/process_image"
|
| 41 |
)
|
| 42 |
+
|
| 43 |
+
duration = time.time() - start_time
|
| 44 |
+
logging.info(f"[VisionService] API call completed in {duration:.2f} seconds.")
|
| 45 |
+
|
| 46 |
+
# Log result type and structure for debugging
|
| 47 |
+
logging.info(f"[VisionService] Result type: {type(result)}")
|
| 48 |
+
|
| 49 |
+
description = ""
|
| 50 |
if isinstance(result, (list, tuple)) and len(result) > 0:
|
| 51 |
+
description = str(result[0])
|
| 52 |
+
logging.info(f"[VisionService] Description extracted (length: {len(description)}).")
|
| 53 |
+
else:
|
| 54 |
+
description = str(result)
|
| 55 |
+
logging.info(f"[VisionService] Result was not a list/tuple, using string representation.")
|
| 56 |
+
|
| 57 |
+
logging.debug(f"[VisionService] Description snippet: {description[:200]}...")
|
| 58 |
+
return description
|
| 59 |
+
|
| 60 |
except Exception as e:
|
| 61 |
+
logging.error(f"[VisionService] Vision API call failed for {image_path}: {e}")
|
| 62 |
+
logging.error(traceback.format_exc())
|
| 63 |
return f"Error describing screenshot: {str(e)}"
|
| 64 |
|
| 65 |
vision_service = VisionService()
|