Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,7 @@ from urllib.parse import urlparse, urljoin
|
|
4 |
from bs4 import BeautifulSoup
|
5 |
import asyncio
|
6 |
|
|
|
7 |
def is_valid_url(url):
|
8 |
"""Checks if the string is a valid URL."""
|
9 |
try:
|
@@ -21,6 +22,7 @@ async def fetch_file_content(url):
|
|
21 |
except:
|
22 |
return "Failed to fetch content."
|
23 |
|
|
|
24 |
async def extract_additional_resources(url):
|
25 |
"""Extracts links to CSS, JS, and images from HTML code."""
|
26 |
try:
|
@@ -80,6 +82,30 @@ async def convert_to_text(url):
|
|
80 |
except requests.exceptions.RequestException as e:
|
81 |
return f"Error: {e}", "", None, [], [], [], [], [] # Return error message and empty data
|
82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
# HTML and JavaScript for the "Copy Code" button
|
84 |
copy_button_html = """
|
85 |
<script>
|
@@ -100,47 +126,73 @@ css = "app.css"
|
|
100 |
|
101 |
# Create the Gradio interface
|
102 |
with gr.Blocks(css=css) as demo:
|
103 |
-
gr.
|
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 |
# Launch the interface
|
146 |
demo.launch()
|
|
|
4 |
from bs4 import BeautifulSoup
|
5 |
import asyncio
|
6 |
|
7 |
+
# Common functions
|
8 |
def is_valid_url(url):
|
9 |
"""Checks if the string is a valid URL."""
|
10 |
try:
|
|
|
22 |
except:
|
23 |
return "Failed to fetch content."
|
24 |
|
25 |
+
# URL to Text Converter
|
26 |
async def extract_additional_resources(url):
|
27 |
"""Extracts links to CSS, JS, and images from HTML code."""
|
28 |
try:
|
|
|
82 |
except requests.exceptions.RequestException as e:
|
83 |
return f"Error: {e}", "", None, [], [], [], [], [] # Return error message and empty data
|
84 |
|
85 |
+
# Model to Text Converter
|
86 |
+
async def fetch_model_content(model_url, file_path=None):
|
87 |
+
"""Fetches content from a model on Hugging Face or GitHub."""
|
88 |
+
try:
|
89 |
+
if model_url:
|
90 |
+
response = await asyncio.to_thread(requests.get, model_url, timeout=5)
|
91 |
+
response.raise_for_status()
|
92 |
+
model_content = response.text
|
93 |
+
else:
|
94 |
+
model_content = "No model URL provided."
|
95 |
+
|
96 |
+
if file_path:
|
97 |
+
try:
|
98 |
+
with open(file_path, "r", encoding="utf-8") as file:
|
99 |
+
file_content = file.read()
|
100 |
+
except Exception as e:
|
101 |
+
file_content = f"Failed to read file: {e}"
|
102 |
+
else:
|
103 |
+
file_content = "No file provided."
|
104 |
+
|
105 |
+
return model_content, file_content
|
106 |
+
except Exception as e:
|
107 |
+
return f"Error: {e}", ""
|
108 |
+
|
109 |
# HTML and JavaScript for the "Copy Code" button
|
110 |
copy_button_html = """
|
111 |
<script>
|
|
|
126 |
|
127 |
# Create the Gradio interface
|
128 |
with gr.Blocks(css=css) as demo:
|
129 |
+
with gr.Tabs():
|
130 |
+
# Tab 1: URL to Text Converter
|
131 |
+
with gr.Tab("URL to Text Converter"):
|
132 |
+
gr.Markdown("## URL to Text Converter")
|
133 |
+
gr.Markdown("Enter a URL to fetch its text content and download it as a .txt file.")
|
134 |
+
|
135 |
+
with gr.Row():
|
136 |
+
url_input = gr.Textbox(label="Enter URL", placeholder="https://example.com")
|
137 |
+
|
138 |
+
with gr.Row():
|
139 |
+
results_output = gr.Textbox(label="Request Results", interactive=False)
|
140 |
+
text_output = gr.Textbox(label="Text Content", interactive=True, elem_id="output-text")
|
141 |
+
|
142 |
+
with gr.Row():
|
143 |
+
gr.HTML(copy_button_html) # Add the "Copy Code" button
|
144 |
+
file_output = gr.File(label="Download File", visible=False) # Hidden file download component
|
145 |
+
|
146 |
+
submit_button = gr.Button("Fetch Content")
|
147 |
+
submit_button.click(
|
148 |
+
fn=convert_to_text,
|
149 |
+
inputs=url_input,
|
150 |
+
outputs=[
|
151 |
+
results_output, text_output, file_output,
|
152 |
+
gr.Textbox(label="CSS Files"), gr.Textbox(label="JS Files"), gr.Textbox(label="Images"),
|
153 |
+
gr.Textbox(label="CSS Content"), gr.Textbox(label="JS Content")
|
154 |
+
]
|
155 |
+
)
|
156 |
+
|
157 |
+
# Add an Accordion to show/hide additional resources
|
158 |
+
with gr.Accordion("Show/Hide Additional Resources", open=False):
|
159 |
+
gr.Markdown("### CSS Files")
|
160 |
+
css_output = gr.Textbox(label="CSS Files", interactive=False)
|
161 |
+
|
162 |
+
gr.Markdown("### JS Files")
|
163 |
+
js_output = gr.Textbox(label="JS Files", interactive=False)
|
164 |
+
|
165 |
+
gr.Markdown("### Images")
|
166 |
+
img_output = gr.Textbox(label="Images", interactive=False)
|
167 |
+
|
168 |
+
gr.Markdown("### CSS Content")
|
169 |
+
css_content_output = gr.Textbox(label="CSS Content", interactive=True)
|
170 |
+
|
171 |
+
gr.Markdown("### JS Content")
|
172 |
+
js_content_output = gr.Textbox(label="JS Content", interactive=True)
|
173 |
+
|
174 |
+
# Tab 2: Model to Text Converter
|
175 |
+
with gr.Tab("Model to Text Converter"):
|
176 |
+
gr.Markdown("## Model to Text Converter")
|
177 |
+
gr.Markdown("Enter a link to a model on Hugging Face or GitHub, and optionally upload a file.")
|
178 |
+
|
179 |
+
with gr.Row():
|
180 |
+
model_url_input = gr.Textbox(label="Model URL", placeholder="https://huggingface.co/... or https://github.com/...")
|
181 |
+
file_input = gr.File(label="Upload File (Optional)")
|
182 |
+
|
183 |
+
with gr.Row():
|
184 |
+
model_content_output = gr.Textbox(label="Model Content", interactive=True)
|
185 |
+
file_content_output = gr.Textbox(label="File Content", interactive=True)
|
186 |
+
|
187 |
+
with gr.Row():
|
188 |
+
gr.HTML(copy_button_html) # Add the "Copy Code" button
|
189 |
+
|
190 |
+
submit_model_button = gr.Button("Fetch Model Content")
|
191 |
+
submit_model_button.click(
|
192 |
+
fn=fetch_model_content,
|
193 |
+
inputs=[model_url_input, file_input],
|
194 |
+
outputs=[model_content_output, file_content_output]
|
195 |
+
)
|
196 |
|
197 |
# Launch the interface
|
198 |
demo.launch()
|