File size: 3,965 Bytes
77f290b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b048432
77f290b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18fe2e4
 
 
 
77f290b
 
 
 
 
 
 
 
b048432
77f290b
 
 
 
 
 
 
 
 
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
125
126
127
128
129
130
import time
import requests
import time
import os
import json
import streamlit as st


def model_predict(client, prompt):
    for message in client.chat_completion(
        messages=[{"role": "system", "content": "You are a chatbot evaluating github repositories, their python codes and corresponding readme files. Strictly answer the questions with Yes or No."}, {"role": "user", "content": prompt}],
        max_tokens=500,
        stream=True,
        ):
        return message.choices[0].delta.content

    return ""


def get_api_link(url):
    username, repo_name = decompose_url(url)
    if (username == None):
        return ""
    return f"https://api.github.com/repos/{username}/{repo_name}/zipball/"

def decompose_url(url):
    try:
        url = url.split("github.com")[1]
        url = url.strip(".")
        url = url.split(".git")[0]
        url = url.strip("/")
        parts = url.split("/")
        username = parts[0]
        repo_name = parts[1]
        return username, repo_name
    except:
        return None, None


def fetch_repo_stars(verbose, repo_url, token):
    headers = {"Authorization": f"token {token}"}
    api_url = get_api_link(repo_url)
    api_url = api_url.replace("/zipball/", "")

    # Sending GET request to GitHub API
    response = requests.get(api_url, headers=headers)

    if response.status_code == 200:
        return json.loads(response.content)["stargazers_count"]
    if (response.status_code == 404):
        log(verbose, "ERROR", "Repository private.")

def fetch_repo(verbose, repo_url, repo_name, token):
    if (os.path.exists(repo_name)):
        os.remove(repo_name)


    if ("github.com" not in repo_url):
        log(verbose, "ERROR", f"URL not for github repo, please evaluate manually ({repo_url}).")
        return

    headers = {"Authorization": f"token {token}"}
    api_url = get_api_link(repo_url)

    if (api_url == ""):
        log(verbose, "ERROR", f"Failed to parse the URL, please evaluate manually ({repo_url}).")
        return

    # Sending GET request to GitHub API
    response = requests.get(api_url, headers=headers)

    if response.status_code == 200:
        with open(repo_name, 'wb') as file:
            file.write(response.content)

        log(verbose, "LOG", "Repository downloaded successfully")
    if (response.status_code == 404):
        log(verbose, "ERROR", "Repository private.")

def fetch_readme(zip):
    readme_files = [readme for readme in zip.namelist() if ((readme.endswith("README.MD") | readme.endswith("README.md") | readme.endswith("readme.md")) & (len(readme.split("/")) == 2))]
    readme = ""
    for readme_file in readme_files:
        readme += zip.open(readme_file).read().decode("utf-8") + "\n\n"
    return readme

def fetch_license(zip):
    license_files = [license for license in zip.namelist() if (("LICENSE" in license) & (len(license.split("/")) == 2))]
    license = None
    if (len(license_files) > 0):
        license = zip.open(license_files[0]).read().decode("utf-8")
    return license

def fetch_openalex(verbose, paper_name, year):
    api_url = f"https://api.openalex.org/works?filter=default.search:{paper_name},publication_year:{year}"

    response = requests.get(api_url)

    if response.status_code == 200:
        return response.json()
    else:
        log(verbose, "WARNING", "Could not find OpenAlex information for paper.")


def log(verbose, log_type, log_text, hf=False):
    if (verbose == 0):
        return

    # Align line-break
    if (log_text.startswith("\n")):
        print("\n")
        log_text = log_text.lstrip('\n')

    if (log_type == "LOG"):
        log_text = f"LOG: {log_text}"
    if (log_type == "ERROR"):
        log_text = f"ERROR: {log_text}"
    if (log_type == "WARNING"):
        log_text = f"WARNING: {log_text}"
        

    if (verbose == 1):
        print(log_text)
        return

    if (verbose == 2):
        st.write(log_text)
        return

    raise Exception(log_text)