Spaces:
Build error
Build error
Commit
路
0115f6d
1
Parent(s):
7e7e6dc
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,3 @@
|
|
| 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
|
|
@@ -13,8 +5,13 @@ import requests
|
|
| 13 |
import gradio as gr
|
| 14 |
from huggingface_hub import InferenceClient
|
| 15 |
|
| 16 |
-
#
|
|
|
|
|
|
|
| 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 |
|
|
@@ -22,31 +19,78 @@ def detect_objects(image):
|
|
| 22 |
outputs = model(**inputs)
|
| 23 |
|
| 24 |
# Convierte las salidas a formato COCO
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
# Formatea los resultados
|
| 29 |
formatted_results = format_detection_results(model, results)
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
return result
|
| 35 |
|
| 36 |
# Define la funci贸n para generar la respuesta con el modelo Zephyr
|
| 37 |
def generate_response(result):
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
prompt = ""
|
| 40 |
-
history = [] #
|
|
|
|
| 41 |
response = generate(prompt, history)
|
| 42 |
-
|
| 43 |
|
| 44 |
# Define la interfaz de Gradio con entrada de imagen
|
| 45 |
iface = gr.Interface(
|
| 46 |
-
fn=detect_objects, #
|
| 47 |
-
inputs=gr.
|
| 48 |
outputs="text" # Salida de texto
|
| 49 |
)
|
| 50 |
|
| 51 |
# Ejecuta la interfaz Gradio
|
| 52 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from transformers import DetrImageProcessor, DetrForObjectDetection
|
| 2 |
import torch
|
| 3 |
from PIL import Image
|
|
|
|
| 5 |
import gradio as gr
|
| 6 |
from huggingface_hub import InferenceClient
|
| 7 |
|
| 8 |
+
# Tama帽o esperado de la imagen para el modelo DETR
|
| 9 |
+
target_size = 800 # Ajusta el tama帽o seg煤n las especificaciones del modelo
|
| 10 |
+
|
| 11 |
def detect_objects(image):
|
| 12 |
+
# Aseg煤rate de que la imagen sea cuadrada y del tama帽o esperado
|
| 13 |
+
image = image.resize((target_size, target_size))
|
| 14 |
+
|
| 15 |
processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50", revision="no_timm")
|
| 16 |
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50", revision="no_timm")
|
| 17 |
|
|
|
|
| 19 |
outputs = model(**inputs)
|
| 20 |
|
| 21 |
# Convierte las salidas a formato COCO
|
| 22 |
+
results = processor.post_process_object_detection(outputs, target_sizes=torch.tensor([image.size[::-1]]), threshold=0.9)[0]
|
| 23 |
+
|
| 24 |
+
def format_detection_results(model, results):
|
| 25 |
+
formatted_results = []
|
| 26 |
+
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
| 27 |
+
box = [round(i, 2) for i in box.tolist()]
|
| 28 |
+
result_str = f"{model.config.id2label[label.item()]}"
|
| 29 |
+
formatted_results.append(result_str)
|
| 30 |
+
return formatted_results
|
| 31 |
|
| 32 |
# Formatea los resultados
|
| 33 |
formatted_results = format_detection_results(model, results)
|
| 34 |
+
|
| 35 |
+
for result in formatted_results:
|
| 36 |
+
print(result)
|
| 37 |
+
|
| 38 |
return result
|
| 39 |
|
| 40 |
# Define la funci贸n para generar la respuesta con el modelo Zephyr
|
| 41 |
def generate_response(result):
|
| 42 |
+
client = InferenceClient("HuggingFaceH4/zephyr-7b-alpha")
|
| 43 |
+
|
| 44 |
+
zephyrToDo = "clasificame la palabra " + result + " en persona, paisaje o objeto"
|
| 45 |
+
|
| 46 |
+
def format_prompt(message, history):
|
| 47 |
+
system = "\nYou are a helpful virtual assistant that answers user's questions with easy-to-understand words.</s>\n"
|
| 48 |
+
prompt = ""
|
| 49 |
+
for user_prompt, bot_response in history:
|
| 50 |
+
prompt += f"\n{user_prompt}</s>\n"
|
| 51 |
+
prompt += f"\n{bot_response}</s>\n"
|
| 52 |
+
prompt += f"\n{zephyrToDo}</s>\n"
|
| 53 |
+
return prompt
|
| 54 |
+
|
| 55 |
+
def generate(
|
| 56 |
+
prompt, history, temperature=0.9, max_new_tokens=10, top_p=0.95, repetition_penalty=1.0,
|
| 57 |
+
):
|
| 58 |
+
temperature = float(temperature)
|
| 59 |
+
if temperature < 1e-2:
|
| 60 |
+
temperature = 1e-2
|
| 61 |
+
top_p = float(top_p)
|
| 62 |
+
|
| 63 |
+
generate_kwargs = dict(
|
| 64 |
+
temperature=temperature,
|
| 65 |
+
max_new_tokens=max_new_tokens,
|
| 66 |
+
top_p=top_p,
|
| 67 |
+
repetition_penalty=repetition_penalty,
|
| 68 |
+
do_sample=True,
|
| 69 |
+
seed=42,
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
formatted_prompt = format_prompt(prompt, history)
|
| 73 |
+
|
| 74 |
+
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
| 75 |
+
response_text = ""
|
| 76 |
+
|
| 77 |
+
for response in stream:
|
| 78 |
+
response_text += response.token.text
|
| 79 |
+
return response_text
|
| 80 |
+
|
| 81 |
+
# Example usage:
|
| 82 |
prompt = ""
|
| 83 |
+
history = [] # You can provide a history of conversations if needed
|
| 84 |
+
|
| 85 |
response = generate(prompt, history)
|
| 86 |
+
print(response) # This will print the model's response
|
| 87 |
|
| 88 |
# Define la interfaz de Gradio con entrada de imagen
|
| 89 |
iface = gr.Interface(
|
| 90 |
+
fn=lambda image: generate_response(detect_objects(image)), # Llama a detect_objects y luego a generate_response
|
| 91 |
+
inputs=gr.Image(type="pil", label="Sube una imagen"), # Entrada de imagen
|
| 92 |
outputs="text" # Salida de texto
|
| 93 |
)
|
| 94 |
|
| 95 |
# Ejecuta la interfaz Gradio
|
| 96 |
+
iface.launch(debug=True)
|