Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,20 @@
|
|
1 |
# Autors: [Escriu aquí el teu nom] i [nom del company si en teniu]
|
2 |
# Pràctica 26/05/2025 - Agent amb eines noves: traductor i resumidor
|
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
5 |
import datetime
|
6 |
import requests
|
@@ -8,19 +22,10 @@ import pytz
|
|
8 |
import yaml
|
9 |
from tools.final_answer import FinalAnswerTool
|
10 |
from Gradio_UI import GradioUI
|
11 |
-
|
12 |
-
# Instal·lació automàtica de llibreries
|
13 |
-
import subprocess, sys
|
14 |
-
|
15 |
-
def install(package):
|
16 |
-
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
|
17 |
-
|
18 |
-
install("deep-translator")
|
19 |
-
install("transformers")
|
20 |
|
21 |
# 🔤 Eina 1: Traducció de text
|
22 |
from deep_translator import GoogleTranslator
|
23 |
-
from langchain.tools import Tool
|
24 |
|
25 |
def translate_text(text: str) -> str:
|
26 |
return GoogleTranslator(source='auto', target='en').translate(text)
|
@@ -34,10 +39,10 @@ translate_tool = Tool(
|
|
34 |
# 🧠 Eina 2: Resumidor de text
|
35 |
from transformers import pipeline
|
36 |
|
37 |
-
summarizer = pipeline("summarization")
|
38 |
|
39 |
def summarize_text(text: str) -> str:
|
40 |
-
summary = summarizer(text, max_length=
|
41 |
return summary[0]['summary_text']
|
42 |
|
43 |
summarize_tool = Tool(
|
@@ -46,8 +51,9 @@ summarize_tool = Tool(
|
|
46 |
description="Summarize input text. Use it for long paragraphs or documents."
|
47 |
)
|
48 |
|
49 |
-
# Eina 3: Exemple de tool personalitzada
|
50 |
-
|
|
|
51 |
"""A tool that does nothing yet
|
52 |
Args:
|
53 |
arg1: the first argument
|
@@ -55,7 +61,7 @@ def my_custom_tool(arg1:str, arg2:int)-> str:
|
|
55 |
"""
|
56 |
return "What magic will you build?"
|
57 |
|
58 |
-
# Eina 4: Consultar l
|
59 |
@tool
|
60 |
def get_current_time_in_timezone(timezone: str) -> str:
|
61 |
"""A tool that fetches the current local time in a specified timezone.
|
@@ -76,11 +82,11 @@ final_answer = FinalAnswerTool()
|
|
76 |
model = HfApiModel(
|
77 |
max_tokens=2096,
|
78 |
temperature=0.5,
|
79 |
-
model_id
|
80 |
custom_role_conversions=None,
|
81 |
)
|
82 |
|
83 |
-
# Carrega eina de generació d
|
84 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
85 |
|
86 |
# Carrega plantilles de prompts
|
@@ -88,8 +94,18 @@ with open("prompts.yaml", 'r') as stream:
|
|
88 |
prompt_templates = yaml.safe_load(stream)
|
89 |
|
90 |
# Agent amb totes les eines
|
91 |
-
agent =
|
92 |
-
|
93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
|
|
|
95 |
GradioUI(agent).launch()
|
|
|
1 |
# Autors: [Escriu aquí el teu nom] i [nom del company si en teniu]
|
2 |
# Pràctica 26/05/2025 - Agent amb eines noves: traductor i resumidor
|
3 |
|
4 |
+
# Instal·lació automàtica de llibreries al principi
|
5 |
+
import subprocess
|
6 |
+
import sys
|
7 |
+
|
8 |
+
def install(package):
|
9 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
|
10 |
+
|
11 |
+
# Instalar todas las dependencias primero
|
12 |
+
install("deep-translator")
|
13 |
+
install("transformers")
|
14 |
+
install("langchain")
|
15 |
+
install("sentencepiece") # Necesario para algunos modelos de transformers
|
16 |
+
|
17 |
+
# Ahora importar el resto de módulos
|
18 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
19 |
import datetime
|
20 |
import requests
|
|
|
22 |
import yaml
|
23 |
from tools.final_answer import FinalAnswerTool
|
24 |
from Gradio_UI import GradioUI
|
25 |
+
from langchain.tools import Tool # Ahora debería funcionar
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
# 🔤 Eina 1: Traducció de text
|
28 |
from deep_translator import GoogleTranslator
|
|
|
29 |
|
30 |
def translate_text(text: str) -> str:
|
31 |
return GoogleTranslator(source='auto', target='en').translate(text)
|
|
|
39 |
# 🧠 Eina 2: Resumidor de text
|
40 |
from transformers import pipeline
|
41 |
|
42 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
43 |
|
44 |
def summarize_text(text: str) -> str:
|
45 |
+
summary = summarizer(text, max_length=130, min_length=30, do_sample=False)
|
46 |
return summary[0]['summary_text']
|
47 |
|
48 |
summarize_tool = Tool(
|
|
|
51 |
description="Summarize input text. Use it for long paragraphs or documents."
|
52 |
)
|
53 |
|
54 |
+
# Eina 3: Exemple de tool personalitzada
|
55 |
+
@tool
|
56 |
+
def my_custom_tool(arg1: str, arg2: int) -> str:
|
57 |
"""A tool that does nothing yet
|
58 |
Args:
|
59 |
arg1: the first argument
|
|
|
61 |
"""
|
62 |
return "What magic will you build?"
|
63 |
|
64 |
+
# Eina 4: Consultar l'hora en una zona horària
|
65 |
@tool
|
66 |
def get_current_time_in_timezone(timezone: str) -> str:
|
67 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
82 |
model = HfApiModel(
|
83 |
max_tokens=2096,
|
84 |
temperature=0.5,
|
85 |
+
model_id="mistralai/Mistral-Small-3.1-24B-Instruct-2503",
|
86 |
custom_role_conversions=None,
|
87 |
)
|
88 |
|
89 |
+
# Carrega eina de generació d'imatges des de Hugging Face Hub
|
90 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
91 |
|
92 |
# Carrega plantilles de prompts
|
|
|
94 |
prompt_templates = yaml.safe_load(stream)
|
95 |
|
96 |
# Agent amb totes les eines
|
97 |
+
agent = CodeAgent(
|
98 |
+
model=model,
|
99 |
+
tools=[
|
100 |
+
translate_tool,
|
101 |
+
summarize_tool,
|
102 |
+
my_custom_tool,
|
103 |
+
get_current_time_in_timezone,
|
104 |
+
image_generation_tool,
|
105 |
+
final_answer
|
106 |
+
],
|
107 |
+
prompt_templates=prompt_templates
|
108 |
+
)
|
109 |
|
110 |
+
# Llançar la interfície
|
111 |
GradioUI(agent).launch()
|