SergeyO7 commited on
Commit
f804acc
·
verified ·
1 Parent(s): 39f7267

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +96 -3
app.py CHANGED
@@ -8,6 +8,8 @@ from skyfield.api import load, Topos, load_file
8
  from skyfield import almanac
9
  from tools.final_answer import FinalAnswerTool
10
  from Gradio_UI import GradioUI
 
 
11
  import os
12
  import base64
13
 
@@ -282,6 +284,18 @@ def get_current_time_raw(timezone: str) -> str:
282
  except Exception as e:
283
  return f"Error: {str(e)}"
284
 
 
 
 
 
 
 
 
 
 
 
 
 
285
  # Model configuration
286
  final_answer = FinalAnswerTool()
287
  model = HfApiModel(
@@ -298,11 +312,38 @@ image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_co
298
  with open("prompts.yaml", 'r') as stream:
299
  prompt_templates = yaml.safe_load(stream)
300
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
301
  # Initialize agent with all tools
302
  agent = CodeAgent(
303
  model=model,
304
- tools=[final_answer, get_moon_info, get_current_time_in_timezone, get_current_time_raw, image_generation_tool],
305
- max_steps=6,
306
  verbosity_level=1,
307
  prompt_templates=prompt_templates
308
  # execution_env=E2BSandbox(
@@ -317,8 +358,60 @@ agent = CodeAgent(
317
  # max_code_iterations=100, # Prevent infinite loops
318
  )
319
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
320
  if __name__ == "__main__":
321
- GradioUI(agent).launch()
322
 
323
  # Change to your username and repo name
324
  # agent.push_to_hub('sergeyo7/Garden_Magus')
 
8
  from skyfield import almanac
9
  from tools.final_answer import FinalAnswerTool
10
  from Gradio_UI import GradioUI
11
+ from langchain.memory import SimpleMemory
12
+ from gradio import Interface
13
  import os
14
  import base64
15
 
 
284
  except Exception as e:
285
  return f"Error: {str(e)}"
286
 
287
+ # Memory initialization
288
+ memory = SimpleMemory(
289
+ memory={
290
+ "location_provided": False,
291
+ "plant": None,
292
+ "root_crop": None,
293
+ "location_cautions": "",
294
+ "answer": "",
295
+ "last_question": None
296
+ }
297
+ )
298
+
299
  # Model configuration
300
  final_answer = FinalAnswerTool()
301
  model = HfApiModel(
 
312
  with open("prompts.yaml", 'r') as stream:
313
  prompt_templates = yaml.safe_load(stream)
314
 
315
+ # Prompt template
316
+ prompt_templates = {
317
+ "main_prompt": """
318
+ Current state:
319
+ - location_provided: {memory.location_provided}
320
+ - plant: {memory.plant}
321
+ - root_crop: {memory.root_crop}
322
+ - location_cautions: {memory.location_cautions}
323
+ - answer: {memory.answer}
324
+ - last_question: {memory.last_question}
325
+
326
+ User's input: {input}
327
+
328
+ Instructions:
329
+ 1. If responding to a clarification question (last_question is not None), interpret the input as the answer to that question and update the state accordingly.
330
+ 2. Otherwise, process the user's request as follows:
331
+ - Check if a plant name is provided and recognized. If not, ask "Please specify the plant you are interested in." If recognized, determine if it’s a root crop or above-ground plant (e.g., known plants: potato=root, tomato=above-ground). If unrecognized, ask "Is this plant a root crop? (yes/no)".
332
+ - Check if a location is provided. If yes, set location_provided to true. If the location is not on Earth (e.g., "Moon", "Mars"), set location_cautions to "Salute you explorer! Moon indices are Earth-specific due to gravitational and tidal influences. For other planets, develop indices based on local celestial cycles." and use it as the final answer. If on Earth, ask "Is this location suitable for outdoor planting? (yes/no)" to determine suitability.
333
+ - Determine if the request is about planting or pruning. For planting, ensure plant is defined (ask if not), then calculate the fertility index using get_moon_info. For pruning, calculate the pruning index.
334
+ - If location_cautions is not empty, append it to the answer.
335
+ 3. When asking a question, format your response as:
336
+ "Action: Ask user\nQuestion: [your question]"
337
+ 4. When all information is gathered, calculate the answer and call FinalAnswerTool.
338
+ """
339
+ }
340
+
341
+
342
  # Initialize agent with all tools
343
  agent = CodeAgent(
344
  model=model,
345
+ tools=[final_answer, get_moon_info, get_current_time_in_timezone, get_current_time_raw],
346
+ max_steps=10,
347
  verbosity_level=1,
348
  prompt_templates=prompt_templates
349
  # execution_env=E2BSandbox(
 
358
  # max_code_iterations=100, # Prevent infinite loops
359
  )
360
 
361
+ # Conversation handler
362
+ def conversation_handler(user_input, history):
363
+ global memory, agent
364
+ if memory["last_question"] is not None:
365
+ if memory["last_question"] == "plant":
366
+ memory["plant"] = user_input
367
+ known_plants = {"potato": True, "tomato": False}
368
+ if user_input in known_plants:
369
+ memory["root_crop"] = known_plants[user_input]
370
+ else:
371
+ memory["last_question"] = "root_crop"
372
+ return "Action: Ask user\nQuestion: Is this plant a root crop? (yes/no)"
373
+ elif memory["last_question"] == "root_crop":
374
+ memory["root_crop"] = user_input.lower() in ["yes", "y"]
375
+ memory["last_question"] = None
376
+ elif memory["last_question"] == "location_suitability":
377
+ if user_input.lower() in ["no", "n"]:
378
+ memory["location_cautions"] = "Ensure required conditions for the plant (e.g., indoor) before relying on the fertility indices."
379
+ else:
380
+ memory["location_cautions"] = ""
381
+ memory["last_question"] = None
382
+
383
+ output = agent.run(user_input)
384
+ if "Action: Ask user" in output:
385
+ question = output.split("Question: ")[1].strip()
386
+ if "plant" in question.lower():
387
+ memory["last_question"] = "plant"
388
+ elif "root crop" in question.lower():
389
+ memory["last_question"] = "root_crop"
390
+ elif "suitable for outdoor" in question.lower():
391
+ memory["last_question"] = "location_suitability"
392
+ memory["location_provided"] = True
393
+ return question
394
+ else:
395
+ if "Salute you explorer!" in output:
396
+ memory["location_cautions"] = output
397
+ memory["answer"] = output
398
+ elif memory["location_cautions"]:
399
+ memory["answer"] = output + " " + memory["location_cautions"]
400
+ else:
401
+ memory["answer"] = output
402
+ return output
403
+
404
+ # Gradio interface
405
+ interface = Interface(
406
+ fn=conversation_handler,
407
+ inputs="text",
408
+ outputs="text",
409
+ title="Garden Magus",
410
+ description="Ask about planting or pruning based on moon indices."
411
+ )
412
+
413
  if __name__ == "__main__":
414
+ interface.launch()
415
 
416
  # Change to your username and repo name
417
  # agent.push_to_hub('sergeyo7/Garden_Magus')