Spaces:
Sleeping
Sleeping
Modified app.py
Browse files
app.py
CHANGED
|
@@ -9,15 +9,48 @@ from Gradio_UI import GradioUI
|
|
| 9 |
|
| 10 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 11 |
@tool
|
| 12 |
-
def
|
| 13 |
#Keep this format for the description / args / args description but feel free to modify the tool
|
| 14 |
-
"""A tool that
|
| 15 |
Args:
|
| 16 |
-
|
| 17 |
-
arg2: the second argument
|
| 18 |
"""
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
@tool
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 23 |
"""A tool that fetches the current local time in a specified timezone.
|
|
@@ -42,7 +75,7 @@ final_answer = FinalAnswerTool()
|
|
| 42 |
model = HfApiModel(
|
| 43 |
max_tokens=2096,
|
| 44 |
temperature=0.5,
|
| 45 |
-
model_id='
|
| 46 |
custom_role_conversions=None,
|
| 47 |
)
|
| 48 |
|
|
@@ -55,7 +88,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 55 |
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
-
tools=[final_answer], ## add your tools here (don't remove final answer)
|
| 59 |
max_steps=6,
|
| 60 |
verbosity_level=1,
|
| 61 |
grammar=None,
|
|
|
|
| 9 |
|
| 10 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 11 |
@tool
|
| 12 |
+
def wikipedia_summary_tool(query:str)-> str: #it's import to specify the return type
|
| 13 |
#Keep this format for the description / args / args description but feel free to modify the tool
|
| 14 |
+
"""A tool that fetches a summary of a topic from wikipedia.
|
| 15 |
Args:
|
| 16 |
+
query: The search term or topic to look up on Wikipedia.
|
|
|
|
| 17 |
"""
|
| 18 |
+
try:
|
| 19 |
+
params = {
|
| 20 |
+
"action": "queryy",
|
| 21 |
+
"format": "json",
|
| 22 |
+
"titles": query,
|
| 23 |
+
"prop": "extracts",
|
| 24 |
+
"exintro": True,
|
| 25 |
+
"explaintext": True,
|
| 26 |
+
"redirects": 1
|
| 27 |
+
}
|
| 28 |
+
API_ENDPOINT = "https://en.wikipedia.org/w/api.php"
|
| 29 |
+
|
| 30 |
+
response = requests.get(API_ENDPOINT, params=params)
|
| 31 |
+
data = response.json()
|
| 32 |
+
|
| 33 |
+
page = next(iter(data['query']['pages'].values()))
|
| 34 |
|
| 35 |
+
if 'extract' in page:
|
| 36 |
+
summary = page['extract']
|
| 37 |
+
if summary: # Pastikan ringkasan tidak kosong
|
| 38 |
+
return f"Summary from Wikipedia for '{query}':\n{summary}"
|
| 39 |
+
else:
|
| 40 |
+
return f"No summary found for '{query}'. The page might exist but has no introductory text, or the query needs to be more specific."
|
| 41 |
+
elif 'missing' in page:
|
| 42 |
+
return f"No Wikipedia page found for '{query}'. Please try a different query."
|
| 43 |
+
else:
|
| 44 |
+
return f"Could not retrieve summary for '{query}'. Unexpected API response."
|
| 45 |
+
|
| 46 |
+
except requests.exceptions.ConnectionError:
|
| 47 |
+
return "Connection error: Could not connect to Wikipedia API. Please check your internet connection."
|
| 48 |
+
except requests.exceptions.Timeout:
|
| 49 |
+
return "Timeout error: Wikipedia API took too long to respond."
|
| 50 |
+
except requests.exceptions.RequestException as e:
|
| 51 |
+
return f"An error occurred while fetching data from Wikipedia: {e}"
|
| 52 |
+
except Exception as e:
|
| 53 |
+
return f"An unexpected error occurred: {e}"
|
| 54 |
@tool
|
| 55 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 56 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
|
| 75 |
model = HfApiModel(
|
| 76 |
max_tokens=2096,
|
| 77 |
temperature=0.5,
|
| 78 |
+
model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',# it is possible that this model may be overloaded
|
| 79 |
custom_role_conversions=None,
|
| 80 |
)
|
| 81 |
|
|
|
|
| 88 |
|
| 89 |
agent = CodeAgent(
|
| 90 |
model=model,
|
| 91 |
+
tools=[final_answer, wikipedia_summary_tool, get_current_time_in_timezone, image_generation_tool], ## add your tools here (don't remove final answer)
|
| 92 |
max_steps=6,
|
| 93 |
verbosity_level=1,
|
| 94 |
grammar=None,
|