|
|
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 = [] |
|
|
|
|
|
|
|
|
|
|
|
current_history = list(history) |
|
|
current_history.append((user_msg, "🛠️ **كوين:** أهلاً بك! بدأت العمل على معمارية المشروع...")) |
|
|
yield current_history, project_state["files"], project_state.get("last_zip") |
|
|
|
|
|
try: |
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
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 |
|
|
) |
|
|
|
|
|
|
|
|
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)**") |
|
|
|
|
|
|
|
|
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] |
|
|
) |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo.launch(theme=gr.themes.Soft()) |
|
|
|