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