Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,7 +3,6 @@ import requests
|
|
3 |
from urllib.parse import urlparse, urljoin
|
4 |
from bs4 import BeautifulSoup
|
5 |
import asyncio
|
6 |
-
import subprocess
|
7 |
|
8 |
# HTML and JavaScript for the "Copy Code" button
|
9 |
copy_button_html = """
|
@@ -69,6 +68,10 @@ async def extract_additional_resources(url):
|
|
69 |
return [], [], [], [], []
|
70 |
|
71 |
async def convert_to_text(url):
|
|
|
|
|
|
|
|
|
72 |
if not is_valid_url(url):
|
73 |
return "Error: Please enter a valid URL.", "", None, [], [], [], [], [] # Return error message and empty data
|
74 |
|
@@ -174,39 +177,6 @@ async def fetch_space_file_content(space_url, file_path):
|
|
174 |
except Exception as e:
|
175 |
return f"Error: {e}"
|
176 |
|
177 |
-
# Function to check code for errors and provide detailed feedback
|
178 |
-
def check_code_for_errors(code):
|
179 |
-
"""Checks the code for errors using flake8 and provides detailed feedback."""
|
180 |
-
try:
|
181 |
-
# Run flake8 to check for errors
|
182 |
-
result = subprocess.run(
|
183 |
-
["flake8", "--stdin-display-name", "app.py", "-"],
|
184 |
-
input=code.encode(),
|
185 |
-
capture_output=True,
|
186 |
-
text=True,
|
187 |
-
)
|
188 |
-
|
189 |
-
if result.returncode == 0:
|
190 |
-
return "No errors found in the code.", code
|
191 |
-
|
192 |
-
# If there are errors, parse the output and provide detailed feedback
|
193 |
-
errors = result.stderr if result.stderr else result.stdout
|
194 |
-
error_messages = []
|
195 |
-
for line in errors.splitlines():
|
196 |
-
if ":" in line:
|
197 |
-
parts = line.split(":")
|
198 |
-
if len(parts) >= 4:
|
199 |
-
file_name, line_number, column, message = parts[0], parts[1], parts[2], ":".join(parts[3:])
|
200 |
-
error_messages.append(f"Line {line_number}, Column {column}: {message.strip()}")
|
201 |
-
|
202 |
-
if not error_messages:
|
203 |
-
return "Errors found, but could not parse details.", code
|
204 |
-
|
205 |
-
detailed_feedback = "Errors found in the code:\n" + "\n".join(error_messages)
|
206 |
-
return detailed_feedback, code
|
207 |
-
except Exception as e:
|
208 |
-
return f"Error: {e}", code
|
209 |
-
|
210 |
# Create the Gradio interface
|
211 |
with gr.Blocks() as demo:
|
212 |
gr.HTML(copy_button_html) # Add the "Copy Code" script
|
@@ -218,7 +188,7 @@ with gr.Blocks() as demo:
|
|
218 |
gr.Markdown("Enter a URL to fetch its text content and download it as a .txt file.")
|
219 |
|
220 |
with gr.Row():
|
221 |
-
url_input = gr.Textbox(label="Enter URL", placeholder="https://example.com")
|
222 |
|
223 |
with gr.Row():
|
224 |
results_output = gr.Textbox(label="Request Results", interactive=False)
|
@@ -256,17 +226,6 @@ with gr.Blocks() as demo:
|
|
256 |
gr.Markdown("### JS Content")
|
257 |
js_content_output = gr.Textbox(label="JS Content", interactive=True)
|
258 |
|
259 |
-
# Add image previews
|
260 |
-
gr.Markdown("### Image Previews")
|
261 |
-
image_previews = gr.Gallery(label="Image Previews")
|
262 |
-
|
263 |
-
# Update the image previews
|
264 |
-
submit_button.click(
|
265 |
-
fn=lambda img_links: [requests.get(link).content for link in img_links],
|
266 |
-
inputs=img_output,
|
267 |
-
outputs=image_previews
|
268 |
-
)
|
269 |
-
|
270 |
# Tab 2: Model to Text Converter
|
271 |
with gr.Tab("Model to Text Converter"):
|
272 |
gr.Markdown("## Model to Text Converter")
|
@@ -313,20 +272,6 @@ with gr.Blocks() as demo:
|
|
313 |
with gr.Row():
|
314 |
gr.HTML("<button onclick='copyCode(\"space-content-output\")'>Copy Code</button>") # Add the "Copy Code" button
|
315 |
|
316 |
-
# Кнопка Correcter
|
317 |
-
correcter_button = gr.Button("Correcter")
|
318 |
-
|
319 |
-
# Обработчик для кнопки Correcter
|
320 |
-
def correct_code_handler(code):
|
321 |
-
result, corrected_code = check_code_for_errors(code)
|
322 |
-
return result, corrected_code
|
323 |
-
|
324 |
-
correcter_button.click(
|
325 |
-
fn=correct_code_handler,
|
326 |
-
inputs=space_content_output,
|
327 |
-
outputs=[gr.Textbox(label="Correction Result"), space_content_output]
|
328 |
-
)
|
329 |
-
|
330 |
submit_space_button = gr.Button("Fetch File Content")
|
331 |
submit_space_button.click(
|
332 |
fn=fetch_space_file_content,
|
|
|
3 |
from urllib.parse import urlparse, urljoin
|
4 |
from bs4 import BeautifulSoup
|
5 |
import asyncio
|
|
|
6 |
|
7 |
# HTML and JavaScript for the "Copy Code" button
|
8 |
copy_button_html = """
|
|
|
68 |
return [], [], [], [], []
|
69 |
|
70 |
async def convert_to_text(url):
|
71 |
+
# Handle view-source: URLs
|
72 |
+
if url.startswith("view-source:"):
|
73 |
+
url = url[len("view-source:"):]
|
74 |
+
|
75 |
if not is_valid_url(url):
|
76 |
return "Error: Please enter a valid URL.", "", None, [], [], [], [], [] # Return error message and empty data
|
77 |
|
|
|
177 |
except Exception as e:
|
178 |
return f"Error: {e}"
|
179 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
180 |
# Create the Gradio interface
|
181 |
with gr.Blocks() as demo:
|
182 |
gr.HTML(copy_button_html) # Add the "Copy Code" script
|
|
|
188 |
gr.Markdown("Enter a URL to fetch its text content and download it as a .txt file.")
|
189 |
|
190 |
with gr.Row():
|
191 |
+
url_input = gr.Textbox(label="Enter URL", placeholder="https://example.com or view-source:https://example.com")
|
192 |
|
193 |
with gr.Row():
|
194 |
results_output = gr.Textbox(label="Request Results", interactive=False)
|
|
|
226 |
gr.Markdown("### JS Content")
|
227 |
js_content_output = gr.Textbox(label="JS Content", interactive=True)
|
228 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
229 |
# Tab 2: Model to Text Converter
|
230 |
with gr.Tab("Model to Text Converter"):
|
231 |
gr.Markdown("## Model to Text Converter")
|
|
|
272 |
with gr.Row():
|
273 |
gr.HTML("<button onclick='copyCode(\"space-content-output\")'>Copy Code</button>") # Add the "Copy Code" button
|
274 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
275 |
submit_space_button = gr.Button("Fetch File Content")
|
276 |
submit_space_button.click(
|
277 |
fn=fetch_space_file_content,
|