|
|
import streamlit as st |
|
|
import requests |
|
|
import os |
|
|
|
|
|
|
|
|
api_key = os.getenv("KEY") |
|
|
|
|
|
|
|
|
st.title("Head Extraction Tool") |
|
|
st.write("Extract heads from images using AI.") |
|
|
|
|
|
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png"]) |
|
|
|
|
|
def extract_head(image): |
|
|
url = "https://head-extraction1.p.rapidapi.com/head-extraction" |
|
|
headers = { |
|
|
"x-rapidapi-key": api_key, |
|
|
"x-rapidapi-host": "head-extraction1.p.rapidapi.com" |
|
|
} |
|
|
files = {"image": (image.name, image, image.type)} |
|
|
response = requests.post(url, files=files, headers=headers) |
|
|
return response.json() |
|
|
|
|
|
if st.button("Extract Head"): |
|
|
if uploaded_file: |
|
|
with st.spinner("Extracting head..."): |
|
|
result = extract_head(uploaded_file) |
|
|
|
|
|
|
|
|
head_url = result.get("message", {}).get("head_image_url") |
|
|
if head_url: |
|
|
st.image(head_url, caption="Extracted Head", use_container_width=True) |
|
|
|
|
|
|
|
|
st.download_button( |
|
|
label="Download Head Image", |
|
|
data=requests.get(head_url).content, |
|
|
file_name="extracted_head.png", |
|
|
mime="image/png" |
|
|
) |
|
|
else: |
|
|
st.error("Failed to extract head. No image URL found in response.") |
|
|
st.write("Response Data:", result) |
|
|
else: |
|
|
st.warning("Please upload an image.") |