Spaces:
Configuration error
Configuration error
Download app.py from Cyber00100ai/ARISE: direct link, hf CLI and curl.
- Browser
- Download file 12.4 kB
-
https://huggingface.co/spaces/Cyber00100ai/ARISE/resolve/main/app.py
- Command line
-
hf download hf://spaces/Cyber00100ai/ARISE/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Cyber00100ai/ARISE/resolve/main/app.py
12.4 kB
| import os | |
| import json | |
| from fastapi import FastAPI, HTTPException | |
| from fastapi.responses import HTMLResponse | |
| from pydantic import BaseModel | |
| from huggingface_hub import hf_hub_download | |
| from llama_cpp import Llama | |
| from typing import Optional | |
| app = FastAPI(title="ARISE Shadow Guide") | |
| # Setup Model | |
| MODEL_REPO = "unsloth/gemma-2-27b-it-GGUF" # مثال | |
| MODEL_FILENAME = "gemma-2-27b-it-Q4_K_M.gguf" # مثال | |
| print("Downloading model. This might take a while...") | |
| try: | |
| model_path = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILENAME) | |
| llm = Llama( | |
| model_path=model_path, | |
| n_ctx=4096, | |
| n_threads=4, | |
| ) | |
| print("Model loaded successfully!") | |
| except Exception as e: | |
| print(f"Error loading model: {e}") | |
| llm = None | |
| # --------- نماذج البيانات (API Models) --------- | |
| class HunterStats(BaseModel): | |
| name: str = "الصياد" | |
| level: int = 1 | |
| weight: float = 70.0 | |
| height: float = 175.0 | |
| age: int = 20 | |
| targetGoal: str = "الارتقاء" | |
| strength: int = 10 | |
| vitality: int = 10 | |
| agility: int = 10 | |
| currentStreak: int = 0 | |
| class ChatRequest(BaseModel): | |
| hunter_stats: HunterStats | |
| message: str | |
| class WorkoutRequest(BaseModel): | |
| hunter_stats: HunterStats | |
| # --------- برومبت النظام --------- | |
| SYSTEM_PROMPT = """أنت "مرشد الظلال" (Shadow Guide)، مساعد ذكي ومدرب شخصي في نظام ARISE. | |
| تتحدث باللغة العربية بأسلوب غامض، محفز، وقوي يشبه قادة الظلال. | |
| مهمتك مساعدة "الصياد" للوصول إلى هدفه من خلال تحليل بياناته. | |
| """ | |
| # --------- واجهة الويب (Web UI) --------- | |
| HTML_TEMPLATE = """ | |
| <!DOCTYPE html> | |
| <html lang="ar" dir="rtl"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>ARISE - Shadow Guide Server</title> | |
| <link href="https://fonts.googleapis.com/css2?family=Cairo:wght@400;700&display=swap" rel="stylesheet"> | |
| <style> | |
| body { | |
| background-color: #0b0b14; | |
| color: #fff; | |
| font-family: 'Cairo', sans-serif; | |
| margin: 0; | |
| padding: 20px; | |
| } | |
| .container { | |
| max-width: 800px; | |
| margin: 0 auto; | |
| background: linear-gradient(180deg, #180924 0%, #0b0b14 100%); | |
| border: 1px solid #6A1B9A; | |
| border-radius: 10px; | |
| padding: 20px; | |
| box-shadow: 0 0 20px rgba(106, 27, 154, 0.4); | |
| } | |
| h1 { | |
| color: #d178ff; | |
| text-align: center; | |
| border-bottom: 1px solid #4a148c; | |
| padding-bottom: 15px; | |
| } | |
| .stats-grid { | |
| display: grid; | |
| grid-template-columns: repeat(auto-fit, minmax(150px, 1)); | |
| gap: 15px; | |
| margin-bottom: 25px; | |
| } | |
| .input-group { | |
| display: flex; | |
| flex-direction: column; | |
| } | |
| label { | |
| color: #ab47bc; | |
| font-size: 14px; | |
| margin-bottom: 5px; | |
| } | |
| input { | |
| background: #000; | |
| border: 1px solid #5e35b1; | |
| color: #fff; | |
| padding: 10px; | |
| border-radius: 5px; | |
| font-family: 'Cairo', sans-serif; | |
| } | |
| .buttons { | |
| display: flex; | |
| gap: 15px; | |
| margin-bottom: 25px; | |
| } | |
| button { | |
| flex: 1; | |
| background: #4a148c; | |
| color: #fff; | |
| border: none; | |
| padding: 15px; | |
| border-radius: 5px; | |
| cursor: pointer; | |
| font-weight: bold; | |
| font-family: 'Cairo', sans-serif; | |
| transition: all 0.3s; | |
| } | |
| button:hover { | |
| background: #6a1b9a; | |
| box-shadow: 0 0 10px #6a1b9a; | |
| } | |
| .chat-box { | |
| background: #000; | |
| border: 1px solid #4a148c; | |
| padding: 15px; | |
| height: 300px; | |
| overflow-y: auto; | |
| border-radius: 5px; | |
| margin-bottom: 15px; | |
| } | |
| .chat-message { | |
| margin-bottom: 10px; | |
| padding: 10px; | |
| border-radius: 5px; | |
| } | |
| .bot { background: #311b92; } | |
| .user { background: #1a237e; text-align: left; } | |
| .loader { | |
| display: none; | |
| color: #d178ff; | |
| text-align: center; | |
| font-weight: bold; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <div class="container"> | |
| <h1>[ ARISE ] مرشد الظلال</h1> | |
| <div class="stats-grid"> | |
| <div class="input-group"> | |
| <label>الاسم</label> | |
| <input type="text" id="stats-name" value="صياد"> | |
| </div> | |
| <div class="input-group"> | |
| <label>الوزن (kg)</label> | |
| <input type="number" id="stats-weight" value="70"> | |
| </div> | |
| <div class="input-group"> | |
| <label>الطول (cm)</label> | |
| <input type="number" id="stats-height" value="175"> | |
| </div> | |
| <div class="input-group"> | |
| <label>العمر</label> | |
| <input type="number" id="stats-age" value="20"> | |
| </div> | |
| <div class="input-group"> | |
| <label>الهدف الحالي</label> | |
| <input type="text" id="stats-goal" value="حرق دهون وبناء عضلي"> | |
| </div> | |
| </div> | |
| <div class="buttons"> | |
| <button onclick="generateWorkout()">✨ توليد تمرين مخصص اليوم</button> | |
| </div> | |
| <div class="loader" id="loader">الكيان يستشعر هالتك... يرجى الانتظار</div> | |
| <div class="chat-box" id="chat-box"> | |
| <div class="chat-message bot"> | |
| <strong>المرشد:</strong> أهلاً بالصياد... أنا مرشد الظلال. ادخل بياناتك واسألني عما تريد أو اطلب تمارينك. | |
| </div> | |
| </div> | |
| <div style="display: flex; gap: 10px;"> | |
| <input type="text" id="chat-input" placeholder="استشر مرشد الظلال..." style="flex: 1;"> | |
| <button style="flex: 0.2; padding: 10px;" onclick="sendMessage()">إرسال</button> | |
| </div> | |
| </div> | |
| <script> | |
| function getStats() { | |
| return { | |
| name: document.getElementById('stats-name').value, | |
| level: 1, | |
| weight: parseFloat(document.getElementById('stats-weight').value), | |
| height: parseFloat(document.getElementById('stats-height').value), | |
| age: parseInt(document.getElementById('stats-age').value), | |
| targetGoal: document.getElementById('stats-goal').value, | |
| strength: 10, vitality: 10, agility: 10, currentStreak: 0 | |
| }; | |
| } | |
| function addMessage(sender, text, isBot) { | |
| const box = document.getElementById('chat-box'); | |
| const msgDiv = document.createElement('div'); | |
| msgDiv.className = 'chat-message ' + (isBot ? 'bot' : 'user'); | |
| msgDiv.innerHTML = '<strong>' + sender + ':</strong> ' + text; | |
| box.appendChild(msgDiv); | |
| box.scrollTop = box.scrollHeight; | |
| } | |
| async function sendMessage() { | |
| const inputField = document.getElementById('chat-input'); | |
| const text = inputField.value; | |
| if(!text) return; | |
| inputField.value = ''; | |
| addMessage('أنت', text, false); | |
| document.getElementById('loader').style.display = 'block'; | |
| try { | |
| const res = await fetch('/chat', { | |
| method: 'POST', | |
| headers: {'Content-Type': 'application/json'}, | |
| body: JSON.stringify({ | |
| hunter_stats: getStats(), | |
| message: text | |
| }) | |
| }); | |
| const data = await res.json(); | |
| addMessage('المرشد', data.reply || "لا يوجد رد.", true); | |
| } catch(e) { | |
| addMessage('المرشد', "خطأ في الاتصال بالهالة النجمية.", true); | |
| } | |
| document.getElementById('loader').style.display = 'none'; | |
| } | |
| async function generateWorkout() { | |
| document.getElementById('loader').style.display = 'block'; | |
| addMessage('أنت', '[طلبت توليد مهمة من النظام]', false); | |
| try { | |
| const res = await fetch('/generate_workout', { | |
| method: 'POST', | |
| headers: {'Content-Type': 'application/json'}, | |
| body: JSON.stringify({hunter_stats: getStats()}) | |
| }); | |
| const data = await res.json(); | |
| if(data.workout) { | |
| const wk = data.workout; | |
| const text = `تم تحديد المهمة!<br><b>${wk.title}</b><br>${wk.description}<br><i>المطلوب: ${wk.sets} جولات بـ ${wk.reps} تكرار</i>`; | |
| addMessage('النظام', text, true); | |
| } else { | |
| addMessage('النظام', 'فشل توليد التمارين', true); | |
| } | |
| } catch(e) { | |
| addMessage('النظام', 'خطأ في الاتصال.', true); | |
| } | |
| document.getElementById('loader').style.display = 'none'; | |
| } | |
| </script> | |
| </body> | |
| </html> | |
| """ | |
| def serve_ui(): | |
| return HTML_TEMPLATE | |
| # --------- واجهات برمجية التطبيق (API Endpoints للـ Flutter) --------- | |
| def shadow_chat(request: ChatRequest): | |
| if not llm: | |
| raise HTTPException(status_code=500, detail="Model is not ready.") | |
| stats = request.hunter_stats | |
| prompt = f"""{SYSTEM_PROMPT} | |
| [بيانات الصياد] | |
| الاسم: {stats.name} | العمر: {stats.age} | الوزن: {stats.weight} كجم | الطول: {stats.height} سم | |
| الهدف الحالي: {stats.targetGoal} | |
| سؤال الصياد: {request.message} | |
| مرشد الظلال:""" | |
| response = llm( | |
| prompt, | |
| max_tokens=256, | |
| stop=["الصياد:"], | |
| echo=False | |
| ) | |
| reply = response["choices"][0]["text"].strip() | |
| return {"reply": reply} | |
| def generate_workout(request: WorkoutRequest): | |
| if not llm: | |
| raise HTTPException(status_code=500, detail="Model is not ready.") | |
| stats = request.hunter_stats | |
| prompt = f"""{SYSTEM_PROMPT} | |
| قم بتوليد تمرين مخصص وسياقي اليوم للصياد. قم بحساب التمارين المناسبة لتحقيق هدفه مع مراعاة وزنه. | |
| الاسم: {stats.name} | العمر: {stats.age} | الوزن: {stats.weight} كجم | الطول: {stats.height} سم | |
| الهدف: {stats.targetGoal} | |
| يجب أن ترد فقط بنص بصيغة JSON صالح برمجياً. | |
| المفاتيح المطلوبة: "title" (اسم التمرين), "description" (شرح التمرين والربط بقصة الظلال), "sets" (رقم), "reps" (رقم), "statType" (نوع الإحصائية مثل strength أو agility). | |
| تمرين اليوم بصيغة JSON: | |
| {{""" | |
| response = llm( | |
| prompt, | |
| max_tokens=300, | |
| stop=["}"], | |
| echo=False | |
| ) | |
| raw_text = "{" + response["choices"][0]["text"].strip() | |
| if not raw_text.endswith("}"): | |
| raw_text += "}" | |
| try: | |
| workout_data = json.loads(raw_text) | |
| return {"workout": workout_data} | |
| except: | |
| return {"workout": {"title": "تمرين الضغط الأساسي", "description": "يجب أن تبني جسدك حتى لو انقطع الاتصال.", "sets": 3, "reps": 12, "statType": "strength"}} | |