File size: 3,115 Bytes
8f72ba3
 
 
 
 
 
 
 
 
 
1df4261
 
 
 
 
 
 
 
 
 
 
8f72ba3
 
1df4261
8f72ba3
 
fbdcef9
 
8f72ba3
 
 
 
 
fbdcef9
8f72ba3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import os
import requests
import gradio as gr
from groq import Groq

def generate_docs(repo_id: str) -> str:
    """
    Fetches repository details from GitHub and generates documentation using the Groq API.
    """
    # Fetch basic repo info from GitHub
    if repo_id.endswith(".git"):
        repo_id = repo_id[:-4]
    prefix = "github.com/"
    start = repo_id.find(prefix)
    if start != -1:
        # Offset start by length of prefix
        repo_path = repo_id[start + len(prefix):]
        print(repo_path)
    else:
        print("Invalid URL format")
    response = requests.get(repo_path)
    if response.status_code != 200:
        return f"Error: Unable to fetch repository details for '{repo_id}'. Please check the repository ID."
    url = f"https://api.github.com/repos/{repo_path}"
    repo_data = response.json()
    repo_description = repo_data.get("description", "No description provided.")
    readme = repo_data.get("README.md", "No description provided.")
  
    repo_name = repo_data.get("name", repo_id)

    # Construct a prompt that includes some of the repository details.
    prompt = (
        f"Generate comprehensive documentation for the GitHub repository '{repo_id}'. "
        f"The repository is named '{repo_name}' and its description is: {repo_description}, you should use {readme} for the base docs.\n\n"
        "Please include sections such as an introduction, installation instructions, usage examples, "
        "and any relevant details that would help a new user understand and work with this repository."
    )

    # Retrieve the Groq API key from environment variables.
    groq_api_key = os.environ.get("GROQ_API_KEY")
    if not groq_api_key:
        return "Error: GROQ_API_KEY environment variable is not set."

    # Initialize the Groq client.
    client = Groq(api_key=groq_api_key)

    try:
        # Call the Groq API with the generated prompt.
        chat_completion = client.chat.completions.create(
            messages=[
                {"role": "user", "content": prompt}
            ],
            model="llama-3.3-70b-versatile",
            stream=False,
        )
    except Exception as e:
        return f"Error calling Groq API: {e}"

    # Extract and return the generated documentation.
    documentation = chat_completion.choices[0].message.content
    return documentation

# Define the Gradio Blocks interface.
with gr.Blocks() as demo:
    gr.Markdown("# GitHub Repository Documentation Generator")
    gr.Markdown(
        "Enter a GitHub repository ID (in the format `user/repo`) below. "
        "This tool fetches repository details and uses the Groq API to generate documentation."
    )
    
    repo_id_input = gr.Textbox(label="GitHub Repository ID (e.g., user/repo)", placeholder="octocat/Hello-World")
    output_box = gr.Textbox(label="Generated Documentation", lines=20)
    generate_button = gr.Button("Generate Documentation")

    # When the button is clicked, call the generate_docs function.
    generate_button.click(fn=generate_docs, inputs=repo_id_input, outputs=output_box)

# Launch the Gradio app.
demo.launch()