Felguk commited on
Commit
4d49ad5
·
verified ·
1 Parent(s): 7f6b64d

Update app.py

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