|
import json |
|
import logging |
|
import datetime |
|
from transformers import AutoModelForCausalLM, AutoTokenizer |
|
|
|
|
|
logging.basicConfig(level=logging.INFO) |
|
logger = logging.getLogger(__name__) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try: |
|
with open('config.json') as f: |
|
config = json.load(f) |
|
except FileNotFoundError: |
|
logger.error("Configuration file 'config.json' not found. Using default settings.") |
|
config = { |
|
"model_name": "acecalisto3/InstructiPhi", |
|
"max_length": 16788, |
|
"logging_level": "INFO" |
|
} |
|
|
|
|
|
model_name = config["model_name"] |
|
tokenizer = AutoTokenizer.from_pretrained(model_name) |
|
model = AutoModelForCausalLM.from_pretrained(model_name) |
|
|
|
|
|
logging.basicConfig(level=config["logging_level"]) |
|
|
|
def handle_request(event, context): |
|
"""Handles incoming requests to the deployed model. |
|
|
|
Args: |
|
event: The event data from the deployment platform. |
|
context: The context data from the deployment platform. |
|
|
|
Returns: |
|
A dictionary containing the response status code and body. |
|
""" |
|
|
|
try: |
|
|
|
input_text = event.get('body') |
|
if not input_text: |
|
return { |
|
'statusCode': 400, |
|
'body': json.dumps({'error': 'Missing input text'}) |
|
} |
|
|
|
|
|
if len(input_text) > 1000: |
|
return { |
|
'statusCode': 400, |
|
'body': json.dumps({'error': 'Input text is too long'}) |
|
} |
|
|
|
|
|
input_ids = tokenizer(input_text, return_tensors="pt").input_ids |
|
|
|
|
|
output = model.generate(input_ids, max_length=config["max_length"]) |
|
|
|
|
|
generated_text = tokenizer.decode(output[0], skip_special_tokens=True) |
|
|
|
|
|
return { |
|
'statusCode': 200, |
|
'body': json.dumps({ |
|
'response': generated_text, |
|
'model': model_name, |
|
'timestamp': datetime.datetime.now().isoformat() |
|
}) |
|
} |
|
|
|
except Exception as e: |
|
|
|
logger.error(f"Error processing request: {e}, input: {input_text}") |
|
return { |
|
'statusCode': 500, |
|
'body': json.dumps({'error': 'Internal server error'}) |
|
} |