|
|
import gradio as gr |
|
|
from openai import OpenAI |
|
|
import os |
|
|
import time |
|
|
import requests |
|
|
from bs4 import BeautifulSoup |
|
|
import json |
|
|
|
|
|
|
|
|
NVIDIA_API_KEY = os.getenv("NVIDIA_API_KEY") |
|
|
|
|
|
|
|
|
if not NVIDIA_API_KEY: |
|
|
raise ValueError("NVIDIA API Key not found in environment variables. Please set it in Hugging Face Space secrets.") |
|
|
|
|
|
|
|
|
client = OpenAI( |
|
|
base_url="https://integrate.api.nvidia.com/v1", |
|
|
api_key=NVIDIA_API_KEY |
|
|
) |
|
|
|
|
|
MODEL_NAME = "qwen/qwen3-next-80b-a3b-thinking" |
|
|
|
|
|
|
|
|
def update_ui_output(output_box, new_text): |
|
|
|
|
|
yield from output_box.update(value=output_box.value + new_text + "\n") |
|
|
|
|
|
|
|
|
def call_nvidia_llm(prompt, stream=False, max_tokens=4096): |
|
|
messages = [{"role": "user", "content": prompt}] |
|
|
|
|
|
|
|
|
completion = client.chat.completions.create( |
|
|
model=MODEL_NAME, |
|
|
messages=messages, |
|
|
temperature=0.6, |
|
|
top_p=0.7, |
|
|
max_tokens=max_tokens, |
|
|
stream=stream |
|
|
) |
|
|
|
|
|
|
|
|
if stream: |
|
|
for chunk in completion: |
|
|
reasoning = getattr(chunk.choices[0].delta, "reasoning_content", None) |
|
|
if reasoning: |
|
|
yield reasoning |
|
|
if chunk.choices[0].delta.content is not None: |
|
|
yield chunk.choices[0].delta.content |
|
|
|
|
|
else: |
|
|
full_response = "" |
|
|
for chunk in completion: |
|
|
full_response += chunk.choices[0].delta.content if chunk.choices[0].delta.content is not None else "" |
|
|
return full_response |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def generate_plan(user_request): |
|
|
prompt = f""" |
|
|
أنت وكيل ذكاء اصطناعي مكلف بتحويل طلبات المستخدمين إلى خطط عمل مفصلة. طلب المستخدم هو: '{user_request}'. |
|
|
أنشئ خطة مفصلة تتضمن خطوات متسلسلة وقابلة للتنفيذ خطوة بخطوة. يجب أن تكون الخطة دقيقة وتأخذ في الاعتبار أي متطلبات خاصة. |
|
|
مثال: |
|
|
1. البحث عن أحدث الإيردروبات. |
|
|
2. تحديد إيردروب مناسب. |
|
|
3. طلب معلومات من المستخدم (مثل عنوان المحفظة). |
|
|
4. تنفيذ مهام الإيردروب. |
|
|
5. التحقق من إكمال المهمة. |
|
|
|
|
|
الآن، أنشئ خطة لطلب المستخدم: '{user_request}'. |
|
|
""" |
|
|
return call_nvidia_llm(prompt) |
|
|
|
|
|
|
|
|
def generate_commands(plan_text, current_step_index): |
|
|
prompt = f""" |
|
|
الخطة الكاملة هي: |
|
|
{plan_text} |
|
|
|
|
|
الخطوة الحالية التي يجب تنفيذها هي الخطوة رقم {current_step_index + 1}. مهمتك هي تحويل هذه الخطوة إلى أوامر تنفيذية محددة. |
|
|
استخدم أدوات برمجية مثل 'requests' و 'BeautifulSoup' للبحث واستخراج البيانات من الويب. |
|
|
لا تستخدم متصفحات كاملة. إذا كانت الخطوة تتطلب معلومات من المستخدم، قم بإنشاء أمر يطلبها. |
|
|
|
|
|
مثال: |
|
|
الخطوة: "البحث عن أحدث الإيردروبات." |
|
|
الأوامر: "استخدم مكتبة 'requests' للبحث في جوجل عن 'أحدث ايردروبات العملات الرقمية'. ثم استخدم 'BeautifulSoup' لتحليل نتائج البحث واستخراج الروابط." |
|
|
|
|
|
الآن، حول الخطوة الحالية إلى أوامر: |
|
|
""" |
|
|
return call_nvidia_llm(prompt) |
|
|
|
|
|
|
|
|
def execute_commands(commands_text): |
|
|
|
|
|
if "requests" in commands_text and "google" in commands_text: |
|
|
try: |
|
|
response = requests.get("https://www.google.com/search?q=أحدث+ايردروبات+العملات+الرقمية") |
|
|
soup = BeautifulSoup(response.text, 'html.parser') |
|
|
|
|
|
return "✅ تم تنفيذ أمر البحث بنجاح (تمثيلي)." |
|
|
except Exception as e: |
|
|
return f"❌ فشل التنفيذ: {str(e)}" |
|
|
|
|
|
|
|
|
return "✅ تم تنفيذ الأوامر بنجاح (تمثيلي)." |
|
|
|
|
|
|
|
|
def review_and_correct(original_plan, execution_result): |
|
|
prompt = f""" |
|
|
الخطة الأصلية كانت: |
|
|
{original_plan} |
|
|
|
|
|
نتيجة التنفيذ كانت: |
|
|
{execution_result} |
|
|
|
|
|
هل تم تحقيق هدف الخطوة؟ إذا كانت الإجابة لا، ما هو سبب الفشل؟ |
|
|
قدم خطة تصحيح جديدة أو عدّل الخطة الأصلية لتجاوز الفشل وإكمال المهمة. |
|
|
إذا كانت النتيجة 'تم التنفيذ بنجاح'، فقدم ملخصًا مختصرًا لما تم إنجازه. |
|
|
""" |
|
|
return call_nvidia_llm(prompt) |
|
|
|
|
|
|
|
|
|
|
|
def run_agent(user_request, output_box, progress=gr.Progress()): |
|
|
|
|
|
yield from output_box.update(value="") |
|
|
|
|
|
|
|
|
yield from update_ui_output(output_box, "🔍 مرحلة التخطيط: يتم الآن توليد خطة عمل للوكيل...") |
|
|
progress.update(0.25, label="25% - جاري التخطيط") |
|
|
plan_text = generate_plan(user_request) |
|
|
yield from update_ui_output(output_box, "✅ تم توليد الخطة بنجاح:\n" + plan_text) |
|
|
time.sleep(1) |
|
|
|
|
|
|
|
|
yield from update_ui_output(output_box, "\n⚙️ مرحلة التكيف: يتم الآن تحويل الخطة إلى أوامر تنفيذية...") |
|
|
progress.update(0.5, label="50% - جاري التكيف") |
|
|
|
|
|
|
|
|
|
|
|
commands_text = generate_commands(plan_text, 0) |
|
|
yield from update_ui_output(output_box, "✅ تم توليد الأوامر بنجاح:\n" + commands_text) |
|
|
time.sleep(1) |
|
|
|
|
|
|
|
|
yield from update_ui_output(output_box, "\n🚀 مرحلة التنفيذ: جاري تنفيذ الأوامر...") |
|
|
progress.update(0.75, label="75% - جاري التنفيذ") |
|
|
execution_result = execute_commands(commands_text) |
|
|
yield from update_ui_output(output_box, "✅ نتيجة التنفيذ:\n" + execution_result) |
|
|
time.sleep(1) |
|
|
|
|
|
|
|
|
yield from update_ui_output(output_box, "\n🔄 مرحلة المراجعة: يتم مراجعة النتائج...") |
|
|
progress.update(1.0, label="100% - اكتملت المراجعة") |
|
|
review_and_correction_text = review_and_correct(plan_text, execution_result) |
|
|
yield from update_ui_output(output_box, "✅ تقرير المراجعة:\n" + review_and_correction_text) |
|
|
|
|
|
yield from output_box.update(value=output_box.value + "\n\n🎉 تم اكتمال مهمة الوكيل بنجاح!") |
|
|
|
|
|
|
|
|
|
|
|
with gr.Blocks(theme=gr.themes.Soft()) as demo: |
|
|
gr.Markdown("# وكيل ذكاء اصطناعي مرن") |
|
|
gr.Markdown("أدخل طلبك ليقوم الوكيل بتحويله إلى خطة عمل، تنفيذها، ومراجعتها تلقائيًا.") |
|
|
|
|
|
with gr.Row(): |
|
|
user_input = gr.Textbox( |
|
|
label="طلبك للوكيل", |
|
|
placeholder="مثال: أريد وكيل يجلب لي ايردروبات ويقوم بتنفيذ المهام." |
|
|
) |
|
|
|
|
|
run_button = gr.Button("🚀 تشغيل الوكيل") |
|
|
|
|
|
with gr.Column(): |
|
|
output_box = gr.Textbox( |
|
|
label="سير عمل الوكيل (الطبقات الأربعة)", |
|
|
interactive=False, |
|
|
lines=20 |
|
|
) |
|
|
|
|
|
run_button.click( |
|
|
fn=run_agent, |
|
|
inputs=[user_input, output_box], |
|
|
outputs=output_box |
|
|
) |
|
|
|
|
|
demo.launch() |