File size: 3,768 Bytes
17a4116
 
 
 
cc117e5
17a4116
 
 
 
 
 
 
cc117e5
17a4116
 
 
 
 
 
d45d243
 
 
17a4116
 
 
 
 
 
 
 
8a86ac1
17a4116
 
 
 
 
 
 
 
 
 
 
 
 
 
cf672b5
17a4116
 
df43c5c
 
 
 
 
 
 
 
 
 
dc562c3
17a4116
 
 
 
 
3a6b7cb
87f974e
 
17a4116
 
aab8da7
87f974e
 
 
 
 
 
 
 
 
17a4116
 
 
ef51715
 
17a4116
cc117e5
ef51715
dc562c3
 
e425200
 
ef51715
e425200
 
 
 
 
 
e01f2c9
e425200
 
 
 
 
17a4116
 
 
 
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
import gradio as gr
import os
import subprocess
import shutil
import openai

css_style = """
.gradio-container {
    font-family: "IBM Plex Mono";
}
"""

def process_repository(url, model):

    # Split the URL to get the repo name
    repo_name = url.split('/')[-1]
    if repo_name.endswith('.git'):
        repo_name = repo_name[:-4]

    # Change permissions
    subprocess.run(['chmod', 'u+w', '.'])
    
    # Clone the repo
    subprocess.run(['git', 'clone', url], check=True)

    try:
        # Change directory to the cloned repo
        os.chdir(repo_name)

        # Run your package command
        subprocess.run(['gpt4readability', '.', '--function', 'readme','--include-md', 'false', '--model', model])

        # Open the README.md file and return its contents
        with open('README.md', 'r') as readme_file:
            readme_contents = readme_file.read()

        return readme_contents
    finally:
        # Change back to the original directory
        os.chdir('..')

        # Delete the repo directory
        if os.path.exists(repo_name):
            shutil.rmtree(repo_name)

def generate_repo(url, api_key, model):
    if api_key:
        os.environ['OPENAI_API_KEY'] = api_key.strip()
        # if model == 'gpt-4':
        #     try:
        #         response = openai.Completion.create(
        #             model="gpt-4",  # or whatever the exact model ID is
        #             prompt="test",
        #             max_tokens=5
        #         )
        #         print("Access to GPT-4 confirmed!")
        #     except:
        #         return "The API key either does not have access to GPT-4 or is not valid."
        return process_repository(url, model)
    else:
        return "Please add a valid OpenAI API Key (you can get them [here](https://platform.openai.com/account/api-keys))"

with gr.Blocks(css=css_style) as demo:
    gr.Markdown(f"""
        # Hello from GPT4Readability (v0.1.3)

        *Project by Dennis Loevlie ([@DennisLoevlie](https://twitter.com/DennisLoevlie))*  
        [![License Badge](https://img.shields.io/github/license/loevlie/GPT4Readability)](https://github.com/loevlie/GPT4Readability/blob/main/LICENSE)

        Welcome to GPT4Readability, a tool designed to help you generate README.md files and suggest improvements for your code repositories.

        - You can find the source code at [GPT4Readability](https://github.com/loevlie/GPT4Readability).
        - It's making use of the [langchain](https://github.com/hwchase17/langchain) library.

        ## Here's how to get started:
        1. Please enter your API Key ([Need more information?](https://platform.openai.com/account/api-keys))
        2. Provide the GitHub Repository URL that you'd like to analyze
        3. Select a model (Please note, the gpt-4 API isn't available to all as of July 2023)
        4. Click to generate a README or suggestions markdown file
        """)

    openai_api_key = gr.Textbox(
        label="OpenAI API Key", placeholder="sk-...", type="password"
    )
    url = gr.Textbox(label="GitHub Repository URL")
    model = gr.Dropdown(["gpt-3.5-turbo", "gpt-4"], type="value", label='Model Type')

    btn = gr.Button("Generate README.md")

    output_md = gr.Markdown()
    output_text = gr.Text()

    def update_output(url, api_key, model):
        content = generate_repo(url, api_key, model)
        output_md.update(value=content)
        output_text.update(value=content)

    btn.click(fn=update_output, inputs=[url, openai_api_key, model], outputs=[])

    with gr.Tabs() as tabs:
        with gr.TabItem("Rendered Markdown"):
            output_md
        with gr.TabItem("Plain Markdown"):
            output_text
    

demo.queue(concurrency_count=20)
demo.launch(share=False)