File size: 3,344 Bytes
9dbab65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import os
import gradio as gr
import json
import re
import zipfile
import shutil
from huggingface_hub import InferenceClient

client = InferenceClient(api_key=os.environ.get("HF_TOKEN"))
project_state = {"files": {}, "last_zip": None}

def agents_dialogue(user_msg, file_upload, history):
    if not history: history = []
    
    # تنسيق Tuples الكلاسيكي المتوافق مع الجميع: [(user, bot), (user, bot)]
    # سنضيف رسالة المستخدم مع رد أولي من كوين
    current_history = list(history)
    current_history.append((user_msg, "🛠️ **كوين:** أهلاً بك! بدأت العمل على معمارية المشروع..."))
    yield current_history, project_state["files"], project_state.get("last_zip")

    try:
        # 1. كوين يبني
        qwen_resp = client.chat.completions.create(
            model="Qwen/Qwen2.5-Coder-32B-Instruct",
            messages=[{"role": "user", "content": f"Generate JSON structure for: {user_msg}"}],
            max_tokens=2000
        )
        draft_json = qwen_resp.choices[0].message.content

        # 2. ديب سيك يتدخل (نضيفها كرسالة جديدة بدون مدخل مستخدم)
        current_history.append((None, "🔍 **ديب سيك:** لحظة، سأقوم بمراجعة الكود وتصحيحه الآن..."))
        yield current_history, project_state["files"], project_state.get("last_zip")

        ds_resp = client.chat.completions.create(
            model="deepseek-ai/DeepSeek-V3",
            messages=[{"role": "user", "content": f"Audit and fix this JSON: {draft_json}"}],
            max_tokens=2000
        )
        
        # 3. النتيجة النهائية
        match = re.search(r'(\{.*\})', ds_resp.choices[0].message.content, re.DOTALL)
        if match:
            project_state["files"] = json.loads(match.group(1))
            current_history.append((None, "✅ **النظام:** تم الاتفاق البرمجي! الملفات جاهزة الآن."))
            yield current_history, project_state["files"], None
            
    except Exception as e:
        current_history.append((None, f"❌ **خطأ:** {str(e)}"))
        yield current_history, project_state["files"], None

# --- واجهة محصنة ضد أخطاء الإصدارات ---
with gr.Blocks() as demo:
    gr.Markdown("# 🏢 **Agentic Developer Room (Ultra-Stable)**")
    
    # إزالة خاصية type تماماً لضمان التشغيل
    chatbot = gr.Chatbot(label="نقاش الوكلاء", height=500)
    
    with gr.Row():
        msg = gr.Textbox(placeholder="اطلب تعديلاً أو ميزة...", scale=4, show_label=False)
        file_btn = gr.UploadButton("📁 ZIP", file_types=[".zip"], scale=1)
    
    submit_btn = gr.Button("إطلاق النقاش البرمجي ⚡", variant="primary")

    with gr.Accordion("📂 ملفات المشروع", open=False):
        json_view = gr.JSON()
        download_btn = gr.File()

    submit_btn.click(
        agents_dialogue, 
        inputs=[msg, file_btn, chatbot], 
        outputs=[chatbot, json_view, download_btn]
    )

# نقل الـ theme هنا يحل التنبيه في Gradio 6 ويقبل العمل في النسخ الأقدم
if __name__ == "__main__":
    demo.launch(theme=gr.themes.Soft())