Spaces:
Sleeping
Sleeping
from PIL import Image | |
import glob | |
import gradio as gr | |
import warnings | |
import io | |
import pandas as pd | |
import replicate | |
import json | |
import requests | |
import os | |
from datetime import date | |
import secrets | |
import string | |
import b2sdk.v2 as b2 #Backblaze img2img upload bucket | |
import os | |
import requests | |
from dotenv import load_dotenv, find_dotenv | |
_ = load_dotenv(find_dotenv()) | |
## Backclaze | |
info = b2.InMemoryAccountInfo() | |
b2_api = b2.B2Api(info) | |
application_key_id = os.getenv("BB_KeyID") | |
application_key = os.getenv("BB_AppKey") | |
#print(application_key_id,application_key) | |
b2_api.authorize_account("production", application_key_id, application_key) | |
BB_bucket_name=os.getenv("BB_bucket") | |
BB_bucket=b2_api.get_bucket_by_name(os.getenv("BB_bucket")) | |
BB_defurl=os.getenv("BB_defurl") | |
def BB_uploadfile(local_file,file_name,b2_api=b2_api,BB_bucket_name=BB_bucket_name,FRIENDLY_URL=True): | |
metadata = {"key": "value"} | |
uploaded_file = BB_bucket.upload_local_file( | |
local_file=local_file, | |
file_name=file_name, | |
file_infos=metadata, | |
) | |
img_url=b2_api.get_download_url_for_fileid(uploaded_file.id_) | |
if FRIENDLY_URL: #Get friendly URP | |
img_url=BB_defurl+"/file/"+BB_bucket_name+"/"+file_name | |
print("backblaze", img_url) | |
return img_url | |
#deepL api | |
def DeepL(text,source_lang="JA",target_lang="EN",API_KEY= os.getenv("DEEPL_Key")): | |
# パラメータの指定 | |
params = { | |
'auth_key' : API_KEY, | |
'text' : text, | |
'source_lang' : source_lang, # 翻訳対象の言語 | |
"target_lang": target_lang # 翻訳後の言語 | |
} | |
# リクエストを投げる | |
request = requests.post("https://api-free.deepl.com/v2/translate", data=params) # URIは有償版, 無償版で異なるため要注意 | |
result = request.json() | |
return result["translations"][0]["text"] | |
##Email client | |
import smtplib | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.text import MIMEText | |
from email.mime.image import MIMEImage | |
from email.utils import formatdate | |
def send_email(image_path, recipient_email,prompt=None,email=os.getenv("email"),pw=os.getenv("email_pw")): | |
sender_email = email | |
password = pw | |
msg = MIMEMultipart() | |
msg['From'] = sender_email | |
msg['To'] = recipient_email | |
msg['Subject'] =f"「Saiseisei Generated Image」: {os.path.basename(image_path)}" | |
msg['Date'] = formatdate() | |
# Attach prompt | |
Text=f""" | |
再生成のワークショップをご参加ありがとうございました | |
生成したAI画像はこのメールに添付しました。 | |
-- | |
file name: {os.path.basename(image_path)} | |
prompt: {prompt} | |
-- | |
再生成について最も知りたいなら、IGで@pp_yokoと@jarvis_labsを連絡してください | |
https://www.instagram.com/jarvis_labs/ | |
https://www.instagram.com/pp_yoko/ | |
-- | |
そして、SNSで画像を共有したいなら、ぜひしてお願いいたします | |
よろしくお願いいたします。 | |
Jarvis | |
""" | |
msg.attach(MIMEText(Text, 'plain', 'utf-8')) | |
# Attach the image | |
with open(image_path, 'rb') as f: | |
img = MIMEImage(f.read()) | |
msg.attach(img) | |
server = smtplib.SMTP('smtp.gmail.com', 587) #smtp_server = "smtp.gmail.com" Google | |
server.ehlo() | |
server.starttls() | |
server.ehlo() | |
server.login(sender_email, password) | |
server.send_message(msg) | |
server.quit() | |
#Other | |
def random_str(n=8): | |
# Define the characters to choose from | |
characters = string.ascii_letters + string.digits | |
# Generate the unreplicable string of length 8 | |
unreplicable_string = ''.join(secrets.choice(characters) for _ in range(n)) | |
return unreplicable_string | |
#Replicate img gen | |
base_model="lucataco/sdxl:c86579ac5193bf45422f1c8b92742135aa859b1850a8e4c531bff222fc75273d" | |
img_loc="./Tiles/" | |
example_data=[] | |
example_data.append({"img":img_loc+"4tile_20240115.png","promt":"Green garden with a water water fountain and a river","ps":0.6}) | |
example_data.append({"img":img_loc+"Tile_20240123_1.jpg","promt": "Outer space dust cloud","ps":0.6}) | |
example_data.append({"img":img_loc+"Tile_20240404.JPG","promt": "Volcano","ps":0.6}) | |
example_data.append({"img":img_loc+"Tile_20240421_1.jpg","promt": "Outer space dust cloud with a small ship flying through","ps":0.6}) | |
#"jarvissan22/kawasehasui_backgrounds:a4bb8bb1beb503b02c93789381544097dcb70afc92c512ea1500e70ccf704bc4" | |
def Gen_image(prompt,img=None,ps=0.7,lr=0.8,model=base_model,save_loc="",w=1024,h=1024): | |
input={ | |
"width": w, | |
"height": h, | |
"prompt": prompt, | |
"refine": "no_refiner", | |
"scheduler": "K_EULER", | |
"lora_scale": lr, | |
"num_outputs": 1, | |
"guidance_scale": 7.5, | |
"apply_watermark": True, | |
"high_noise_frac": 0.8, | |
"negative_prompt": "", | |
"prompt_strength": ps, | |
"num_inference_steps": 50 | |
} | |
if img: #Upload image to backblaze | |
img_url=BB_uploadfile(img,os.path.basename(img)) | |
input["image"]=img_url | |
#Gen image | |
output = replicate.run( | |
model, | |
input=input, | |
) | |
#Get image | |
img_url=output[0] | |
r=requests.get(img_url) | |
image = Image.open(io.BytesIO(r.content)) | |
#Save image | |
file_saveloc=os.path.join(save_loc,f"GenImage_{date.today().strftime('%Y-%m-%d')}_{random_str()}.jpg") | |
image.save(file_saveloc) | |
#Upload image to backblaze | |
_=BB_uploadfile(file_saveloc,os.path.basename(file_saveloc)) | |
return image,file_saveloc |