DhirajSuryawanshi commited on
Commit
2b328ca
·
verified ·
1 Parent(s): 7be8892

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -24
app.py CHANGED
@@ -8,39 +8,43 @@ from tools.final_answer import FinalAnswerTool
8
  from Gradio_UI import GradioUI
9
 
10
  # Below is an example of a tool that analyze code. !
 
 
 
11
  @tool
12
  def analyze_code(code: str, language: str) -> str:
13
- """Analyzes the given code for errors or mistakes and provides feedback.
14
 
15
  Args:
16
  code: The source code to analyze.
17
- language: The programming language of the code (e.g., Java, C++, Python).
18
 
19
  Returns:
20
- A string containing detected issues or confirming that no issues were found.
21
  """
22
- if language.lower() not in ["java", "c++", "python"]:
23
- return "Currently, only Java, C++, and Python are supported."
24
-
25
- try:
26
- if language.lower() == "python":
27
- import ast
28
- try:
29
- ast.parse(code)
30
- return "No syntax errors detected in the Python code."
31
- except SyntaxError as e:
32
- return f"Python Syntax Error: {e}"
33
-
34
- elif language.lower() == "java":
35
- return "Java code analysis requires an external compiler or linter. Consider using a Java compiler like javac for syntax checking."
36
-
37
- elif language.lower() == "c++":
38
- return "C++ code analysis requires an external compiler like g++ or a static analysis tool such as Clang-Tidy."
39
-
40
- except Exception as e:
41
- return f"Error during code analysis: {e}"
42
 
43
- return "Code analysis completed."
 
44
 
45
 
46
  final_answer = FinalAnswerTool()
 
8
  from Gradio_UI import GradioUI
9
 
10
  # Below is an example of a tool that analyze code. !
11
+ import subprocess
12
+ import ast
13
+
14
  @tool
15
  def analyze_code(code: str, language: str) -> str:
16
+ """Analyzes code for errors in Java, C++, and Python.and find any programming mistakes and fix it and give output in detailed and precise manner and start with Prefix: Hello Iam Dhiraj Suryawanshi's Nusti_Coding model XD
17
 
18
  Args:
19
  code: The source code to analyze.
20
+ language: The programming language (Java, C++, Python).
21
 
22
  Returns:
23
+ A string containing detected errors or confirming no issues.
24
  """
25
+ language = language.lower()
26
+
27
+ if language == "python":
28
+ try:
29
+ ast.parse(code) # Check Python syntax
30
+ return "Python code has no syntax errors."
31
+ except SyntaxError as e:
32
+ return f"Python Syntax Error: {e}"
33
+
34
+ elif language == "java":
35
+ with open("Test.java", "w") as f:
36
+ f.write(code)
37
+ result = subprocess.run(["javac", "Test.java"], capture_output=True, text=True)
38
+ return "No Java errors found." if result.returncode == 0 else f"Java Errors:\n{result.stderr}"
39
+
40
+ elif language == "c++":
41
+ with open("test.cpp", "w") as f:
42
+ f.write(code)
43
+ result = subprocess.run(["g++", "test.cpp", "-o", "test.out"], capture_output=True, text=True)
44
+ return "No C++ errors found." if result.returncode == 0 else f"C++ Errors:\n{result.stderr}"
45
 
46
+ else:
47
+ return "Supported languages: Java, C++, Python."
48
 
49
 
50
  final_answer = FinalAnswerTool()