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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
# from img_classification import teachable_machine_classification
|
| 3 |
+
from PIL import Image, ImageOps
|
| 4 |
+
import streamlit_authenticator as stauth
|
| 5 |
+
import yaml
|
| 6 |
+
from yaml.loader import SafeLoader
|
| 7 |
+
import os.path as osp
|
| 8 |
+
import glob
|
| 9 |
+
# import cv2
|
| 10 |
+
import numpy as np
|
| 11 |
+
|
| 12 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 13 |
+
from PIL import Image
|
| 14 |
+
import requests
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 19 |
+
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
# authentification
|
| 23 |
+
with open('./config.yaml') as file:
|
| 24 |
+
config = yaml.load(file, Loader=SafeLoader)
|
| 25 |
+
authenticator = stauth.Authenticate(
|
| 26 |
+
config['credentials'],
|
| 27 |
+
config['cookie']['name'],
|
| 28 |
+
config['cookie']['key'],
|
| 29 |
+
config['cookie']['expiry_days'],
|
| 30 |
+
config['preauthorized']
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
name, authentication_status, username = authenticator.login('Login', 'main')
|
| 34 |
+
if authentication_status:
|
| 35 |
+
authenticator.logout('Logout', 'main')
|
| 36 |
+
|
| 37 |
+
page = st.sidebar.selectbox("探索或预测", ("image_caption",
|
| 38 |
+
"image_to_text"
|
| 39 |
+
))
|
| 40 |
+
|
| 41 |
+
if page == "image_caption":
|
| 42 |
+
|
| 43 |
+
st.title("Image caption")
|
| 44 |
+
st.write("Model[link](https://huggingface.co/Salesforce/blip-image-captioning-base)")
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
uploaded_file = st.file_uploader("Select..", type=["jpg","png","jpeg"])
|
| 48 |
+
if uploaded_file is not None:
|
| 49 |
+
raw_image = Image.open(uploaded_file).convert('RGB')
|
| 50 |
+
st.image(raw_image, caption='image', use_column_width=True)
|
| 51 |
+
st.write("")
|
| 52 |
+
|
| 53 |
+
# unconditional image captioning
|
| 54 |
+
inputs = processor(raw_image, return_tensors="pt")
|
| 55 |
+
|
| 56 |
+
out = model.generate(**inputs)
|
| 57 |
+
st.text(processor.decode(out[0], skip_special_tokens=True))
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
# st.text(generated_text)
|
| 62 |
+
|
| 63 |
+
urll = st.text_input("image url", value="")
|
| 64 |
+
if st.button("send"):
|
| 65 |
+
raw_image = Image.open(requests.get(urll, stream=True).raw).convert('RGB')
|
| 66 |
+
|
| 67 |
+
inputs = processor(raw_image, return_tensors="pt")
|
| 68 |
+
|
| 69 |
+
out = model.generate(**inputs)
|
| 70 |
+
st.text(processor.decode(out[0], skip_special_tokens=True))
|
| 71 |
+
|
| 72 |
+
elif page == "image_to_text":
|
| 73 |
+
pass
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
# page = st.sidebar.selectbox("探索或预测", ("将图像放大为高清",
|
| 79 |
+
# "肺炎x_ray图像分类",
|
| 80 |
+
# "生成动漫人脸图像"
|
| 81 |
+
# ))
|
| 82 |
+
|
| 83 |
+
# if page == "肺炎x_ray图像分类":
|
| 84 |
+
# st.title("使用谷歌的可教机器进行图像分类")
|
| 85 |
+
# st.write("Google Teachable machine"" [link](https://teachablemachine.withgoogle.com/train/image)")
|
| 86 |
+
# st.header("肺炎x_ray")
|
| 87 |
+
# st.text("上传肺x_ray图片")
|
| 88 |
+
|
| 89 |
+
# uploaded_file = st.file_uploader("选择..", type=["jpg","png","jpeg"])
|
| 90 |
+
# if uploaded_file is not None:
|
| 91 |
+
# image = Image.open(uploaded_file).convert('RGB')
|
| 92 |
+
# st.image(image, caption='上传了图片。', use_column_width=True)
|
| 93 |
+
# st.write("")
|
| 94 |
+
# st.write("分类...")
|
| 95 |
+
# label = teachable_machine_classification(image, 'pneumonia__x_ray_image_classify_normal_vs_penumonia.h5')
|
| 96 |
+
# if label == 0:
|
| 97 |
+
# st.write("正常")
|
| 98 |
+
# else:
|
| 99 |
+
# st.write("肺炎")
|
| 100 |
+
|
| 101 |
+
# st.text("类:正常,肺炎")
|
| 102 |
+
|
| 103 |
+
# # 0 normal
|
| 104 |
+
# # 1 pneumonia
|
| 105 |
+
# elif page =="将图像放大为高清":
|
| 106 |
+
# st.title("使用 ESGAN 放大图像")
|
| 107 |
+
# st.write("ESGAN 安装"" [link](https://github.com/xinntao/ESRGAN)")
|
| 108 |
+
# st.write("ESGAN 模型下载"" [link](https://drive.google.com/drive/u/0/folders/17VYV_SoZZesU6mbxz2dMAIccSSlqLecY)")
|
| 109 |
+
# st.header("将图像放大为高清")
|
| 110 |
+
# st.text("上传图片")
|
| 111 |
+
|
| 112 |
+
# model_path = './RRDB_ESRGAN_x4.pth' # models/RRDB_ESRGAN_x4.pth OR models/RRDB_PSNR_x4.pth
|
| 113 |
+
# # device = torch.device('cuda') # if you want to run on CPU, change 'cuda' -> cpu
|
| 114 |
+
# device = torch.device('cpu')
|
| 115 |
+
|
| 116 |
+
# # test_img_folder = 'LR/*'
|
| 117 |
+
# uploaded_file = st.file_uploader("选择..", type=["jpg","png","jpeg"])
|
| 118 |
+
# if uploaded_file is not None:
|
| 119 |
+
# img = Image.open(uploaded_file).convert('RGB')
|
| 120 |
+
# st.image(img, caption='上传了图片。', use_column_width=True)
|
| 121 |
+
# st.write("")
|
| 122 |
+
# st.write("")
|
| 123 |
+
# st.write("放大图像,大约等待时间:1 分钟,请稍候...")
|
| 124 |
+
|
| 125 |
+
# rrdb_esrgan_model = arch.RRDBNet(3, 3, 64, 23, gc=32)
|
| 126 |
+
# rrdb_esrgan_model.load_state_dict(torch.load(model_path), strict=True)
|
| 127 |
+
# rrdb_esrgan_model.eval()
|
| 128 |
+
# rrdb_esrgan_model = rrdb_esrgan_model.to(device)
|
| 129 |
+
|
| 130 |
+
# idx = 0
|
| 131 |
+
|
| 132 |
+
# # img = np.array(img.getdata()).reshape(img.size[0], img.size[1], 3) * 1.0 / 255
|
| 133 |
+
# # uploaded_file = st.file_uploader("Upload Image")
|
| 134 |
+
# # image = Image.open(uploaded_file)
|
| 135 |
+
# # st.image(image, caption='Input', use_column_width=True)
|
| 136 |
+
# img = np.array(img)* 1.0 / 255
|
| 137 |
+
# # cv2.imwrite('out.jpg', cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR))
|
| 138 |
+
|
| 139 |
+
# img = torch.from_numpy(np.transpose(img[:, :, [2, 1, 0]], (2, 0, 1))).float()
|
| 140 |
+
# img_LR = img.unsqueeze(0)
|
| 141 |
+
# img_LR = img_LR.to(device)
|
| 142 |
+
|
| 143 |
+
# with torch.no_grad():
|
| 144 |
+
# output = rrdb_esrgan_model(img_LR).data.squeeze().float().cpu().clamp_(0, 1).numpy()
|
| 145 |
+
# output = np.transpose(output[[2, 1, 0], :, :], (1, 2, 0))
|
| 146 |
+
# output = torch.tensor((output * 255.0).round())
|
| 147 |
+
# fig1 = plt.figure(figsize=(14,8))
|
| 148 |
+
|
| 149 |
+
# fig1.suptitle("Upscaled image")
|
| 150 |
+
# plt.imshow(np.transpose(vutils.make_grid(output, padding=2, normalize=True), (0,1, 2)))
|
| 151 |
+
|
| 152 |
+
# st.pyplot(fig1)
|
| 153 |
+
|
| 154 |
+
# elif page =="生成动漫人脸图像":
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
# # Number of GPUs available. Use 0 for CPU mode.
|
| 158 |
+
# ngpu = 1
|
| 159 |
+
|
| 160 |
+
# # device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 161 |
+
# device = torch.device("cpu")
|
| 162 |
+
# # anime_face_gan_gen_model = AnimeFaceGenerator(ngpu).to(device)
|
| 163 |
+
# anime_face_gan_gen_model = torch.load("./anime_face_gan_generator64_64.pt",map_location=torch.device('cpu') )
|
| 164 |
+
# pp1=st.slider("p1",-5.01,5.00)
|
| 165 |
+
# pp2=st.slider("p2",-5.01,5.00)
|
| 166 |
+
# pp3=st.slider("p3",-5.01,5.00)
|
| 167 |
+
# pp4=st.slider("p4",-5.01,5.00)
|
| 168 |
+
# pp5=st.slider("p5",-5.01,5.00)
|
| 169 |
+
# pp6=st.slider("p6",-5.01,5.00)
|
| 170 |
+
# pp7=st.slider("p7",-5.01,5.00)
|
| 171 |
+
# pp8=st.slider("p8",-5.01,5.00)
|
| 172 |
+
# anime_face_gan_gen_model.eval()
|
| 173 |
+
# bla = [pp1,pp2,pp3,pp4,pp5,pp6,pp7,pp8]
|
| 174 |
+
# randomlist = []
|
| 175 |
+
# for i in range(0,92):
|
| 176 |
+
# n = random.random()
|
| 177 |
+
# randomlist.append(n)
|
| 178 |
+
# res = bla + randomlist
|
| 179 |
+
# # print(res)
|
| 180 |
+
|
| 181 |
+
# fixed_noise = torch.tensor(res).reshape(1,100,1,1)
|
| 182 |
+
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
# # fixed_noise = torch.randn(1, nz, 1, 1, device=device)
|
| 186 |
+
# print(fixed_noise)
|
| 187 |
+
# fake = anime_face_gan_gen_model(fixed_noise)
|
| 188 |
+
|
| 189 |
+
# fig1 = plt.figure(figsize=(14,8))
|
| 190 |
+
|
| 191 |
+
# fig1.suptitle("随机生成的动漫脸")
|
| 192 |
+
|
| 193 |
+
# plt.imshow(np.transpose(vutils.make_grid(fake, padding=2, normalize=True), (1, 2, 0)))
|
| 194 |
+
|
| 195 |
+
# st.pyplot(fig1)
|
| 196 |
+
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
elif authentication_status == False:
|
| 200 |
+
st.error("用户名/密码不正确")
|
| 201 |
+
elif authentication_status == None:
|
| 202 |
+
st.warning('请输入您的用户名和密码')
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
|
| 207 |
+
|
| 208 |
+
|
| 209 |
+
|
| 210 |
+
|
config.yaml
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
credentials:
|
| 2 |
+
usernames:
|
| 3 |
+
jsmith:
|
| 4 |
+
email: [email protected]
|
| 5 |
+
name: John Smith
|
| 6 |
+
password: $2b$12$ry8j7c.cyCtv5X92THQxmeMTTnafUOr.pTzdb4T1B7v2p1DdayooW #abc # To be replaced with hashed password
|
| 7 |
+
rbriggs:
|
| 8 |
+
email: [email protected]
|
| 9 |
+
name: Rebecca Briggs
|
| 10 |
+
password: 2b$12$A9k.KJXV2/pjXTf0ve7.eualBqrpUug9Dt2Hze1zORDFx6/UosKKW #def # To be replaced with hashed password
|
| 11 |
+
cookie:
|
| 12 |
+
expiry_days: 30
|
| 13 |
+
key: random_signature_key # Must be string
|
| 14 |
+
name: random_cookie_name
|
| 15 |
+
preauthorized:
|
| 16 |
+
emails:
|
| 17 |
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit-authenticator==0.2.1
|
| 2 |
+
openai==0.27.4
|
| 3 |
+
torch==2.0.0
|
| 4 |
+
transformers==4.27.4
|