matteopilotto
commited on
Commit
β’
3eea664
1
Parent(s):
81cbae1
add app.py
Browse files
app.py
ADDED
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
def comment_datetime(comment_json):
|
2 |
+
dt = datetime.fromisoformat(comment_json["created_at"])
|
3 |
+
return comment_json["user"]["login"] + " commented on " + dt.strftime("%b %d, %Y") + ":"
|
4 |
+
|
5 |
+
str_format = """
|
6 |
+
{author}\n
|
7 |
+
{comment}
|
8 |
+
""".strip()
|
9 |
+
|
10 |
+
def get_comments(url):
|
11 |
+
response = requests.get(url, headers=None)
|
12 |
+
json_content = response.json()
|
13 |
+
comments = "<|endoftext|>".join([str_format.format(author=comment_datetime(x).strip(), comment=x["body"].strip()) for x in json_content])
|
14 |
+
|
15 |
+
return comments
|
16 |
+
|
17 |
+
|
18 |
+
template = """
|
19 |
+
### ISSUE #{issue_num}\n
|
20 |
+
### TITLE: {title}\n
|
21 |
+
### URL: {html_url}\n
|
22 |
+
### LABELS: {labels}\n
|
23 |
+
### BODY:\n
|
24 |
+
{body}\n
|
25 |
+
### COMMENTS:\n
|
26 |
+
{comments}\n
|
27 |
+
""".strip()
|
28 |
+
|
29 |
+
def get_full_context(row):
|
30 |
+
labels = row["labels"].split("<|endoftext|>")
|
31 |
+
comments = row["comments"].replace("<|endoftext|>", "\n\n")
|
32 |
+
context = template.format(
|
33 |
+
issue_num=row["issue_num"],
|
34 |
+
url=row["url"],
|
35 |
+
comments_url=row["comments_url"],
|
36 |
+
html_url=row["html_url"],
|
37 |
+
title=row["title"],
|
38 |
+
labels=labels,
|
39 |
+
body=row["body"],
|
40 |
+
comments=comments
|
41 |
+
)
|
42 |
+
|
43 |
+
return context
|
44 |
+
|
45 |
+
|
46 |
+
def get_issue_info(url):
|
47 |
+
url_items = url.split("/")
|
48 |
+
url_api = "https://api.github.com/repos/{}/{}/issues/{}".format(url_items[-4], url_items[-3], url_items[-1])
|
49 |
+
headers = {"Authorization": f"token {os.getenv('GITHUB_TOKEN')}"}
|
50 |
+
response = requests.get(url_api, headers=None)
|
51 |
+
issue = response.json()
|
52 |
+
issue_short = {k: v for k, v in issue.items() if k in ["id", "url", "comments_url", "html_url", "title", "body"]}
|
53 |
+
issue_short["labels"] = "<|endoftext|>".join([x["name"] for x in issue["labels"]])
|
54 |
+
issue_short["issue_num"] = issue["number"]
|
55 |
+
issue_short["comment_count"] = issue["comments"]
|
56 |
+
|
57 |
+
# get comments
|
58 |
+
issue_short["comments"] = get_comments(issue_short["comments_url"])
|
59 |
+
|
60 |
+
# get context
|
61 |
+
context = get_full_context(issue_short)
|
62 |
+
|
63 |
+
return context
|
64 |
+
|
65 |
+
|
66 |
+
def init_chain(openai_api_key):
|
67 |
+
llm = ChatOpenAI(
|
68 |
+
model_name="gpt-3.5-turbo-16k",
|
69 |
+
temperature=0.0,
|
70 |
+
openai_api_key=openai_api_key
|
71 |
+
)
|
72 |
+
|
73 |
+
chain = load_summarize_chain(llm, chain_type="stuff")
|
74 |
+
|
75 |
+
return chain
|
76 |
+
|
77 |
+
|
78 |
+
def get_summary(text, chain):
|
79 |
+
doc = Document(page_content=text)
|
80 |
+
summary = chain.run([doc]).strip()
|
81 |
+
|
82 |
+
return summary
|
83 |
+
|
84 |
+
|
85 |
+
template_output = """
|
86 |
+
## SUMMARY π
|
87 |
+
### {summary}
|
88 |
+
***
|
89 |
+
## Source πΎ
|
90 |
+
{context}
|
91 |
+
""".strip()
|
92 |
+
|
93 |
+
def run_all(openai_api_key, url):
|
94 |
+
try:
|
95 |
+
context = get_issue_info(url)
|
96 |
+
except (IndexError, KeyError) as error:
|
97 |
+
return "## Invalid Github Issue URL.\n ## See examples below for reference."
|
98 |
+
|
99 |
+
try:
|
100 |
+
chain = init_chain(openai_api_key)
|
101 |
+
summary = get_summary(context, chain)
|
102 |
+
return template_output.format(summary=summary, context=context)
|
103 |
+
except (ValueError, AuthenticationError) as error:
|
104 |
+
return "## Invalid OpenAI API key provided."
|
105 |
+
|
106 |
+
html = """
|
107 |
+
<h1 style='text-align: center;'>GitHub Issue Summarizer</h1>
|
108 |
+
<img src="https://octodex.github.com/images/Professortocat_v2.png" width=100 height=100 style="display: block; margin: 0 auto;">
|
109 |
+
""".strip()
|
110 |
+
|
111 |
+
examples = [
|
112 |
+
[None, "https://github.com/rust-lang/rust/issues/97362"],
|
113 |
+
[None, "https://github.com/rust-lang/rust/issues/83788"],
|
114 |
+
[None, "https://github.com/langchain-ai/langchain/issues/9733"],
|
115 |
+
[None, "https://github.com/huggingface/transformers/issues/25720"]
|
116 |
+
]
|
117 |
+
|
118 |
+
with gr.Blocks() as demo:
|
119 |
+
with gr.Row():
|
120 |
+
gr.HTML(html)
|
121 |
+
|
122 |
+
with gr.Row():
|
123 |
+
with gr.Column():
|
124 |
+
with gr.Row():
|
125 |
+
textbox_api_key = components.Textbox(placeholder="sk-...", label="OpenAI API Key", value=None, type="password")
|
126 |
+
with gr.Row():
|
127 |
+
textbox = gr.Textbox(label="Type a GitHub Issue URL:", placeholder="https://github.com/rust-lang/rust/issues/97362", lines=1)
|
128 |
+
with gr.Row():
|
129 |
+
summarize = gr.Button("Summarize", variant="primary")
|
130 |
+
clear_button = gr.ClearButton()
|
131 |
+
with gr.Row():
|
132 |
+
gr.Examples(examples=examples, inputs=[textbox_api_key, textbox])
|
133 |
+
with gr.Column(scale=1, min_width=1000):
|
134 |
+
summary = gr.Markdown(value="Summary")
|
135 |
+
summarize.click(run_all, [textbox_api_key, textbox], outputs=summary)
|
136 |
+
clear_button.click(fn=lambda: [None, None, "Summary"], outputs=[textbox_api_key, textbox, summary])
|
137 |
+
|
138 |
+
demo.launch()
|