File size: 9,510 Bytes
314657b |
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
import os
import argparse
import json
import numpy as np
from tqdm import tqdm
import nltk
from nltk.translate.bleu_score import sentence_bleu, SmoothingFunction
from rouge import Rouge
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
import re
from textstat import flesch_reading_ease
from datasets import load_dataset
import openai
from datetime import datetime
nltk.download('punkt', quiet=True)
nltk.download('averaged_perceptron_tagger', quiet=True)
def preprocess(text):
return nltk.word_tokenize(text.lower())
def calculate_bleu(reference, candidate):
reference_tokens = preprocess(reference)
candidate_tokens = preprocess(candidate)
smoothie = SmoothingFunction().method1
return sentence_bleu([reference_tokens], candidate_tokens, smoothing_function=smoothie)
def calculate_rouge(reference, candidate):
rouge = Rouge()
scores = rouge.get_scores(candidate, reference)
return {
'rouge-1': scores[0]['rouge-1']['f'],
'rouge-2': scores[0]['rouge-2']['f'],
'rouge-l': scores[0]['rouge-l']['f']
}
def calculate_cosine_similarity(reference, candidate):
vectorizer = TfidfVectorizer()
tfidf_matrix = vectorizer.fit_transform([reference, candidate])
return cosine_similarity(tfidf_matrix[0:1], tfidf_matrix[1:2])[0][0]
def extract_sections(readme):
sections = []
current_section = ""
for line in readme.split('\n'):
if line.strip().startswith('#'):
if current_section:
sections.append(current_section.strip())
current_section = line + "\n"
else:
current_section += line + "\n"
if current_section:
sections.append(current_section.strip())
return sections
def calculate_structural_similarity(reference, candidate):
ref_sections = extract_sections(reference)
cand_sections = extract_sections(candidate)
section_diff = abs(len(ref_sections) - len(cand_sections))
ref_titles = [s.split('\n')[0] for s in ref_sections]
cand_titles = [s.split('\n')[0] for s in cand_sections]
title_similarity = len(set(ref_titles) & set(cand_titles)) / max(len(ref_titles), len(cand_titles))
return {
'section_difference': section_diff,
'title_similarity': title_similarity
}
def information_retrieval_score(readme):
key_sections = ['installation', 'usage', 'api', 'example', 'license']
found_sections = sum(1 for section in key_sections if section in readme.lower())
return found_sections / len(key_sections)
def code_readme_consistency(repo_content, readme):
code_elements = set(re.findall(r'def\s+(\w+)', repo_content) +
re.findall(r'class\s+(\w+)', repo_content))
mentioned_elements = sum(1 for element in code_elements if element in readme)
return mentioned_elements / len(code_elements) if code_elements else 0
def calculate_readability(text):
return flesch_reading_ease(text) / 100
def evaluate_readme(reference_readme, generated_readme, repo_content):
bleu_score = calculate_bleu(reference_readme, generated_readme)
rouge_scores = calculate_rouge(reference_readme, generated_readme)
cosine_sim = calculate_cosine_similarity(reference_readme, generated_readme)
structural_sim = calculate_structural_similarity(reference_readme, generated_readme)
info_retrieval = information_retrieval_score(generated_readme)
code_consistency = code_readme_consistency(repo_content, generated_readme)
readability = calculate_readability(generated_readme)
weights = {
'bleu': 0.1,
'rouge-1': 0.1,
'rouge-2': 0.1,
'rouge-l': 0.1,
'cosine_similarity': 0.1,
'structural_similarity': 0.1,
'information_retrieval': 0.15,
'code_consistency': 0.15,
'readability': 0.1
}
weighted_score = (
weights['bleu'] * bleu_score +
weights['rouge-1'] * rouge_scores['rouge-1'] +
weights['rouge-2'] * rouge_scores['rouge-2'] +
weights['rouge-l'] * rouge_scores['rouge-l'] +
weights['cosine_similarity'] * cosine_sim +
weights['structural_similarity'] * structural_sim['title_similarity'] +
weights['information_retrieval'] * info_retrieval +
weights['code_consistency'] * code_consistency +
weights['readability'] * readability
)
return {
'bleu': bleu_score,
'rouge': rouge_scores,
'cosine_similarity': cosine_sim,
'structural_similarity': structural_sim,
'information_retrieval': info_retrieval,
'code_consistency': code_consistency,
'readability': readability,
'weighted_score': weighted_score
}
def generate_readme(repo_content, model, client):
system_prompt = """You are an AI assistant tasked with creating a README.md file for a GitHub repository.
Your response should contain ONLY the content of the README.md file, without any additional explanations or markdown code blocks.
The README should include the following sections:
1. Project Title
2. Description
3. Installation
4. Usage
5. Features
6. Contributing
7. License
Ensure that your response is well-structured, informative, and directly usable as a README.md file."""
user_prompt = f"Here is the content of the repository:\n\n{repo_content}\n\nBased on this content, please generate a README.md file."
response = client.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt}
]
)
return response.choices[0].message.content
def main(args):
openai.api_key = os.getenv("OPENAI_API_KEY")
if not openai.api_key:
raise ValueError("OPENAI_API_KEY environment variable is not set")
client = openai.OpenAI(base_url=args.base_url) if args.base_url else openai.OpenAI()
dataset = load_dataset("patched-codes/generate-readme-eval")
results = []
for item in tqdm(dataset['test'], desc="Processing repos"):
try:
generated_readme = generate_readme(item['repo_content'], args.model, client)
eval_result = evaluate_readme(item['repo_readme'], generated_readme, item['repo_content'])
# Add repo_name to the eval_result
eval_result['repo_name'] = item['repo_name']
results.append(eval_result)
except Exception as e:
print(f"Error processing repo {item['repo_name']}: {e}")
continue
average_scores = {
'bleu': np.mean([r['bleu'] for r in results]),
'rouge-1': np.mean([r['rouge']['rouge-1'] for r in results]),
'rouge-2': np.mean([r['rouge']['rouge-2'] for r in results]),
'rouge-l': np.mean([r['rouge']['rouge-l'] for r in results]),
'cosine_similarity': np.mean([r['cosine_similarity'] for r in results]),
'title_similarity': np.mean([r['structural_similarity']['title_similarity'] for r in results]),
'information_retrieval': np.mean([r['information_retrieval'] for r in results]),
'code_consistency': np.mean([r['code_consistency'] for r in results]),
'readability': np.mean([r['readability'] for r in results]),
'weighted_score': np.mean([r['weighted_score'] for r in results])
}
# Print results to console
print("\nEvaluation Results:")
for metric, score in average_scores.items():
print(f"{metric}: {score:.4f}")
# Save results to log file
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
log_filename = f"{args.model}_results_{timestamp}.log"
with open(log_filename, 'w') as log_file:
log_file.write(f"Evaluation Results for model: {args.model}\n")
log_file.write(f"Timestamp: {timestamp}\n\n")
log_file.write("Average Scores:\n")
for metric, score in average_scores.items():
log_file.write(f"{metric}: {score:.4f}\n")
log_file.write(f"\nDetailed Results:\n")
for result in results:
log_file.write(f"\nRepository: {result['repo_name']}\n")
log_file.write("Scores:\n")
log_file.write(f" BLEU: {result['bleu']:.4f}\n")
log_file.write(f" ROUGE-1: {result['rouge']['rouge-1']:.4f}\n")
log_file.write(f" ROUGE-2: {result['rouge']['rouge-2']:.4f}\n")
log_file.write(f" ROUGE-L: {result['rouge']['rouge-l']:.4f}\n")
log_file.write(f" Cosine Similarity: {result['cosine_similarity']:.4f}\n")
log_file.write(f" Title Similarity: {result['structural_similarity']['title_similarity']:.4f}\n")
log_file.write(f" Information Retrieval: {result['information_retrieval']:.4f}\n")
log_file.write(f" Code Consistency: {result['code_consistency']:.4f}\n")
log_file.write(f" Readability: {result['readability']:.4f}\n")
log_file.write(f" Weighted Score: {result['weighted_score']:.4f}\n")
print(f"\nResults saved to {log_filename}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Generate and evaluate README files using OpenAI API")
parser.add_argument("model", help="OpenAI model to use")
parser.add_argument("--base_url", help="Optional base URL for OpenAI API", default=None)
args = parser.parse_args()
main(args)
|