Update app.py
Browse files4 unmodified lines56789101 unmodified line12131415161729 unmodified lines4748495051522 unmodified lines55565758596061626364656667686914 unmodified lines848586878889909192934 unmodified linesimport numpy as npimport ioimport base64app = Flask(__name__)1 unmodified lineprocessor = CLIPSegProcessor.from_pretrained("CIDAS/clipseg-rd64-refined")model = CLIPSegForImageSegmentation.from_pretrained("CIDAS/clipseg-rd64-refined")# Function to process image and generate maskdef process_image(image, prompt):inputs = processor(29 unmodified lines# Function to extract image using positive and negative promptsdef extract_image(pos_prompts, neg_prompts, img, threshold):positive_masks = get_masks(pos_prompts, img, 0.5)negative_masks = get_masks(neg_prompts, img, 0.5)2 unmodified linesfinal_mask = pos_mask & ~neg_maskfinal_mask = Image.fromarray(final_mask.astype(np.uint8) * 255, "L")output_image = Image.new("RGBA", img.size, (0, 0, 0, 0))output_image.paste(img, mask=final_mask)# Convert final mask to base64buffered = io.BytesIO()final_mask.save(buffered, format="PNG")final_mask_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")return final_mask_base64@app.route('/')def hello_world():14 unmodified linesthreshold = float(data.get('threshold', 0.4))# Perform image segmentationfinal_mask_base64 = extract_image(pos_prompts, neg_prompts, img, threshold)return jsonify({'final_mask_base64': final_mask_base64})if __name__ == '__main__':print("Server starting. Verify it is running by visiting http://0.0.0.0:7860/")app.run(host='0.0.0.0', port=7860, debug=True)4 unmodified lines567891011121 unmodified line14151617181920212229 unmodified lines52535455565758596061622 unmodified lines6566676869707172737475767778798014 unmodified lines95969798991001011021031041051061071081091101111121131141154 unmodified linesimport numpy as npimport ioimport base64import threadingimport timeapp = Flask(__name__)1 unmodified lineprocessor = CLIPSegProcessor.from_pretrained("CIDAS/clipseg-rd64-refined")model = CLIPSegForImageSegmentation.from_pretrained("CIDAS/clipseg-rd64-refined")# Global variable for caching resultscache = {}# Function to process image and generate maskdef process_image(image, prompt):inputs = processor(29 unmodified lines# Function to extract image using positive and negative promptsdef extract_image(pos_prompts, neg_prompts, img, threshold):cache_key = (pos_prompts, neg_prompts, threshold)if cache_key in cache:return cache[cache_key]positive_masks = get_masks(pos_prompts, img, 0.5)negative_masks = get_masks(neg_prompts, img, 0.5)2 unmodified linesfinal_mask = pos_mask & ~neg_maskfinal_mask = Image.fromarray(final_mask.astype(np.uint8) * 255, "L")# Convert final mask to base64buffered = io.BytesIO()final_mask.save(buffered, format="PNG")final_mask_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")# Cache the resultcache[cache_key] = {'final_mask_base64': final_mask_base64}return cache[cache_key]@app.route('/')def hello_world():14 unmodified linesthreshold = float(data.get('threshold', 0.4))# Perform image segmentationresult = extract_image(pos_prompts, neg_prompts, img, threshold)return jsonify(result)# Keep the server alive using a periodic taskdef keep_alive():while True:time.sleep(300) # 5 minutesrequests.get('http://127.0.0.1:7860/') # Send a request to keep the server aliveif __name__ == '__main__':print("Server starting. Verify it is running by visiting http://0.0.0.0:7860/")# Start the keep-alive threadkeep_alive_thread = threading.Thread(target=keep_alive)keep_alive_thread.start()app.run(host='0.0.0.0', port=7860, debug=True)
app.py+27-5