import gradio as gr import os import requests import git import tempfile import shutil from openai import OpenAI import dotenv def generate_prompt(repo_url, role, seniority, assignment_details, repo_data, focus_areas=""): """Generates the AI analysis prompt with repo details.""" return f""" You are an AI expert in evaluating software engineering candidates based on their GitHub repositories. Your goal is to assess the quality, organization, and best practices of the submitted code repository. Below is the candidate's information: ### Candidate Information: - **Role Applied For**: {role} - **Seniority Level**: {seniority} - **GitHub Repository URL**: {repo_url} ### Guidelines: - Focus on the candidate's ability to write clean, efficient, and maintainable code. - Take into consideration the seniority level when evaluating the code. - Focus on the assignment details provided. What was requested of him and how he responded to it. ### Assignment Details: {assignment_details} ### Repository Analysis: You are provided with the repository's cloned structure and its contents. Analyze the following aspects: {repo_data} ### Additional Evaluation Criteria: 1. **Code Organization & Architecture** 2. **Code Quality & Best Practices** 3. **Language Proficiency & Best Practices** 4. **Use of Frameworks & Libraries** ### Optional Key Focus Areas: {focus_areas} ### **Final Score Calculation** - Score the repository **out of 100** based on the criteria above. - Justify the **score** by explaining the candidate’s strengths and weaknesses. ### **Expected Output:** - **Strengths**: What is done well? - **Weaknesses**: What needs improvement? - **Final Score (0-100)**: Provide a numeric score with an explanation. - **Summary**: Briefly summarize the candidate’s proficiency based on this analysis. """ # Load environment variables dotenv.load_dotenv() client = OpenAI() PASSWORD = os.getenv("PASSWORD", "defaultpass") def authenticate(password): """Check if the entered password is correct.""" if password != PASSWORD: return "❌ Incorrect password! Access denied.", None return None, "✅ Access granted! You may proceed." def analyze_repo(repo_url, role, seniority, assignment_details, focus_areas, password): auth_error, auth_success = authenticate(password) if auth_error: return auth_error, gr.update(visible=False) # If incorrect password, return error """Clone and analyze a GitHub repository with a loading state.""" if not repo_url.startswith("https://github.com/"): return "❌ Invalid GitHub URL!", gr.update(visible=False) # Extract repo details repo_name = repo_url.split("/")[-1] temp_dir = tempfile.mkdtemp() try: progress = gr.update(value="🔄 Cloning repository...", visible=True) # Clone the repo repo_path = os.path.join(temp_dir, repo_name) git.Repo.clone_from(repo_url, repo_path) progress = gr.update(value="📂 Analyzing repository structure...", visible=True) # Gather repository file structure and contents repo_data = "" file_count = 0 # Initialize file counter for root, _, filenames in os.walk(repo_path): for file in filenames: file_count += 1 # Increment file counter file_path = os.path.join(root, file) try: with open(file_path, "r", encoding="utf-8") as f: file_content = f.read() repo_data += f"\n**File {file_count}:** {file_path.replace(repo_path, '')}\n```\n{file_content[:1000]}\n```\n" except Exception: repo_data += f"\n**File {file_count}:** {file_path.replace(repo_path, '')} (⚠️ Cannot read binary file)\n" progress = gr.update(value="🤖 Sending data to AI for evaluation...", visible=True) # AI-based evaluation evaluation = f"✅ **Evaluation for Role: {role}**\n\n" evaluation += f"📂 Repository `{repo_name}` has `{file_count}` files.\n" evaluation += f"💡 Key focus areas: {focus_areas}\n\n" evaluation += "**🔍 Code Quality Analysis:**\n" completion = client.chat.completions.create( model="o1", messages=[ { "role": "user", "content": generate_prompt(repo_url, role, seniority, assignment_details, repo_data, focus_areas) } ] ) evaluation += "\n" + completion.choices[0].message.content progress = gr.update(value="✅ Analysis complete!", visible=True) return evaluation, progress except Exception as e: return f"❌ Error analyzing repository: {str(e)}", gr.update(visible=False) finally: shutil.rmtree(temp_dir) # Cleanup # Gradio UI with gr.Blocks() as app: gr.Markdown("# 🛠️ AI-Powered Candidate Evaluation System") password = gr.Textbox(label="Enter Password") role = gr.Textbox(label="Role the Candidate is Applying For") seniority = gr.Dropdown( ["Junior", "Mid", "Senior"], label="Seniority Level", value="Mid" ) assignment_details = gr.Textbox(label="Assignment Details", lines=8) repo_url = gr.Textbox(label="GitHub Repository URL") focus_areas = gr.Textbox(label="Optional Focus Areas (e.g., Clean Code, Performance)") output = gr.Markdown() progress = gr.Markdown(visible=False) # Loader submit_btn = gr.Button("🔍 Evaluate") submit_btn.click( fn=analyze_repo, inputs=[repo_url, role, seniority, assignment_details, focus_areas, password], outputs=[output, progress] ) app.launch()