Spaces:
Build error
Build error
| # Importando las librer铆as Gradio, requests, PIL e io | |
| import gradio as gr | |
| import requests | |
| from PIL import Image | |
| from io import BytesIO | |
| import argparse | |
| import os | |
| import cv2 | |
| import requests | |
| import numpy as np | |
| from pathlib import Path | |
| import warnings | |
| import torch | |
| from groundingdino.models import build_model | |
| from groundingdino.util.slconfig import SLConfig | |
| from groundingdino.util.utils import clean_state_dict | |
| from groundingdino.util.inference import annotate, load_image, predict | |
| import groundingdino.datasets.transforms as T | |
| from huggingface_hub import hf_hub_download | |
| # Use this command for evaluate the GLIP-T model | |
| config_file = "groundingdino/config/GroundingDINO_SwinT_OGC.py" | |
| ckpt_repo_id = "ShilongLiu/GroundingDINO" | |
| ckpt_filenmae = "groundingdino_swint_ogc.pth" | |
| def load_model_hf(model_config_path, repo_id, filename, device='cpu'): | |
| args = SLConfig.fromfile(model_config_path) | |
| model = build_model(args) | |
| args.device = device | |
| cache_file = hf_hub_download(repo_id=repo_id, filename=filename) | |
| checkpoint = torch.load(cache_file, map_location='cpu') | |
| log = model.load_state_dict(clean_state_dict(checkpoint['model']), strict=False) | |
| print("Model loaded from {} \n => {}".format(cache_file, log)) | |
| _ = model.eval() | |
| return model | |
| def image_transform_grounding(init_image): | |
| transform = T.Compose([ | |
| T.RandomResize([800], max_size=1333), | |
| T.ToTensor(), | |
| T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) | |
| ]) | |
| image, _ = transform(init_image, None) # 3, h, w | |
| return init_image, image | |
| def image_transform_grounding_for_vis(init_image): | |
| transform = T.Compose([ | |
| T.RandomResize([800], max_size=1333), | |
| ]) | |
| image, _ = transform(init_image, None) # 3, h, w | |
| return image | |
| model = load_model_hf(config_file, ckpt_repo_id, ckpt_filenmae) | |
| def run_grounding(input_image, grounding_caption, box_threshold, text_threshold): | |
| init_image = input_image.convert("RGB") | |
| original_size = init_image.size | |
| _, image_tensor = image_transform_grounding(init_image) | |
| image_pil: Image = image_transform_grounding_for_vis(init_image) | |
| # run grounding | |
| boxes, logits, phrases = predict(model, image_tensor, grounding_caption, box_threshold, text_threshold, device='cpu') | |
| annotated_frame = annotate(image_source=np.asarray(image_pil), boxes=boxes, logits=logits, phrases=phrases) | |
| image_with_box = Image.fromarray(cv2.cvtColor(annotated_frame, cv2.COLOR_BGR2RGB)) | |
| return image_with_box | |
| # Definiendo la funci贸n captura_pagina | |
| def captura_pagina(url): | |
| # Asignando la clave de la API y la URL | |
| api_key = 'b77e9ec7b82e4447b93c73cf1af4a93f' | |
| api_url = f'https://api.apiflash.com/v1/urltoimage?access_key={api_key}&url={url}' | |
| # Haciendo una solicitud GET a la API | |
| respuesta = requests.get(api_url, stream=True) | |
| # Si la solicitud es exitosa, se procesa la imagen | |
| if respuesta.status_code == 200: | |
| image_data = b'' | |
| for chunk in respuesta.iter_content(8192): | |
| image_data += chunk | |
| image = Image.open(BytesIO(image_data)) | |
| # Set the fixed box_threshold and text_threshold | |
| box_threshold = 0.38 | |
| text_threshold = 0.25 | |
| grounding_caption = "Find the webform in the picture of a web." | |
| # Run the Grounding DINO model on the image | |
| image_with_bb = run_grounding(image, grounding_caption, box_threshold, text_threshold) | |
| return "隆P谩gina web capturada con 茅xito!", image, image_with_bb | |
| else: | |
| # Si la solicitud no es exitosa, se retorna un mensaje de error | |
| return f'Error: {respuesta.status_code}', None, None | |
| # Definiendo la funci贸n captura_pagina_app | |
| def captura_pagina_app(): | |
| # Creando un objeto de la clase Row de Gradio | |
| with gr.Row(): | |
| with gr.Column(): | |
| # Agregando un cuadro de texto para ingresar la URL | |
| textbox_url = gr.Textbox(label='URL') | |
| # Agregando un bot贸n para capturar la p谩gina web | |
| btn_predecir = gr.Button(value='Predecir') | |
| with gr.Column(): | |
| # Agregando un cuadro de texto para mostrar el estado | |
| output_mensaje = gr.Textbox(label='Estado') | |
| # Agregando dos im谩genes para mostrar la captura de la p谩gina web | |
| output_img1 = gr.Image() | |
| output_img2 = gr.Image() | |
| # Asociando la funci贸n captura_pagina con el bot贸n | |
| btn_predecir.click( | |
| fn=captura_pagina, | |
| inputs=textbox_url, | |
| outputs=[output_mensaje, output_img1, output_img2] | |
| ) |