Spaces:
Running
Running
Streetmarkets
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoProcessor, AutoModelForZeroShotImageClassification
|
3 |
+
from PIL import Image
|
4 |
+
import requests
|
5 |
+
import torch
|
6 |
+
|
7 |
+
# Load the FashionCLIP processor and model
|
8 |
+
processor = AutoProcessor.from_pretrained("patrickjohncyh/fashion-clip")
|
9 |
+
model = AutoModelForZeroShotImageClassification.from_pretrained("patrickjohncyh/fashion-clip")
|
10 |
+
|
11 |
+
# Define the function to process image and text
|
12 |
+
def process_image_and_text(product_title, image_url):
|
13 |
+
try:
|
14 |
+
# Fetch and process the image
|
15 |
+
response = requests.get(image_url, stream=True)
|
16 |
+
response.raise_for_status()
|
17 |
+
image = Image.open(response.raw)
|
18 |
+
|
19 |
+
# Prepare inputs for the model
|
20 |
+
inputs = processor(
|
21 |
+
text=[product_title],
|
22 |
+
images=image,
|
23 |
+
return_tensors="pt",
|
24 |
+
padding=True
|
25 |
+
)
|
26 |
+
|
27 |
+
# Perform inference
|
28 |
+
with torch.no_grad():
|
29 |
+
outputs = model(**inputs)
|
30 |
+
|
31 |
+
# Extract similarity score and embeddings
|
32 |
+
similarity_score = outputs.logits_per_image[0].item()
|
33 |
+
text_embedding = outputs.logits_per_text.cpu().numpy().tolist()
|
34 |
+
image_embedding = outputs.logits_per_image.cpu().numpy().tolist()
|
35 |
+
|
36 |
+
return {
|
37 |
+
"similarity_score": similarity_score,
|
38 |
+
"text_embedding": text_embedding,
|
39 |
+
"image_embedding": image_embedding
|
40 |
+
}
|
41 |
+
except Exception as e:
|
42 |
+
return {"error": str(e)}
|
43 |
+
|
44 |
+
# Create the Gradio interface
|
45 |
+
interface = gr.Interface(
|
46 |
+
fn=process_image_and_text,
|
47 |
+
inputs=[
|
48 |
+
gr.Textbox(label="Product Title", placeholder="e.g., ring for men"),
|
49 |
+
gr.Textbox(label="Image URL", placeholder="e.g., https://example.com/image.jpg")
|
50 |
+
],
|
51 |
+
outputs="json",
|
52 |
+
title="FashionCLIP API",
|
53 |
+
description="Provide a product title and an image URL to compute similarity score and embeddings."
|
54 |
+
)
|
55 |
+
|
56 |
+
# Launch the app
|
57 |
+
if __name__ == "__main__":
|
58 |
+
interface.launch()
|