kumar1907 commited on
Commit
cf44d65
Β·
verified Β·
1 Parent(s): 26c9196

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -79
app.py CHANGED
@@ -1,87 +1,18 @@
1
  import streamlit as st
2
- import requests
3
- from transformers import pipeline
4
 
5
  # -----------------------------
6
- # Load Hugging Face Model
7
- # -----------------------------
8
- @st.cache_resource
9
- def load_model():
10
- return pipeline("text2text-generation", model="google/flan-t5-small") # faster
11
-
12
- reviewer = load_model()
13
-
14
- # -----------------------------
15
- # Fetch PR diff from GitHub
16
- # -----------------------------
17
- def fetch_pr_diff(owner, repo, pr_number, token=None):
18
- url = f"https://api.github.com/repos/{owner}/{repo}/pulls/{pr_number}"
19
- headers = {}
20
- if token:
21
- headers["Authorization"] = f"token {token}"
22
- r = requests.get(url, headers=headers)
23
- if r.status_code != 200:
24
- return None, f"Error fetching PR: {r.text}"
25
-
26
- pr = r.json()
27
- diff_url = pr.get("diff_url")
28
- diff_r = requests.get(diff_url, headers=headers)
29
- if diff_r.status_code != 200:
30
- return None, f"Error fetching diff: {diff_r.text}"
31
- return diff_r.text, None
32
-
33
- # -----------------------------
34
- # Review code with LLM
35
- # -----------------------------
36
- def review_code(code_text):
37
- try:
38
- if len(code_text) > 2000:
39
- code_text = code_text[:2000] + "\n... [TRUNCATED] ..."
40
-
41
- prompt = f"""
42
- You are an AI code reviewer.
43
- Review the following GitHub Pull Request diff:
44
-
45
- {code_text}
46
-
47
- Give two things:
48
- 1. FEEDBACK: clear review notes (issues + improvements)
49
- 2. CODE: improved/refactored version
50
-
51
- Format:
52
-
53
- FEEDBACK:
54
- <feedback>
55
-
56
- CODE:
57
- <code>
58
- """
59
-
60
- output = reviewer(prompt, max_length=700, do_sample=False)
61
- response = output[0]['generated_text'].strip()
62
-
63
- # Split output safely
64
- feedback, code = "", ""
65
- if "CODE:" in response:
66
- parts = response.split("CODE:", 1)
67
- feedback = parts[0].replace("FEEDBACK:", "").strip()
68
- code = parts[1].strip()
69
- else:
70
- feedback = response # fallback
71
-
72
- return feedback, code
73
-
74
- except Exception as e:
75
- return f"⚠️ Error during review: {str(e)}", ""
76
-
77
- # -----------------------------
78
- # Streamlit UI
79
  # -----------------------------
80
  st.set_page_config(page_title="Code Review Agent", page_icon="πŸ€–", layout="wide")
81
 
82
  st.title("πŸ€– Code Review Agent (Hugging Face Edition)")
83
- st.write("Fetch a Pull Request from GitHub and get AI-powered review + improved code")
84
 
 
 
 
85
  with st.sidebar:
86
  st.header("πŸ“‚ GitHub PR Details")
87
  owner = st.text_input("GitHub Repo Owner", "kumar1905")
@@ -90,11 +21,15 @@ with st.sidebar:
90
  token = st.text_input("GitHub Token (optional if public repo)", type="password")
91
  fetch_btn = st.button("πŸš€ Fetch & Review PR")
92
 
 
 
 
93
  if fetch_btn:
94
  with st.spinner("Fetching PR diff..."):
95
- diff, err = fetch_pr_diff(owner, repo, pr_number, token)
96
- if err:
97
- st.error(err)
 
98
  else:
99
  st.success("βœ… PR diff fetched successfully!")
100
  with st.expander("πŸ“„ View Raw PR Diff"):
@@ -103,14 +38,22 @@ if fetch_btn:
103
  with st.spinner("Analyzing code with Hugging Face model..."):
104
  feedback, code = review_code(diff)
105
 
 
106
  st.subheader("πŸ’‘ Review Feedback")
107
  if feedback:
108
  st.write(feedback)
109
  else:
110
  st.warning("No feedback generated.")
111
 
 
112
  st.subheader("✨ Suggested Improved Code")
113
  if code:
114
  st.code(code, language="python")
115
  else:
116
  st.warning("No improved code generated.")
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ from code_reviewer import review_code
3
+ from pr_fetcher import fetch_pr_diff
4
 
5
  # -----------------------------
6
+ # Streamlit Page Config
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  # -----------------------------
8
  st.set_page_config(page_title="Code Review Agent", page_icon="πŸ€–", layout="wide")
9
 
10
  st.title("πŸ€– Code Review Agent (Hugging Face Edition)")
11
+ st.write("Fetch a Pull Request from GitHub and get **AI-powered review + improved code**")
12
 
13
+ # -----------------------------
14
+ # Sidebar Inputs
15
+ # -----------------------------
16
  with st.sidebar:
17
  st.header("πŸ“‚ GitHub PR Details")
18
  owner = st.text_input("GitHub Repo Owner", "kumar1905")
 
21
  token = st.text_input("GitHub Token (optional if public repo)", type="password")
22
  fetch_btn = st.button("πŸš€ Fetch & Review PR")
23
 
24
+ # -----------------------------
25
+ # Fetch + Review
26
+ # -----------------------------
27
  if fetch_btn:
28
  with st.spinner("Fetching PR diff..."):
29
+ diff = fetch_pr_diff(owner, repo, pr_number, token)
30
+
31
+ if diff.startswith("Error"):
32
+ st.error(diff)
33
  else:
34
  st.success("βœ… PR diff fetched successfully!")
35
  with st.expander("πŸ“„ View Raw PR Diff"):
 
38
  with st.spinner("Analyzing code with Hugging Face model..."):
39
  feedback, code = review_code(diff)
40
 
41
+ # Feedback Section
42
  st.subheader("πŸ’‘ Review Feedback")
43
  if feedback:
44
  st.write(feedback)
45
  else:
46
  st.warning("No feedback generated.")
47
 
48
+ # Improved Code Section
49
  st.subheader("✨ Suggested Improved Code")
50
  if code:
51
  st.code(code, language="python")
52
  else:
53
  st.warning("No improved code generated.")
54
+
55
+ # -----------------------------
56
+ # Footer
57
+ # -----------------------------
58
+ st.markdown("---")
59
+ st.markdown("Built with ❀️ using [Streamlit](https://streamlit.io/) & [Hugging Face Transformers](https://huggingface.co/)")