Spaces:
Sleeping
Sleeping
| from dotenv import load_dotenv | |
| from langchain_google_genai import ChatGoogleGenerativeAI | |
| from langchain_core.tools import tool | |
| from langchain.agents import create_tool_calling_agent, AgentExecutor | |
| from langchain_core.prompts import ChatPromptTemplate | |
| import gradio as gr | |
| load_dotenv() | |
| # Tool: Search or generate recipes from ingredients | |
| def recipe_finder(query: str) -> str: | |
| recipes = { | |
| "pasta": "Try Creamy Garlic Pasta: Cook pasta, sauté garlic in butter, add cream, parmesan, and toss with parsley.", | |
| "chicken": "Grilled Lemon Chicken: Marinate chicken in lemon juice, olive oil, garlic, and grill until golden brown.", | |
| "vegetarian": "Vegetable Stir Fry: Sauté bell peppers, broccoli, carrots, add soy sauce and sesame seeds.", | |
| "dessert": "Chocolate Mug Cake: Mix flour, cocoa powder, sugar, milk, oil, microwave for 2 minutes." | |
| } | |
| for key, val in recipes.items(): | |
| if key in query.lower(): | |
| return val | |
| return f"Sorry, I don’t have a preset recipe for '{query}', but try asking me to generate one!" | |
| # Main assistant function | |
| def cooking_assistant(message: str, history: list) -> str: | |
| llm = ChatGoogleGenerativeAI(model="gemini-2.5-flash-lite") | |
| system_message = ( | |
| "You are a friendly AI Recipe Assistant. " | |
| "Suggest recipes, cooking steps, and variations. " | |
| "If users mention ingredients, cuisine, or dietary preferences, use the recipe_finder tool. " | |
| "Otherwise, answer with your own cooking knowledge." | |
| ) | |
| prompt = ChatPromptTemplate.from_messages([ | |
| ("system", system_message), | |
| ("human", "{input}"), | |
| ("placeholder", "{agent_scratchpad}") | |
| ]) | |
| agent = create_tool_calling_agent(llm, [recipe_finder], prompt) | |
| executor = AgentExecutor(agent=agent, tools=[recipe_finder], verbose=True) | |
| result = executor.invoke({"input": message}) | |
| return result["output"] | |
| # Quick test | |
| if __name__ == "__main__": | |
| print(cooking_assistant("I have chicken and garlic. Suggest a recipe", [])) | |
| # --- Gradio UI --- | |
| LOGO_URL = "https://raw.githubusercontent.com/hereandnowai/images/refs/heads/main/logos/logo-of-here-and-now-ai.png" | |
| with gr.Blocks() as demo: | |
| gr.HTML(f'<div style="text-align: center;"><img src="{LOGO_URL}" style="height: 60px;"><h1>🍳 Caramel AI - Recipe Assistant</h1></div>') | |
| gr.ChatInterface( | |
| fn=cooking_assistant, | |
| description="Ask me for recipes, cooking steps, or meal ideas! 🍲", | |
| examples=[ | |
| "Suggest a pasta recipe with tomatoes and cheese", | |
| "Give me a vegetarian dinner idea", | |
| "How to make chocolate cake?", | |
| "Quick breakfast with oats?", | |
| "Healthy lunch with chicken and spinach" | |
| ], | |
| type="messages" | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch(share=True) | |