Spaces:
Sleeping
Sleeping
File size: 7,174 Bytes
7f6b64d 4d49ad5 7f6b64d 4d49ad5 7f6b64d 4d49ad5 7f6b64d 4d49ad5 7f6b64d 4d49ad5 7f6b64d 4d49ad5 7f6b64d 4d49ad5 7f6b64d 4d49ad5 7f6b64d 4d49ad5 7f6b64d 4d49ad5 7f6b64d 4d49ad5 7f6b64d 4d49ad5 7f6b64d 4d49ad5 7f6b64d 4d49ad5 7f6b64d 4d49ad5 7f6b64d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
import gradio as gr
from PIL import Image
import requests
from bs4 import BeautifulSoup
import os
import subprocess
from huggingface_hub import ModelCard, SpaceCard, HfApi
# PNG Info Tool
def png_info(file):
img = Image.open(file)
metadata = img.info
metadata_str = "\n".join([f"{key}: {value}" for key, value in metadata.items()])
return metadata_str
# Web Info Tool
def web_info(url):
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
title = soup.title.string if soup.title else ""
meta_description = soup.find("meta", attrs={"name": "description"})
description = meta_description["content"] if meta_description else ""
meta_tags = soup.find_all("meta")
headers = {f"h{i}": len(soup.find_all(f"h{i}")) for i in range(1, 7)}
links = [a["href"] for a in soup.find_all("a", href=True)]
basic_info = f"Title: {title}\nDescription: {description}"
html_info = f"Meta Tags: {len(meta_tags)}\nHeaders: {headers}\nLinks: {len(links)}"
return f"{basic_info}\n\nHTML Info:\n{html_info}"
# View Source Info Tool
def view_source_info(url):
if url.startswith("view-source:"):
url = url.replace("view-source:", "").strip()
if not url.startswith(("http://", "https://")):
url = "https://" + url
response = requests.get(url)
return response.text
# Document Info Tool
def document_info(file):
file_extension = os.path.splitext(file.name)[1].lower()
content = ""
preview = None
if file_extension in [".txt", ".md"]:
with open(file.name, "r") as f:
content = f.read()
elif file_extension in [".png", ".jpg", ".jpeg", ".bmp", ".gif"]:
preview = file.name
else:
content = "Unsupported file format"
return content, preview
# Video Info Tool
def video_info(file):
video_display = file
result = subprocess.run(
["ffprobe", "-v", "error", "-show_format", "-show_streams", file],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
return video_display, result.stdout
# Model Info Tool
def model_info(model_id):
try:
card = ModelCard.load(model_id)
api = HfApi()
model_info = api.model_info(model_id)
by = model_info.author if model_info.author else ""
description = card.content if card.content else ""
related_spaces = model_info.siblings
related_spaces = [s.rfilename for s in related_spaces if s.rfilename.endswith(".py")]
related_spaces = "\n".join(related_spaces) if related_spaces else ""
install_instructions = f"pip install transformers\n\n# To use the model:\nfrom transformers import AutoModel, AutoTokenizer\nmodel = AutoModel.from_pretrained('{model_id}')\ntokenizer = AutoTokenizer.from_pretrained('{model_id}')"
return by, description, related_spaces, install_instructions
except Exception:
return "", "", "", ""
# Space Info Tool
def space_info(space_id):
try:
card = SpaceCard.load(space_id)
api = HfApi()
space_info = api.space_info(space_id)
makes = space_info.author if space_info.author else ""
description = card.content if card.content else ""
code_files = space_info.siblings
code_files = [s.rfilename for s in code_files if s.rfilename.endswith(".py")]
code = "\n".join(code_files) if code_files else ""
return makes, code, description
except Exception:
return "", "", ""
# Gradio Interface
with gr.Blocks() as demo:
gr.Markdown("## Information Extraction Tools")
with gr.Tab("PNG Info"):
png_input = gr.File(label="Upload PNG File")
png_output = gr.Textbox(label="PNG Metadata", lines=10)
png_button = gr.Button("Extract PNG Info")
png_button.click(png_info, inputs=png_input, outputs=png_output)
with gr.Tab("Web Info"):
web_input = gr.Textbox(label="Enter URL")
web_output = gr.Textbox(label="Web Info")
web_button = gr.Button("Extract Web Info")
web_button.click(web_info, inputs=web_input, outputs=web_output)
with gr.Tab("View Source Info"):
source_input = gr.Textbox(label="Enter URL (with or without 'view-source:')", placeholder="e.g., https://example.com or view-source:example.com")
source_output = gr.Textbox(label="HTML Source Code", lines=20)
source_button = gr.Button("View Source")
source_button.click(view_source_info, inputs=source_input, outputs=source_output)
with gr.Tab("Document Info"):
doc_input = gr.File(label="Upload Document")
content_output = gr.Textbox(label="Content", lines=10, visible=False)
preview_output = gr.Image(label="Preview (Image)", visible=False)
doc_button = gr.Button("Extract Document Info")
def update_output_components(file):
if file is None:
return gr.Textbox(visible=False), gr.Image(visible=False)
file_extension = os.path.splitext(file.name)[1].lower()
if file_extension in [".txt", ".md"]:
return gr.Textbox(visible=True), gr.Image(visible=False)
elif file_extension in [".png", ".jpg", ".jpeg", ".bmp", ".gif"]:
return gr.Textbox(visible=False), gr.Image(visible=True)
else:
return gr.Textbox(visible=True), gr.Image(visible=False)
doc_button.click(document_info, inputs=doc_input, outputs=[content_output, preview_output])
doc_input.change(update_output_components, inputs=doc_input, outputs=[content_output, preview_output])
with gr.Tab("Video Info"):
video_input = gr.Video(label="Upload Video")
video_output = gr.Video(label="Video Preview")
metadata_output = gr.Textbox(label="Video Metadata", lines=10)
video_button = gr.Button("Extract Video Info")
video_button.click(video_info, inputs=video_input, outputs=[video_output, metadata_output])
with gr.Tab("Model Info"):
model_input = gr.Textbox(label="Enter Model ID", placeholder="e.g., bert-base-uncased")
by_output = gr.Textbox(label="By", value="")
description_output = gr.Textbox(label="Description", value="", lines=5)
spaces_output = gr.Textbox(label="Related Spaces", value="", lines=3)
install_output = gr.Textbox(label="How to Install", value="", lines=5)
model_button = gr.Button("Fetch Model Info")
model_button.click(model_info, inputs=model_input, outputs=[by_output, description_output, spaces_output, install_output])
with gr.Tab("Space Info"):
space_input = gr.Textbox(label="Enter Space ID", placeholder="e.g., gradio/hello-world")
makes_output = gr.Textbox(label="Makes", value="")
code_output = gr.Textbox(label="Code", value="", lines=5)
description_output = gr.Textbox(label="Description", value="", lines=10)
space_button = gr.Button("Fetch Space Info")
space_button.click(space_info, inputs=space_input, outputs=[makes_output, code_output, description_output])
# Launch the Gradio app
demo.launch() |