Neodpy commited on
Commit
8929e00
·
verified ·
1 Parent(s): 1df4261

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -28
app.py CHANGED
@@ -1,37 +1,93 @@
1
  import os
 
2
  import requests
3
  import gradio as gr
4
  from groq import Groq
 
5
 
6
- def generate_docs(repo_id: str) -> str:
 
 
 
 
7
  """
8
- Fetches repository details from GitHub and generates documentation using the Groq API.
9
  """
10
- # Fetch basic repo info from GitHub
11
- if repo_id.endswith(".git"):
12
- repo_id = repo_id[:-4]
13
- prefix = "github.com/"
14
- start = repo_id.find(prefix)
15
- if start != -1:
16
- # Offset start by length of prefix
17
- repo_path = repo_id[start + len(prefix):]
18
- print(repo_path)
 
 
 
 
 
 
 
 
 
 
19
  else:
20
- print("Invalid URL format")
21
- response = requests.get(repo_path)
 
 
 
 
 
 
 
 
22
  if response.status_code != 200:
23
- return f"Error: Unable to fetch repository details for '{repo_id}'. Please check the repository ID."
24
- url = f"https://api.github.com/repos/{repo_path}"
25
- repo_data = response.json()
26
- repo_description = repo_data.get("description", "No description provided.")
27
- readme = repo_data.get("README.md", "No description provided.")
28
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  repo_name = repo_data.get("name", repo_id)
 
 
30
 
31
- # Construct a prompt that includes some of the repository details.
32
  prompt = (
33
  f"Generate comprehensive documentation for the GitHub repository '{repo_id}'. "
34
- f"The repository is named '{repo_name}' and its description is: {repo_description}, you should use {readme} for the base docs.\n\n"
 
35
  "Please include sections such as an introduction, installation instructions, usage examples, "
36
  "and any relevant details that would help a new user understand and work with this repository."
37
  )
@@ -47,9 +103,7 @@ def generate_docs(repo_id: str) -> str:
47
  try:
48
  # Call the Groq API with the generated prompt.
49
  chat_completion = client.chat.completions.create(
50
- messages=[
51
- {"role": "user", "content": prompt}
52
- ],
53
  model="llama-3.3-70b-versatile",
54
  stream=False,
55
  )
@@ -60,15 +114,19 @@ def generate_docs(repo_id: str) -> str:
60
  documentation = chat_completion.choices[0].message.content
61
  return documentation
62
 
 
63
  # Define the Gradio Blocks interface.
64
  with gr.Blocks() as demo:
65
  gr.Markdown("# GitHub Repository Documentation Generator")
66
  gr.Markdown(
67
- "Enter a GitHub repository ID (in the format `user/repo`) below. "
68
  "This tool fetches repository details and uses the Groq API to generate documentation."
69
  )
70
-
71
- repo_id_input = gr.Textbox(label="GitHub Repository ID (e.g., user/repo)", placeholder="octocat/Hello-World")
 
 
 
72
  output_box = gr.Textbox(label="Generated Documentation", lines=20)
73
  generate_button = gr.Button("Generate Documentation")
74
 
@@ -76,4 +134,4 @@ with gr.Blocks() as demo:
76
  generate_button.click(fn=generate_docs, inputs=repo_id_input, outputs=output_box)
77
 
78
  # Launch the Gradio app.
79
- demo.launch()
 
1
  import os
2
+ import logging
3
  import requests
4
  import gradio as gr
5
  from groq import Groq
6
+ from urllib.parse import urlparse
7
 
8
+ # Configure logging
9
+ logging.basicConfig(level=logging.INFO)
10
+
11
+
12
+ def extract_repo_id(input_str: str) -> str:
13
  """
14
+ Extract the repository ID in the format 'user/repo' from a URL or plain string.
15
  """
