codeanalyze / app.py
kevin1911's picture
Update app.py
6b56fd3 verified
raw
history blame
3.07 kB
import gradio as gr
import requests
import matplotlib.pyplot as plt
# Function to fetch repository details from GitHub and generate a visualization
def analyze_github_repo(repo_url):
# Extract the username and repo name from the GitHub URL
if "github.com" not in repo_url:
return "Please provide a valid GitHub repository URL."
parts = repo_url.split('/')
if len(parts) < 5:
return "URL should be in the format: https://github.com/username/repository"
user, repo = parts[3], parts[4]
# GitHub API endpoint to get repository details
api_url = f"https://api.github.com/repos/{user}/{repo}"
response = requests.get(api_url)
# Check if repository exists
if response.status_code == 404:
return "Repository not found."
# Parse the response
data = response.json()
# Extract relevant information
stars = data.get('stargazers_count', 'N/A')
forks = data.get('forks_count', 'N/A')
issues = data.get('open_issues_count', 'N/A')
contributors_url = data.get('contributors_url', None)
# Get number of contributors and their contributions (commit count)
contributors = []
if contributors_url:
contributors_data = requests.get(contributors_url).json()
for contributor in contributors_data:
contributors.append({
'login': contributor['login'],
'contributions': contributor['contributions']
})
# Data to plot
metrics = {
'Stars': stars,
'Forks': forks,
'Open Issues': issues,
'Contributors': len(contributors)
}
# Create the bar plot
fig, ax = plt.subplots()
ax.bar(metrics.keys(), metrics.values(), color=['blue', 'green', 'red', 'orange'])
ax.set_xlabel('Metrics')
ax.set_ylabel('Count')
ax.set_title(f"GitHub Repository Analysis: {repo_url}")
# Save the plot as an image
plt.tight_layout()
plt.savefig("/mnt/data/github_analysis.png")
plt.close()
# Prepare contributor data for display
contributor_info = "Contributors and Their Commit Contributions:\n"
for contributor in contributors:
contributor_info += f"{contributor['login']}: {contributor['contributions']} contributions\n"
# Combine the visualization link and contributor data
result = f"Repository: {repo_url}\n\n{contributor_info}\nImage of Repository Analysis:\n/mnt/data/github_analysis.png"
return result, "/mnt/data/github_analysis.png"
# Gradio interface for the tool
iface = gr.Interface(
fn=analyze_github_repo,
inputs=gr.Textbox(label="Enter GitHub Repository URL", placeholder="https://github.com/username/repository"),
outputs=[gr.Textbox(label="Repository Analysis"), gr.Image(label="Repository Analysis Visualization")],
title="GitHub Repository Analysis Tool",
description="Enter a GitHub repository URL to get basic analytics including stars, forks, issues, and contributors, along with a visual chart and contributor details."
)
# Launch the app
iface.launch()