AutoReadmeAgent / app.py
bogeumkim's picture
Modify Gradio UI
c0f87f9
raw
history blame
5.74 kB
# import re
# import requests
# import gradio as gr
# from markdownify import markdownify
# from requests.exceptions import RequestException
# from utils.preprocessor import Preprocessor
# # https://modal.com/docs/guide/webhook-urls
# MODAL_ENDPOINT = "https://auto-readme-agent--text-to-speech-gradio-app.modal.run"
# def 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 methods
# section = Preprocessor.extract_section(markdown_content)
# dir_paths, files = Preprocessor.extract_dirs_from_text(section)
# # Format the result
# result = (
# f"paths: {dir_paths}\n\n"
# f"files: {files}"
# )
# return result
# except 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"),
# gr.Textbox(label="Files or folders you want to highlight in the README (comma or newline separated)")
# ],
# outputs=gr.Textbox(label="Extracted Section, Directory Paths, and File Paths", show_copy_button=True),
# title="Webpage Section and Path Extractor",
# description="Enter a website URL. This tool fetches the page, extracts a markdown section, and lists directory paths and files found in that section. You can also specify files or folders you want to highlight in the generated README."
# )
# if __name__ == "__main__":
# demo.launch(debug=True)
import re
import requests
import gradio as gr
from markdownify import markdownify
MODAL_API_URL = "https://agents-mcp-hackathon--auto-readme-agent-fastapi-app.modal.run" # Replace with your deployed Modal endpoint
# def generate_readme_from_github(repo_url):
# try:
# response = requests.post(
# MODAL_API_URL,
# json={"repo_url": repo_url},
# timeout=180,
# )
# if response.status_code == 200:
# return response.json().get("readme", "No README generated.")
# else:
# return f"Error: {response.status_code}\n{response.text}"
# except Exception as e:
# return f"Exception: {str(e)}"
# with gr.Blocks() as demo:
# gr.Markdown("# πŸ“ GitHub Repo β†’ README.md Generator\nPaste a public GitHub repo link to generate a draft README.md using AI.")
# repo_input = gr.Textbox(label="GitHub Repository URL", placeholder="https://github.com/owner/repo")
# output = gr.Textbox(label="Generated README.md", lines=20)
# btn = gr.Button("Generate README")
# btn.click(generate_readme_from_github, inputs=repo_input, outputs=output)
# if __name__ == "__main__":
# demo.launch()
def generate_readme(query):
try:
response = requests.post(
MODAL_API_URL,
json={"query": query},
timeout=120
)
if response.status_code == 200:
return response.json().get("result", "No result returned.")
else:
return f"Error: {response.status_code}\n{response.text}"
except Exception as e:
return f"Exception: {str(e)}"
def fetch_github_content(url: str) -> str:
try:
response = requests.get(url, timeout=20)
response.raise_for_status()
markdown_content = markdownify(response.text)
markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)
return markdown_content
except Exception as e:
return f"Error fetching content: {str(e)}"
with gr.Blocks() as demo:
# 제λͺ© κ°€μš΄λ° μ •λ ¬
gr.HTML("<h1 style='text-align:center;'>πŸ€– ARA: Auto README.md Agent πŸ“</h1>")
repo_input = gr.Textbox(
label="GitHub Repository URL",
placeholder="Enter the GitHub repository URL (e.g. https://github.com/username/repo)"
)
with gr.Row():
with gr.Column():
gr.Markdown("#### πŸ–ΌοΈ Preview (Markdown Rendered)")
# μ™Όμͺ½: μˆ˜μ • κ°€λŠ₯ν•œ 프리뷰 λ°•μŠ€
readme_preview = gr.Textbox(
lines=24,
label="Preview (Markdown Rendered)",
interactive=True
)
with gr.Column():
gr.Markdown("#### πŸ“ Markdown File (Copyable)")
# 였λ₯Έμͺ½: μˆ˜μ • λΆˆκ°€, 볡사 λ²„νŠΌ μžˆλŠ” λ°•μŠ€
readme_markdown = gr.Textbox(
lines=24,
label="Markdown",
interactive=False,
show_copy_button=True
)
generate_btn = gr.Button("Generate README.md")
# λ²„νŠΌ 클릭 μ‹œ 두 λ°•μŠ€ λͺ¨λ‘ 채움
def dual_output(readme):
return readme, readme
generate_btn.click(
generate_readme,
inputs=repo_input,
outputs=[readme_preview, readme_markdown]
)
if __name__ == "__main__":
demo.launch()