Neodpy commited on
Commit
3c07033
·
verified ·
1 Parent(s): a963563

Create docs.py

Browse files
Files changed (1) hide show
  1. docs.py +115 -0
docs.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ )
94
+
95
+ # Retrieve the Groq API key from environment variables.
96
+ groq_api_key = os.environ.get("GROQ_API_KEY")
97
+ if not groq_api_key:
98
+ return "Error: GROQ_API_KEY environment variable is not set."
99
+
100
+ # Initialize the Groq client.
101
+ client = Groq(api_key=groq_api_key)
102
+
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="deepseek-r1-distill-llama-70b",
108
+ stream=False,
109
+ )
110
+ except Exception as e:
111
+ return f"Error calling Groq API: {e}"
112
+
113
+ # Extract and return the generated documentation.
114
+ documentation = chat_completion.choices[0].message.content
115
+ return documentation