Spaces:
Runtime error
Runtime error
Upload app_gradio.py
Browse files- app_gradio.py +44 -0
app_gradio.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# Load API Key from Environment Variable
|
| 6 |
+
HUGGING_FACE_API_KEY = os.getenv("HUGGING_FACE_API_KEY")
|
| 7 |
+
|
| 8 |
+
# Hugging Face Model API URL
|
| 9 |
+
MODEL_URL = "https://api-inference.huggingface.co/models/ramim36/Kolors-Virtual-Try-On"
|
| 10 |
+
|
| 11 |
+
def virtual_tryon(user_image, cloth_image):
|
| 12 |
+
"""Send images to Hugging Face model and return result."""
|
| 13 |
+
if user_image is None or cloth_image is None:
|
| 14 |
+
return "Please upload both user and cloth images."
|
| 15 |
+
|
| 16 |
+
headers = {"Authorization": f"Bearer {HUGGING_FACE_API_KEY}"}
|
| 17 |
+
|
| 18 |
+
files = {
|
| 19 |
+
"user_image": user_image,
|
| 20 |
+
"cloth_image": cloth_image,
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
response = requests.post(MODEL_URL, headers=headers, files=files)
|
| 24 |
+
|
| 25 |
+
if response.status_code == 200:
|
| 26 |
+
return response.json() # Return model output
|
| 27 |
+
else:
|
| 28 |
+
return {"error": "Failed to process request", "details": response.text}
|
| 29 |
+
|
| 30 |
+
# Gradio Interface
|
| 31 |
+
iface = gr.Interface(
|
| 32 |
+
fn=virtual_tryon,
|
| 33 |
+
inputs=[
|
| 34 |
+
gr.Image(type="file", label="Upload User Image"),
|
| 35 |
+
gr.Image(type="file", label="Upload Cloth Image"),
|
| 36 |
+
],
|
| 37 |
+
outputs="json",
|
| 38 |
+
title="Virtual Try-On",
|
| 39 |
+
description="Upload a user image and a clothing image to try them on virtually.",
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
# Launch Gradio app
|
| 43 |
+
if __name__ == "__main__":
|
| 44 |
+
iface.launch()
|