Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import json
|
| 4 |
+
import base64
|
| 5 |
+
import re
|
| 6 |
+
|
| 7 |
+
# GitHub Configuration
|
| 8 |
+
GITHUB_TOKEN = "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" # Replace with your token
|
| 9 |
+
REPO_NAME = "arssite/CBTLLM" # Replace with your repo name
|
| 10 |
+
FILE_PATH = "url.json" # Path to the file in your repo
|
| 11 |
+
BRANCH = "main"
|
| 12 |
+
|
| 13 |
+
# Regex for URL Validation
|
| 14 |
+
URL_REGEX = re.compile(
|
| 15 |
+
r'^(https?://)?' # Protocol (optional)
|
| 16 |
+
r'([a-zA-Z0-9.-]+)' # Domain
|
| 17 |
+
r'(\.[a-zA-Z]{2,})' # Top-level domain
|
| 18 |
+
r'(:[0-9]{1,5})?' # Port (optional)
|
| 19 |
+
r'(/.*)?$', # Path (optional)
|
| 20 |
+
re.IGNORECASE
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
# Function to validate URLs
|
| 24 |
+
def validate_urls(urls):
|
| 25 |
+
valid_urls = []
|
| 26 |
+
for url in urls:
|
| 27 |
+
url = url.strip() # Remove extra spaces
|
| 28 |
+
if re.match(URL_REGEX, url):
|
| 29 |
+
valid_urls.append(url if url.startswith("http") else "http://" + url) # Ensure URL has a protocol
|
| 30 |
+
return valid_urls
|
| 31 |
+
|
| 32 |
+
# Fetch the file content from GitHub
|
| 33 |
+
def fetch_file_content():
|
| 34 |
+
url = f"https://api.github.com/repos/{REPO_NAME}/contents/{FILE_PATH}"
|
| 35 |
+
headers = {"Authorization": f"token {GITHUB_TOKEN}"}
|
| 36 |
+
response = requests.get(url, headers=headers)
|
| 37 |
+
if response.status_code == 200:
|
| 38 |
+
content = response.json()
|
| 39 |
+
file_content = base64.b64decode(content['content']).decode('utf-8')
|
| 40 |
+
sha = content['sha']
|
| 41 |
+
return json.loads(file_content), sha
|
| 42 |
+
else:
|
| 43 |
+
raise Exception(f"Error fetching file: {response.status_code} - {response.text}")
|
| 44 |
+
|
| 45 |
+
# Update the file on GitHub
|
| 46 |
+
def update_file_on_github(new_urls):
|
| 47 |
+
# Fetch current content and SHA
|
| 48 |
+
current_content, sha = fetch_file_content()
|
| 49 |
+
|
| 50 |
+
# Parse, validate, and add unique URLs
|
| 51 |
+
input_urls = validate_urls(new_urls.split("\n")) # Validate input URLs
|
| 52 |
+
if not input_urls:
|
| 53 |
+
raise ValueError("No valid URLs provided. Please enter valid URLs.")
|
| 54 |
+
|
| 55 |
+
updated_urls = list(set(current_content) | set(input_urls)) # Merge current and input URLs, ensuring uniqueness
|
| 56 |
+
|
| 57 |
+
# Prepare updated content
|
| 58 |
+
updated_content = json.dumps(updated_urls, indent=2)
|
| 59 |
+
encoded_content = base64.b64encode(updated_content.encode('utf-8')).decode('utf-8')
|
| 60 |
+
|
| 61 |
+
# Push updated content
|
| 62 |
+
url = f"https://api.github.com/repos/{REPO_NAME}/contents/{FILE_PATH}"
|
| 63 |
+
headers = {"Authorization": f"token {GITHUB_TOKEN}"}
|
| 64 |
+
data = {
|
| 65 |
+
"message": "Update URLs",
|
| 66 |
+
"content": encoded_content,
|
| 67 |
+
"sha": sha,
|
| 68 |
+
"branch": BRANCH
|
| 69 |
+
}
|
| 70 |
+
response = requests.put(url, headers=headers, json=data)
|
| 71 |
+
if response.status_code == 200:
|
| 72 |
+
return len(input_urls), len(updated_urls)
|
| 73 |
+
else:
|
| 74 |
+
raise Exception(f"Error updating file: {response.status_code} - {response.text}")
|
| 75 |
+
|
| 76 |
+
# Gradio Interface
|
| 77 |
+
def submit_urls(new_urls):
|
| 78 |
+
try:
|
| 79 |
+
if not new_urls.strip():
|
| 80 |
+
return "No URLs provided. Please enter at least one URL."
|
| 81 |
+
|
| 82 |
+
user_count, total_count = update_file_on_github(new_urls)
|
| 83 |
+
total_count+=108
|
| 84 |
+
return (
|
| 85 |
+
f"Thanks for your contribution!\n"
|
| 86 |
+
f"You added {user_count} unique URLs.\n"
|
| 87 |
+
f"The file now contains {total_count} unique URLs."
|
| 88 |
+
)
|
| 89 |
+
except ValueError as e:
|
| 90 |
+
return str(e)
|
| 91 |
+
except Exception as e:
|
| 92 |
+
return f"An error occurred: {str(e)}"
|
| 93 |
+
|
| 94 |
+
def resubmit():
|
| 95 |
+
return gr.update(value="") # Clears the input area for a new submission
|
| 96 |
+
|
| 97 |
+
# Gradio UI
|
| 98 |
+
with gr.Blocks() as app:
|
| 99 |
+
with gr.Row():
|
| 100 |
+
gr.Markdown("## URL Submission Program")
|
| 101 |
+
with gr.Row():
|
| 102 |
+
input_area = gr.Textbox(
|
| 103 |
+
label="Enter URLs (one per line):",
|
| 104 |
+
placeholder="https://example1.com\nhttps://example2.com\n...",
|
| 105 |
+
lines=5
|
| 106 |
+
)
|
| 107 |
+
with gr.Row():
|
| 108 |
+
submit_button = gr.Button("Submit")
|
| 109 |
+
with gr.Row():
|
| 110 |
+
output_area = gr.Textbox(
|
| 111 |
+
label="Output",
|
| 112 |
+
interactive=False
|
| 113 |
+
)
|
| 114 |
+
with gr.Row():
|
| 115 |
+
resubmit_button = gr.Button("Resubmit")
|
| 116 |
+
|
| 117 |
+
# Define functionality
|
| 118 |
+
submit_button.click(submit_urls, inputs=input_area, outputs=output_area)
|
| 119 |
+
resubmit_button.click(resubmit, inputs=None, outputs=input_area)
|
| 120 |
+
|
| 121 |
+
# Launch the app
|
| 122 |
+
app.launch()
|