Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,13 @@
|
|
| 1 |
-
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
|
| 8 |
-
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 my_cutom_tool(arg1:str, arg2:int)-> str:
|
| 13 |
-
#Keep this format for the description / args / args description but feel free to modify the tool
|
| 14 |
"""A tool that does nothing yet
|
| 15 |
Args:
|
| 16 |
arg1: the first argument
|
|
@@ -18,28 +15,26 @@ def my_cutom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
|
|
| 18 |
"""
|
| 19 |
return "What magic will you build ?"
|
| 20 |
|
| 21 |
-
@tool
|
| 22 |
def get_sura(sura_number: int) -> str:
|
| 23 |
"""A tool that gets the text of a sura based on its sura number
|
| 24 |
Args:
|
| 25 |
sura_number: The sura number of the sura.
|
| 26 |
"""
|
| 27 |
-
url = f"
|
| 28 |
response = requests.get(url)
|
| 29 |
-
|
| 30 |
if response.status_code == 200:
|
| 31 |
data = response.json()
|
| 32 |
result = ""
|
| 33 |
-
|
| 34 |
for verse in data:
|
| 35 |
nr = verse['nr']
|
| 36 |
-
tanzil_text = verse['
|
| 37 |
result += f"{nr}: {tanzil_text}\n"
|
| 38 |
-
|
| 39 |
-
return f"The text of the sura is:\n {result}"
|
| 40 |
else:
|
| 41 |
return "Error: Could not fetch the sura data."
|
| 42 |
-
|
| 43 |
@tool
|
| 44 |
def search_quran(word: str) -> str:
|
| 45 |
"""A tool that searches for a word in the Quran.
|
|
@@ -53,17 +48,16 @@ def search_quran(word: str) -> str:
|
|
| 53 |
data = response.json()
|
| 54 |
result = ""
|
| 55 |
|
| 56 |
-
for verse in data:
|
| 57 |
nr = verse['nr']
|
| 58 |
-
tanzil_text = verse['
|
| 59 |
result += f"{nr}: {tanzil_text}\n"
|
| 60 |
|
| 61 |
return f"The {word} is found in the following verses:\n {result}"
|
| 62 |
else:
|
| 63 |
return "Error: Could not fetch the sura data."
|
| 64 |
|
| 65 |
-
|
| 66 |
-
@tool
|
| 67 |
def calculate_expenses(sallery: int) -> str:
|
| 68 |
"""A tool that calculate the expenses of a given sallery.
|
| 69 |
Args:
|
|
@@ -73,12 +67,12 @@ def calculate_expenses(sallery: int) -> str:
|
|
| 73 |
|
| 74 |
@tool
|
| 75 |
def encode_text(text: str) -> str:
|
| 76 |
-
"""A tool that encodes a given text.
|
| 77 |
Args:
|
| 78 |
text: The text to be encoded.
|
| 79 |
"""
|
| 80 |
return ''.join(chr(ord(char) + 2) for char in text)
|
| 81 |
-
|
| 82 |
@tool
|
| 83 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 84 |
"""A tool that fetches the current local time in a specified timezone.
|
|
@@ -97,13 +91,12 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 97 |
|
| 98 |
final_answer = FinalAnswerTool()
|
| 99 |
model = HfApiModel(
|
| 100 |
-
max_tokens=2096,
|
| 101 |
-
temperature=0.5,
|
| 102 |
-
model_id='meta-llama/Meta-Llama-3-8B-Instruct'
|
| 103 |
-
custom_role_conversions=None,
|
| 104 |
)
|
| 105 |
|
| 106 |
-
|
| 107 |
# Import tool from Hub
|
| 108 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 109 |
|
|
@@ -112,7 +105,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 112 |
|
| 113 |
agent = CodeAgent(
|
| 114 |
model=model,
|
| 115 |
-
tools=[final_answer, encode_text, get_sura,
|
| 116 |
max_steps=6,
|
| 117 |
verbosity_level=1,
|
| 118 |
grammar=None,
|
|
@@ -122,5 +115,25 @@ agent = CodeAgent(
|
|
| 122 |
prompt_templates=prompt_templates
|
| 123 |
)
|
| 124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool, OpenAIServerModel
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
|
|
|
|
|
|
|
| 8 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 9 |
@tool
|
| 10 |
+
def my_cutom_tool(arg1:str, arg2:int)-> str:
|
|
|
|
| 11 |
"""A tool that does nothing yet
|
| 12 |
Args:
|
| 13 |
arg1: the first argument
|
|
|
|
| 15 |
"""
|
| 16 |
return "What magic will you build ?"
|
| 17 |
|
| 18 |
+
@tool
|
| 19 |
def get_sura(sura_number: int) -> str:
|
| 20 |
"""A tool that gets the text of a sura based on its sura number
|
| 21 |
Args:
|
| 22 |
sura_number: The sura number of the sura.
|
| 23 |
"""
|
| 24 |
+
url = f"https://quran-challenge.com/app/php/getverses.php?sura_number={sura_number}"
|
| 25 |
response = requests.get(url)
|
| 26 |
+
|
| 27 |
if response.status_code == 200:
|
| 28 |
data = response.json()
|
| 29 |
result = ""
|
| 30 |
+
|
| 31 |
for verse in data:
|
| 32 |
nr = verse['nr']
|
| 33 |
+
tanzil_text = verse['text']
|
| 34 |
result += f"{nr}: {tanzil_text}\n"
|
| 35 |
+
return f"The text of the sura {sura_number} is:\n {result}"
|
|
|
|
| 36 |
else:
|
| 37 |
return "Error: Could not fetch the sura data."
|
|
|
|
| 38 |
@tool
|
| 39 |
def search_quran(word: str) -> str:
|
| 40 |
"""A tool that searches for a word in the Quran.
|
|
|
|
| 48 |
data = response.json()
|
| 49 |
result = ""
|
| 50 |
|
| 51 |
+
for verse in data['result']:
|
| 52 |
nr = verse['nr']
|
| 53 |
+
tanzil_text = verse['text']
|
| 54 |
result += f"{nr}: {tanzil_text}\n"
|
| 55 |
|
| 56 |
return f"The {word} is found in the following verses:\n {result}"
|
| 57 |
else:
|
| 58 |
return "Error: Could not fetch the sura data."
|
| 59 |
|
| 60 |
+
@tool
|
|
|
|
| 61 |
def calculate_expenses(sallery: int) -> str:
|
| 62 |
"""A tool that calculate the expenses of a given sallery.
|
| 63 |
Args:
|
|
|
|
| 67 |
|
| 68 |
@tool
|
| 69 |
def encode_text(text: str) -> str:
|
| 70 |
+
"""A tool that encodes a given text using the sabonzo method.
|
| 71 |
Args:
|
| 72 |
text: The text to be encoded.
|
| 73 |
"""
|
| 74 |
return ''.join(chr(ord(char) + 2) for char in text)
|
| 75 |
+
|
| 76 |
@tool
|
| 77 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 78 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
|
| 91 |
|
| 92 |
final_answer = FinalAnswerTool()
|
| 93 |
model = HfApiModel(
|
| 94 |
+
max_tokens=2096,
|
| 95 |
+
temperature=0.5,
|
| 96 |
+
model_id='meta-llama/Meta-Llama-3-8B-Instruct',
|
| 97 |
+
custom_role_conversions=None,
|
| 98 |
)
|
| 99 |
|
|
|
|
| 100 |
# Import tool from Hub
|
| 101 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 102 |
|
|
|
|
| 105 |
|
| 106 |
agent = CodeAgent(
|
| 107 |
model=model,
|
| 108 |
+
tools=[final_answer, calculate_expenses, image_generation_tool, encode_text, get_sura, search_quran],
|
| 109 |
max_steps=6,
|
| 110 |
verbosity_level=1,
|
| 111 |
grammar=None,
|
|
|
|
| 115 |
prompt_templates=prompt_templates
|
| 116 |
)
|
| 117 |
|
| 118 |
+
def run_agent():
|
| 119 |
+
# Sample interaction: Get user input and use the agent to process it
|
| 120 |
+
print("Welcome! Type a prompt for the agent:")
|
| 121 |
+
user_input = input("Your prompt: ")
|
| 122 |
+
#user_input = "What is the text of sura 113?"
|
| 123 |
+
|
| 124 |
+
# Ensure user input is a valid string
|
| 125 |
+
if not isinstance(user_input, str) or not user_input.strip():
|
| 126 |
+
print("Invalid input. Please provide a non-empty string.")
|
| 127 |
+
return
|
| 128 |
+
|
| 129 |
+
# Here you can map user_input to your tools if necessary, or you can let the agent decide
|
| 130 |
+
try:
|
| 131 |
+
response = agent.run(user_input)
|
| 132 |
+
except Exception as e:
|
| 133 |
+
print(f"Error while running agent: {e}")
|
| 134 |
+
return
|
| 135 |
|
| 136 |
+
print("\nAgent's response:")
|
| 137 |
+
print(response)
|
| 138 |
+
if __name__ == "__main__":
|
| 139 |
+
run_agent()
|