Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
from PIL.PngImagePlugin import PngInfo
|
4 |
+
import requests
|
5 |
+
from bs4 import BeautifulSoup
|
6 |
+
import os
|
7 |
+
import subprocess
|
8 |
+
from huggingface_hub import ModelCard, SpaceCard
|
9 |
+
|
10 |
+
# PNG Info Tool
|
11 |
+
def png_info(file):
|
12 |
+
img = Image.open(file)
|
13 |
+
info = img.info
|
14 |
+
return str(info)
|
15 |
+
|
16 |
+
# Web Info Tool (with HTML Info)
|
17 |
+
def web_info(url):
|
18 |
+
# Fetch the webpage
|
19 |
+
response = requests.get(url)
|
20 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
21 |
+
|
22 |
+
# Extract basic info
|
23 |
+
title = soup.title.string if soup.title else "No Title"
|
24 |
+
meta_description = soup.find("meta", attrs={"name": "description"})
|
25 |
+
description = meta_description["content"] if meta_description else "No Description"
|
26 |
+
|
27 |
+
# Extract additional HTML info
|
28 |
+
meta_tags = soup.find_all("meta")
|
29 |
+
headers = {f"h{i}": len(soup.find_all(f"h{i}")) for i in range(1, 7)}
|
30 |
+
links = [a["href"] for a in soup.find_all("a", href=True)]
|
31 |
+
|
32 |
+
# Format the output
|
33 |
+
basic_info = f"Title: {title}\nDescription: {description}"
|
34 |
+
html_info = f"Meta Tags: {len(meta_tags)}\nHeaders: {headers}\nLinks: {len(links)}"
|
35 |
+
return f"{basic_info}\n\nHTML Info:\n{html_info}"
|
36 |
+
|
37 |
+
# View Source Info Tool
|
38 |
+
def view_source_info(url):
|
39 |
+
if url.startswith("view-source:"):
|
40 |
+
url = url.replace("view-source:", "").strip()
|
41 |
+
if not url.startswith(("http://", "https://")):
|
42 |
+
url = "https://" + url
|
43 |
+
response = requests.get(url)
|
44 |
+
return response.text
|
45 |
+
|
46 |
+
# Document Info Tool
|
47 |
+
def document_info(file):
|
48 |
+
file_size = os.path.getsize(file)
|
49 |
+
file_type = os.path.splitext(file)[1]
|
50 |
+
return f"File Type: {file_type}\nFile Size: {file_size} bytes"
|
51 |
+
|
52 |
+
# Video Info Tool
|
53 |
+
def video_info(file):
|
54 |
+
# Display the video
|
55 |
+
video_display = file
|
56 |
+
|
57 |
+
# Extract video metadata using ffprobe
|
58 |
+
result = subprocess.run(
|
59 |
+
["ffprobe", "-v", "error", "-show_format", "-show_streams", file],
|
60 |
+
stdout=subprocess.PIPE,
|
61 |
+
stderr=subprocess.PIPE,
|
62 |
+
text=True,
|
63 |
+
)
|
64 |
+
|
65 |
+
# Return the video display and metadata
|
66 |
+
return video_display, result.stdout
|
67 |
+
|
68 |
+
# Model Info Tool
|
69 |
+
def model_info(model_id):
|
70 |
+
# Fetch model card from Hugging Face Hub
|
71 |
+
card = ModelCard.load(model_id)
|
72 |
+
return card.content # Return the model card content (markdown)
|
73 |
+
|
74 |
+
# Space Info Tool
|
75 |
+
def space_info(space_id):
|
76 |
+
# Fetch space card from Hugging Face Hub
|
77 |
+
card = SpaceCard.load(space_id)
|
78 |
+
return card.content # Return the space card content (markdown)
|
79 |
+
|
80 |
+
# Gradio Interface
|
81 |
+
with gr.Blocks() as demo:
|
82 |
+
gr.Markdown("## Information Extraction Tools")
|
83 |
+
|
84 |
+
with gr.Tab("PNG Info"):
|
85 |
+
png_input = gr.File(label="Upload PNG File")
|
86 |
+
png_output = gr.Textbox(label="PNG Info")
|
87 |
+
png_button = gr.Button("Extract PNG Info")
|
88 |
+
png_button.click(png_info, inputs=png_input, outputs=png_output)
|
89 |
+
|
90 |
+
with gr.Tab("Web Info"):
|
91 |
+
web_input = gr.Textbox(label="Enter URL")
|
92 |
+
web_output = gr.Textbox(label="Web Info")
|
93 |
+
web_button = gr.Button("Extract Web Info")
|
94 |
+
web_button.click(web_info, inputs=web_input, outputs=web_output)
|
95 |
+
|
96 |
+
with gr.Tab("View Source Info"):
|
97 |
+
source_input = gr.Textbox(label="Enter URL (with or without 'view-source:')", placeholder="e.g., https://example.com or view-source:example.com")
|
98 |
+
source_output = gr.Textbox(label="HTML Source Code", lines=20)
|
99 |
+
source_button = gr.Button("View Source")
|
100 |
+
source_button.click(view_source_info, inputs=source_input, outputs=source_output)
|
101 |
+
|
102 |
+
with gr.Tab("Document Info"):
|
103 |
+
doc_input = gr.File(label="Upload Document")
|
104 |
+
doc_output = gr.Textbox(label="Document Info")
|
105 |
+
doc_button = gr.Button("Extract Document Info")
|
106 |
+
doc_button.click(document_info, inputs=doc_input, outputs=doc_output)
|
107 |
+
|
108 |
+
with gr.Tab("Video Info"):
|
109 |
+
video_input = gr.Video(label="Upload Video")
|
110 |
+
video_output = gr.Video(label="Video Preview")
|
111 |
+
metadata_output = gr.Textbox(label="Video Metadata", lines=10)
|
112 |
+
video_button = gr.Button("Extract Video Info")
|
113 |
+
video_button.click(video_info, inputs=video_input, outputs=[video_output, metadata_output])
|
114 |
+
|
115 |
+
with gr.Tab("Model Info"):
|
116 |
+
model_input = gr.Textbox(label="Enter Model ID", placeholder="e.g., bert-base-uncased")
|
117 |
+
model_output = gr.Markdown(label="Model Info")
|
118 |
+
model_button = gr.Button("Fetch Model Info")
|
119 |
+
model_button.click(model_info, inputs=model_input, outputs=model_output)
|
120 |
+
|
121 |
+
with gr.Tab("Space Info"):
|
122 |
+
space_input = gr.Textbox(label="Enter Space ID", placeholder="e.g., gradio/hello-world")
|
123 |
+
space_output = gr.Markdown(label="Space Info")
|
124 |
+
space_button = gr.Button("Fetch Space Info")
|
125 |
+
space_button.click(space_info, inputs=space_input, outputs=space_output)
|
126 |
+
|
127 |
+
# Launch the Gradio app
|
128 |
+
demo.launch()
|