Not-Grim-Refer commited on
Commit
099b6c0
·
1 Parent(s): eebbcf7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -64
app.py CHANGED
@@ -1,64 +1,46 @@
1
- import streamlit as st
2
- from github import Github
3
- from github import UnknownObjectException
4
- import logging
5
-
6
- logging.basicConfig(level=logging.INFO)
7
- logger = logging.getLogger(__name__)
8
-
9
- st.set_page_config(page_title="GitHub File Search", page_icon=":mag_right:")
10
- access_token = "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"
11
-
12
- def find_file(repo, filename):
13
- contents = repo.get_contents("", ref=repo.default_branch)
14
- while contents:
15
- content = contents.pop(0)
16
- if content.type == "file" and content.name == filename:
17
- return content
18
- elif content.type == "dir":
19
- contents.extend(repo.get_contents(content.path))
20
-
21
- def get_repositories_with_file(filename):
22
- g = Github(access_token)
23
- repositories = g.search_repositories(query=f"filename:{filename}", sort="stars", order="desc")
24
- repo_info = []
25
-
26
- for repo in repositories:
27
- try:
28
- content = find_file(repo, filename)
29
- if content:
30
- repo_info.append({
31
- "name": repo.name,
32
- "description": repo.description,
33
- "url": repo.html_url
34
- })
35
- except UnknownObjectException as e:
36
- logger.warning(f"File '{filename}' not found in repository '{repo.full_name}'.")
37
- st.warning(f"File '{filename}' not found in repository '{repo.full_name}'.")
38
- return repo_info
39
-
40
- def app():
41
- st.title("GitHub File Search")
42
- st.write("Enter a file name to search for on GitHub:")
43
- filename = st.text_input("File name")
44
- if filename:
45
- repo_info = get_repositories_with_file(filename)
46
- if len(repo_info) > 0:
47
- st.success(f"Found {len(repo_info)} repositories with the file '{filename}':")
48
- for repo in repo_info:
49
- st.write(f"- **{repo['name']}**: {repo['description']}")
50
- st.write(f" URL: [{repo['url']}]({repo['url']})")
51
- else:
52
- logger.warning(f"No repositories found with the file '{filename}'.")
53
- st.warning(f"No repositories found with the file '{filename}'.")
54
-
55
- st.sidebar.title("GitHub File Search Options")
56
- show_logs = st.sidebar.checkbox("Show Logs")
57
- if show_logs:
58
- st.sidebar.subheader("Logs")
59
- with open("app.log", "r") as log_file:
60
- logs = log_file.read()
61
- st.sidebar.text(logs)
62
-
63
- if __name__ == "__main__":
64
- app()
 
1
+ import gradio as gr
2
+ import requests
3
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
4
+
5
+ # Initialize the CodeBERT model and tokenizer
6
+ tokenizer = AutoTokenizer.from_pretrained("microsoft/CodeBERT-base")
7
+ model = AutoModelForSeq2SeqLM.from_pretrained("microsoft/CodeBERT-base")
8
+
9
+ def fetch_repo_contents(repo_url):
10
+ # Extract username and repo name from URL
11
+ username, repo_name = repo_url.split("github.com/")[-1].split("/")
12
+
13
+ # Fetch repo contents using GitHub API
14
+ api_url = f"https://api.github.com/repos/{username}/{repo_name}/contents"
15
+ response = requests.get(api_url)
16
+ response.raise_for_status()
17
+ return response.json()
18
+
19
+ def generate_chatbot_response(repo_url, question):
20
+ # Fetch repository contents
21
+ repo_contents = fetch_repo_contents(repo_url)
22
+
23
+ # Generate a prompt based on the user's question and repository contents
24
+ prompt = f"Answer the question about the repository {repo_url}: {question}\n\n"
25
+ for item in repo_contents:
26
+ prompt += f"{item['name']}:\n{item['download_url']}\n\n"
27
+
28
+ # Tokenize the prompt and generate a response using the CodeBERT model
29
+ inputs = tokenizer.encode(prompt, return_tensors="pt", max_length=1024, truncation=True)
30
+ outputs = model.generate(inputs, max_length=150, num_return_sequences=1)
31
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
32
+
33
+ return response
34
+
35
+ # Gradio UI
36
+ repo_url_input = gr.inputs.Textbox(lines=1, label="GitHub Repository URL")
37
+ question_input = gr.inputs.Textbox(lines=2, label="Question")
38
+ output_text = gr.outputs.Textbox(label="Answer")
39
+
40
+ gr.Interface(
41
+ generate_chatbot_response,
42
+ inputs=[repo_url_input, question_input],
43
+ outputs=output_text,
44
+ title="Create a Conversational AI Chatbot for Your Public GitHub Repository Codebase",
45
+ theme="huggingface_dark",
46
+ ).launch()