KebabLover commited on
Commit
6d855d2
·
verified ·
1 Parent(s): 9dc831a

Mise à jour de app.py

Browse files
Files changed (1) hide show
  1. app.py +136 -0
app.py ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
+ import datetime
3
+ import requests
4
+ import pytz
5
+ import yaml
6
+ import os
7
+ import sys
8
+ import subprocess # Ajout de l'import manquant pour ShellCommandTool
9
+ import io
10
+ import json
11
+ from huggingface_hub import HfApi
12
+ from tools.final_answer import FinalAnswerTool
13
+ from tools.visit_webpage import VisitWebpageTool
14
+ from tools.web_search import DuckDuckGoSearchTool
15
+ from Gradio_UI import GradioUI
16
+ from smolagents.models import OpenAIServerModel
17
+ from tools.create_file_tool import CreateFileTool
18
+ from tools.modify_file_tool import ModifyFileTool
19
+
20
+ # Below is an example of a tool that does nothing. Amaze us with your creativity !
21
+ @tool
22
+ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
23
+ #Keep this format for the description / args / args description but feel free to modify the tool
24
+ """A tool that does nothing yet
25
+ Args:
26
+ arg1: the first argument
27
+ arg2: the second argument
28
+ """
29
+ return "What magic will you build ?"
30
+
31
+ # Below is an example of a tool that does nothing. Amaze us with your creativity !
32
+ @tool
33
+ def get_current_realtime()-> str: #it's import to specify the return type
34
+ #Keep this format for the description / args / args description but feel free to modify the tool
35
+ """A tool that get the current realtime
36
+ """
37
+ return datetime.datetime.now()
38
+ @tool
39
+ def get_current_time_in_timezone(timezone: str) -> str:
40
+ """A tool that fetches the current local time in a specified timezone.
41
+ Args:
42
+ timezone: A string representing a valid timezone (e.g., 'America/New_York').
43
+ """
44
+ try:
45
+ # Create timezone object
46
+ tz = pytz.timezone(timezone)
47
+ # Get current time in that timezone
48
+ local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
49
+ return f"The current local time in {timezone} is: {local_time}"
50
+ except Exception as e:
51
+ return f"Error fetching time for timezone '{timezone}': {str(e)}"
52
+
53
+
54
+ final_answer = FinalAnswerTool()
55
+
56
+ # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
57
+ # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud' è
58
+
59
+ # model = HfApiModel(
60
+ # model_id="http://192.168.1.141:1234/v1",
61
+ # max_new_tokens=2096,
62
+ # temperature=0.5
63
+ # )
64
+ # Configuration du modèle pour se connecter au LLM hébergé localement via LMStudio
65
+ model = OpenAIServerModel(
66
+ api_base ="http://192.168.1.141:1234/v1",
67
+ model_id="Qwen/Qwen2.5-Coder-14B-Instruct-GGUF", # Nom arbitraire pour le modèle local
68
+ api_key="sk-dummy-key" # Clé factice pour LMStudio
69
+ # max_tokens=2096,
70
+
71
+ )
72
+
73
+ # Import tool from Hub
74
+ image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
75
+
76
+ with open("prompts.yaml", 'r') as stream:
77
+ prompt_templates = yaml.safe_load(stream)
78
+
79
+ # Tentative de correction pour ShellCommandTool
80
+ try:
81
+ from tools.shell_tool import ShellCommandTool
82
+ shell_tool = ShellCommandTool()
83
+ except Exception as e:
84
+ print(f"Erreur lors du chargement de ShellCommandTool: {e}")
85
+ # Créer une version simplifiée de l'outil si nécessaire
86
+ shell_tool = None
87
+
88
+ agent = CodeAgent(
89
+ model=model,
90
+ tools=[final_answer, DuckDuckGoSearchTool(), VisitWebpageTool(), CreateFileTool(), ModifyFileTool()],
91
+ max_steps=6,
92
+ verbosity_level=1,
93
+ grammar=None,
94
+ planning_interval=None,
95
+ name=None,
96
+ description=None,
97
+ prompt_templates=prompt_templates
98
+ )
99
+
100
+ # Ajouter ShellCommandTool conditionnellement
101
+ if shell_tool is not None:
102
+ agent.tools['shell_command'] = shell_tool
103
+
104
+ # Sauvegarder manuellement sans utiliser to_dict() pour éviter les erreurs de validation
105
+ agent_data = {
106
+ "name": agent.name,
107
+ "description": agent.description,
108
+ "model": agent.model.to_dict() if hasattr(agent.model, "to_dict") else str(agent.model),
109
+ "tools": [tool.__class__.__name__ for tool in agent.tools.values()],
110
+ "max_steps": agent.max_steps,
111
+ "grammar": agent.grammar,
112
+ "planning_interval": agent.planning_interval,
113
+ }
114
+
115
+ # Sauvegarder l'agent au format JSON personnalisé
116
+ with open("agent.json", "w", encoding="utf-8") as f:
117
+ json.dump(agent_data, f, ensure_ascii=False, indent=2)
118
+
119
+ # La méthode push_to_hub pose problème avec les emojis, utiliser plutôt le script push_to_hf.py
120
+ print("Agent sauvegardé dans agent.json. Utilisez push_to_hf.py pour le pousser sur Hugging Face.")
121
+
122
+ # Utiliser l'API Hugging Face directement avec encodage UTF-8
123
+ # try:
124
+ # api = HfApi()
125
+ # api.upload_file(
126
+ # path_or_fileobj="agent.json",
127
+ # path_in_repo="agent.json",
128
+ # repo_id="KebabLover/SmolCoderAgent_0_1",
129
+ # repo_type="space",
130
+ # commit_message="Mise à jour de l'agent"
131
+ # )
132
+ # print("Agent poussé avec succès vers Hugging Face!")
133
+ # except Exception as e:
134
+ # print(f"Erreur lors du push vers Hugging Face: {e}")
135
+
136
+ # GradioUI(agent).launch()