Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
import os
|
4 |
+
from PIL import Image
|
5 |
+
|
6 |
+
# Streamlit app title
|
7 |
+
st.title("Background Remover App")
|
8 |
+
|
9 |
+
# Input for API Key
|
10 |
+
api_key = st.text_input("Enter Stability API Key", type="password")
|
11 |
+
|
12 |
+
# File uploader for images
|
13 |
+
uploaded_files = st.file_uploader("Upload Images", type=["png", "jpg", "jpeg", "webp"], accept_multiple_files=True)
|
14 |
+
|
15 |
+
# Output format selector
|
16 |
+
output_format = st.selectbox("Select Output Format", options=["png", "webp"], index=0)
|
17 |
+
|
18 |
+
# Process button
|
19 |
+
if st.button("Start Processing"):
|
20 |
+
if not api_key:
|
21 |
+
st.error("Please enter your API key.")
|
22 |
+
elif not uploaded_files:
|
23 |
+
st.error("Please upload at least one image.")
|
24 |
+
else:
|
25 |
+
progress_bar = st.progress(0)
|
26 |
+
results = []
|
27 |
+
for i, uploaded_file in enumerate(uploaded_files):
|
28 |
+
image = uploaded_file.read()
|
29 |
+
try:
|
30 |
+
response = requests.post(
|
31 |
+
"https://api.stability.ai/v2beta/stable-image/edit/remove-background",
|
32 |
+
headers={
|
33 |
+
"authorization": f"Bearer {api_key}",
|
34 |
+
"accept": "image/*"
|
35 |
+
},
|
36 |
+
files={"image": image},
|
37 |
+
data={"output_format": output_format},
|
38 |
+
)
|
39 |
+
|
40 |
+
if response.status_code == 200:
|
41 |
+
output_file_path = f"{uploaded_file.name.split('.')[0]}_processed.{output_format}"
|
42 |
+
with open(output_file_path, "wb") as f:
|
43 |
+
f.write(response.content)
|
44 |
+
|
45 |
+
results.append((uploaded_file.name, output_file_path))
|
46 |
+
st.success(f"Processed: {uploaded_file.name}")
|
47 |
+
st.image(Image.open(output_file_path), caption=f"Processed: {output_file_path}")
|
48 |
+
else:
|
49 |
+
st.error(f"Failed to process {uploaded_file.name}: {response.json()}")
|
50 |
+
except Exception as e:
|
51 |
+
st.error(f"Error processing {uploaded_file.name}: {e}")
|
52 |
+
progress_bar.progress((i + 1) / len(uploaded_files))
|
53 |
+
|
54 |
+
if results:
|
55 |
+
st.success("Processing completed!")
|
56 |
+
|