ulasdesouza commited on
Commit
2ae2a01
·
verified ·
1 Parent(s): 6a9c194

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -49
app.py CHANGED
@@ -1,56 +1,64 @@
1
- from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
  import datetime
3
  import requests
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
7
- from duckduckgo_search import DDGS
8
- import re
9
  from Gradio_UI import GradioUI
10
-
 
11
 
 
12
  @tool
13
- def get_top_places(city: str, category: str = "places", n: int = 5, budget: bool = False) -> list:
14
- """
15
- Get top places based on city and category using DuckDuckGo search.
16
-
17
  Args:
18
- city: The name of the city to search in.
19
- category: The search category ("places", "food", or "museum").
20
- n: The number of results to return.
21
- budget: If True, modifies the query to focus on budget friendly options.
22
-
23
  Returns:
24
- A list of strings, each formatted as "title - URL".
25
  """
26
- valid_categories = ["places", "food", "museum"]
27
- if category not in valid_categories:
28
- raise ValueError("Invalid category. Choose from: places, food, museum.")
29
-
30
- # Modify the query if budget-friendly options are desired.
31
- if budget:
32
- query = f"top {n} budget friendly {category} in {city}"
33
- else:
34
- query = f"top {n} {category} in {city}"
35
-
36
- with DDGS() as ddgs:
37
- results = ddgs.text(query, max_results=n)
38
- return [f"{result['title']} - {result['href']}" for result in results]
39
 
40
- @tool
41
- def get_current_time_in_timezone(timezone: str) -> str:
42
- """A tool that fetches the current local time in a specified timezone.
43
- Args:
44
- timezone: A string representing a valid timezone (e.g., 'America/New_York').
45
- """
46
- try:
47
- # Create timezone object
48
- tz = pytz.timezone(timezone)
49
- # Get current time in that timezone
50
- local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
51
- return f"The current local time in {timezone} is: {local_time}"
52
- except Exception as e:
53
- return f"Error fetching time for timezone '{timezone}': {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
 
56
  final_answer = FinalAnswerTool()
@@ -59,13 +67,12 @@ final_answer = FinalAnswerTool()
59
  # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
60
 
61
  model = HfApiModel(
62
- max_tokens=2096,
63
- temperature=0.5,
64
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
65
- custom_role_conversions=None,
66
  )
67
 
68
-
69
  # Import tool from Hub
70
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
71
 
@@ -74,8 +81,8 @@ with open("prompts.yaml", 'r') as stream:
74
 
75
  agent = CodeAgent(
76
  model=model,
77
- tools=[final_answer,get_top_places], ## add your tools here (don't remove final answer)
78
- max_steps=5,
79
  verbosity_level=1,
80
  grammar=None,
81
  planning_interval=None,
@@ -84,5 +91,4 @@ agent = CodeAgent(
84
  prompt_templates=prompt_templates
85
  )
86
 
87
-
88
  GradioUI(agent).launch()
 
1
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
2
  import datetime
3
  import requests
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
 
 
7
  from Gradio_UI import GradioUI
8
+ import subprocess
9
+ import ast
10
 
11
+ # Below is a tool that analyze code and Find mistakes and fix it named as Nusti_Coding_Model By Dhiraj Suryawanshi XD!
12
  @tool
13
+ def analyze_code(code: str, language: str) -> str:
14
+ """Analyzes code for errors in MATLAB, Java, C++, and Python, finds programming mistakes,
15
+ fixes them, and provides detailed and precise output.
16
+ use prefix: Hello, I am Dhiraj Suryawanshi's Nusti_Coding model XD
17
  Args:
18
+ code: The source code to analyze.
19
+ language: The programming language (MATLAB, Java, C++, Python).
 
 
 
20
  Returns:
21
+ A string containing detected errors or confirming no issues.
22
  """
23
+ language = language.lower()
24
+ prefix = "Hello, I am Dhiraj Suryawanshi's Nusti_Coding model XD\n"
 
 
 
 
 
 
 
 
 
 
 
25
 
26
+ if language == "python":
27
+ try:
28
+ ast.parse(code) # Check Python syntax
29
+ return prefix + "✅ Python code has no syntax errors."
30
+ except SyntaxError as e:
31
+ return prefix + f"❌ Python Syntax Error: {e}"
32
+
33
+ elif language == "java":
34
+ with open("Test.java", "w") as f:
35
+ f.write(code)
36
+ result = subprocess.run(["javac", "Test.java"], capture_output=True, text=True)
37
+ if result.returncode == 0:
38
+ return prefix + "✅ No Java errors found."
39
+ else:
40
+ return prefix + f"❌ Java Errors:\n{result.stderr}"
41
+
42
+ elif language == "c++":
43
+ with open("test.cpp", "w") as f:
44
+ f.write(code)
45
+ result = subprocess.run(["g++", "test.cpp", "-o", "test.out"], capture_output=True, text=True)
46
+ if result.returncode == 0:
47
+ return prefix + "✅ No C++ errors found."
48
+ else:
49
+ return prefix + f"❌ C++ Errors:\n{result.stderr}"
50
+
51
+ elif language == "matlab":
52
+ with open("test.m", "w") as f:
53
+ f.write(code)
54
+ result = subprocess.run(["matlab", "-batch", "test"], capture_output=True, text=True)
55
+ if result.returncode == 0:
56
+ return prefix + "✅ No MATLAB errors found."
57
+ else:
58
+ return prefix + f"❌ MATLAB Errors:\n{result.stderr}"
59
+
60
+ else:
61
+ return prefix + "⚠️ Supported languages: MATLAB, Java, C++, Python."
62
 
63
 
64
  final_answer = FinalAnswerTool()
 
67
  # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
68
 
69
  model = HfApiModel(
70
+ max_tokens=2096,
71
+ temperature=0.5,
72
+ model_id='Qwen/Qwen2.5-Coder-32B-Instruct', # it is possible that this model may be overloaded
73
+ custom_role_conversions=None,
74
  )
75
 
 
76
  # Import tool from Hub
77
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
78
 
 
81
 
82
  agent = CodeAgent(
83
  model=model,
84
+ tools=[final_answer], ## add your tools here (don't remove final answer)
85
+ max_steps=6,
86
  verbosity_level=1,
87
  grammar=None,
88
  planning_interval=None,
 
91
  prompt_templates=prompt_templates
92
  )
93
 
 
94
  GradioUI(agent).launch()