16
+ input_str = input_str.strip()
17
+
18
+ # Remove trailing '.git' if present
19
+ if input_str.endswith(".git"):
20
+ input_str = input_str[:-4]
21
+
22
+ # If input is a URL, parse it to extract the repo path.
23
+ if input_str.startswith("http"):
24
+ try:
25
+ parsed = urlparse(input_str)
26
+ path_parts = parsed.path.strip("/").split("/")
27
+ if len(path_parts) >= 2:
28
+ repo_id = "/".join(path_parts[:2])
29
+ logging.info("Extracted repo_id: %s", repo_id)
30
+ return repo_id
31
+ else:
32
+ raise ValueError("Invalid GitHub URL format.")
33
+ except Exception as e:
34
+ raise ValueError(f"Error parsing URL: {e}")
35
  else:
36
+ # Assume it's already in the correct format.
37
+ return input_str
38
+
39
+
40
+ def fetch_repo_details(repo_id: str) -> dict:
41
+ """
42
+ Fetch repository details from GitHub using its API.
43
+ """
44
+ api_url = f"https://api.github.com/repos/{repo_id}"
45
+ response = requests.get(api_url)
46
  if response.status_code != 200:
47
+ raise ValueError(
48
+ f"Error: Unable to fetch repository details for '{repo_id}'. "
49
+ f"Status code: {response.status_code}"
50
+ )
51
+ return response.json()
52
+
53
+
54
+ def fetch_readme(repo_id: str) -> str:
55
+ """
56
+ Attempt to fetch the README.md from the repository's main branch.
57
+ """
58
+ readme_url = f"https://raw.githubusercontent.com/{repo_id}/main/README.md"
59
+ response = requests.get(readme_url)
60
+ if response.status_code == 200:
61
+ return response.text
62
+ else:
63
+ return "No README found."
64
+
65
+
66
+ def generate_docs(repo_input: str) -> str:
67
+ """
68
+ Fetch repository details from GitHub and generate documentation using the Groq API.
69
+ """
70
+ try:
71
+ # Extract a clean repository ID.
72
+ repo_id = extract_repo_id(repo_input)
73
+ except ValueError as e:
74
+ return str(e)
75
+
76
+ try:
77
+ repo_data = fetch_repo_details(repo_id)
78
+ except ValueError as e:
79
+ return str(e)
80
+
81
+ # Extract repository information.
82
  repo_name = repo_data.get("name", repo_id)
83
+ repo_description = repo_data.get("description", "No description provided.")
84
+ readme_content = fetch_readme(repo_id)
85
 
86
+ # Construct the prompt for the Groq API.
87
  prompt = (
88
  f"Generate comprehensive documentation for the GitHub repository '{repo_id}'. "
89
+ f"The repository is named '{repo_name}' and its description is: {repo_description}. "
90
+ f"Use the following README content as reference:\n\n{readme_content}\n\n"
91
  "Please include sections such as an introduction, installation instructions, usage examples, "
92
  "and any relevant details that would help a new user understand and work with this repository."
93
  )
 
103
  try:
104
  # Call the Groq API with the generated prompt.
105
  chat_completion = client.chat.completions.create(
106
+ messages=[{"role": "user", "content": prompt}],
 
 
107
  model="llama-3.3-70b-versatile",
108
  stream=False,
109
  )
 
114
  documentation = chat_completion.choices[0].message.content
115
  return documentation
116
 
117
+
118
  # Define the Gradio Blocks interface.
119
  with gr.Blocks() as demo:
120
  gr.Markdown("# GitHub Repository Documentation Generator")
121
  gr.Markdown(
122
+ "Enter a GitHub repository URL or ID (in the format `user/repo`) below. "
123
  "This tool fetches repository details and uses the Groq API to generate documentation."
124
  )
125
+
126
+ repo_id_input = gr.Textbox(
127
+ label="GitHub Repository URL or ID",
128
+ placeholder="https://github.com/octocat/Hello-World or octocat/Hello-World",
129
+ )
130
  output_box = gr.Textbox(label="Generated Documentation", lines=20)
131
  generate_button = gr.Button("Generate Documentation")
132
 
 
134
  generate_button.click(fn=generate_docs, inputs=repo_id_input, outputs=output_box)
135
 
136
  # Launch the Gradio app.
137
+ demo.launch(ssr=False, debug=True)