Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,53 +1,51 @@
|
|
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
|
13 |
-
|
14 |
-
"""A tool that does nothing yet
|
15 |
Args:
|
16 |
-
|
17 |
-
|
18 |
"""
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
@tool
|
22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
23 |
-
"""
|
24 |
-
Args:
|
25 |
-
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
26 |
-
"""
|
27 |
try:
|
28 |
-
# Create timezone object
|
29 |
tz = pytz.timezone(timezone)
|
30 |
-
# Get current time in that timezone
|
31 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
32 |
return f"The current local time in {timezone} is: {local_time}"
|
33 |
except Exception as e:
|
34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
35 |
|
36 |
-
|
37 |
final_answer = FinalAnswerTool()
|
38 |
-
|
39 |
-
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
|
40 |
-
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
41 |
-
|
42 |
model = HfApiModel(
|
43 |
-
max_tokens=2096,
|
44 |
-
temperature=0.5,
|
45 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct'
|
46 |
-
custom_role_conversions=None,
|
47 |
)
|
48 |
|
49 |
-
|
50 |
-
# Import tool from Hub
|
51 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
52 |
|
53 |
with open("prompts.yaml", 'r') as stream:
|
@@ -55,7 +53,12 @@ with open("prompts.yaml", 'r') as stream:
|
|
55 |
|
56 |
agent = CodeAgent(
|
57 |
model=model,
|
58 |
-
tools=[
|
|
|
|
|
|
|
|
|
|
|
59 |
max_steps=6,
|
60 |
verbosity_level=1,
|
61 |
grammar=None,
|
@@ -65,5 +68,4 @@ agent = CodeAgent(
|
|
65 |
prompt_templates=prompt_templates
|
66 |
)
|
67 |
|
68 |
-
|
69 |
GradioUI(agent).launch()
|
|
|
1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
2 |
import datetime
|
3 |
import requests
|
4 |
import pytz
|
5 |
import yaml
|
6 |
+
import os # <--- أضف هذا السطر
|
7 |
from tools.final_answer import FinalAnswerTool
|
|
|
8 |
from Gradio_UI import GradioUI
|
9 |
|
|
|
10 |
@tool
|
11 |
+
def summarize_text(text: str, max_length: int = 130) -> str:
|
12 |
+
"""تلخيص النصوص باستخدام نموذج BART من Hugging Face
|
|
|
13 |
Args:
|
14 |
+
text: النص الأصلي المراد تلخيصه
|
15 |
+
max_length: الحد الأقصى لطول الملخص (افتراضي: 130 كلمة)
|
16 |
"""
|
17 |
+
try:
|
18 |
+
API_URL = "https://api-inference.huggingface.co/models/facebook/bart-large-cnn"
|
19 |
+
headers = {"Authorization": f"Bearer {os.environ['HF_TOKEN']}"} # <--- تصحيح هنا
|
20 |
+
data = {
|
21 |
+
"inputs": text,
|
22 |
+
"parameters": {"max_length": max_length}
|
23 |
+
}
|
24 |
+
response = requests.post(API_URL, headers=headers, json=data)
|
25 |
+
response.raise_for_status()
|
26 |
+
summary = response.json()[0]['summary_text']
|
27 |
+
return f"الملخص: {summary}"
|
28 |
+
except Exception as e:
|
29 |
+
return f"خطأ في التلخيص: {str(e)}"
|
30 |
|
31 |
@tool
|
32 |
def get_current_time_in_timezone(timezone: str) -> str:
|
33 |
+
"""أداة لجلب الوقت الحالي في منطقة زمنية محددة"""
|
|
|
|
|
|
|
34 |
try:
|
|
|
35 |
tz = pytz.timezone(timezone)
|
|
|
36 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
37 |
return f"The current local time in {timezone} is: {local_time}"
|
38 |
except Exception as e:
|
39 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
40 |
|
|
|
41 |
final_answer = FinalAnswerTool()
|
|
|
|
|
|
|
|
|
42 |
model = HfApiModel(
|
43 |
+
max_tokens=2096,
|
44 |
+
temperature=0.5,
|
45 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
46 |
+
custom_role_conversions=None,
|
47 |
)
|
48 |
|
|
|
|
|
49 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
50 |
|
51 |
with open("prompts.yaml", 'r') as stream:
|
|
|
53 |
|
54 |
agent = CodeAgent(
|
55 |
model=model,
|
56 |
+
tools=[
|
57 |
+
final_answer,
|
58 |
+
DuckDuckGoSearchTool(),
|
59 |
+
image_generation_tool,
|
60 |
+
summarize_text # <--- الأداة الجديدة هنا
|
61 |
+
],
|
62 |
max_steps=6,
|
63 |
verbosity_level=1,
|
64 |
grammar=None,
|
|
|
68 |
prompt_templates=prompt_templates
|
69 |
)
|
70 |
|
|
|
71 |
GradioUI(agent).launch()
|