Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,121 +3,128 @@ import requests
|
|
3 |
from bs4 import BeautifulSoup
|
4 |
import json
|
5 |
import time
|
6 |
-
from tqdm import tqdm
|
7 |
import zipfile
|
8 |
-
import io
|
9 |
import os
|
10 |
import tempfile
|
11 |
import mimetypes
|
|
|
12 |
|
13 |
def fetch_content(url):
|
14 |
-
|
15 |
-
|
16 |
-
response.
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
21 |
|
22 |
def extract_text(html):
|
23 |
-
|
24 |
-
|
25 |
-
script
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
return
|
31 |
|
32 |
def process_urls(urls):
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
"
|
41 |
-
"
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
45 |
|
46 |
def process_file(file):
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
"
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
"
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
"
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
"
|
86 |
-
|
87 |
-
|
|
|
|
|
88 |
|
89 |
def process_text(text):
|
90 |
-
|
91 |
-
|
92 |
-
"
|
93 |
-
|
|
|
94 |
|
95 |
def create_dataset(urls, file, text_input):
|
96 |
-
dataset
|
97 |
-
|
98 |
-
|
99 |
-
if
|
100 |
-
|
101 |
-
|
102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
|
104 |
-
|
105 |
-
with open('combined_dataset.json', 'w') as f:
|
106 |
-
json.dump(dataset, f, indent=2)
|
107 |
|
108 |
-
|
109 |
-
Gradio Interface
|
110 |
iface = gr.Interface(
|
111 |
-
fn=create_dataset,
|
112 |
-
inputs=[
|
113 |
-
gr.Textbox(lines=5, label="Enter comma-separated URLs"),
|
114 |
-
gr.File(label="Upload file (including zip files)"),
|
115 |
-
gr.Textbox(lines=10, label="Enter or paste large text")
|
116 |
-
],
|
117 |
-
outputs=gr.File(label="Download Combined Dataset"),
|
118 |
-
title="URL, File, and Text to Dataset Converter",
|
119 |
-
description="Enter URLs, upload files (including zip files), and/or paste text to create a combined dataset for AI training.",
|
120 |
)
|
121 |
|
122 |
-
Launch the interface
|
123 |
iface.launch()
|
|
|
3 |
from bs4 import BeautifulSoup
|
4 |
import json
|
5 |
import time
|
|
|
6 |
import zipfile
|
|
|
7 |
import os
|
8 |
import tempfile
|
9 |
import mimetypes
|
10 |
+
from tqdm import tqdm
|
11 |
|
12 |
def fetch_content(url):
|
13 |
+
"""Fetch content from a given URL."""
|
14 |
+
try:
|
15 |
+
response = requests.get(url, timeout=10)
|
16 |
+
response.raise_for_status()
|
17 |
+
return response.text
|
18 |
+
except requests.RequestException as e:
|
19 |
+
print(f"Error fetching {url}: {e}")
|
20 |
+
return None
|
21 |
|
22 |
def extract_text(html):
|
23 |
+
"""Extract text from HTML content."""
|
24 |
+
soup = BeautifulSoup(html, 'html.parser')
|
25 |
+
for script in soup(["script", "style"]):
|
26 |
+
script.decompose()
|
27 |
+
text = soup.get_text()
|
28 |
+
lines = (line.strip() for line in text.splitlines())
|
29 |
+
chunks = (phrase.strip() for line in lines for phrase in line.split(" "))
|
30 |
+
return '\n'.join(chunk for chunk in chunks if chunk)
|
31 |
|
32 |
def process_urls(urls):
|
33 |
+
"""Process a list of URLs and return their extracted text."""
|
34 |
+
dataset = []
|
35 |
+
for url in tqdm(urls, desc="Fetching URLs"):
|
36 |
+
html = fetch_content(url)
|
37 |
+
if html:
|
38 |
+
text = extract_text(html)
|
39 |
+
dataset.append({
|
40 |
+
"source": "url",
|
41 |
+
"url": url,
|
42 |
+
"content": text
|
43 |
+
})
|
44 |
+
time.sleep(1) # Be polite to the server
|
45 |
+
return dataset
|
46 |
|
47 |
def process_file(file):
|
48 |
+
"""Process uploaded files (including zip files) and extract text."""
|
49 |
+
dataset = []
|
50 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
51 |
+
if zipfile.is_zipfile(file.name):
|
52 |
+
with zipfile.ZipFile(file.name, 'r') as zip_ref:
|
53 |
+
zip_ref.extractall(temp_dir)
|
54 |
+
# Process each extracted file
|
55 |
+
for root, _, files in os.walk(temp_dir):
|
56 |
+
for filename in files:
|
57 |
+
filepath = os.path.join(root, filename)
|
58 |
+
mime_type, _ = mimetypes.guess_type(filepath)
|
59 |
+
if mime_type and mime_type.startswith('text'):
|
60 |
+
with open(filepath, 'r', errors='ignore') as f:
|
61 |
+
content = f.read()
|
62 |
+
dataset.append({
|
63 |
+
"source": "file",
|
64 |
+
"filename": filename,
|
65 |
+
"content": content
|
66 |
+
})
|
67 |
+
else:
|
68 |
+
# For non-text files, just store the filename
|
69 |
+
dataset.append({
|
70 |
+
"source": "file",
|
71 |
+
"filename": filename,
|
72 |
+
"content": "Binary file - content not extracted"
|
73 |
+
})
|
74 |
+
else:
|
75 |
+
mime_type, _ = mimetypes.guess_type(file.name)
|
76 |
+
if mime_type and mime_type.startswith('text'):
|
77 |
+
content = file.read().decode('utf-8', errors='ignore')
|
78 |
+
dataset.append({
|
79 |
+
"source": "file",
|
80 |
+
"filename": os.path.basename(file.name),
|
81 |
+
"content": content
|
82 |
+
})
|
83 |
+
else:
|
84 |
+
# For non-text files, just store the filename
|
85 |
+
dataset.append({
|
86 |
+
"source": "file",
|
87 |
+
"filename": os.path.basename(file.name),
|
88 |
+
"content": "Binary file - content not extracted"
|
89 |
+
})
|
90 |
+
return dataset
|
91 |
|
92 |
def process_text(text):
|
93 |
+
"""Process raw text input."""
|
94 |
+
return [{
|
95 |
+
"source": "text_input",
|
96 |
+
"content": text
|
97 |
+
}]
|
98 |
|
99 |
def create_dataset(urls, file, text_input):
|
100 |
+
"""Create a combined dataset from URLs, uploaded files, and text input."""
|
101 |
+
dataset = []
|
102 |
+
if urls:
|
103 |
+
dataset.extend(process_urls([url.strip() for url in urls.split(',') if url.strip()]))
|
104 |
+
if file:
|
105 |
+
dataset.extend(process_file(file))
|
106 |
+
if text_input:
|
107 |
+
dataset.extend(process_text(text_input))
|
108 |
+
|
109 |
+
# Save the dataset as JSON
|
110 |
+
output_file = 'combined_dataset.json'
|
111 |
+
with open(output_file, 'w') as f:
|
112 |
+
json.dump(dataset, f, indent=2)
|
113 |
|
114 |
+
return output_file
|
|
|
|
|
115 |
|
116 |
+
# Gradio Interface
|
|
|
117 |
iface = gr.Interface(
|
118 |
+
fn=create_dataset,
|
119 |
+
inputs=[
|
120 |
+
gr.Textbox(lines=5, label="Enter comma-separated URLs"),
|
121 |
+
gr.File(label="Upload file (including zip files)"),
|
122 |
+
gr.Textbox(lines=10, label="Enter or paste large text")
|
123 |
+
],
|
124 |
+
outputs=gr.File(label="Download Combined Dataset"),
|
125 |
+
title="URL, File, and Text to Dataset Converter",
|
126 |
+
description="Enter URLs, upload files (including zip files), and/or paste text to create a combined dataset for AI training.",
|
127 |
)
|
128 |
|
129 |
+
# Launch the interface
|
130 |
iface.launch()
|