File size: 4,002 Bytes
575b676 b4d9c2a 970fab8 575b676 dd96bb5 1649492 575b676 5010418 bd3af1b 575b676 eace5b7 855934d 575b676 c28f0b4 575b676 5010418 bd3af1b 575b676 05814ee bd3af1b 575b676 9203a57 575b676 547f951 ba781d3 757a638 ba781d3 445631a 05814ee 28a4907 1a7ab6b fc0da4b eace5b7 068bd8f eace5b7 1a7ab6b fc0da4b eace5b7 068bd8f 70dfe2a e678952 982854b 1a7ab6b 28a4907 db816cc 575b676 27b47c1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
import gradio as gr
import random
from smolagents import GradioUI, CodeAgent, InferenceClientModel
from smolagents import LiteLLMModel
from smolagents import ToolCallingAgent, PythonInterpreterTool, PromptTemplates
import os
from PIL import Image
#from smolagents.src.smolagents.prompts
# Import our custom tools from their modules
from tools import evaluate_consumption, evaluate_consumption_example
from retriever import FrugalAI_methods
#HF_TOKEN = os.environ.get("HF_TOKEN")
try:
API_KEY = os.environ.get("API_KEY")
except:
print("Please provide the Anthropic API_KEY in the secret variables")
# Initialize the Hugging Face model
model = LiteLLMModel(
model_id="anthropic/claude-3-5-sonnet-latest",
temperature=0.2,
api_key=API_KEY
)
evaluate_consumption = evaluate_consumption()
evaluate_consumption_example = evaluate_consumption_example()
FrugalAI_methods=FrugalAI_methods()
# Create agent with all the tools
fruggy = CodeAgent(
tools=[evaluate_consumption, evaluate_consumption_example, FrugalAI_methods],
model=model,
additional_authorized_imports=['os'],
add_base_tools=True, # Add any additional base tools
planning_interval=10
)
with gr.Blocks(theme=gr.themes.Ocean()) as demo:
with gr.Row():
with gr.Column(scale=1):
image = gr.Image(value=Image.open("frugal.jpg"), label="Frugal Image")
hello = gr.Interface(
fn=fruggy,
inputs="text",
outputs="text",
title="Frugalize it!",
examples = [
"You are an AI agent that receives Python code from a manager. "
"Your task is to test its resource consumption using a custom tool. If no code is provided, use evaluate_consumption_example. If a code is provided by the manager, use evaluate_consumption."
"Then, propose frugal alternatives, such as pruning, using your dedicated tool β always use it. "
"Additionally, feel free to search the internet for frugal methods and present them to the manager. "
"Examples include Knowledge Distillation, Transfer Learning, and others. "
"Show me what you can do using your custom tool evaluate_consumption_example, and provide examples based on the code in that tool to optimize it for frugality. "
"Use both your tools and web searches if needed. If implementation guidance is given by your custom tools, always give it in your final answer to the manager.",
"You are an AI agent that receives Python code from a manager. "
"Your task is to test its resource consumption using a custom tool. If no code is provided, use evaluate_consumption_example. If a code is provided by the manager, use evaluate_consumption."
"Then, propose frugal alternatives, such as pruning, using your dedicated tool β always use it. "
"Additionally, feel free to search the internet for frugal methods and present them to the manager. "
"Examples include Knowledge Distillation, Transfer Learning, and others. If implementation guidance is given by your custom tools, always give it in your final answer to the manager. "
"Here is my code: "
"import anthropic "
"API_KEY = os.environ.get('API_KEY') "
"client = anthropic.Anthropic(api_key=API_KEY) "
"message = client.messages.create(model='claude-sonnet-4-20250514',max_tokens=1024,messages=[{'role': 'user', 'content': 'Hello, Claude'}]) "
"print(message.content) "
"Please give me frugal alternatives."
],
description="Share your Python code with this AI agent! It will track its CO2 emissions using CodeCarbon and recommend greener, frugal AI alternatives."
)
if __name__ == "__main__":
demo.launch(mcp_server=True, share=True) |