Spaces:
Build error
Build error
| import subprocess | |
| import tempfile | |
| import openai | |
| import os | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| openai.api_key = os.getenv("OPENAI_API_KEY") | |
| def review_agent_with_pylint(code): | |
| """ | |
| Review code using pylint. | |
| """ | |
| with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f: | |
| f.write(code) | |
| f.flush() | |
| result = subprocess.run(['pylint', f.name], capture_output=True, text=True) | |
| os.unlink(f.name) | |
| return result.stdout | |
| def review_agent_with_llm(code): | |
| """ | |
| Use an LLM to review the code for logical errors and improvements. | |
| """ | |
| prompt = f"Review the following Python code for logical errors, style, and potential improvements:\n\n```python\n{code}\n```" | |
| response = openai.ChatCompletion.create( | |
| model="gpt-4", | |
| messages=[ | |
| {"role": "system", "content": "You are a code reviewer. Provide feedback on the code."}, | |
| {"role": "user", "content": prompt} | |
| ], | |
| temperature=0.3, | |
| max_tokens=300 | |
| ) | |
| return response.choices[0].message.content | |
| def review_agent(code): | |
| """ | |
| Combine pylint and LLM review. | |
| """ | |
| pylint_output = review_agent_with_pylint(code) | |
| llm_output = review_agent_with_llm(code) | |
| return { | |
| "pylint": pylint_output, | |
| "llm_review": llm_output | |
| } |