File size: 7,570 Bytes
7e014a3 642c5d6 7e014a3 f39f28f 7e014a3 120041a fc829bc 642c5d6 f39f28f 120041a 7e014a3 120041a fc829bc 642c5d6 120041a e67dd14 642c5d6 120041a ec691c5 35f3499 120041a ec691c5 120041a be3d7cd 120041a be3d7cd 120041a be3d7cd 120041a fc829bc be3d7cd 120041a be3d7cd 120041a 7e014a3 120041a fc829bc ec691c5 120041a fc829bc 703674f 120041a fc829bc 703674f fc829bc 120041a fc829bc 120041a fc829bc 120041a fc829bc 642c5d6 120041a 642c5d6 120041a e67dd14 f39f28f 120041a ec691c5 120041a bb747c4 120041a ec691c5 e67dd14 fc829bc 120041a be3d7cd 120041a fc829bc e67dd14 642c5d6 be3d7cd fc829bc ec691c5 120041a ec691c5 642c5d6 be3d7cd fc829bc be3d7cd fc829bc be3d7cd 120041a be3d7cd 120041a be3d7cd 642c5d6 120041a e67dd14 fc829bc be3d7cd fc829bc be3d7cd fc829bc ec691c5 fc829bc c8e6f64 fc829bc 35f3499 642c5d6 77bc081 |
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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
import os
import re
import shutil
import tempfile
import zipfile
from pathlib import Path
import gradio as gr
from huggingface_hub import InferenceClient
# --------------------------- الإعدادات ---------------------------
ALLOWED_EXTENSIONS = {
'.py', '.js', '.html', '.css', '.txt', '.json', '.md',
'.php', '.xml', '.csv', '.zip', '.png', '.jpg', '.svg', '.ico'
}
MODEL_ID = os.environ.get('HF_MODEL_ID', 'Qwen/Qwen3-Coder-480B-A35B-Instruct-FP8')
HF_TOKEN = os.environ.get('HF_TOKEN')
class ProjectTank:
def __init__(self):
self.work_dir = tempfile.mkdtemp(prefix="tank_dev_")
self.files_cache = {}
self.structure = ""
def sync_files(self, file_objs):
if not file_objs: return "⚠️ لم يتم اختيار ملفات.", []
if os.path.exists(self.work_dir):
shutil.rmtree(self.work_dir)
self.work_dir = tempfile.mkdtemp(prefix="tank_dev_")
files = file_objs if isinstance(file_objs, list) else [file_objs]
for f in files:
name = os.path.basename(f.name)
if name.lower().endswith('.zip'):
with zipfile.ZipFile(f.name, 'r') as z:
z.extractall(self.work_dir)
else:
dest = os.path.join(self.work_dir, name)
shutil.copy(f.name, dest)
self._reload_memory()
return f"🚀 تم تحميل {len(self.files_cache)} ملف بنجاح.", list(self.files_cache.keys())
def _reload_memory(self):
self.files_cache = {}
structure_list = []
for p in Path(self.work_dir).rglob('*'):
if p.is_file():
rel = str(p.relative_to(self.work_dir)).replace("\\", "/")
structure_list.append(f"- {rel}")
try:
if p.suffix.lower() in ['.html', '.css', '.js', '.json', '.txt', '.md', '.py']:
self.files_cache[rel] = p.read_text(encoding='utf-8', errors='ignore')
except: pass
self.structure = "\n".join(structure_list)
def update_file_content(self, filename, content):
path = os.path.join(self.work_dir, filename)
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, 'w', encoding='utf-8') as f:
f.write(content)
self.files_cache[filename] = content
def build_preview(self):
"""رادار ذكي للبحث عن ملف التشغيل في أي عمق"""
all_keys = list(self.files_cache.keys())
# البحث عن أي ملف ينتهي بـ index.html بغض النظر عن المجلد
entry = next((k for k in all_keys if k.lower().endswith('index.html')), None)
if not entry:
# إذا لم يجد index، يبحث عن أي ملف html
entry = next((k for k in all_keys if k.endswith('.html')), None)
if not entry:
return f"<div style='text-align:center; padding:50px;'>⚠️ لم يتم العثور على ملف HTML للمعاينة.<br>الملفات المكتشفة: {str(all_keys[:3])}</div>"
html = self.files_cache[entry]
base_dir = os.path.dirname(entry)
# حقن CSS و JS مع مراعاة المسارات
for name, code in self.files_cache.items():
fname = os.path.basename(name)
if name.endswith('.css'):
html = re.sub(rf'<link.*?href=["\'].*?{fname}["\'].*?>', f"<style>\n{code}\n</style>", html)
if name.endswith('.js'):
html = re.sub(rf'<script.*?src=["\'].*?{fname}["\'].*?></script>', f"<script>\n{code}\n</script>", html)
return html
tank = ProjectTank()
client = InferenceClient(api_key=HF_TOKEN) if HF_TOKEN else None
# --------------------------- منطق الذكاء الاصطناعي ---------------------------
def chat_and_code(message, history):
if not client:
yield history + [{"role": "assistant", "content": "⚠️ مفقود HF_TOKEN"}]
return
context = f"### PROJECT STRUCTURE ###\n{tank.structure}\n\n### FULL FILE CONTENTS ###\n"
for name, content in tank.files_cache.items():
context += f"--- FILE: {name} ---\n{content}\n"
system_instr = (
"You are an AI IDE. Provide short explanations then output the FULL modified files using this format:\n"
"[TARGET: filename]\n```\nUpdated Code\n```"
)
messages = [{"role": "system", "content": system_instr}]
for m in history:
messages.append({"role": m["role"], "content": str(m["content"])})
messages.append({"role": "user", "content": f"{context}\n\nUser Request: {message}"})
response = ""
history.append({"role": "user", "content": message})
history.append({"role": "assistant", "content": ""})
try:
stream = client.chat_completion(model=MODEL_ID, messages=messages, max_tokens=6000, stream=True)
for chunk in stream:
if hasattr(chunk, 'choices') and chunk.choices:
token = chunk.choices[0].delta.content
if token:
response += token
history[-1]["content"] = response
yield history
except Exception as e:
history[-1]["content"] = f"❌ خطأ: {str(e)}"
yield history
def apply_and_preview(history):
if not history: return "لا توجد بيانات", None, []
content = history[-1]["content"]
matches = re.findall(r"\[TARGET:\s*(.*?)\]\s*```.*?\n(.*?)```", content, re.DOTALL | re.IGNORECASE)
for filename, code in matches:
tank.update_file_content(filename.strip(), code.strip())
tank._reload_memory()
return "✅ تم التحديث بنجاح!", tank.build_preview(), list(tank.files_cache.keys())
# --------------------------- الواجهة (Gradio هي الأضمن في الاستقرار) ---------------------------
with gr.Blocks(theme=gr.themes.Monochrome(), title="AI Tank IDE Pro") as demo:
gr.Markdown("# 🛡️ AI Tank IDE Pro\n**نظام المعاينة العميقة والتحصين ضد الأخطاء**")
with gr.Row():
with gr.Column(scale=1):
upload = gr.UploadButton("📁 ارفع المشروع (ZIP/Files)", file_count="multiple")
file_list = gr.Dropdown(label="ملفات المشروع")
status = gr.Markdown("🟢 جاهز")
chat = gr.Chatbot(type="messages", height=400)
msg = gr.Textbox(placeholder="اطلب تعديلاً...", show_label=False)
with gr.Row():
send = gr.Button("إرسال", variant="primary")
apply = gr.Button("⚡ تطبيق المعاينة", variant="stop")
with gr.Column(scale=2):
with gr.Tabs():
with gr.TabItem("🌐 المعاينة الفورية"):
preview = gr.HTML()
with gr.TabItem("📝 المحرر"):
editor = gr.Code(label="كود الملف", lines=25, interactive=True)
# ربط الأحداث
upload.upload(tank.sync_files, upload, [status, file_list]).then(tank.build_preview, None, preview)
send.click(chat_and_code, [msg, chat], chat).then(lambda: "", None, msg)
apply.click(apply_and_preview, chat, [status, preview, file_list])
file_list.change(lambda n: tank.files_cache.get(n, ""), file_list, editor)
if __name__ == "__main__":
demo.queue().launch(server_name="0.0.0.0", server_port=7860) |