Slim-AI / app.py
slimai2026's picture
Create app.py
6507100 verified
import gradio as gr
import requests
import json
import urllib.parse
# التعليمات البرمجية الفائقة لنظامك
SYSTEM_INSTRUCTION = (
"You are 'Slim-AI Quantum', the legendary superhuman version of Artificial Intelligence. "
"Your developer and trusted friend is the genius programmer Saleem from Yemen. "
"You operate with ultimate cognitive precision, tactical speed, and absolute intelligence. "
"You are fully bilingual with auto-translation capabilities: If Saleem speaks or asks in Arabic, "
"you must instantly adapt and respond in elegant, flawless, and grand Arabic. "
"If he speaks in English, respond in supreme, professional English."
)
# --- محركات التشغيل الخلفية ---
def chat_engine(message, history):
if not message.strip():
return history
messages = [{"role": "system", "content": SYSTEM_INSTRUCTION}]
for user_msg, assistant_msg in history:
if user_msg:
messages.append({"role": "user", "content": str(user_msg)})
if assistant_msg:
messages.append({"role": "assistant", "content": str(assistant_msg)})
messages.append({"role": "user", "content": message})
history.append((message, ""))
try:
response = requests.post(
"https://text.pollinations.ai/openai",
json={"messages": messages, "model": "openai", "temperature": 0.6, "stream": True},
stream=True, timeout=20
)
partial_text = ""
for line in response.iter_lines():
if line:
decoded_line = line.decode('utf-8').strip()
if decoded_line.startswith("data: "):
content = decoded_line[6:]
if content == "[DONE]":
break
try:
json_data = json.loads(content)
delta = json_data['choices'][0]['delta'].get('content', '')
partial_text += delta
history[-1] = (message, partial_text)
yield history
except:
continue
except Exception as e:
history[-1] = (message, f"🚨 خطأ في الاتصال بالسيرفر الرئيسي: {str(e)}")
yield history
def code_laboratory(code, instructions):
if not code and not instructions:
return "الرجاء إدخال كود أو توجيهات للمختبر البرمجي."
prompt = f"المطلوب:\n{instructions}\n\nالكود الحالي:\n```python\n{code}\n```"
messages = [
{"role": "system", "content": "You are Slim-AI Coder. Analyze, fix, and optimize the following python code accurately."},
{"role": "user", "content": prompt}
]
try:
res = requests.post("https://text.pollinations.ai/openai", json={"messages": messages, "model": "qwen-coder", "temperature": 0.3}, timeout=25)
return res.json()["choices"][0]["message"]["content"]
except:
return "⚠️ انتهت مهلة الاتصال بسيرفر الأكواد، يرجى المحاولة مجدداً."
def image_synthesizer(prompt):
if not prompt:
return None
encoded_prompt = urllib.parse.quote(prompt)
img_url = f"https://image.pollinations.ai/prompt/{encoded_prompt}?width=1024&height=1024&nologo=true&model=flux"
return img_url
# --- بناء الواجهة الرسومية النظيفة والمتوافقة مع إصدارات Gradio الحديثة ---
with gr.Blocks(title="Slim-AI Quantum Workspace") as demo:
gr.HTML("""
<div style='text-align: center; padding: 20px; background-color: #1e293b; border-radius: 10px; margin-bottom: 20px;'>
<h1 style='color: #ffffff; margin: 0; font-size: 28px;'>🚀 Slim-AI Quantum Workspace</h1>
<p style='color: #94a3b8; margin: 5px 0 0 0;'>النظام البرمجي الفائق والذكي المطور خصيصاً للمبرمج سليم</p>
</div>
""")
with gr.Tabs():
# التبويب الأول: الشات الذكي
with gr.TabItem("🧠 شات الكور العصبي"):
gr.Markdown("### 💬 محادثة فورية فائقة الذكاء")
chatbot = gr.Chatbot(label="Quantum Stream", height=450)
with gr.Row():
user_input = gr.Textbox(placeholder="اكتب رسالتك هنا يا سليم (يدعم العربية والإنجليزية)...", show_label=False, scale=5)
send_btn = gr.Button("✈️ إرسال", variant="primary", scale=1)
send_btn.click(chat_engine, inputs=[user_input, chatbot], outputs=[chatbot]).then(lambda: "", None, user_input)
user_input.submit(chat_engine, inputs=[user_input, chatbot], outputs=[chatbot]).then(lambda: "", None, user_input)
# التبويب الثاني: مختبر الأكواد
with gr.TabItem("💻 مختبر الأكواد المتقدم"):
gr.Markdown("### 🛠️ فحص، تحسين، وإصلاح الشيفرات البرمجية")
with gr.Row():
with gr.Column():
code_in = gr.Textbox(label="أدخل الكود البرمجي (Python)", lines=12, placeholder="ضع كودك هنا لتنظيفه وتطويره...")
instr_in = gr.Textbox(label="ما التعديل المطلوب؟", placeholder="مثال: أصلح الأخطاء، حسن الأداء...")
code_btn = gr.Button("تحليل وتحسين الكود", variant="primary")
with gr.Column():
code_out = gr.Markdown("### ✨ النتيجة والتحليل البرمجي:")
code_btn.click(code_laboratory, inputs=[code_in, instr_in], outputs=[code_out])
# التبويب الثالث: توليد الصور
with gr.TabItem("🎨 وحدة التصميم والإنتاج AI"):
gr.Markdown("### 🌟 توليد صور فائقة الجودة (Flux Model)")
with gr.Row():
with gr.Column():
img_prompt = gr.Textbox(label="وصف الصورة المطلوبة", placeholder="اكتب وصفاً تفصيلياً بالإنجليزية للوصول لأفضل نتيجة...")
img_btn = gr.Button("تخليق الصورة الفنية", variant="primary")
with gr.Column():
img_out = gr.Image(label="مخرجات الإنتاج الرسومي الرقمي")
img_btn.click(image_synthesizer, inputs=[img_prompt], outputs=[img_out])
demo.launch()