Skip to content

Commit

Permalink
Implement timeout functionality for prompt cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
EtienneDosSantos committed Jun 8, 2024
1 parent 5923555 commit e654a5a
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import re
import torch
import uuid
import threading

# Initialize global settings
device = "cuda"
Expand Down Expand Up @@ -47,6 +48,25 @@ def clean_prompt(prompt):
prompt = ', '.join(prompt_parts)
return prompt

def clean_prompt_with_timeout(prompt, timeout):
def wrapper():
try:
cleaned_prompt = clean_prompt(prompt)
wrapper.result = cleaned_prompt
except Exception as e:
print(f"Error occurred during prompt cleaning: {str(e)}. Using original prompt.")
wrapper.result = prompt

thread = threading.Thread(target=wrapper)
thread.start()
thread.join(timeout)

if thread.is_alive():
print("Prompt cleaning timed out. Using original prompt.")
thread.result = prompt

return wrapper.result

def generate_images(prompt, height, width, negative_prompt, guidance_scale, num_inference_steps, num_images_per_prompt, seed):
"""
Generates images based on the provided parameters and settings.
Expand All @@ -56,8 +76,8 @@ def generate_images(prompt, height, width, negative_prompt, guidance_scale, num_
calculated_steps_prior = int(num_inference_steps * 2 / 3)
calculated_steps_decoder = int(num_inference_steps * 1 / 3)

# Sanitize user input prompt before using it
cleaned_prompt = clean_prompt(prompt)
# Sanitize user input prompt before using it, with a timeout of 5 seconds
cleaned_prompt = clean_prompt_with_timeout(prompt, timeout=5)
print("Processed prompt:", cleaned_prompt)

# Load, use, and discard the prior model
Expand Down

0 comments on commit e654a5a

Please sign in to comment.