ʱ¼ä£º2024-04-08¡¡|¡¡À´Ô´£º¡¡|¡¡ÔĶÁ£º104
±¾ÆªÎÄÕÂÔ´ÓÚËûÈË´´ÒâµÄÆô·¢ÓëÉîÈë˼¿¼£¬ÎÒͶÈëÁË´óÁ¿Ê±¼äÓ뾫Á¦£¬ÖÂÁ¦ÓÚEdgeOneÓëHAIÖ®¼äµÄÐͬµ÷ÊÔ£¬¿Ë·þÁËÖڶ༼ÊõÆæÝâÎÊÌ⡣ͨ¹ý²»Ð¸µÄŬÁ¦£¬³É¹¦µØ½«ÕâÁ½¸öÇ¿´óµÄƽ̨ÕûºÏÔÚÒ»Æ𣬴òÔì³öÒ»¸ö»¹²»´íµÄ²Î¿¼°¸Àý¡£Õâ´Î̽Ë÷Ò²ÊÇÎÒ¸öÈËÊ×´ÎÉæ×ã±ßÔµº¯Êýʽ²úÆ·µÄʵ¼ùÖ®Âá£ÎÒÏ£Íûͨ¹ý·ÖÏíÕâ´ÎµÄ¾ÀúºÍ³É¹û£¬Äܹ»¼¤
ÔÚµ±½ñ¿ìËÙ·¢Õ¹µÄÊý×Öʱ´ú£¬°²È«ºÍËÙ¶ÈÒѳÉΪÍøÂç·þÎñµÄ»ùʯ¡£EdgeOne£¬×÷ΪÌÚѶÔÆÌṩµÄ±ßÔµ°²È«¼ÓËÙƽ̨£¬ÒÔÆäÈ«Çò²¿ÊðµÄ½ÚµãºÍÇ¿´óµÄ°²È«·À»¤¹¦ÄÜ£¬ÎªÓû§ÌṩÁËÎȶ¨¶ø¸ßЧµÄÍøÂçÌåÑé¡£¶øHAI£¨HyperApplicationInventor£©£¬ÌÚѶÔÆÍƳöµÄ¸ßÐÔÄÜÓ¦Ó÷þÎñ£¬Í¨¹ýÆäÒ×ÓõÄͼÐλ¯½çÃæºÍ·á¸»µÄÄ£ÐͿ⣬ʹµÃAIÓ¦Óÿª·¢±äµÃ´¥Êֿɼ°¡£±¾ÎĽ«Ì½ÌÖEdgeOneÓëHAIµÄ½áºÏÈçºÎΪÓû§Ìṩһ¸ö¼È°²È«ÓÖ¸ßЧµÄAIÓ¦Óÿª·¢»·¾³¡£
²»¶à˵ÁË£¬Ê£ÏµÄÄÚÈݶ¼ÒѾÔÚ¹Ù·½ÎĵµÖÐÓÐÏêϸÃèÊö¡£¾ÍÎÒÃǸöÈ˶øÑÔ£¬EdgeOne¾ßÓÐÒÔÏÂÖ÷ÒªÓŵ㣬²¢ÇÒÎÒÒ²¸½ÉÏÁËÐèÒªµÄ¹Ø¼üÎĵµµØÖ·£º
HAIÌṩÁË¿ÉÊÓ»¯½»»¥½çÃ棬֧³ÖJupyterLab¡¢WebUIµÈ¶àÖÖËãÁ¦Á¬½Ó·½Ê½£¬Ê¹µÃÓû§¼´Ê¹Ã»ÓÐÉîºñµÄ±à³Ì±³¾°Ò²ÄÜÇáËÉ¿ª·¢AIÓ¦Óá£ÕâÖÖ¡°ÓÐÊÖ¾ÍÄÜ¿ª·¢¡±µÄÉè¼ÆÀíÄ¼«´óµØ½µµÍÁËAI¼¼ÊõµÄÓ¦ÓÃÃż÷¡£Ö§³ÖѧÊõ¼ÓËÙ£¬Í¨¹ýÏß·×Ô¶¯ÔñÓÅ£¬Äܹ»´ó·ùÌáÉýÖ÷Á÷ѧÊõ×ÊԴƽ̨µÄ·ÃÎÊ¡¢ÏÂÔØËٶȡ£Í¬Ê±£¬HAIԤװÁËStableDiffusion¡¢ChatGLMµÈÈÈÃÅÄ£ÐÍ£¬Óû§¿ÉÒÔÔÚÊý·ÖÖÓÄÚ¹¹½¨×Ô¼ºµÄ´óÓïÑÔÄ£ÐÍ¡¢AI×÷»µÈÓ¦Óû·¾³¡£
Õâ¸ö»°Ìâ²»ÐèÒªÏêϸÌÖÂÛÁË£¬ÎÒÒѾÕûÀíÁË¿ª·¢ÖÐÐèÒªÁ˽âµÄÖ÷ÒªÓŵãºÍÏà¹ØÎĵµ£¬ÒÔ±ã´ó¼ÒÄܹ»¿ìËÙÈëÊÖ¿ª·¢£º
ÏÖÔÚÈÃÎÒÃÇÀ´Ì½ÌÖÒ»ÏÂΪºÎ¿ÉÒÔ²ÉÓÃÁª¶¯¿ª·¢µÄ·½Ê½¡£ÎÒÃǵÄÄ¿±êÊÇÒÔ×îΪ¾¼Ãʵ»ÝµÄ¼Û¸ñ²¿ÊðÎÒÃǵÄAI»æ»Ó¦Óá£
ÎÒÃǽÓÏÂÀ´¿´ÏÂÎÒÃÇ»áÓõ½ÄÄЩ¼¼Êõ£ºHTML¡¢Javascript¡¢CSS¡¢Nginx¡¢Python¡¢±ßÔµº¯ÊýÖеĸ÷¸öAPI¡¢Linux»ù±¾ÃüÁî¡£
¹ØÓÚ´´½¨·þÎñÆ÷µÄ²½Ö裬ÎҾͲ»ÔÙ½øÐÐÑÝʾÁË£¬ÇëÖ±½Ó²é¿´¹Ù·½½Ì³Ì£¬Ïà¹ØµØÖ·Ò²ÒѾÌṩ¡£ÎÒÃǽ«ÖصãÑÝʾÒÔÏÂÄÚÈÝ£ºÏÂÔØ/ÉÏ´«Ä£ÐÍ¡¢Í¨¹ýWEB UIÑÝʾAI»æ»¹¦ÄÜ¡¢ÏÂÔز¢ÅäÖÃNginxÒÔ¼àÌý80¶Ë¿Ú¡¢Æô¶¯API½Ó¿Ú·þÎñ¡£
ÕâÒ»²½ÎÒµ±Ê±Ê¹ÓùýÈçºÎ¹Ù·½Ä¬ÈϵÄJupyterLabÒ³ÃæÉÏ´«£¬ËÙ¶ÈÊÇÕæµÄºÜ¿É¹Û~ÎÒÖ±½Ó·ÅÆúÁË¡£Ñ¡ÔñÁËCOS¶ÔÏó´æ´¢¡£ÎÒÃÇÏÈÈ¥ÏÂÔغÿ´µÄÄ£ÐÍ¡£ÕâÀïÒÔhttps://civitai.com/ ΪÀý¡£
Ä£ÐÍÏÂÔص½±¾µØÖ®ºó£¬È¥´´½¨COS¶ÔÏó´æ´¢·þÎñ¡£
ÈçºÎ´´½¨Õâô¼òµ¥µÄ½Ì³Ì£¬ÎҾͲ»ÑÝʾÁË£¬ÎÒÃÇÖ±½Ó¿´ÉÏ´«ºÍÏÂÔØ¡£ÕâÀï´´½¨´æ´¢Í°µÄʱºò£¬ËùÊôµØÓòÒ»¶¨ÒªºÍÄãHAI»æ»·þÎñÆ÷ÔÚÒ»Æð¡£·ñÔò»á²úÉúºÜ¶à¶îÍâµÄ»¨Ïú¡£½÷¼Ç~
½øÈë´æ´¢Í°Ö®ºó£¬Ö±½Óµã»÷Ò³ÃæµÄÉÏ´«Îļþ¼´¿É£¬ÇëÑ¡Ôñ¸Õ²ÅÏÂÔصÄÄ£ÐÍÎļþ¡£ÉÏ´«µÄ¹ý³ÌÖв»Òª¶¯ä¯ÀÀÆ÷£¬Ò»µ©Ë¢Ðº͹رÕä¯ÀÀÆ÷¾Í»áÇ°¹¦¾¡Æú¡£
ÉÔµÈƬ¿Ì£¬È»ºóµã»÷Ä£ÐÍÎļþµÄÏêÇ飬½øÈë»ù±¾ÐÅÏ¢Ò³Ãæ¡£ÔÚÕâÀÇëÈ·±£½«È¨ÏÞÉèÖÃΪ¹«ÓжÁÈ¡£¬·ñÔòHAI·þÎñÆ÷½«ÎÞ·¨ÏÂÔص½Ä£ÐÍÎļþ¡£È»ºó£¬¸´ÖÆÁÙʱÁ´½Ó¡£
ÎÒÃǽøÈëÒѾ´´½¨ºÃµÄHAI·þÎñÆ÷¡£
½øÈëÖնˣ¬Ê¹ÓÃһϼ¸¸öÃüÁÏÂÔØÄ£ÐÍÎļþµ½Ä£ÐÍÎļþ¼Ð¡£
cd stable-diffusion-webui/models/Stable-diffusion
wget ÁÙʱÁ´½ÓÕ³Ìùµ½´Ë´¦
µÈ´ýÏÂÔØÍê±Ï¡£
ÈÃÎÒÃÇÇ°ÍùWEB UIÒ³Ã棬³¢ÊÔһϿ´¿´ÊÇ·ñ³É¹¦°²×°ÁË¡£
ÔÚWEB UIÒ³ÃæÉÏ£¬Ñ¡ÔñÄú×Ô¼ºµÄÄ£ÐÍÎļþ£¬²¢Ê¹ÓÃÒ»¶ÎÉñÆæµÄÌáʾ´Ê¡£µ±È»£¬Ìáʾ´ÊÔ½ºÃ£¬Éú³ÉµÄ½á¹ûÒ²»áÔ½ºÃ¡£Èç¹ûÓÐÈκβ»Çå³þµÄµØ·½£¬Äú¿ÉÒԲο¼ÄúÏÂÔصÄÄ£Ð͸½´øµÄ½Ì³Ì¡£ÕâÑù»ù±¾ÉϾÍûÓÐÎÊÌâÁË¡£½ÓÏÂÀ´£¬ÈÃÎÒÃÇÔÙÀ´¿´¿´ÈçºÎÈÃHAIÌṩվµã·þÎñ¡£
ÒÀÈ»ÊÇͬÑùµÄ²½Ö裬ÔÚJupyterLabÖеÄÖÕ¶ËÖÐÖ±½ÓʹÓÃÒÔÏÂÃüÁî²Ù×÷£º
apt install nginx
½øÐа²×°¡£
service nginx start
Æô¶¯·þÎñ¡£
µ±ÄúÖ±½ÓʹÓÃIPµØÖ·ÔÚä¯ÀÀÆ÷ÖзÃÎʺó£¬Èç¹ûÏÔʾÒÔϽçÃ棬ÄǾͱíʾ°²×°³É¹¦ÁË¡£
½ÓÏÂÀ´£¬ÎÒÃǼÌÐøÐÞ¸ÄNginxµÄÅäÖÃÎļþ£º
ʹÓÃÒÔÏÂÃüÁî±à¼ÅäÖÃÎļþ£º
vim /etc/nginx/sites-available/default
È»ºó£¬ÔÚÎļþÖÐÌí¼ÓÒÔÏÂÅäÖãº
# ÅäÖÃ/images·¾¶µÄ·ÃÎʹæÔò
location /images/ {
add_header 'Access-Control-Allow-Origin' "$http_origin";
add_header 'Access-Control-Expose-Headers' 'strict-origin-when-cross-origin';
alias /root/stable-diffusion-webui/outputs/star/images/; # ÄãµÄ¾²Ì¬Ò³Ãæ
expires 1d;
}
location /sdapi/ {
# Add CORS headers
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' always;
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;
proxy_pass http://127.0.0.1:7862/sdapi/;
if ($request_method = "OPTIONS"){
return 200;
}
}
ÕâÀïÅäÖÃÁË/images·¾¶µÄ·ÃÎʹæÔòÒÔ¼°API·þÎñµÄ´úÀí¡£ÎªÊ²Ã´ÒªÅäÖôúÀíÄØ£¿ÕâÏÔÈ»ÊÇÒòΪ»æ»µÄAPI·þÎñ²»Ö§³Ö¿çÓò·ÃÎÊ£¬ËùÒÔÎÒÃÇÌí¼ÓÁËÒ»¸ö·´Ïò´úÀí¡£ÓÉÓÚÎÒÃǵÄÇëÇóÊÇPOST·Ç¼òµ¥ÇëÇó£¬Òò´Ëä¯ÀÀÆ÷»¹»á·¢ËÍÒ»¸öOPTIONSµÄÔ¤¼ìÇëÇó¡£ÎÒÒ²×öÁËÏàÓ¦µÄ´¦Àí¡£ÒÔÃâ¿çÓò±¨´í¡£
ÐÞ¸ÄÍê³Éºó£¬Ê¹ÓÃÃüÁî
service nginx reload
À´ÖØмÓÔØNginxµÄÅäÖá£Äú¿ÉÒÔ¿´µ½ÎÒÃÇÅäÖõÄ/images·¾¶¶ÔÓ¦µÄÎļþ¡£Õâ¸ö·¾¶Äú¿ÉÒÔËæÐÄÅäÖã¬Ö»ÒªÈ·±£ÆäÖаüº¬Äú×Ô¼ºµÄͼƬ¼´¿É¡£Äú¿ÉÒÔÑ¡Ôñ×Ô¼ºÎ¬»¤ÕâЩͼƬ£¬Ò²¿ÉÒÔͨ¹ý³ÌÐòÉú³É¡£ÎªÁËÑÝʾ£¬ÎÒÖ»¼òµ¥¸´ÖÆÁ˼¸ÕÅÎÒʹÓÃAI»æ»³öÀ´µÄͼƬ¡£
ÈÃÎÒÃÇͨ¹ýNginxÀ´·ÃÎÊÒ»ÏÂͼƬ£¬ÒÔÈ·±£¿ÉÒÔÕý³£·ÃÎÊ¡£
ÔÚä¯ÀÀÆ÷ÖÐÊäÈëÒÔϵØÖ·½øÐзÃÎÊ£º
http://ÄãµÄIP/images/1.png
½ÓÏÂÀ´£¬ÎÒÃǼÌÐøµ÷ÊÔAPI·þÎñ¡£ÇëÆô¶¯HAI»æ»Ó¦ÓõÄAPI·þÎñ£¬Ê¹ÓÃÒÔÏÂÃüÁ
python launch.py --nowebui --xformers --opt-split-attention --listen --port 7862
È»ºó£¬ÔÚä¯ÀÀÆ÷ÖÐÊäÈëIPµØÖ·ºÍ¶Ë¿ÚºÅ£¬ºóÃæ¼ÓÉÏ/docs·¾¶¼´¿ÉÕý³£·ÃÎÊ·¾¶ÁË¡£
ÈÃÎÒÃÇͨ¹ý¹¤¾ßÀ´²âÊÔһϣ¬¿´¿´ÄÜ·ñͨ¹ýNginxÕý³£·ÃÎʽӿڡ£Äã¿ÉÒÔËæÒâÑ¡ÔñÒ»¸ö½Ó¿Ú·¾¶½øÐвâÊÔ¡£ÎÒÑ¡ÔñÁËÒ»¸ö¼òµ¥µÄÎÞÇëÇó²ÎÊýµÄGETÇëÇó×÷ΪʾÀý¡£Í¨³£Çé¿öÏ£¬Õâ¸öÇëÇó»á´øÓж˿ںš£
ʹÓõ÷ÊÔ¹¤¾ßÀ´µ÷ÓÃÒ»ÏÂNginxÅäÖõÄ·¾¶£¬È·±£Ò»ÇÐÕý³£¡£
Èç¹ûÄú»¹²»È·¶¨µÄ»°£¬¿ÉÒÔͨ¹ý²é¿´Ä¬ÈϵÄNginx·ÃÎÊ·¾¶À´È·ÈÏ¡£Äú¿ÉÒÔʹÓÃÒÔÏÂÃüÁîÀ´ÊµÊ±²é¿´ÈÕÖ¾£º
tail -f /var/log/nginx/access.log
ÕâÑùÄú¾Í¿ÉÒÔÈ·¶¨APIÊÇ·ñÄܹ»±»ÕýÈ·ÃüÖС£
ΪÁ˽â¾öNginxÿ´Î¹Ø»úºóÎÞ·¨Õý³£Æô¶¯µÄÄÑÌ⣬ÎÒÃDzÉÈ¡ÁËÒ»ÏîÇÉÃîµÄ½â¾ö·½°¸£º½«Æô¶¯ÃüÁîÖ±½ÓдÈëAPIÆô¶¯·þÎñ½Ó¿ÚÖУ¬´Ó¶øÈ·±£·þÎñÄܹ»ÔÚÿ´ÎÆô¶¯Ê±ÕýÈ·ÅäÖÃNginx¡£¾ßÌå²Ù×÷ÈçÏ£ºÊ×ÏÈ£¬ÎÒÃÇÐèÒªÕÒµ½ÏîÄ¿ÖеÄlaunch.pyÎļþ¡£Ò»µ©ÕÒµ½Á˸ÃÎļþ£¬ÎÒÃÇÖ»ÐèÔÚÆäÖÐÌí¼Ó¼¸ÐÐÃüÁî¼´¿É¡£ÕâЩÃüÁȷ±£NginxÔÚ·þÎñÆ÷Æô¶¯Ê±µÃµ½ÕýÈ·µÄÅäÖ㬲¢Äܹ»Ë³ÀûÆô¶¯¡£ÕâÖÖ·½·¨¼òµ¥¶ø¸ßЧ£¬Äܹ»ÓÐЧ½â¾öNginxÆô¶¯ÎÊÌ⣬±£Ö¤·þÎñµÄÎȶ¨ÐԺͿɿ¿ÐÔ¡£
´úÂëÈçÏ£º
import subprocess
# ´Ë´¦Ê¡ÂԺܶà´úÂë~~
def main():
# ʹÓÃsubprocess.run()Ö´ÐÐÃüÁî
result = subprocess.run(['service', 'nginx', 'start'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
# ¼ì²éÃüÁîÊÇ·ñÖ´Ðгɹ¦
if result.returncode == 0:
print("Nginx Òѳɹ¦Æô¶¯")
else:
print("Æô¶¯ Nginx ʱ³ö´í")
print("´íÎóÐÅÏ¢:", result.stderr)
# ´Ë´¦Ê¡ÂԺܶà´úÂë~~
ΪÁËÑéÖ¤ÎÒÃǵĽâ¾ö·½°¸µÄÓÐЧÐÔ£¬ÈÃÎÒÃÇÔÙ´ÎÖØÐÂÆô¶¯API·þÎñ£¬¹Û²ìÊÇ·ñÄܹ»³É¹¦Æô¶¯Nginx¡£
ͨ¹ýÒÔÉϲ½Ö裬ÎÒÃǵÄHAI¸ßÐÔÄÜ·þÎñÆ÷ÒѾÍê³ÉÁË»ù±¾ÅäÖá£ÎÞÂÛ½øÐжàÉٴιػúÖØÆô£¬ÎÒÃǵÄNginx¶¼»áËæ×ÅAPI½Ó¿ÚÒ»ÆðÆô¶¯£¬È·±£ÁËϵͳµÄÎȶ¨ÐԺͿɿ¿ÐÔ¡£ÏÖÔÚ£¬ÈÃÎÒÃǼÌÐøÉîÈë̽ÌÖEdgeOneµÄÅäÖùý³Ì¡£
ÔÚÅäÖÃEdgeOneʱ£¬Ê×ÏÈÐèҪȷ±£Íê³ÉÎÞÓòÃûÕ¾µãµÄÅäÖã¬Äã¿ÉÒÔÖ±½Ó²Î¿¼¹Ù·½Îĵµ½øÐвÙ×÷¡£½Ó×Å£¬ÎÒÃÇÐèÒª´´½¨±ßÔµº¯ÊýÀ´ÊµÏÖÏàÓ¦µÄ»º´æ´¦Àí¡£¿¼Âǵ½ÄãûÓÐÆóÒµ°æȨÏÞ£¬Òò´ËÎÞ·¨Ê¹ÓÃËIJã¼ÓËÙ£¬µ«¿ÉÒÔͨ¹ý±ßÔµº¯ÊýÀ´´ïµ½ÀàËƵÄЧ¹û¡£¹ØÓÚ±ßÔµº¯ÊýÖдúÂëµÄ¾ßÌåʹÓÃÎÊÌ⣬ÎÒ½¨ÒéÄã×ÐϸÔĶÁ¹Ù·½Îĵµ£¬ÆäÖаüº¬ÁËÏêϸµÄ²Ù×÷Ö¸ÄϺÍʾÀý´úÂ룬¿ÉÒÔ°ïÖúÄã¿ìËÙÉÏÊÖ¡£Äã¿ÉÒÔµã»÷ÒÔÏÂÁ´½Ó²é¿´¹Ù·½Îĵµ£º EdgeOne ¹Ù·½Îĵµ
ÔÚÅäÖñßÔµº¯Êýʱ£¬Äã¿ÉÒÔÑ¡ÔñHello WorldÄ£°å×÷ΪÆðµã£¬È»ºóÒÀ´Îµã»÷ÏÂÒ»²½½øÐÐÅäÖá£ËäȻѡÔñÁËHello WorldÄ£°å£¬µ«ËüÖ»ÊÇÒ»¸öÆðµã£¬ÎÒÃǽ«ÔÚ´Ë»ù´¡ÉϽøÐÐÉîÈëµÄ¶¨ÖÆ£¬ÒÔʵÏÖÎÒÃÇËùÐèÒªµÄ¹¦ÄÜ¡£
Íê³ÉËùÓÐÅäÖúó£¬ÏµÍ³»áΪÄãÉú³ÉÒ»¸öĬÈϵÄÓòÃû¡£½ÓÏÂÀ´£¬ÎÒÃÇÐèÒª±àд´úÂëÀ´ÊµÏÖ¹¦ÄÜ¡£Í¨³£Çé¿öÏ£¬ÎÒ»á¶îÍâ±àдһ¸ötestº¯Êý£¬×¨ÃÅÓÃÓÚ²âÊÔ¡£µ±È»£¬Èç¹ûÄã¾õµÃ²»ÐèÒª£¬Ò²¿ÉÒÔºöÂÔ¡£ÔÚÕâ´ÎÅäÖÃÖУ¬ÎÒÃÇ´´½¨ÁËÁ½¸ö±ßÔµº¯Êý¡£ÕâЩº¯ÊýÒѾ¾¹ýÎÒ·â×°£¬Í¨¹ý×îСµÄ±ä¶¯À´Ó¦¶ÔHAI·þÎñÆ÷ÍâÍøIPµÄ±ä»¯£¬È·±£ÁËϵͳµÄÎȶ¨ÐԺͿɿ¿ÐÔ¡£
Ò»¸ö»æ»Ó¦ÓõĹؼüÖ®Ò»ÔÚÓÚÄܹ»ÌṩÓÅÖʵÄͼƬչʾ¹¦ÄÜ¡£±Ï¾¹£¬»×÷µÄÊÓ¾õЧ¹ûÊÇÎüÒýÓû§µÄ¹Ø¼üÖ®Ò»¡£È±ÉÙÁËͼƬչʾ¹¦ÄÜ£¬Õâ¸öÓ¦ÓþͺñÈÊÇÒ»±¾Ã»ÓвåͼµÄ»²á£¬È±·¦ÎüÒýÁ¦£¬ÄÑÒÔÒýÆðÓû§µÄÐËȤ¡£
async function fetchJquery(event, request) {
const cache = caches.default;
// »º´æûÓÐÃüÖУ¬»ØÔ´²¢»º´æ
let response = await fetch(request);
// ÔÚÏìӦͷÌí¼Ó Cahe-Control£¬ÉèÖûº´æʱ³¤ 10s
response.headers.append('Cache-Control', 's-maxage=600');
event.waitUntil(cache.put(request, response.clone()));
// δÃüÖлº´æ£¬ÉèÖÃÏìӦͷ±êʶ
response.headers.append('x-edgefunctions-cache', 'miss');
return response;
}
async function handleEvent(event) {
const urlInfo = new URL(event.request.url);
var url = new URL(event.request.url);
var ip = url.searchParams.get('ip');
// ÇëÇó·ÇͼƬ×ÊÔ´
if (!/\.(jpe?g|png)$/.test(urlInfo.pathname)) {
return event.respondWith(new Response('Error thrown ûÃüÖÐͼƬURL' + urlInfo.pathname));
}
// ×ÊÔ´µØÖ·£¬Ò²×÷Ϊ»º´æ¼ü
const request = new Request(ip + urlInfo.pathname);
// »º´æĬÈÏʵÀý
const cache = caches.default;
try {
// »ñÈ¡¹ØÁªµÄ»º´æÄÚÈÝ£¬»º´æ¹ý£¬½Ó¿Úµ×²ã²»Ö÷¶¯»ØÔ´£¬Å׳ö 504 ´íÎó
let response = await cache.match(request);
// »º´æ²»´æÔÚ£¬ÖØлñÈ¡Ô¶³Ì×ÊÔ´
if (!response) {
return fetchJquery(event, request);
}
// ÃüÖлº´æ£¬ÉèÖÃÏìӦͷ±êʶ
response.headers.append('x-edgefunctions-cache', 'hit');
return response;
} catch (e) {
await cache.delete(request);
// »º´æ¹ýÆÚ»òÆäËûÒì³££¬ÖØлñÈ¡Ô¶³Ì×ÊÔ´
return fetchJquery(event, request);
}
}
addEventListener('fetch', (event) => {
event.respondWith(handleEvent(event));
});
Õâ¶Î´úÂëµÄÖ÷Òª¹¦ÄÜÊÇÓÃÓÚ´¦ÀíÍøÂçÇëÇó²¢¹ÜÀí×ÊÔ´µÄ»º´æ²ßÂÔ£¬È·±£¿ìËÙÏìÓ¦Óû§ÇëÇóµÄͬʱ£¬¼õÉÙ²»±ØÒªµÄÍøÂçÁ÷Á¿¡£
fetchJquery
º¯Êý£º
fetch
º¯Êý·¢ÆðÍøÂçÇëÇ󣬻ñÈ¡×ÊÔ´µÄ×îÐÂÏìÓ¦¡£
Cache-Control
£¬ÉèÖÃ×ÊÔ´ÔÚ·þÎñ¶Ë»º´æÖеÄ×ÓÐЧʱ¼äΪ600Ãë¡£
cache.put
·½·¨½«»ñÈ¡µÄÏìÓ¦¿Ë¡һ·Ý²¢´æÈ뻺´æÖУ¬ÒÔ±ãºóÐøÇëÇó¿ÉÒÔÖ±½Ó´Ó»º´æÖлñÈ¡¡£
x-edgefunctions-cache
Ϊ
miss
£¬±íʾÕâÊÇÒ»¸öδÃüÖлº´æµÄÇëÇó¡£
handleEvent
º¯Êý£º
x-edgefunctions-cache
Ϊ
hit
£¬±íʾÕâÊÇÒ»¸öÃüÖлº´æµÄÇëÇ󣬲¢·µ»Ø»º´æÖеÄ×ÊÔ´¡£
fetchJquery
º¯Êý»ñÈ¡×ÊÔ´¡£
ÔÚ·ÃÎÊͼƬ×ÊԴʱ£¬ÐèÒªÌرð×¢ÒâµÄÊÇ£¬ÎÒ²ÉÓÃÁË·¾¶ºóÃæµÄ IP ²ÎÊý½øÐзÃÎÊ¡£ÕâÊÇÒòΪ HAI ÿ´ÎÖØÆôºóÍâÍø IP ¶¼»á·¢Éú±ä»¯£¬Èç¹ûÿ´Î¶¼ÒªÐÞ¸Äÿ¸ö±ßÔµº¯ÊýµÄ»°£¬»áÏ൱·±Ëö¡£ÎªÁ˱ÜÃâÕâÖÖÇé¿ö£¬ÎÒÑ¡Ôñͨ¹ý²ÎÊý´«µÝµÄ·½Ê½À´»ñȡͼƬ×ÊÔ´¡£ÕâÑùÒ»À´£¬»ñȡͼƬ×ÊÔ´µÄ±ßÔµº¯Êý¾Í²»ÐèÒªÔÙ½øÐÐÈκÎÐÞ¸ÄÁË¡£ÁíÍ⣬Ä㻹ÐèÒª¶ÔÆ¥Åä´¥·¢¹æÔò½øÐÐÅäÖ㬱ÈÈçÎÒÉèÖõĹæÔòÊÇ°üº¬ "/images" µÄ·¾¶²Å»á´¥·¢ÏàÓ¦µÄ²Ù×÷¡£
ÔÚä¯ÀÀÆ÷ÖÐÊäÈëĬÈÏÓòÃû£¬²¢ËæÒâÌí¼Ó "/images/*.png" ¼´¿É·ÃÎÊÏàӦͼƬ£¬Ç°ÌáÊǸÃͼƬ´æ´¢ÓÚÎÒÃǵķþÎñÆ÷ÉÏ¡£ÏÖÔÚÈÃÎÒÃÇÀ´¹Û²ìÒ»ÏÂЧ¹û¡£ÖµµÃ×¢ÒâµÄÊÇ£¬ÕâÖÖ·½Ê½µÄÃüÖÐÂʲ¢²»ÊǺܸߣ¬ËùÒÔÔÚ¸Õ²¿ÊðÍê±ßÔµº¯Êýºó£¬ÐèÒª¶àˢм¸´Î²ÅÄÜ¿´µ½Ð§¹û¡£
Õâ¸ö±ßÔµº¯ÊýרÃÅÓÃÓÚ´¦ÀíÓû§½»»¥¡£
const html = `
AI��ͼ
��ͼ���
�ı�����
`;
async function handleRequest(request) {
return new Response(html, {
headers: {
'content-type': 'text/html; charset=UTF-8',
'x-edgefunctions-test': 'Welcome to use Edge Functions.',
},
});
}
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});
��δ��붨����һ��HTMLҳ�棬����չʾһ��AI��ͼӦ�õ��û����棬����������֮������JavaScript���롣��Ҫ���ܺ���ɲ������£�
container
�����ڲ���������Ҫ���֣���ͼ���չʾ���ı��������롣
image-container
������չʾAI���ɵ�ͼ��
text-description
��
text-description2
�����û��������������ͷ�������������ָ��AI��ͼ��
generate-button
�����ڴ���AI��ͼ���̡�
createImageList
��������̬����һ��ͼƬ�б���չʾ��ҳ���ϡ�
generate-button
��ť�ĵ���¼����ռ��û������������ѡ��IJ���������һ��AI��ͼ���󣬲����͵���������
XMLHttpRequest
������POST����ָ���ķ�������ַ��������Ӧ����̬����ҳ���ϵ�ͼ��������չʾ���ɵ�ͼ��
���ǿ�һ������Ч����
���Ѿ��� IP ��ַ���������������ȷ����С���䶯���������Ҫ������ֻ����� IP ��ַ��Ȼ�����²��𼴿����·��ʡ�
�������ٴι۲�һ������ͼƬ��Ч�����������ʱ����̨�ӿڵ���ӦҲ���������ġ�
����ȷʵ���������ռ䣬����ϸ�����һ�£�����������ܳ���ģ�͵������ϡ����磬���ڿ��߱Ȳ����������ε�ͼ�񣬿�����΢��һ����һ�㡣�������ɵ�ͼ�����߹����ԡ�����Щ���ⶼ�ǿ���ͨ�����Խ���ġ��ܵ���˵�����ǵ�EdgeOne��HAI���λ������Ѿ�ȡ���˽׶��Եijɹ������Ǹ�һ�����ˡ�
��ƪ����Դ�����˴��������������˼������Ͷ���˴���ʱ���뾫����������EdgeOne��HAI֮���Ðͬ���ԣ��˷����ڶ༼���������⡣ͨ����и��Ŭ�����ɹ��ؽ�������ǿ���ƽ̨������һ�𣬴����һ���������IJο����������̽��Ҳ���Ҹ����״������Ե����ʽ��Ʒ��ʵ��֮�á���ϣ��ͨ��������εľ����ͳɹ����ܹ����������˶���Щǰ�ؼ�������Ȥ��̽�����������ҵ���Ŀ����Ȥ�����ߴ��л����������������������֧�֣�Ҳ��Ó��ע�ң�һ����ѧϰ����ͬ������
��ICP��2022002427��-10 �湫��������43070202000427��
© 2013~2024 haote.com ������