import requests import gradio as gr import json import base64 import re # GitHub Configuration GITHUB_TOKEN = "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" # Replace with your token REPO_NAME = "arssite/CBTLLM" # Replace with your repo name FILE_PATH = "url.json" # Path to the file in your repo BRANCH = "main" # Regex for URL Validation URL_REGEX = re.compile( r'^(https?://)?' # Protocol (optional) r'([a-zA-Z0-9.-]+)' # Domain r'(\.[a-zA-Z]{2,})' # Top-level domain r'(:[0-9]{1,5})?' # Port (optional) r'(/.*)?$', # Path (optional) re.IGNORECASE ) # Function to validate URLs def validate_urls(urls): valid_urls = [] for url in urls: url = url.strip() # Remove extra spaces if re.match(URL_REGEX, url): valid_urls.append(url if url.startswith("http") else "http://" + url) # Ensure URL has a protocol return valid_urls # Fetch the file content from GitHub 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}") # Update the file on GitHub def update_file_on_github(new_urls): # Fetch current content and SHA current_content, sha = fetch_file_content() # Parse, validate, and add unique URLs input_urls = validate_urls(new_urls.split("\n")) # Validate input URLs if not input_urls: raise ValueError("No valid URLs provided. Please enter valid URLs.") updated_urls = list(set(current_content) | set(input_urls)) # Merge current and input URLs, ensuring uniqueness # Prepare updated content updated_content = json.dumps(updated_urls, indent=2) encoded_content = base64.b64encode(updated_content.encode('utf-8')).decode('utf-8') # Push updated content 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}") # Gradio Interface 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="") # Clears the input area for a new submission # Gradio UI 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") # Define functionality submit_button.click(submit_urls, inputs=input_area, outputs=output_area) resubmit_button.click(resubmit, inputs=None, outputs=input_area) # Launch the app app.launch()