File size: 1,517 Bytes
c89c44a 10e9b7d c89c44a e80aab9 c89c44a 31243f4 c89c44a 31243f4 c89c44a 3c4371f c89c44a eccf8e4 c89c44a 7d65c66 c89c44a 31243f4 c89c44a e80aab9 c89c44a e80aab9 c89c44a e514fd7 c89c44a e514fd7 c89c44a e80aab9 c89c44a |
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 |
from langchain_huggingface import HuggingFacePipeline
from transformers import pipeline
from langchain.agents import initialize_agent
from langchain.tools import Tool
from langchain_community.tools import DuckDuckGoSearchRun
from datetime import datetime
import pytz
import gradio as gr
# --- Tools ---
search_web = DuckDuckGoSearchRun()
search_tool = Tool("web_search", search_web.invoke, "Look up current info")
def calculator(query: str) -> str:
try:
return str(eval(query, {"__builtins__": None}, {}))
except Exception as e:
return f"Error: {e}"
calculator_tool = Tool("calculator_tool", calculator, "Arithmetic calculator")
def get_time(location: str) -> str:
try:
tz = pytz.timezone(location)
return datetime.now(tz).strftime("The current time in %Z is %H:%M:%S.")
except Exception as e:
return f"Error: {e}"
time_tool = Tool("time_tool", get_time, "Time in a timezone")
tools = [search_tool, calculator_tool, time_tool]
# --- LLM ---
hf_pipeline = pipeline("text-generation", model="distilgpt2")
llm = HuggingFacePipeline(pipeline=hf_pipeline)
# --- Agent ---
agent = initialize_agent(
tools,
llm,
agent_type="chat-conversational-react-description",
verbose=True
)
# --- Gradio ---
def alfred_chat(user_input: str):
return agent.run(user_input)
iface = gr.Interface(
fn=alfred_chat,
inputs=gr.Textbox(lines=2, placeholder="Ask Alfred something..."),
outputs="text"
)
if __name__ == "__main__":
iface.launch()
|