Arcadia822 commited on
Commit
282ada1
·
1 Parent(s): da349bb

update demo

Browse files
Files changed (2) hide show
  1. app.py +19 -5
  2. codedog_demo/callbacks.py +30 -6
app.py CHANGED
@@ -9,11 +9,25 @@ with gr.Blocks(theme="xiaobaiyuan/theme_brief") as demo:
9
  gr.Markdown("# Codedog Demo")
10
 
11
  with gr.Tab(label="Try Yourself"):
12
- custom_pr_url = gr.Textbox(max_lines=1, placeholder="Paste Github PR URL here", show_label=False)
13
- custom_submit = gr.Button(value="Review It")
14
- custom_content = gr.Markdown(value="")
15
-
16
- custom_submit.click(request_pr_review, inputs=[custom_pr_url], outputs=[custom_content])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
  with gr.Tab(label="Samples"):
19
  sample_choice = gr.Radio(choices=sample_choices, type="index", show_label=False)
 
9
  gr.Markdown("# Codedog Demo")
10
 
11
  with gr.Tab(label="Try Yourself"):
12
+ with gr.Row():
13
+ custom_pr_url = gr.Textbox(
14
+ max_lines=1,
15
+ value="https://github.com/codedog-ai/codedog/pull/2",
16
+ placeholder="Paste Github PR URL here",
17
+ show_label=False,
18
+ )
19
+ with gr.Row():
20
+ custom_submit = gr.Button(value="Review It")
21
+ with gr.Row():
22
+ with gr.Tab(label="Markdown"):
23
+ custom_content = gr.Markdown(value="")
24
+ with gr.Tab(label="Raw"):
25
+ custom_content_raw = gr.Textbox(show_label=False)
26
+ custom_submit.click(
27
+ request_pr_review,
28
+ inputs=[custom_pr_url],
29
+ outputs=[custom_content, custom_content_raw],
30
+ )
31
 
32
  with gr.Tab(label="Samples"):
33
  sample_choice = gr.Radio(choices=sample_choices, type="index", show_label=False)
codedog_demo/callbacks.py CHANGED
@@ -1,14 +1,17 @@
 
 
 
1
  from os import environ as env
2
  from os import listdir
3
  from typing import List
4
 
 
5
  import requests
6
 
7
  from codedog_demo.github_utils import parse_github_pr_url
8
 
9
  codedog_api = env.get("CODEDOG_API_URL", "")
10
- installation = int(env.get("INSTALLATION_ID", 0))
11
-
12
 
13
  sample_names = []
14
  sample_contents = []
@@ -19,17 +22,38 @@ for file in listdir("samples"):
19
 
20
 
21
  def request_pr_review(url: str):
22
- repo, pr_number = parse_github_pr_url(url)
23
- if not repo or not pr_number:
24
- return "Invalid URL. Accept format is: https://www.github.com/{owner}/{repository}/pull/{pr_number}"
 
 
 
 
 
 
 
 
25
 
 
 
 
26
  response = requests.post(
27
- codedog_api, json={"repository": repo, "pull_request_number": pr_number, "installation_id": installation}
28
  )
29
  result = response.text
 
 
 
 
 
30
  return result
31
 
32
 
 
 
 
 
 
33
  def get_sample_choices() -> List[str]:
34
  return sample_names
35
 
 
1
+ import time
2
+ import traceback
3
+ from functools import lru_cache
4
  from os import environ as env
5
  from os import listdir
6
  from typing import List
7
 
8
+ import gradio as gr
9
  import requests
10
 
11
  from codedog_demo.github_utils import parse_github_pr_url
12
 
13
  codedog_api = env.get("CODEDOG_API_URL", "")
14
+ github_token = env.get("GITHUB_TOKEN", "")
 
15
 
16
  sample_names = []
17
  sample_contents = []
 
22
 
23
 
24
  def request_pr_review(url: str):
25
+ try:
26
+ repo, pr_number = parse_github_pr_url(url)
27
+ if not repo or not pr_number:
28
+ return "Invalid URL. Accept format is: https://www.github.com/{owner}/{repository}/pull/{pr_number}", ""
29
+
30
+ result = _request_pr_review(repo, pr_number, ttl_hash=get_ttl_hash())
31
+
32
+ except Exception:
33
+ traceback.print_exc()
34
+ return "Something went wrong. Please try again later.", ""
35
+ return result, result
36
 
37
+
38
+ @lru_cache(maxsize=100)
39
+ def _request_pr_review(repo: str, pr_number: int, ttl_hash=None):
40
  response = requests.post(
41
+ codedog_api, json={"repository": repo, "pull_request_number": pr_number, "token": github_token}
42
  )
43
  result = response.text
44
+ if len(result) < 100:
45
+ if result == "stream timeout":
46
+ raise ValueError("Timeout")
47
+ print(f"Error result: {result}")
48
+ raise ValueError()
49
  return result
50
 
51
 
52
+ def get_ttl_hash(seconds=120):
53
+ """Return the same value withing `seconds` time period"""
54
+ return round(time.time() / seconds)
55
+
56
+
57
  def get_sample_choices() -> List[str]:
58
  return sample_names
59