|
import requests |
|
import gradio as gr |
|
import json |
|
import base64 |
|
import re |
|
|
|
|
|
GITHUB_TOKEN = "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" |
|
REPO_NAME = "arssite/CBTLLM" |
|
FILE_PATH = "url.json" |
|
BRANCH = "main" |
|
|
|
|
|
URL_REGEX = re.compile( |
|
r'^(https?://)?' |
|
r'([a-zA-Z0-9.-]+)' |
|
r'(\.[a-zA-Z]{2,})' |
|
r'(:[0-9]{1,5})?' |
|
r'(/.*)?$', |
|
re.IGNORECASE |
|
) |
|
|
|
|
|
def validate_urls(urls): |
|
valid_urls = [] |
|
for url in urls: |
|
url = url.strip() |
|
if re.match(URL_REGEX, url): |
|
valid_urls.append(url if url.startswith("http") else "http://" + url) |
|
return valid_urls |
|
|
|
|
|
def fetch_file_content(): |
|
url = f"https://api.github.com/repos/{REPO_NAME}/contents/{FILE_PATH}" |
|
headers = {"Authorization": f"token {GITHUB_TOKEN}"} |
|
response = requests.get(url, headers=headers) |
|
if response.status_code == 200: |
|
content = response.json() |
|
file_content = base64.b64decode(content['content']).decode('utf-8') |
|
sha = content['sha'] |
|
return json.loads(file_content), sha |
|
else: |
|
raise Exception(f"Error fetching file: {response.status_code} - {response.text}") |
|
|
|
|
|
def update_file_on_github(new_urls): |
|
|
|
current_content, sha = fetch_file_content() |
|
|
|
|
|
input_urls = validate_urls(new_urls.split("\n")) |
|
if not input_urls: |
|
raise ValueError("No valid URLs provided. Please enter valid URLs.") |
|
|
|
updated_urls = list(set(current_content) | set(input_urls)) |
|
|
|
|
|
updated_content = json.dumps(updated_urls, indent=2) |
|
encoded_content = base64.b64encode(updated_content.encode('utf-8')).decode('utf-8') |
|
|
|
|
|
url = f"https://api.github.com/repos/{REPO_NAME}/contents/{FILE_PATH}" |
|
headers = {"Authorization": f"token {GITHUB_TOKEN}"} |
|
data = { |
|
"message": "Update URLs", |
|
"content": encoded_content, |
|
"sha": sha, |
|
"branch": BRANCH |
|
} |
|
response = requests.put(url, headers=headers, json=data) |
|
if response.status_code == 200: |
|
return len(input_urls), len(updated_urls) |
|
else: |
|
raise Exception(f"Error updating file: {response.status_code} - {response.text}") |
|
|
|
|
|
def submit_urls(new_urls): |
|
try: |
|
if not new_urls.strip(): |
|
return "No URLs provided. Please enter at least one URL." |
|
|
|
user_count, total_count = update_file_on_github(new_urls) |
|
total_count+=108 |
|
return ( |
|
f"Thanks for your contribution!\n" |
|
f"You added {user_count} unique URLs.\n" |
|
f"The file now contains {total_count} unique URLs." |
|
) |
|
except ValueError as e: |
|
return str(e) |
|
except Exception as e: |
|
return f"An error occurred: {str(e)}" |
|
|
|
def resubmit(): |
|
return gr.update(value="") |
|
|
|
|
|
with gr.Blocks() as app: |
|
with gr.Row(): |
|
gr.Markdown("## URLink Submission Program For CBT LLM ") |
|
gr.Markdown("Requesting To contribute with Link or Url of Any News Article, Blog, Case Study, EBook, Any Source Related to Cognitive Behavioral.") |
|
with gr.Row(): |
|
input_area = gr.Textbox( |
|
label="Enter URLs ( try one per line):", |
|
placeholder="https://example1.com\nhttps://example2.com\n...", |
|
lines=5 |
|
) |
|
with gr.Row(): |
|
submit_button = gr.Button("Submit") |
|
with gr.Row(): |
|
output_area = gr.Textbox( |
|
label="Response ", |
|
interactive=False |
|
) |
|
with gr.Row(): |
|
resubmit_button = gr.Button("Resubmit") |
|
|
|
|
|
submit_button.click(submit_urls, inputs=input_area, outputs=output_area) |
|
resubmit_button.click(resubmit, inputs=None, outputs=input_area) |
|
|
|
|
|
app.launch() |
|
|