File size: 2,884 Bytes
0ec6f26
4539a09
 
 
 
 
 
 
 
 
1aa6f8f
4539a09
0ec6f26
 
 
 
4539a09
 
 
 
 
 
 
 
 
1aa6f8f
4539a09
 
1aa6f8f
4539a09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import os
import yaml
import gradio as gr

from smolagents import CodeAgent, HfApiModel

from tools.party_theme_tool import PartyThemeTool
from tools.search import search
from tools.categorize_occasion import categorize_occasion
from tools.estimate_preparation_time import estimate_preparation_time
from tools.web_search import DuckDuckGoSearchTool as WebSearch

from huggingface_hub import login

login(token=os.getenv('HF_TOKEN'))

model = HfApiModel(
    model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
    provider=None,
)


with open("prompts.yaml", "r") as stream:
    prompt_templates = yaml.safe_load(stream)

web_search = WebSearch()

agent = CodeAgent(
    tools=[categorize_occasion, search, web_search, estimate_preparation_time, PartyThemeTool()],
    model=model,
    managed_agents=[],
    max_steps=10,
    verbosity_level=2,
    grammar=None,
    planning_interval=None,
    description=None,
    prompt_templates=prompt_templates,
)


def agent_interface(user_input):
    """
    This function defines the interaction with the agent.

    Args:
        user_input: The input provided by the user in the Gradio interface.

    Returns:
        The agent's response, formatted for readability.
    """
    try:
        response = agent.run(user_input)

        if isinstance(response, dict):
            formatted_response = ""
            for key, value in response.items():
                formatted_response += f"{key.upper()}\n\n"
                if isinstance(value, dict):
                    for nested_key, nested_value in value.items():
                        if isinstance(nested_value, list):
                            nested_value = "\n  - " + "\n  - ".join(nested_value)
                        formatted_response += (
                            f"- {nested_key.capitalize()}:\n  {nested_value}\n\n"
                        )

                elif isinstance(value, list):
                    formatted_response += "\n  - " + "\n  - ".join(value) + "\n\n"

                else:
                    formatted_response += f"{value}\n\n"

            response = formatted_response.strip()

    except Exception as e:
        response = f"Error: {e}"

    return response


interface = gr.Interface(
    fn=agent_interface,
    inputs=gr.Textbox(
        lines=1, placeholder="Enter your request for the agent", label=""
    ),
    outputs=gr.Textbox(label=""),
    title="Party Prep Agent",
    description="Ask the agent for help planning your party!",
    flagging_mode="never",
    show_progress="full",
    examples=[
        ["Suggest a menu and a theme for formal dinner party"],
        ["Suggest a menu, music list for highschool graduation party"],
        ["Sugest a fine dining menu and how long it till take to prpate"],
        ["Teen birthday party theme and menu ideas"],
    ],
)

if __name__ == "__main__":
    interface.launch(debug=True)