handle py
#21
by
ismailhakki37
- opened
- handler.py +88 -88
handler.py
CHANGED
|
@@ -1,5 +1,12 @@
|
|
| 1 |
import os
|
| 2 |
import datetime
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
# Try to import cv2, but make it optional
|
| 5 |
try:
|
|
@@ -8,45 +15,57 @@ try:
|
|
| 8 |
except ImportError:
|
| 9 |
CV2_AVAILABLE = False
|
| 10 |
print("Warning: cv2 (OpenCV) not available. Video processing will be disabled.")
|
| 11 |
-
|
| 12 |
-
import
|
| 13 |
-
|
| 14 |
-
import
|
| 15 |
-
import
|
| 16 |
-
import
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
from llava import
|
| 24 |
-
from llava.
|
| 25 |
-
from llava.
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
)
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
# Initialize Hugging Face API
|
| 46 |
-
if "HF_TOKEN" in os.environ:
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
else:
|
| 51 |
api = None
|
| 52 |
repo_name = ""
|
|
@@ -62,8 +81,6 @@ image_processor = None
|
|
| 62 |
context_len = None
|
| 63 |
args = None
|
| 64 |
|
| 65 |
-
# Gradio artık kullanılmıyor - Hugging Face endpoint için gerekli değil
|
| 66 |
-
|
| 67 |
def get_conv_log_filename():
|
| 68 |
t = datetime.datetime.now()
|
| 69 |
name = os.path.join(LOGDIR, f"{t.year}-{t.month:02d}-{t.day:02d}-user_conv.json")
|
|
@@ -78,14 +95,15 @@ def get_conv_vote_filename():
|
|
| 78 |
|
| 79 |
def vote_last_response(state, vote_type, model_selector):
|
| 80 |
if api and repo_name:
|
| 81 |
-
with open(get_conv_vote_filename(), "a") as fout:
|
| 82 |
-
data = {
|
| 83 |
-
"type": vote_type,
|
| 84 |
-
"model": model_selector,
|
| 85 |
-
"state": state,
|
| 86 |
-
}
|
| 87 |
-
fout.write(json.dumps(data) + "\n")
|
| 88 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
api.upload_file(
|
| 90 |
path_or_fileobj=get_conv_vote_filename(),
|
| 91 |
path_in_repo=get_conv_vote_filename().replace("./votes/", ""),
|
|
@@ -171,6 +189,9 @@ def process_image_input(image_input):
|
|
| 171 |
|
| 172 |
class InferenceDemo(object):
|
| 173 |
def __init__(self, args, model_path, tokenizer, model, image_processor, context_len) -> None:
|
|
|
|
|
|
|
|
|
|
| 174 |
disable_torch_init()
|
| 175 |
|
| 176 |
self.tokenizer, self.model, self.image_processor, self.context_len = (
|
|
@@ -224,27 +245,25 @@ chat_manager = ChatSessionManager()
|
|
| 224 |
|
| 225 |
def clear_history():
|
| 226 |
"""Clear conversation history"""
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
|
| 231 |
def add_message(message_text, image_input=None):
|
| 232 |
"""Add a message to the conversation"""
|
| 233 |
-
global chat_image_num
|
| 234 |
-
|
| 235 |
-
if not hasattr(add_message, 'chat_image_num'):
|
| 236 |
-
add_message.chat_image_num = 0
|
| 237 |
-
|
| 238 |
-
if image_input:
|
| 239 |
-
add_message.chat_image_num += 1
|
| 240 |
-
if add_message.chat_image_num > 1:
|
| 241 |
-
chat_manager.reset_chatbot()
|
| 242 |
-
add_message.chat_image_num = 1
|
| 243 |
-
|
| 244 |
return {"status": "success", "message": "Message added"}
|
| 245 |
|
| 246 |
def generate_response(message_text, image_input, temperature=0.05, top_p=1.0, max_output_tokens=4096):
|
| 247 |
"""Generate response for the given message and image"""
|
|
|
|
|
|
|
|
|
|
| 248 |
try:
|
| 249 |
if not message_text or not image_input:
|
| 250 |
return {"error": "Both message text and image are required"}
|
|
@@ -400,6 +419,10 @@ def initialize_model():
|
|
| 400 |
"""Initialize the model and tokenizer"""
|
| 401 |
global tokenizer, model, image_processor, context_len, args
|
| 402 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 403 |
try:
|
| 404 |
# Set default arguments
|
| 405 |
class Args:
|
|
@@ -480,7 +503,10 @@ def health_check():
|
|
| 480 |
return {
|
| 481 |
"status": "healthy",
|
| 482 |
"model_initialized": model_initialized,
|
| 483 |
-
"cuda_available": torch.cuda.is_available()
|
|
|
|
|
|
|
|
|
|
| 484 |
}
|
| 485 |
|
| 486 |
def get_model_info():
|
|
@@ -497,32 +523,6 @@ def get_model_info():
|
|
| 497 |
|
| 498 |
# For backward compatibility and testing
|
| 499 |
if __name__ == "__main__":
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
argparser = argparse.ArgumentParser()
|
| 503 |
-
argparser.add_argument("--server_name", default="0.0.0.0", type=str)
|
| 504 |
-
argparser.add_argument("--port", default="6123", type=str)
|
| 505 |
-
argparser.add_argument("--model_path", default="PULSE-ECG/PULSE-7B", type=str)
|
| 506 |
-
argparser.add_argument("--model-base", type=str, default=None)
|
| 507 |
-
argparser.add_argument("--num-gpus", type=int, default=1)
|
| 508 |
-
argparser.add_argument("--conv-mode", type=str, default=None)
|
| 509 |
-
argparser.add_argument("--temperature", type=float, default=0.05)
|
| 510 |
-
argparser.add_argument("--max-new-tokens", type=int, default=1024)
|
| 511 |
-
argparser.add_argument("--num_frames", type=int, default=16)
|
| 512 |
-
argparser.add_argument("--load-8bit", action="store_true")
|
| 513 |
-
argparser.add_argument("--load-4bit", action="store_true")
|
| 514 |
-
argparser.add_argument("--debug", action="store_true")
|
| 515 |
-
|
| 516 |
-
args = argparser.parse_args()
|
| 517 |
-
|
| 518 |
-
model_path = args.model_path
|
| 519 |
-
filt_invalid = "cut"
|
| 520 |
-
model_name = get_model_name_from_path(args.model_path)
|
| 521 |
-
tokenizer, model, image_processor, context_len = load_pretrained_model(args.model_path, args.model_base, model_name, args.load_8bit, args.load_4bit)
|
| 522 |
-
print("### image_processor",image_processor)
|
| 523 |
-
print("### tokenzier",tokenizer)
|
| 524 |
-
model=model.to(torch.device('cuda'))
|
| 525 |
-
|
| 526 |
-
print("Model initialized successfully!")
|
| 527 |
print("This handler is now ready for Hugging Face endpoints.")
|
| 528 |
print("Use the 'query' function as the main endpoint.")
|
|
|
|
| 1 |
import os
|
| 2 |
import datetime
|
| 3 |
+
import torch
|
| 4 |
+
import numpy as np
|
| 5 |
+
import hashlib
|
| 6 |
+
import json
|
| 7 |
+
import requests
|
| 8 |
+
from PIL import Image
|
| 9 |
+
from io import BytesIO
|
| 10 |
|
| 11 |
# Try to import cv2, but make it optional
|
| 12 |
try:
|
|
|
|
| 15 |
except ImportError:
|
| 16 |
CV2_AVAILABLE = False
|
| 17 |
print("Warning: cv2 (OpenCV) not available. Video processing will be disabled.")
|
| 18 |
+
|
| 19 |
+
# Try to import llava modules, but make them optional
|
| 20 |
+
try:
|
| 21 |
+
from llava import conversation as conversation_lib
|
| 22 |
+
from llava.constants import DEFAULT_IMAGE_TOKEN
|
| 23 |
+
from llava.constants import (
|
| 24 |
+
IMAGE_TOKEN_INDEX,
|
| 25 |
+
DEFAULT_IMAGE_TOKEN,
|
| 26 |
+
DEFAULT_IM_START_TOKEN,
|
| 27 |
+
DEFAULT_IM_END_TOKEN,
|
| 28 |
+
)
|
| 29 |
+
from llava.conversation import conv_templates, SeparatorStyle
|
| 30 |
+
from llava.model.builder import load_pretrained_model
|
| 31 |
+
from llava.utils import disable_torch_init
|
| 32 |
+
from llava.mm_utils import (
|
| 33 |
+
tokenizer_image_token,
|
| 34 |
+
process_images,
|
| 35 |
+
get_model_name_from_path,
|
| 36 |
+
KeywordsStoppingCriteria,
|
| 37 |
+
)
|
| 38 |
+
LLAVA_AVAILABLE = True
|
| 39 |
+
except ImportError as e:
|
| 40 |
+
LLAVA_AVAILABLE = False
|
| 41 |
+
print(f"Warning: LLaVA modules not available: {e}")
|
| 42 |
+
|
| 43 |
+
# Try to import transformers
|
| 44 |
+
try:
|
| 45 |
+
from transformers import TextStreamer, TextIteratorStreamer
|
| 46 |
+
TRANSFORMERS_AVAILABLE = True
|
| 47 |
+
except ImportError:
|
| 48 |
+
TRANSFORMERS_AVAILABLE = False
|
| 49 |
+
print("Warning: Transformers not available")
|
| 50 |
+
|
| 51 |
+
# Try to import huggingface_hub
|
| 52 |
+
try:
|
| 53 |
+
from huggingface_hub import HfApi, login
|
| 54 |
+
HF_HUB_AVAILABLE = True
|
| 55 |
+
except ImportError:
|
| 56 |
+
HF_HUB_AVAILABLE = False
|
| 57 |
+
print("Warning: Hugging Face Hub not available")
|
| 58 |
|
| 59 |
# Initialize Hugging Face API
|
| 60 |
+
if HF_HUB_AVAILABLE and "HF_TOKEN" in os.environ:
|
| 61 |
+
try:
|
| 62 |
+
login(token=os.environ["HF_TOKEN"], write_permission=True)
|
| 63 |
+
api = HfApi()
|
| 64 |
+
repo_name = os.environ.get("LOG_REPO", "")
|
| 65 |
+
except Exception as e:
|
| 66 |
+
print(f"Failed to initialize HF API: {e}")
|
| 67 |
+
api = None
|
| 68 |
+
repo_name = ""
|
| 69 |
else:
|
| 70 |
api = None
|
| 71 |
repo_name = ""
|
|
|
|
| 81 |
context_len = None
|
| 82 |
args = None
|
| 83 |
|
|
|
|
|
|
|
| 84 |
def get_conv_log_filename():
|
| 85 |
t = datetime.datetime.now()
|
| 86 |
name = os.path.join(LOGDIR, f"{t.year}-{t.month:02d}-{t.day:02d}-user_conv.json")
|
|
|
|
| 95 |
|
| 96 |
def vote_last_response(state, vote_type, model_selector):
|
| 97 |
if api and repo_name:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
try:
|
| 99 |
+
with open(get_conv_vote_filename(), "a") as fout:
|
| 100 |
+
data = {
|
| 101 |
+
"type": vote_type,
|
| 102 |
+
"model": model_selector,
|
| 103 |
+
"state": state,
|
| 104 |
+
}
|
| 105 |
+
fout.write(json.dumps(data) + "\n")
|
| 106 |
+
|
| 107 |
api.upload_file(
|
| 108 |
path_or_fileobj=get_conv_vote_filename(),
|
| 109 |
path_in_repo=get_conv_vote_filename().replace("./votes/", ""),
|
|
|
|
| 189 |
|
| 190 |
class InferenceDemo(object):
|
| 191 |
def __init__(self, args, model_path, tokenizer, model, image_processor, context_len) -> None:
|
| 192 |
+
if not LLAVA_AVAILABLE:
|
| 193 |
+
raise ImportError("LLaVA modules not available")
|
| 194 |
+
|
| 195 |
disable_torch_init()
|
| 196 |
|
| 197 |
self.tokenizer, self.model, self.image_processor, self.context_len = (
|
|
|
|
| 245 |
|
| 246 |
def clear_history():
|
| 247 |
"""Clear conversation history"""
|
| 248 |
+
if not LLAVA_AVAILABLE:
|
| 249 |
+
return {"error": "LLaVA modules not available"}
|
| 250 |
+
|
| 251 |
+
try:
|
| 252 |
+
chatbot_instance = chat_manager.get_chatbot(args, model_path, tokenizer, model, image_processor, context_len)
|
| 253 |
+
chatbot_instance.conversation = conv_templates[chatbot_instance.conv_mode].copy()
|
| 254 |
+
return {"status": "success", "message": "Conversation history cleared"}
|
| 255 |
+
except Exception as e:
|
| 256 |
+
return {"error": f"Failed to clear history: {str(e)}"}
|
| 257 |
|
| 258 |
def add_message(message_text, image_input=None):
|
| 259 |
"""Add a message to the conversation"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
return {"status": "success", "message": "Message added"}
|
| 261 |
|
| 262 |
def generate_response(message_text, image_input, temperature=0.05, top_p=1.0, max_output_tokens=4096):
|
| 263 |
"""Generate response for the given message and image"""
|
| 264 |
+
if not LLAVA_AVAILABLE:
|
| 265 |
+
return {"error": "LLaVA modules not available"}
|
| 266 |
+
|
| 267 |
try:
|
| 268 |
if not message_text or not image_input:
|
| 269 |
return {"error": "Both message text and image are required"}
|
|
|
|
| 419 |
"""Initialize the model and tokenizer"""
|
| 420 |
global tokenizer, model, image_processor, context_len, args
|
| 421 |
|
| 422 |
+
if not LLAVA_AVAILABLE:
|
| 423 |
+
print("LLaVA modules not available, skipping model initialization")
|
| 424 |
+
return False
|
| 425 |
+
|
| 426 |
try:
|
| 427 |
# Set default arguments
|
| 428 |
class Args:
|
|
|
|
| 503 |
return {
|
| 504 |
"status": "healthy",
|
| 505 |
"model_initialized": model_initialized,
|
| 506 |
+
"cuda_available": torch.cuda.is_available(),
|
| 507 |
+
"llava_available": LLAVA_AVAILABLE,
|
| 508 |
+
"transformers_available": TRANSFORMERS_AVAILABLE,
|
| 509 |
+
"cv2_available": CV2_AVAILABLE
|
| 510 |
}
|
| 511 |
|
| 512 |
def get_model_info():
|
|
|
|
| 523 |
|
| 524 |
# For backward compatibility and testing
|
| 525 |
if __name__ == "__main__":
|
| 526 |
+
print("Handler module loaded successfully!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 527 |
print("This handler is now ready for Hugging Face endpoints.")
|
| 528 |
print("Use the 'query' function as the main endpoint.")
|