binuser007 commited on
Commit
bb7923c
·
verified ·
1 Parent(s): 07c9187

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -46
app.py CHANGED
@@ -1,49 +1,40 @@
1
- import gradio as gr
2
  import subprocess
 
 
3
  import os
4
 
5
- def crawl_website(url):
6
- """Crawls a website using Katana and saves the output to a text file."""
7
-
8
- output_file = "katana_output.txt"
9
- katana_command = [
10
- "katana",
11
- "-u", url,
12
- "-o", output_file,
13
- "-silent" # Only display output, no progress bars or logs
14
- ]
15
-
16
- # Execute Katana command
17
- try:
18
- subprocess.run(katana_command, check=True)
19
- except subprocess.CalledProcessError as e:
20
- return f"Error running Katana: {e}", None
21
-
22
- # Read output file content
23
- with open(output_file, "r") as f:
24
- output_content = f.read()
25
-
26
- return output_content, output_file
27
-
28
- with gr.Blocks() as demo:
29
- gr.Markdown("## Website Crawler using Katana")
30
- with gr.Row():
31
- url_input = gr.Textbox(label="Enter Website URL")
32
- output_text = gr.Textbox(label="Crawled Endpoints", lines=15)
33
- download_button = gr.Button("Download Output File")
34
-
35
- # Trigger crawling on button click
36
- url_input.submit(
37
- fn=crawl_website,
38
- inputs=url_input,
39
- outputs=[output_text, download_button]
40
- )
41
-
42
- # Download output file
43
- download_button.click(
44
- fn=lambda file: file,
45
- inputs=download_button,
46
- outputs=gr.File(label="Download Results")
47
- )
48
-
49
- demo.launch()
 
 
1
  import subprocess
2
+ import gradio as gr
3
+ import tempfile
4
  import os
5
 
6
+ def run_katana(url):
7
+ try:
8
+ # Create a temporary file to store the results
9
+ with tempfile.NamedTemporaryFile(mode='w+', delete=False, suffix='.txt') as temp_file:
10
+ # Run Katana and save output to the temporary file
11
+ subprocess.run(["katana", "-u", url, "-o", temp_file.name], check=True)
12
+
13
+ # Read the contents of the file
14
+ temp_file.seek(0)
15
+ result = temp_file.read()
16
+
17
+ # Return the result and the path to the temporary file
18
+ return result, temp_file.name
19
+ except Exception as e:
20
+ return str(e), None
21
+
22
+ def process_and_display(url):
23
+ result, file_path = run_katana(url)
24
+ if file_path:
25
+ return result, file_path
26
+ else:
27
+ return result, None
28
+
29
+ iface = gr.Interface(
30
+ fn=process_and_display,
31
+ inputs="text",
32
+ outputs=[
33
+ gr.Textbox(label="Crawl Results"),
34
+ gr.File(label="Download Results")
35
+ ],
36
+ title="Katana Crawler",
37
+ description="Enter a URL to crawl using Katana. Results will be displayed and available for download."
38
+ )
39
+
40
+ iface.launch(server_name="0.0.0.0", server_port=7860)