Spaces:
Build error
Build error
Commit 路
8281a24
1
Parent(s): f106732
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
from transformers import DetrImageProcessor, DetrForObjectDetection
|
| 2 |
import torch
|
| 3 |
from PIL import Image
|
| 4 |
-
import requests
|
| 5 |
import gradio as gr
|
| 6 |
from huggingface_hub import InferenceClient
|
| 7 |
|
|
@@ -21,73 +20,26 @@ def detect_objects(image):
|
|
| 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 |
-
|
| 36 |
-
|
| 37 |
|
| 38 |
return result
|
| 39 |
|
| 40 |
# Define la funci贸n para generar la respuesta con el modelo Zephyr
|
| 41 |
def generate_response(result):
|
| 42 |
-
|
| 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 = [] #
|
| 84 |
-
|
| 85 |
-
|
| 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=
|
| 91 |
inputs=gr.Image(type="pil", label="Sube una imagen"), # Entrada de imagen
|
| 92 |
outputs="text" # Salida de texto
|
| 93 |
)
|
|
|
|
| 1 |
from transformers import DetrImageProcessor, DetrForObjectDetection
|
| 2 |
import torch
|
| 3 |
from PIL import Image
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
from huggingface_hub import InferenceClient
|
| 6 |
|
|
|
|
| 20 |
# Convierte las salidas a formato COCO
|
| 21 |
results = processor.post_process_object_detection(outputs, target_sizes=torch.tensor([image.size[::-1]]), threshold=0.9)[0]
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
# Formatea los resultados
|
| 24 |
formatted_results = format_detection_results(model, results)
|
| 25 |
|
| 26 |
+
# Convierte los resultados en una cadena de texto separada por comas
|
| 27 |
+
result = ", ".join(formatted_results)
|
| 28 |
|
| 29 |
return result
|
| 30 |
|
| 31 |
# Define la funci贸n para generar la respuesta con el modelo Zephyr
|
| 32 |
def generate_response(result):
|
| 33 |
+
result_str = str(result) # Convierte la imagen a cadena
|
| 34 |
+
zephyrToDo = "clasificame la palabra " + result_str + " en persona, paisaje u objeto"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
prompt = ""
|
| 36 |
+
history = [] # Puedes proporcionar un historial de conversaciones si es necesario
|
| 37 |
+
response = generate(prompt, history) # Reemplaza esto con la funci贸n real que genera la respuesta
|
| 38 |
+
return response
|
|
|
|
| 39 |
|
| 40 |
# Define la interfaz de Gradio con entrada de imagen
|
| 41 |
iface = gr.Interface(
|
| 42 |
+
fn=generate_response, # Cambia la funci贸n para que sea generate_response
|
| 43 |
inputs=gr.Image(type="pil", label="Sube una imagen"), # Entrada de imagen
|
| 44 |
outputs="text" # Salida de texto
|
| 45 |
)
|