Vladt-Tempest commited on
Commit
9646e34
·
1 Parent(s): 64d85a1

Primer Commit

Browse files
Files changed (2) hide show
  1. .gitignore +83 -0
  2. app.py +145 -0
.gitignore ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Environments
2
+ .env
3
+ .venv
4
+ env/
5
+ venv/
6
+ ENV/
7
+ env.bak/
8
+ venv.bak/
9
+
10
+ # Python
11
+ __pycache__/
12
+ *.py[cod]
13
+ *$py.class
14
+ *.so
15
+ .Python
16
+ build/
17
+ develop-eggs/
18
+ dist/
19
+ downloads/
20
+ eggs/
21
+ .eggs/
22
+ lib/
23
+ lib64/
24
+ parts/
25
+ sdist/
26
+ var/
27
+ wheels/
28
+ *.egg-info/
29
+ .installed.cfg
30
+ *.egg
31
+
32
+ # Jupyter Notebook
33
+ .ipynb_checkpoints
34
+
35
+ # VSCode
36
+ .vscode/
37
+ *.code-workspace
38
+
39
+ # PyCharm
40
+ .idea/
41
+
42
+ # Distribution / packaging
43
+ .Python
44
+ build/
45
+ develop-eggs/
46
+ dist/
47
+ downloads/
48
+ eggs/
49
+ .eggs/
50
+ lib/
51
+ lib64/
52
+ parts/
53
+ sdist/
54
+ var/
55
+ wheels/
56
+ *.egg-info/
57
+ .installed.cfg
58
+ *.egg
59
+
60
+ # Logs
61
+ *.log
62
+ logs/
63
+ *.log.*
64
+
65
+ # Local development settings
66
+ .env.local
67
+ .env.development.local
68
+ .env.test.local
69
+ .env.production.local
70
+
71
+ # Database
72
+ *.db
73
+ *.sqlite3
74
+ *.sqlite
75
+
76
+ # OS generated files
77
+ .DS_Store
78
+ .DS_Store?
79
+ ._*
80
+ .Spotlight-V100
81
+ .Trashes
82
+ ehthumbs.db
83
+ Thumbs.db
app.py ADDED
@@ -0,0 +1,145 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import random
3
+ from smolagents import GradioUI, CodeAgent, HfApiModel
4
+ from smolagents import Tool
5
+ from langchain_community.retrievers import BM25Retriever
6
+ from langchain.docstore.document import Document
7
+ import datasets
8
+ from smolagents import DuckDuckGoSearchTool
9
+ from smolagents import Tool
10
+ import random
11
+ from huggingface_hub import list_models
12
+ import os
13
+ from huggingface_hub import login
14
+ from dotenv import load_dotenv
15
+
16
+ # Cargar variables de entorno desde el archivo .env
17
+ load_dotenv()
18
+
19
+ class GuestInfoRetrieverTool(Tool):
20
+ name = "guest_info_retriever"
21
+ description = "Retrieves detailed information about gala guests based on their name or relation."
22
+ inputs = {
23
+ "query": {
24
+ "type": "string",
25
+ "description": "The name or relation of the guest you want information about."
26
+ }
27
+ }
28
+ output_type = "string"
29
+
30
+ def __init__(self, docs):
31
+ self.is_initialized = False
32
+ self.retriever = BM25Retriever.from_documents(docs)
33
+
34
+
35
+ def forward(self, query: str):
36
+ results = self.retriever.get_relevant_documents(query)
37
+ if results:
38
+ return "\n\n".join([doc.page_content for doc in results[:3]])
39
+ else:
40
+ return "No matching guest information found."
41
+
42
+
43
+ def load_guest_dataset():
44
+ # Load the dataset
45
+ guest_dataset = datasets.load_dataset("agents-course/unit3-invitees", split="train")
46
+
47
+ # Convert dataset entries into Document objects
48
+ docs = [
49
+ Document(
50
+ page_content="\n".join([
51
+ f"Name: {guest['name']}",
52
+ f"Relation: {guest['relation']}",
53
+ f"Description: {guest['description']}",
54
+ f"Email: {guest['email']}"
55
+ ]),
56
+ metadata={"name": guest["name"]}
57
+ )
58
+ for guest in guest_dataset
59
+ ]
60
+
61
+ # Return the tool
62
+ return GuestInfoRetrieverTool(docs)
63
+
64
+
65
+ search_tool = DuckDuckGoSearchTool()
66
+
67
+ class WeatherInfoTool(Tool):
68
+ name = "weather_info"
69
+ description = "Fetches dummy weather information for a given location."
70
+ inputs = {
71
+ "location": {
72
+ "type": "string",
73
+ "description": "The location to get weather information for."
74
+ }
75
+ }
76
+ output_type = "string"
77
+
78
+ def forward(self, location: str):
79
+ # Dummy weather data
80
+ weather_conditions = [
81
+ {"condition": "Rainy", "temp_c": 15},
82
+ {"condition": "Clear", "temp_c": 25},
83
+ {"condition": "Windy", "temp_c": 20}
84
+ ]
85
+ # Randomly select a weather condition
86
+ data = random.choice(weather_conditions)
87
+ return f"Weather in {location}: {data['condition']}, {data['temp_c']}°C"
88
+
89
+ class HubStatsTool(Tool):
90
+ name = "hub_stats"
91
+ description = "Fetches the most downloaded model from a specific author on the Hugging Face Hub."
92
+ inputs = {
93
+ "author": {
94
+ "type": "string",
95
+ "description": "The username of the model author/organization to find models from."
96
+ }
97
+ }
98
+ output_type = "string"
99
+
100
+ def forward(self, author: str):
101
+ try:
102
+ # List models from the specified author, sorted by downloads
103
+ models = list(list_models(author=author, sort="downloads", direction=-1, limit=1))
104
+
105
+ if models:
106
+ model = models[0]
107
+ return f"The most downloaded model by {author} is {model.id} with {model.downloads:,} downloads."
108
+ else:
109
+ return f"No models found for author {author}."
110
+ except Exception as e:
111
+ return f"Error fetching models for {author}: {str(e)}"
112
+
113
+
114
+
115
+
116
+ # Initialize the Hugging Face model
117
+ token = os.getenv("HUGGINGFACE_TOKEN")
118
+ if not token:
119
+ raise ValueError("Por favor, configura HUGGINGFACE_TOKEN en el archivo .env")
120
+ login(token=token)
121
+ model = HfApiModel()
122
+
123
+ # Initialize the web search tool
124
+ search_tool = DuckDuckGoSearchTool()
125
+
126
+ # Initialize the weather tool
127
+ weather_info_tool = WeatherInfoTool()
128
+
129
+ # Initialize the Hub stats tool
130
+ hub_stats_tool = HubStatsTool()
131
+
132
+ # Load the guest dataset and initialize the guest info tool
133
+ guest_info_tool = load_guest_dataset()
134
+
135
+ # Create Alfred with all the tools
136
+ alfred = CodeAgent(
137
+ tools=[guest_info_tool, weather_info_tool, hub_stats_tool, search_tool],
138
+ model=model,
139
+ add_base_tools=True, # Add any additional base tools
140
+ planning_interval=3, # Enable planning every 3 steps
141
+ max_steps=5, # Limitar el número máximo de pasos
142
+ )
143
+
144
+ if __name__ == "__main__":
145
+ GradioUI(alfred).launch(share=False)