Spaces:
Build error
Build error
Commit
路
7e7e6dc
1
Parent(s):
9ea1437
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import DetrImageProcessor, DetrForObjectDetection
|
| 2 |
+
import torch
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import requests
|
| 5 |
+
from huggingface_hub import InferenceClient
|
| 6 |
+
import numpy as np
|
| 7 |
+
import gradio as gr
|
| 8 |
+
|
| 9 |
+
from transformers import DetrImageProcessor, DetrForObjectDetection
|
| 10 |
+
import torch
|
| 11 |
+
from PIL import Image
|
| 12 |
+
import requests
|
| 13 |
+
import gradio as gr
|
| 14 |
+
from huggingface_hub import InferenceClient
|
| 15 |
+
|
| 16 |
+
# Define una funci贸n que toma una imagen como entrada y obtiene el resultado
|
| 17 |
+
def detect_objects(image):
|
| 18 |
+
processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50", revision="no_timm")
|
| 19 |
+
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50", revision="no_timm")
|
| 20 |
+
|
| 21 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 22 |
+
outputs = model(**inputs)
|
| 23 |
+
|
| 24 |
+
# Convierte las salidas a formato COCO
|
| 25 |
+
target_sizes = torch.tensor([image.size[::-1])
|
| 26 |
+
results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0]
|
| 27 |
+
|
| 28 |
+
# Formatea los resultados
|
| 29 |
+
formatted_results = format_detection_results(model, results)
|
| 30 |
+
|
| 31 |
+
# Convierte los resultados en una cadena de texto separada por comas
|
| 32 |
+
result = ", ".join(formatted_results)
|
| 33 |
+
|
| 34 |
+
return result
|
| 35 |
+
|
| 36 |
+
# Define la funci贸n para generar la respuesta con el modelo Zephyr
|
| 37 |
+
def generate_response(result):
|
| 38 |
+
zephyrToDo = "clasificame la palabra " + result + " en persona, paisaje u objeto"
|
| 39 |
+
prompt = ""
|
| 40 |
+
history = [] # Puedes proporcionar un historial de conversaciones si es necesario
|
| 41 |
+
response = generate(prompt, history)
|
| 42 |
+
return response
|
| 43 |
+
|
| 44 |
+
# Define la interfaz de Gradio con entrada de imagen
|
| 45 |
+
iface = gr.Interface(
|
| 46 |
+
fn=detect_objects, # La funci贸n que procesa la imagen y obtiene el resultado
|
| 47 |
+
inputs=gr.inputs.Image(type="pil", label="Sube una imagen"), # Entrada de imagen
|
| 48 |
+
outputs="text" # Salida de texto
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
# Ejecuta la interfaz Gradio
|
| 52 |
+
iface.launch()
|