Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import DetrImageProcessor, DetrForObjectDetection, pipeline
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
# Load pre-trained image recognition model
|
| 6 |
+
detection_model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50")
|
| 7 |
+
detection_processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
|
| 8 |
+
|
| 9 |
+
# Load text generation model
|
| 10 |
+
description_generator = pipeline("text-generation", model="gpt-2")
|
| 11 |
+
|
| 12 |
+
# Function for recognizing product
|
| 13 |
+
def recognize_and_describe(image):
|
| 14 |
+
# Recognize product
|
| 15 |
+
inputs = detection_processor(images=image, return_tensors="pt")
|
| 16 |
+
outputs = detection_model(**inputs)
|
| 17 |
+
logits = outputs.logits.argmax(-1).item()
|
| 18 |
+
product_label = f"Product Class: {logits}" # Replace with class-to-label mapping if needed
|
| 19 |
+
|
| 20 |
+
# Generate description
|
| 21 |
+
prompt = f"Describe the product: {product_label}"
|
| 22 |
+
description = description_generator(prompt, max_length=50, num_return_sequences=1)
|
| 23 |
+
return product_label, description[0]["generated_text"]
|
| 24 |
+
|
| 25 |
+
# Create Gradio Interface
|
| 26 |
+
interface = gr.Interface(
|
| 27 |
+
fn=recognize_and_describe,
|
| 28 |
+
inputs="image",
|
| 29 |
+
outputs=["text", "text"],
|
| 30 |
+
title="SETA: Product Description App",
|
| 31 |
+
description="Upload a product image to get its description."
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
# Launch the app
|
| 35 |
+
if __name__ == "__main__":
|
| 36 |
+
interface.launch()
|