Update app.py
Browse files
app.py
CHANGED
|
@@ -1,87 +1,70 @@
|
|
| 1 |
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
from smolagents import CodeAgent, HfApiModel, tool
|
| 7 |
-
|
| 8 |
-
# ------------------------------------------------------------------------------
|
| 9 |
-
# Import YAML to load extra instructions from a file.
|
| 10 |
import yaml
|
| 11 |
-
|
| 12 |
-
# ------------------------------------------------------------------------------
|
| 13 |
-
# Import our helper modules.
|
| 14 |
-
# - FinalAnswerTool: Packages the final answer for the user.
|
| 15 |
-
# - GradioUI: Creates a simple webpage so users can interact with our agent.
|
| 16 |
from tools.final_answer import FinalAnswerTool
|
| 17 |
-
from Gradio_UI import GradioUI
|
| 18 |
|
| 19 |
-
|
| 20 |
-
# Set up our AI model.
|
| 21 |
-
# The model processes text and returns answers.
|
| 22 |
-
model = HfApiModel(
|
| 23 |
-
max_tokens=2096, # Maximum length of the answer.
|
| 24 |
-
temperature=0.5, # How creative or focused the answer is.
|
| 25 |
-
# Use this endpoint as our model's address.
|
| 26 |
-
model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',
|
| 27 |
-
# (To use the original model instead, change the model_id to 'Qwen/Qwen2.5-Coder-32B-Instruct')
|
| 28 |
-
custom_role_conversions=None
|
| 29 |
-
)
|
| 30 |
|
| 31 |
-
#
|
| 32 |
-
# Define the simplify_text tool.
|
| 33 |
-
# This tool takes technical text and asks the model to convert it into plain language.
|
| 34 |
-
# The prompt instructs the model to break down complex ideas into short sentences and to define any technical terms in simple words.
|
| 35 |
@tool
|
| 36 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
"""
|
| 38 |
-
|
| 39 |
-
It explains technical terms using simple definitions (in parentheses)
|
| 40 |
-
and breaks complex ideas into short, clear sentences.
|
| 41 |
|
|
|
|
|
|
|
|
|
|
| 42 |
Args:
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
Returns:
|
| 46 |
-
A simplified explanation of the text.
|
| 47 |
"""
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
f"
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
# ------------------------------------------------------------------------------
|
| 61 |
-
# Load additional instructions from the prompts.yaml file.
|
| 62 |
-
# This file contains extra guidance for how the model should behave.
|
| 63 |
with open("prompts.yaml", 'r') as stream:
|
| 64 |
prompt_templates = yaml.safe_load(stream)
|
| 65 |
-
|
| 66 |
-
# ------------------------------------------------------------------------------
|
| 67 |
-
# Create our AI agent by combining the model and our tools.
|
| 68 |
-
# Our agent uses:
|
| 69 |
-
# - FinalAnswerTool to package the final answer.
|
| 70 |
-
# - simplify_text to convert technical text into plain language.
|
| 71 |
agent = CodeAgent(
|
| 72 |
model=model,
|
| 73 |
-
tools=[
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
| 79 |
prompt_templates=prompt_templates
|
| 80 |
)
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
# GradioUI creates a webpage so users can type in technical text and see the simplified version.
|
| 85 |
-
# The launch command includes share=True so the app remains active with a public URL.
|
| 86 |
-
if __name__ == "__main__":
|
| 87 |
-
GradioUI(agent).launch(server_name="0.0.0.0", server_port=7860, share=True)
|
|
|
|
| 1 |
|
| 2 |
+
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
| 3 |
+
import datetime
|
| 4 |
+
import requests
|
| 5 |
+
import pytz
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
import yaml
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
| 8 |
|
| 9 |
+
from Gradio_UI import GradioUI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
|
|
|
|
|
|
|
|
|
| 12 |
@tool
|
| 13 |
+
def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
|
| 14 |
+
#Keep this format for the description / args / args description but feel free to modify the tool
|
| 15 |
+
"""A tool that does nothing yet
|
| 16 |
+
Args:
|
| 17 |
+
arg1: the first argument
|
| 18 |
+
arg2: the second argument
|
| 19 |
"""
|
| 20 |
+
return "What magic will you build ?"
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
@tool
|
| 23 |
+
def get_current_time_in_timezone(timezone: str) -> str:
|
| 24 |
+
"""A tool that fetches the current local time in a specified timezone.
|
| 25 |
Args:
|
| 26 |
+
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
|
|
|
|
|
|
|
|
|
| 27 |
"""
|
| 28 |
+
try:
|
| 29 |
+
# Create timezone object
|
| 30 |
+
tz = pytz.timezone(timezone)
|
| 31 |
+
# Get current time in that timezone
|
| 32 |
+
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 33 |
+
return f"The current local time in {timezone} is: {local_time}"
|
| 34 |
+
except Exception as e:
|
| 35 |
+
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
final_answer = FinalAnswerTool()
|
| 39 |
+
|
| 40 |
+
# 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:
|
| 41 |
+
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 42 |
+
|
| 43 |
+
model = HfApiModel(
|
| 44 |
+
max_tokens=2096,
|
| 45 |
+
temperature=0.5,
|
| 46 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
|
| 47 |
+
custom_role_conversions=None,
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
# Import tool from Hub
|
| 52 |
+
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 53 |
|
|
|
|
|
|
|
|
|
|
| 54 |
with open("prompts.yaml", 'r') as stream:
|
| 55 |
prompt_templates = yaml.safe_load(stream)
|
| 56 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
agent = CodeAgent(
|
| 58 |
model=model,
|
| 59 |
+
tools=[final_answer], ## add your tools here (don't remove final answer)
|
| 60 |
+
max_steps=6,
|
| 61 |
+
verbosity_level=1,
|
| 62 |
+
grammar=None,
|
| 63 |
+
planning_interval=None,
|
| 64 |
+
name=None,
|
| 65 |
+
description=None,
|
| 66 |
prompt_templates=prompt_templates
|
| 67 |
)
|
| 68 |
|
| 69 |
+
|
| 70 |
+
GradioUI(agent).launch()
|
|
|
|
|
|
|
|
|
|
|
|