ZainMalik0925 commited on
Commit
7205f40
·
verified ·
1 Parent(s): 45ab0e3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -0
app.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ import requests
4
+ from io import BytesIO
5
+ import os
6
+
7
+ # Title and Description
8
+ st.title("Denim Virtual Try-On")
9
+ st.write("Upload your leg images and the front/back images of denim pants to see how they fit.")
10
+
11
+ # File Upload Section
12
+ st.sidebar.header("Upload Images")
13
+ subject_image = st.sidebar.file_uploader("Upload Subject Image (Legs)", type=["jpg", "jpeg", "png"])
14
+ pant_front_image = st.sidebar.file_uploader("Upload Denim Pant Front Image", type=["jpg", "jpeg", "png"])
15
+ pant_back_image = st.sidebar.file_uploader("Upload Denim Pant Back Image", type=["jpg", "jpeg", "png"])
16
+
17
+ # Function to process images using Hugging Face API
18
+ def virtual_tryon(subject_img, front_img, back_img):
19
+ url = "https://api-inference.huggingface.co/models/ramim36/Kolors-Virtual-Try-On"
20
+ headers = {"Authorization": f"Bearer {os.getenv('HF_API_TOKEN')}"} # Set your Hugging Face API token in env variable
21
+
22
+ # Prepare data
23
+ files = {
24
+ "subject_image": subject_img,
25
+ "front_image": front_img,
26
+ "back_image": back_img
27
+ }
28
+
29
+ response = requests.post(url, headers=headers, files=files)
30
+ if response.status_code == 200:
31
+ return Image.open(BytesIO(response.content))
32
+ else:
33
+ st.error(f"Error: {response.status_code} - {response.text}")
34
+ return None
35
+
36
+ # Display Input Images
37
+ if subject_image:
38
+ st.image(subject_image, caption="Uploaded Subject Image", use_column_width=True)
39
+ if pant_front_image:
40
+ st.image(pant_front_image, caption="Uploaded Front Denim Image", use_column_width=True)
41
+ if pant_back_image:
42
+ st.image(pant_back_image, caption="Uploaded Back Denim Image", use_column_width=True)
43
+
44
+ # Generate Try-On Output
45
+ if st.button("Generate Virtual Try-On"):
46
+ if subject_image and pant_front_image and pant_back_image:
47
+ with st.spinner("Processing... Please wait."):
48
+ try:
49
+ composite_image = virtual_tryon(subject_image, pant_front_image, pant_back_image)
50
+ if composite_image:
51
+ st.image(composite_image, caption="Virtual Try-On Result", use_column_width=True)
52
+ except Exception as e:
53
+ st.error(f"An error occurred: {e}")
54
+ else:
55
+ st.error("Please upload all required images.")