Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,16 +7,35 @@ from tools.final_answer import FinalAnswerTool
|
|
| 7 |
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
| 11 |
@tool
|
| 12 |
-
def
|
| 13 |
-
|
| 14 |
-
"""A tool that does nothing yet
|
| 15 |
Args:
|
| 16 |
-
|
| 17 |
-
arg2: the second argument
|
| 18 |
"""
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
@tool
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
@@ -51,7 +70,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 51 |
|
| 52 |
agent = CodeAgent(
|
| 53 |
model=model,
|
| 54 |
-
tools=[final_answer, get_current_time_in_timezone], # add your tools here (don't remove final_answer)
|
| 55 |
max_steps=6,
|
| 56 |
verbosity_level=1,
|
| 57 |
grammar=None,
|
|
|
|
| 7 |
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
+
from sentence_transformers import SentenceTransformer, util
|
| 11 |
+
import wikipedia
|
| 12 |
+
|
| 13 |
@tool
|
| 14 |
+
def find_unexpected_analogy(topic: str) -> str:
|
| 15 |
+
"""A tool that Finds an unexpected but relevant analogy for a given topic
|
|
|
|
| 16 |
Args:
|
| 17 |
+
topic: A string that provides text for analogy search
|
|
|
|
| 18 |
"""
|
| 19 |
+
try:
|
| 20 |
+
# Get a summary from Wikipedia
|
| 21 |
+
summary = wikipedia.summary(topic, sentences=2)
|
| 22 |
+
|
| 23 |
+
# Pretrained model for embeddings (semantic similarity search)
|
| 24 |
+
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 25 |
+
topic_embedding = model.encode(topic, convert_to_tensor=True)
|
| 26 |
+
|
| 27 |
+
# Potential analogy candidates (could be expanded)
|
| 28 |
+
candidate_topics = ["ant colonies", "internet routing", "biological ecosystems", "black holes", "swarm intelligence", "musical composition"]
|
| 29 |
+
candidate_embeddings = model.encode(candidate_topics, convert_to_tensor=True)
|
| 30 |
+
|
| 31 |
+
# Find most unexpected but semantically relevant analogy
|
| 32 |
+
similarities = util.pytorch_cos_sim(topic_embedding, candidate_embeddings)
|
| 33 |
+
best_index = similarities.argmin().item() # Find the most *distant* but related topic
|
| 34 |
+
analogy = candidate_topics[best_index]
|
| 35 |
+
|
| 36 |
+
return f"For '{topic}', an unexpected analogy is '{analogy}'. Why? {summary}"
|
| 37 |
+
except Exception as e:
|
| 38 |
+
return f"Error generating analogy: {str(e)}"
|
| 39 |
|
| 40 |
@tool
|
| 41 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
|
| 70 |
|
| 71 |
agent = CodeAgent(
|
| 72 |
model=model,
|
| 73 |
+
tools=[final_answer, get_current_time_in_timezone, find_unexpected_analogy], # add your tools here (don't remove final_answer)
|
| 74 |
max_steps=6,
|
| 75 |
verbosity_level=1,
|
| 76 |
grammar=None,
|