Spaces:
Runtime error
Runtime error
Add preprocessor.py
Browse filesFiles changed (6)
- app.py+41-5
- modal_app.py+12
- pyproject.toml+3
- utils/ignore.txtAdded
- utils/preprocessor.pyAdded+48
- uv.lock
123456789101112import gradio as grdef greet(name, intensity):return "Hello, " + name + "!" * int(intensity)demo = gr.Interface(fn=greet,inputs=["text", "slider"],outputs=["text"],)demo.launch()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748import reimport requestsfrom markdownify import markdownifyfrom requests.exceptions import RequestExceptionimport gradio as gr# Import the Preprocessor classfrom utils.preprocessor import Preprocessordef visit_webpage(url, max_output_length=40000):"""Fetch the webpage, convert to markdown, and use Preprocessor methods."""try:response = requests.get(url, timeout=20)response.raise_for_status()markdown_content = markdownify(response.text).strip()markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)if len(markdown_content) > max_output_length:markdown_content = (markdown_content[: max_output_length // 2]+ f"\n..._This content has been truncated to stay below {max_output_length} characters_...\n"+ markdown_content[-max_output_length // 2 :])# Use Preprocessor class methodssection = Preprocessor.extract_section(markdown_content)dir_paths, files = Preprocessor.extract_dirs_from_text(section)# Format the resultresult = (f"paths: {dir_paths}\n\n"f"files: {files}")return resultexcept requests.exceptions.Timeout:return "The request timed out. Please try again later or check the URL."except RequestException as e:return f"Error fetching the webpage: {str(e)}"except Exception as e:return f"An unexpected error occurred: {str(e)}"demo = gr.Interface(fn=visit_webpage,inputs=gr.Textbox(label="Website URL"),outputs=gr.Textbox(label="Extracted Section, Directory Paths, and File Paths"),title="Webpage Section and Path Extractor")demo.launch()
123456789101112import modalapp = modal.App("auto-readme-agent")@app.function()def square(x):print("This code is running on a remote worker!")return x**2@app.local_entrypoint()def main():print("the square is", square.remote(42))
5 unmodified lines67895 unmodified linesrequires-python = ">=3.11"dependencies = ["gradio>=5.33.0",]5 unmodified lines67891011125 unmodified linesrequires-python = ">=3.11"dependencies = ["gradio>=5.33.0","markdownify>=1.1.0","modal>=1.0.3","smolagents>=1.17.0",]
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748import re
class Preprocessor: """ Provides methods to preprocess markdown content: - extract a section after a specific keyword - extract directories from markdown links - extract file paths from markdown links """
@staticmethod def extract_section(markdown_content, keyword="Latest commit"): """ Extract lines starting from the line after the one containing 'keyword' up to the next empty line. """ lines = markdown_content.splitlines() extract = [] found = False for i, line in enumerate(lines): if not found and keyword in line: found = True # Start collecting from the next line start_idx = i + 1 while start_idx < len(lines): next_line = lines[start_idx] if next_line.strip() == "": break extract.append(next_line) start_idx += 1 break return "\n".join(extract) if extract else f"No content found after '{keyword}'."
@staticmethod def extract_dirs_from_text(text): """ Extract unique directory paths from markdown links in text. Only the path part before the filename is kept. """ pattern = r'\((/[^ )]+)\s+"([^"]+)"\)' matches = re.findall(pattern, text) dirs = set() files = set() for match in matches: dirs.add(match[0]) if not match[1].startswith("."): files.add(match[1]) return dirs, filesapp.py+41-5
modal_app.py+12
pyproject.toml+3
utils/ignore.txtAdded
utils/preprocessor.pyAdded+48
uv.lock