File size: 4,283 Bytes
3144fec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25e4261
 
3144fec
 
25e4261
3144fec
 
 
 
 
 
 
35ddcea
3144fec
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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()