File size: 4,686 Bytes
56620ea
 
 
 
 
 
 
c1fdf4f
 
56620ea
 
 
3eea664
2539ad3
3eea664
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1fdf4f
3eea664
 
 
 
 
 
 
 
 
 
 
c1fdf4f
3eea664
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1fdf4f
3eea664
c1fdf4f
3eea664
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import os
import requests
from datetime import datetime

import gradio as gr
from gradio import components

import openai
from langchain_openai import ChatOpenAI
from langchain.chains.summarize import load_summarize_chain
from langchain.docstore.document import Document

def comment_datetime(comment_json):
    dt = datetime.fromisoformat(comment_json["created_at"][:-1])
    return comment_json["user"]["login"] + " commented on " + dt.strftime("%b %d, %Y") + ":"

str_format = """
{author}\n
{comment}
""".strip()

def get_comments(url):
    response = requests.get(url, headers=None)
    json_content = response.json()
    comments = "<|endoftext|>".join([str_format.format(author=comment_datetime(x).strip(), comment=x["body"].strip()) for x in json_content])

    return comments


template = """
### ISSUE #{issue_num}\n
### TITLE: {title}\n
### URL: {html_url}\n
### LABELS: {labels}\n
### BODY:\n
{body}\n
### COMMENTS:\n
{comments}\n
""".strip()

def get_full_context(row):
    labels = row["labels"].split("<|endoftext|>")
    comments = row["comments"].replace("<|endoftext|>", "\n\n")
    context = template.format(
        issue_num=row["issue_num"],
        url=row["url"],
        comments_url=row["comments_url"],
        html_url=row["html_url"],
        title=row["title"],
        labels=labels,
        body=row["body"],
        comments=comments
    )

    return context


def get_issue_info(url):
    url_items = url.split("/")
    url_api = "https://api.github.com/repos/{}/{}/issues/{}".format(url_items[-4], url_items[-3], url_items[-1])
    headers = {"Authorization": f"token {os.getenv('GITHUB_TOKEN')}"}
    response = requests.get(url_api, headers=None)
    issue = response.json()
    issue_short = {k: v for k, v in issue.items() if k in ["id", "url", "comments_url", "html_url", "title", "body"]}
    issue_short["labels"] = "<|endoftext|>".join([x["name"] for x in issue["labels"]])
    issue_short["issue_num"] = issue["number"]
    issue_short["comment_count"] = issue["comments"]

    # get comments
    issue_short["comments"] = get_comments(issue_short["comments_url"])

    # get context
    context = get_full_context(issue_short)
    
    return context
    

def init_chain(openai_api_key):
    llm = ChatOpenAI(
        model_name="gpt-4o",
        temperature=0.0,
        openai_api_key=openai_api_key
    )
    
    chain = load_summarize_chain(llm, chain_type="stuff")

    return chain


def get_summary(text, chain):
    doc = Document(page_content=text)
    summary = chain.invoke([doc])["output_text"].strip()

    return summary


template_output = """
## SUMMARY ๐Ÿ“
### {summary}
***
## Source ๐Ÿ‘พ
{context}
""".strip()

def run_all(openai_api_key, url):
    try:
        context = get_issue_info(url)
    except (IndexError, KeyError) as error:
        return "## Invalid Github Issue URL.\n ## See examples below for reference."
    
    try:
        chain = init_chain(openai_api_key)
        summary = get_summary(context, chain)
        print(summary)
        return template_output.format(summary=summary, context=context)
    except (ValueError, openai.AuthenticationError) as error:
        return "## Invalid OpenAI API key provided."
    
html = """
<h1 style='text-align: center;'>GitHub Issue Summarizer</h1>
<img src="https://octodex.github.com/images/Professortocat_v2.png" width=100 height=100 style="display: block; margin: 0 auto;">
 """.strip()

examples = [
    [None, "https://github.com/rust-lang/rust/issues/97362"],
    [None, "https://github.com/rust-lang/rust/issues/83788"],
    [None, "https://github.com/langchain-ai/langchain/issues/9733"],
    [None, "https://github.com/huggingface/transformers/issues/25720"]
]

with gr.Blocks() as demo:
    with gr.Row():
        gr.HTML(html)

    with gr.Row():
        with gr.Column():
            with gr.Row():
                textbox_api_key = components.Textbox(placeholder="sk-...", label="OpenAI API Key", value=None, type="password")
            with gr.Row():
                textbox = gr.Textbox(label="Type a GitHub Issue URL:", placeholder="https://github.com/rust-lang/rust/issues/97362", lines=1)
            with gr.Row():
                summarize = gr.Button("Summarize", variant="primary")
                clear_button = gr.ClearButton()
            with gr.Row():
                gr.Examples(examples=examples, inputs=[textbox_api_key, textbox])
        with gr.Column(scale=1, min_width=1000):
            summary = gr.Markdown(value="Summary")
    summarize.click(run_all, [textbox_api_key, textbox], outputs=summary)
    clear_button.click(fn=lambda: [None, None, "Summary"], outputs=[textbox_api_key, textbox, summary])

demo.launch()