Chat.bot / app.py
1Egyb's picture
Upload 6 files
9dbab65 verified
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())