Spaces:
Sleeping
Sleeping
Neodpy
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
url = f"https://api.github.com/repos/{repo_id}"
|
12 |
+
response = requests.get(url)
|
13 |
+
if response.status_code != 200:
|
14 |
+
return f"Error: Unable to fetch repository details for '{repo_id}'. Please check the repository ID."
|
15 |
+
|
16 |
+
repo_data = response.json()
|
17 |
+
repo_description = repo_data.get("description", "No description provided.")
|
18 |
+
repo_name = repo_data.get("name", repo_id)
|
19 |
+
|
20 |
+
# Construct a prompt that includes some of the repository details.
|
21 |
+
prompt = (
|
22 |
+
f"Generate comprehensive documentation for the GitHub repository '{repo_id}'. "
|
23 |
+
f"The repository is named '{repo_name}' and its description is: {repo_description}.\n\n"
|
24 |
+
"Please include sections such as an introduction, installation instructions, usage examples, "
|
25 |
+
"and any relevant details that would help a new user understand and work with this repository."
|
26 |
+
)
|
27 |
+
|
28 |
+
# Retrieve the Groq API key from environment variables.
|
29 |
+
groq_api_key = os.environ.get("GROQ_API_KEY")
|
30 |
+
if not groq_api_key:
|
31 |
+
return "Error: GROQ_API_KEY environment variable is not set."
|
32 |
+
|
33 |
+
# Initialize the Groq client.
|
34 |
+
client = Groq(api_key=groq_api_key)
|
35 |
+
|
36 |
+
try:
|
37 |
+
# Call the Groq API with the generated prompt.
|
38 |
+
chat_completion = client.chat.completions.create(
|
39 |
+
messages=[
|
40 |
+
{"role": "user", "content": prompt}
|
41 |
+
],
|
42 |
+
model="llama-3.3-70b-versatile",
|
43 |
+
stream=False,
|
44 |
+
)
|
45 |
+
except Exception as e:
|
46 |
+
return f"Error calling Groq API: {e}"
|
47 |
+
|
48 |
+
# Extract and return the generated documentation.
|
49 |
+
documentation = chat_completion.choices[0].message.content
|
50 |
+
return documentation
|
51 |
+
|
52 |
+
# Define the Gradio Blocks interface.
|
53 |
+
with gr.Blocks() as demo:
|
54 |
+
gr.Markdown("# GitHub Repository Documentation Generator")
|
55 |
+
gr.Markdown(
|
56 |
+
"Enter a GitHub repository ID (in the format `user/repo`) below. "
|
57 |
+
"This tool fetches repository details and uses the Groq API to generate documentation."
|
58 |
+
)
|
59 |
+
|
60 |
+
repo_id_input = gr.Textbox(label="GitHub Repository ID (e.g., user/repo)", placeholder="octocat/Hello-World")
|
61 |
+
output_box = gr.Textbox(label="Generated Documentation", lines=20)
|
62 |
+
generate_button = gr.Button("Generate Documentation")
|
63 |
+
|
64 |
+
# When the button is clicked, call the generate_docs function.
|
65 |
+
generate_button.click(fn=generate_docs, inputs=repo_id_input, outputs=output_box)
|
66 |
+
|
67 |
+
# Launch the Gradio app.
|
68 |
+
demo.launch()
|