Spaces:
Sleeping
Sleeping
import openai | |
import os | |
def analyze_code(code: str) -> str: | |
""" | |
Uses OpenAI's GPT-4.1 mini model to analyze the given code. | |
Returns the analysis as a string. | |
""" | |
system_prompt = "You are a helpful assistant. Analyze the code given to you. Provide insights, strengths, weaknesses, and suggestions for improvement." | |
response = openai.ChatCompletion.create( | |
model="gpt-4.1-mini", # GPT-4.1 mini | |
messages=[ | |
{"role": "system", "content": system_prompt}, | |
{"role": "user", "content": code} | |
], | |
max_tokens=512, | |
temperature=0.7 | |
) | |
return response.choices[0].message["content"] | |
def combine_repo_files_for_llm(repo_dir="repo_files", output_file="combined_repo.txt"): | |
""" | |
Combines all .py and .md files in the given directory (recursively) into a single text file. | |
Returns the path to the combined file. | |
""" | |
combined_content = [] | |
for root, _, files in os.walk(repo_dir): | |
for file in files: | |
if file.endswith(".py") or file.endswith(".md"): | |
file_path = os.path.join(root, file) | |
try: | |
with open(file_path, "r", encoding="utf-8") as f: | |
combined_content.append(f"\n# ===== File: {file} =====\n") | |
combined_content.append(f.read()) | |
except Exception as e: | |
combined_content.append(f"\n# Could not read {file_path}: {e}\n") | |
with open(output_file, "w", encoding="utf-8") as out_f: | |
out_f.write("\n".join(combined_content)) | |
return output_file | |
def analyze_combined_file(output_file="combined_repo.txt"): | |
""" | |
Reads the combined file and passes its contents to analyze_code, returning the LLM's output. | |
""" | |
try: | |
with open(output_file, "r", encoding="utf-8") as f: | |
code = f.read() | |
return analyze_code(code) | |
except Exception as e: | |
return f"Error analyzing combined file: {e}" | |