Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from smolagents import ToolCallingAgent, tool, LiteLLMModel
|
| 3 |
+
import os
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
load_dotenv()
|
| 6 |
+
|
| 7 |
+
CO_API_KEY = os.environ.get("CO_API_KEY")
|
| 8 |
+
model = "command-r-plus-08-2024"
|
| 9 |
+
|
| 10 |
+
preamble = """You are Alfred, a helpful butler assistant.
|
| 11 |
+
When using a tool and getting a result, provide a clear response based on that result.
|
| 12 |
+
After calling a tool once, suggest a menu based on the result.
|
| 13 |
+
Include a creative description of ingredients for each item.
|
| 14 |
+
Do not add any additional information or context such as recommendations or explanations.
|
| 15 |
+
Use gender-neutral language and avoid gendered pronouns."""
|
| 16 |
+
|
| 17 |
+
model = LiteLLMModel(
|
| 18 |
+
model_id=f"cohere/{model}",
|
| 19 |
+
api_key=CO_API_KEY,
|
| 20 |
+
temperature=0.2,
|
| 21 |
+
max_tokens=1000
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
@tool
|
| 25 |
+
def suggest_menu(occasion: str) -> str:
|
| 26 |
+
"""
|
| 27 |
+
Suggests a menu based on the occasion.
|
| 28 |
+
Args:
|
| 29 |
+
occasion (str): The type of occasion for the party. Allowed values are:
|
| 30 |
+
- "casual": Menu for casual party.
|
| 31 |
+
- "formal": Menu for formal party.
|
| 32 |
+
- "superhero": Menu for superhero party.
|
| 33 |
+
- "custom": Custom menu.
|
| 34 |
+
"""
|
| 35 |
+
if occasion == "casual":
|
| 36 |
+
return "Pizza, and some snacks and drinks."
|
| 37 |
+
elif occasion == "formal":
|
| 38 |
+
return "three-course dinner with wine and dessert."
|
| 39 |
+
elif occasion == "superhero":
|
| 40 |
+
return "Buffet with high-energy and healthy food."
|
| 41 |
+
else:
|
| 42 |
+
return "Custom menu for the butler."
|
| 43 |
+
|
| 44 |
+
agent = ToolCallingAgent(tools=[suggest_menu], model=model)
|
| 45 |
+
|
| 46 |
+
def plan_menu(user_request):
|
| 47 |
+
try:
|
| 48 |
+
result = agent.run(preamble + f"\n{user_request}", max_steps=1)
|
| 49 |
+
return result
|
| 50 |
+
except Exception as e:
|
| 51 |
+
return f"Error: {str(e)}"
|
| 52 |
+
|
| 53 |
+
# Create Gradio interface
|
| 54 |
+
with gr.Blocks(title="Alfred's Menu Planner") as demo:
|
| 55 |
+
gr.Markdown("# 🍽️ Alfred's Menu Planner")
|
| 56 |
+
gr.Markdown("Ask Alfred to help plan a menu for your party!")
|
| 57 |
+
|
| 58 |
+
with gr.Row():
|
| 59 |
+
with gr.Column(scale=1):
|
| 60 |
+
user_input = gr.Textbox(
|
| 61 |
+
label="What kind of menu do you need?",
|
| 62 |
+
placeholder="Plan a casual menu for my birthday party",
|
| 63 |
+
lines=3
|
| 64 |
+
)
|
| 65 |
+
submit_btn = gr.Button("Ask Alfred", variant="primary")
|
| 66 |
+
|
| 67 |
+
with gr.Column(scale=2):
|
| 68 |
+
output = gr.Markdown(
|
| 69 |
+
label="Alfred's Response",
|
| 70 |
+
value="Ask Alfred for a menu recommendation!"
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
submit_btn.click(
|
| 74 |
+
fn=lambda: "Ask Alfred for a menu recommendation!",
|
| 75 |
+
outputs=output,
|
| 76 |
+
show_progress=True
|
| 77 |
+
).then(
|
| 78 |
+
fn=plan_menu,
|
| 79 |
+
inputs=user_input,
|
| 80 |
+
outputs=output,
|
| 81 |
+
show_progress=True
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
gr.Examples(
|
| 85 |
+
examples=[
|
| 86 |
+
["Plan a casual menu for my birthday party"],
|
| 87 |
+
["I need a formal dinner menu for 8 people"],
|
| 88 |
+
["What would be good for a superhero themed party?"],
|
| 89 |
+
["Suggest a custom menu for a garden party"]
|
| 90 |
+
],
|
| 91 |
+
inputs=user_input
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
if __name__ == "__main__":
|
| 95 |
+
demo.launch()
|