Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,38 +1,10 @@
|
|
1 |
-
import
|
|
|
2 |
import pytz
|
3 |
import yaml
|
4 |
-
import os
|
5 |
from tools.final_answer import FinalAnswerTool
|
6 |
from Gradio_UI import GradioUI
|
7 |
-
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
8 |
-
|
9 |
-
# ----- تصحيح: إضافة الاستيراد الناقص لـ datetime -----
|
10 |
-
import datetime
|
11 |
-
|
12 |
-
# ----- تصحيح: إغلاق الأقواس الناقصة في أداة التلخيص -----
|
13 |
-
@tool
|
14 |
-
def summarize_text(text: str, max_length: int = 130) -> str:
|
15 |
-
"""تلخيص النصوص باستخدام نموذج BART من Hugging Face
|
16 |
-
|
17 |
-
Args:
|
18 |
-
text: النص الأصلي المراد تلخيصه
|
19 |
-
max_length: الحد الأقصى لطول الملخص (افتراضي: 130 كلمة)
|
20 |
-
"""
|
21 |
-
try:
|
22 |
-
API_URL = "https://api-inference.huggingface.co/models/facebook/bart-large-cnn"
|
23 |
-
headers = {"Authorization": f"Bearer {os.environ['HF_TOKEN']}"}
|
24 |
-
data = {
|
25 |
-
"inputs": text,
|
26 |
-
"parameters": {"max_length": max_length}
|
27 |
-
} # <--- إضافة القوس المفقود هنا
|
28 |
-
response = requests.post(API_URL, headers=headers, json=data)
|
29 |
-
response.raise_for_status()
|
30 |
-
summary = response.json()[0]['summary_text']
|
31 |
-
return f"الملخص: {summary}"
|
32 |
-
except Exception as e:
|
33 |
-
return f"خطأ في التلخيص: {str(e)}"
|
34 |
|
35 |
-
# ----- تصحيح: إضافة النقطة في نهاية الجملة الأولى من الـ Docstring -----
|
36 |
@tool
|
37 |
def get_current_time_in_timezone(timezone: str) -> str:
|
38 |
"""أداة لجلب الوقت الحالي في منطقة زمنية محددة.
|
@@ -47,7 +19,8 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
47 |
except Exception as e:
|
48 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
49 |
|
50 |
-
|
|
|
51 |
model = HfApiModel(
|
52 |
max_tokens=2096,
|
53 |
temperature=0.5,
|
@@ -55,17 +28,17 @@ model = HfApiModel(
|
|
55 |
custom_role_conversions=None,
|
56 |
)
|
57 |
|
58 |
-
# ----- تصحيح: إضافة الأداة المُستوردة من الـ Hub -----
|
59 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
60 |
|
61 |
-
|
|
|
|
|
62 |
agent = CodeAgent(
|
63 |
model=model,
|
64 |
tools=[
|
65 |
-
|
66 |
DuckDuckGoSearchTool(),
|
67 |
image_generation_tool,
|
68 |
-
summarize_text,
|
69 |
get_current_time_in_timezone
|
70 |
],
|
71 |
max_steps=6,
|
@@ -74,7 +47,7 @@ agent = CodeAgent(
|
|
74 |
planning_interval=None,
|
75 |
name=None,
|
76 |
description=None,
|
77 |
-
prompt_templates=
|
78 |
)
|
79 |
|
80 |
GradioUI(agent).launch()
|
|
|
1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
2 |
+
import datetime
|
3 |
import pytz
|
4 |
import yaml
|
|
|
5 |
from tools.final_answer import FinalAnswerTool
|
6 |
from Gradio_UI import GradioUI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
|
|
8 |
@tool
|
9 |
def get_current_time_in_timezone(timezone: str) -> str:
|
10 |
"""أداة لجلب الوقت الحالي في منطقة زمنية محددة.
|
|
|
19 |
except Exception as e:
|
20 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
21 |
|
22 |
+
final_answer = FinalAnswerTool()
|
23 |
+
|
24 |
model = HfApiModel(
|
25 |
max_tokens=2096,
|
26 |
temperature=0.5,
|
|
|
28 |
custom_role_conversions=None,
|
29 |
)
|
30 |
|
|
|
31 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
32 |
|
33 |
+
with open("prompts.yaml", 'r') as stream:
|
34 |
+
prompt_templates = yaml.safe_load(stream)
|
35 |
+
|
36 |
agent = CodeAgent(
|
37 |
model=model,
|
38 |
tools=[
|
39 |
+
final_answer,
|
40 |
DuckDuckGoSearchTool(),
|
41 |
image_generation_tool,
|
|
|
42 |
get_current_time_in_timezone
|
43 |
],
|
44 |
max_steps=6,
|
|
|
47 |
planning_interval=None,
|
48 |
name=None,
|
49 |
description=None,
|
50 |
+
prompt_templates=prompt_templates
|
51 |
)
|
52 |
|
53 |
GradioUI(agent).launch()
|