Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,16 @@
|
|
| 1 |
-
# app.py
|
| 2 |
-
import
|
| 3 |
from PIL import Image
|
| 4 |
import torch
|
| 5 |
import torchvision.transforms as transforms
|
| 6 |
import numpy as np
|
| 7 |
import sys
|
|
|
|
| 8 |
|
| 9 |
# Install and import (for Hugging Face environment)
|
| 10 |
try:
|
| 11 |
import pytorch_hub_examples
|
| 12 |
except ModuleNotFoundError: # Handle if not installed (Hugging Face)
|
| 13 |
-
import subprocess
|
| 14 |
subprocess.run(["git", "clone", "https://github.com/facebookresearch/pytorch_hub_examples.git"])
|
| 15 |
subprocess.run(["cd", "pytorch_hub_examples", "&&", "pip", "install", "-e", "."])
|
| 16 |
import pytorch_hub_examples
|
|
@@ -49,13 +49,20 @@ def remove_background(image):
|
|
| 49 |
|
| 50 |
return new_image
|
| 51 |
except Exception as e:
|
| 52 |
-
|
|
|
|
| 53 |
|
| 54 |
|
| 55 |
-
|
| 56 |
-
image_input = gr.Image(type="pil")
|
| 57 |
-
image_output = gr.Image()
|
| 58 |
-
image_button = gr.Button("Remove Background")
|
| 59 |
-
image_button.click(remove_background, inputs=image_input, outputs=image_output)
|
| 60 |
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py (Streamlit version)
|
| 2 |
+
import streamlit as st
|
| 3 |
from PIL import Image
|
| 4 |
import torch
|
| 5 |
import torchvision.transforms as transforms
|
| 6 |
import numpy as np
|
| 7 |
import sys
|
| 8 |
+
import subprocess
|
| 9 |
|
| 10 |
# Install and import (for Hugging Face environment)
|
| 11 |
try:
|
| 12 |
import pytorch_hub_examples
|
| 13 |
except ModuleNotFoundError: # Handle if not installed (Hugging Face)
|
|
|
|
| 14 |
subprocess.run(["git", "clone", "https://github.com/facebookresearch/pytorch_hub_examples.git"])
|
| 15 |
subprocess.run(["cd", "pytorch_hub_examples", "&&", "pip", "install", "-e", "."])
|
| 16 |
import pytorch_hub_examples
|
|
|
|
| 49 |
|
| 50 |
return new_image
|
| 51 |
except Exception as e:
|
| 52 |
+
st.error(f"Error: {e}") # Display error in Streamlit
|
| 53 |
+
return None # Return None in case of error
|
| 54 |
|
| 55 |
|
| 56 |
+
st.title("Background Remover")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 59 |
+
|
| 60 |
+
if uploaded_file is not None:
|
| 61 |
+
image = Image.open(uploaded_file).convert("RGB") # Ensure RGB for transform
|
| 62 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 63 |
+
|
| 64 |
+
if st.button("Remove Background"):
|
| 65 |
+
with st.spinner("Removing background..."): # Show a spinner while processing
|
| 66 |
+
result_image = remove_background(image)
|
| 67 |
+
if result_image:
|
| 68 |
+
st.image(result_image, caption="Background Removed", use_column_width=True)
|