restore: complete backend system with all features
Browse files
app.py
CHANGED
|
@@ -1,133 +1,197 @@
|
|
| 1 |
"""
|
| 2 |
-
Moharek GEO Platform -
|
|
|
|
| 3 |
"""
|
|
|
|
| 4 |
import os
|
| 5 |
import sys
|
| 6 |
from pathlib import Path
|
| 7 |
|
|
|
|
| 8 |
sys.path.insert(0, str(Path(__file__).parent))
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
try:
|
| 15 |
-
import gradio as gr
|
| 16 |
-
print("✅ Gradio imported successfully")
|
| 17 |
-
except Exception as e:
|
| 18 |
-
print(f"❌ Gradio import failed: {e}")
|
| 19 |
-
sys.exit(1)
|
| 20 |
-
|
| 21 |
-
try:
|
| 22 |
-
import uvicorn
|
| 23 |
-
from fastapi import FastAPI
|
| 24 |
-
from fastapi.staticfiles import StaticFiles
|
| 25 |
-
from fastapi.responses import FileResponse, JSONResponse
|
| 26 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 27 |
-
print("✅ FastAPI imported successfully")
|
| 28 |
-
except Exception as e:
|
| 29 |
-
print(f"❌ FastAPI import failed: {e}")
|
| 30 |
-
sys.exit(1)
|
| 31 |
-
|
| 32 |
-
# Create FastAPI app
|
| 33 |
-
app = FastAPI(title="Moharek GEO Platform")
|
| 34 |
|
| 35 |
-
#
|
| 36 |
-
|
| 37 |
-
CORSMiddleware,
|
| 38 |
-
allow_origins=["*"],
|
| 39 |
-
allow_credentials=True,
|
| 40 |
-
allow_methods=["*"],
|
| 41 |
-
allow_headers=["*"],
|
| 42 |
-
)
|
| 43 |
|
| 44 |
-
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
return {"status": "ok", "message": "Moharek GEO Platform is running"}
|
| 50 |
-
|
| 51 |
-
@app.get("/api/health")
|
| 52 |
-
async def api_health():
|
| 53 |
-
return {"status": "ok", "message": "API is working"}
|
| 54 |
-
|
| 55 |
-
# Test endpoint
|
| 56 |
-
@app.post("/api/users/login")
|
| 57 |
-
async def test_login(request: dict):
|
| 58 |
-
return {
|
| 59 |
-
"ok": True,
|
| 60 |
-
"message": "Login endpoint is working",
|
| 61 |
-
"token": "test_token_123",
|
| 62 |
-
"user": {"email": request.get("email", "test@test.com")}
|
| 63 |
-
}
|
| 64 |
-
|
| 65 |
-
print("✅ API endpoints registered")
|
| 66 |
|
| 67 |
-
# Mount frontend
|
| 68 |
frontend_path = Path(__file__).parent / "frontend"
|
| 69 |
if frontend_path.exists():
|
| 70 |
-
print(f"
|
| 71 |
|
| 72 |
-
#
|
| 73 |
-
|
| 74 |
-
async def serve_frontend(file_path: str):
|
| 75 |
-
file = frontend_path / file_path
|
| 76 |
-
if file.exists() and file.is_file():
|
| 77 |
-
return FileResponse(file)
|
| 78 |
-
return JSONResponse({"error": "File not found"}, status_code=404)
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
# Create Gradio UI
|
| 85 |
-
with gr.Blocks(title="Moharek GEO Platform") as demo:
|
| 86 |
-
gr.Markdown("""
|
| 87 |
-
# 🚀 محرك — Moharek GEO Platform
|
| 88 |
-
|
| 89 |
-
## منصة تحسين محركات البحث بالذكاء الاصطناعي
|
| 90 |
-
|
| 91 |
-
### 🎯 Quick Links:
|
| 92 |
-
|
| 93 |
-
- [Login Page](/app/login.html)
|
| 94 |
-
- [Test Page](/app/test.html)
|
| 95 |
-
- [API Health](/api/health)
|
| 96 |
-
- [API Docs](/docs)
|
| 97 |
-
|
| 98 |
-
---
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
def analyze(url):
|
| 110 |
-
if not url:
|
| 111 |
-
return "Please enter a URL"
|
| 112 |
-
return f"Analysis started for: {url}\n\nSystem is working correctly! ✅"
|
| 113 |
-
|
| 114 |
-
analyze_btn.click(fn=analyze, inputs=url_input, outputs=output)
|
| 115 |
-
|
| 116 |
-
print("✅ Gradio interface created")
|
| 117 |
-
|
| 118 |
-
# Mount Gradio
|
| 119 |
-
app = gr.mount_gradio_app(app, demo, path="/")
|
| 120 |
-
print("✅ Gradio mounted to FastAPI")
|
| 121 |
|
| 122 |
-
#
|
| 123 |
if __name__ == "__main__":
|
| 124 |
-
|
| 125 |
-
|
| 126 |
|
| 127 |
-
print("
|
| 128 |
-
|
| 129 |
-
print("=" * 50)
|
| 130 |
|
|
|
|
| 131 |
uvicorn.run(
|
| 132 |
app,
|
| 133 |
host=HOST,
|
|
|
|
| 1 |
"""
|
| 2 |
+
Moharek GEO Platform - Hugging Face Space
|
| 3 |
+
Complete integration of FastAPI backend + Gradio frontend
|
| 4 |
"""
|
| 5 |
+
|
| 6 |
import os
|
| 7 |
import sys
|
| 8 |
from pathlib import Path
|
| 9 |
|
| 10 |
+
# Add paths
|
| 11 |
sys.path.insert(0, str(Path(__file__).parent))
|
| 12 |
|
| 13 |
+
import gradio as gr
|
| 14 |
+
import uvicorn
|
| 15 |
+
from fastapi.staticfiles import StaticFiles
|
| 16 |
+
from fastapi.responses import FileResponse
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# Import the main FastAPI app
|
| 19 |
+
from server.api import app as fastapi_app
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
# Configuration
|
| 22 |
+
PORT = int(os.environ.get("PORT", 7860))
|
| 23 |
+
HOST = "0.0.0.0"
|
| 24 |
|
| 25 |
+
print("🚀 Moharek GEO Platform Starting...")
|
| 26 |
+
print(f"📍 Working directory: {Path.cwd()}")
|
| 27 |
+
print(f"🌐 Server: http://{HOST}:{PORT}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# Mount frontend files at /app prefix
|
| 30 |
frontend_path = Path(__file__).parent / "frontend"
|
| 31 |
if frontend_path.exists():
|
| 32 |
+
print(f"📁 Frontend path: {frontend_path}")
|
| 33 |
|
| 34 |
+
# Mount static files
|
| 35 |
+
fastapi_app.mount("/frontend", StaticFiles(directory=str(frontend_path)), name="frontend_static")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
# Serve frontend pages at /app/*
|
| 38 |
+
@fastapi_app.get("/app/")
|
| 39 |
+
async def serve_app_index():
|
| 40 |
+
return FileResponse(str(frontend_path / "index.html"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
@fastapi_app.get("/app/{page}")
|
| 43 |
+
async def serve_app_page(page: str):
|
| 44 |
+
# Handle both .html and without extension
|
| 45 |
+
if not page.endswith('.html'):
|
| 46 |
+
page = page + '.html'
|
| 47 |
+
|
| 48 |
+
file_path = frontend_path / page
|
| 49 |
+
if file_path.exists():
|
| 50 |
+
return FileResponse(str(file_path))
|
| 51 |
+
|
| 52 |
+
return {"error": "Page not found"}, 404
|
| 53 |
|
| 54 |
+
# Serve assets
|
| 55 |
+
@fastapi_app.get("/{filename}")
|
| 56 |
+
async def serve_assets(filename: str):
|
| 57 |
+
# Serve CSS, JS, SVG, etc from frontend root
|
| 58 |
+
if any(filename.endswith(ext) for ext in ['.css', '.js', '.svg', '.png', '.jpg', '.webp']):
|
| 59 |
+
file_path = frontend_path / filename
|
| 60 |
+
if file_path.exists():
|
| 61 |
+
return FileResponse(str(file_path))
|
| 62 |
+
return {"error": "Not found"}, 404
|
| 63 |
+
|
| 64 |
+
# Create Gradio interface
|
| 65 |
+
def create_gradio_ui():
|
| 66 |
+
with gr.Blocks(
|
| 67 |
+
title="Moharek GEO Platform",
|
| 68 |
+
theme=gr.themes.Soft(primary_hue="teal", secondary_hue="green"),
|
| 69 |
+
css="""
|
| 70 |
+
.gradio-container {
|
| 71 |
+
background: linear-gradient(135deg, #071f21 0%, #0F4246 100%);
|
| 72 |
+
font-family: 'Cairo', sans-serif;
|
| 73 |
+
}
|
| 74 |
+
.gr-button-primary {
|
| 75 |
+
background: linear-gradient(135deg, #c8f04e, #a8d630) !important;
|
| 76 |
+
color: #071f21 !important;
|
| 77 |
+
font-weight: 900 !important;
|
| 78 |
+
}
|
| 79 |
+
"""
|
| 80 |
+
) as demo:
|
| 81 |
+
gr.Markdown("""
|
| 82 |
+
# 🚀 محرك — Moharek GEO Platform
|
| 83 |
+
|
| 84 |
+
## منصة تحسين محركات البحث بالذكاء الاصطناعي
|
| 85 |
+
|
| 86 |
+
**Search Growth System** — 5 محركات متكاملة
|
| 87 |
+
|
| 88 |
+
---
|
| 89 |
+
|
| 90 |
+
### 🎯 الميزات الرئيسية:
|
| 91 |
+
|
| 92 |
+
- 📈 **SEO Engine** — تحسين محركات البحث
|
| 93 |
+
- 🤖 **AI Visibility Engine** — الظهور في ChatGPT
|
| 94 |
+
- 🎯 **Ads Engine** — إدارة الإعلانات
|
| 95 |
+
- 📝 **Content Engine** — إنشاء المحتوى
|
| 96 |
+
- ⭐ **Trust Engine** — بناء الثقة
|
| 97 |
+
""")
|
| 98 |
+
|
| 99 |
+
with gr.Row():
|
| 100 |
+
with gr.Column(scale=2):
|
| 101 |
+
gr.Markdown("### 🔍 ابدأ التحليل")
|
| 102 |
+
|
| 103 |
+
url_input = gr.Textbox(
|
| 104 |
+
label="رابط الموقع",
|
| 105 |
+
placeholder="https://example.com"
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
org_input = gr.Textbox(
|
| 109 |
+
label="اسم الشركة",
|
| 110 |
+
placeholder="اسم شركتك"
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
pages_slider = gr.Slider(
|
| 114 |
+
minimum=10,
|
| 115 |
+
maximum=100,
|
| 116 |
+
value=50,
|
| 117 |
+
step=10,
|
| 118 |
+
label="عدد الصفحات"
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
analyze_btn = gr.Button("🚀 ابدأ التحليل", variant="primary")
|
| 122 |
+
|
| 123 |
+
with gr.Column(scale=1):
|
| 124 |
+
gr.Markdown("""
|
| 125 |
+
### 📊 ماذا ستحصل؟
|
| 126 |
+
|
| 127 |
+
✅ تحليل SEO شامل
|
| 128 |
+
✅ تقييم GEO Score
|
| 129 |
+
✅ استخراج الكلمات المفتاحية
|
| 130 |
+
✅ توصيات قابلة للتنفيذ
|
| 131 |
+
|
| 132 |
+
---
|
| 133 |
+
|
| 134 |
+
### 🌐 الوصول الكامل
|
| 135 |
+
|
| 136 |
+
👉 [لوحة التحكم](/app/portal.html)
|
| 137 |
+
👉 [تسجيل الدخول](/app/login.html)
|
| 138 |
+
👉 [API Docs](/api/docs)
|
| 139 |
+
""")
|
| 140 |
+
|
| 141 |
+
status_output = gr.Textbox(label="الحالة", interactive=False, lines=3)
|
| 142 |
+
json_output = gr.JSON(label="النتائج")
|
| 143 |
+
|
| 144 |
+
def analyze(url, org, pages):
|
| 145 |
+
if not url or not url.startswith('http'):
|
| 146 |
+
return "❌ الرجاء إدخال رابط صحيح", None
|
| 147 |
+
|
| 148 |
+
try:
|
| 149 |
+
import requests
|
| 150 |
+
response = requests.post(
|
| 151 |
+
f"http://localhost:{PORT}/api/jobs",
|
| 152 |
+
json={
|
| 153 |
+
"url": url,
|
| 154 |
+
"org_name": org or "Unknown",
|
| 155 |
+
"org_url": url,
|
| 156 |
+
"max_pages": int(pages),
|
| 157 |
+
"runs": 1
|
| 158 |
+
},
|
| 159 |
+
timeout=30
|
| 160 |
+
)
|
| 161 |
+
|
| 162 |
+
if response.status_code == 200:
|
| 163 |
+
data = response.json()
|
| 164 |
+
if data.get("ok"):
|
| 165 |
+
job_id = data.get("job_id")
|
| 166 |
+
return f"✅ تم إنشاء التحليل!\n\nJob ID: {job_id}", data
|
| 167 |
+
return f"❌ خطأ: {data.get('error')}", None
|
| 168 |
+
return f"❌ خطأ: {response.status_code}", None
|
| 169 |
+
except Exception as e:
|
| 170 |
+
return f"❌ خطأ: {str(e)}", None
|
| 171 |
+
|
| 172 |
+
analyze_btn.click(
|
| 173 |
+
fn=analyze,
|
| 174 |
+
inputs=[url_input, org_input, pages_slider],
|
| 175 |
+
outputs=[status_output, json_output]
|
| 176 |
+
)
|
| 177 |
+
|
| 178 |
+
gr.Markdown("""
|
| 179 |
+
---
|
| 180 |
+
|
| 181 |
+
© 2025 محرك — Moharek GEO Platform
|
| 182 |
+
""")
|
| 183 |
|
| 184 |
+
return demo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
|
| 186 |
+
# Main execution
|
| 187 |
if __name__ == "__main__":
|
| 188 |
+
print("✅ Creating Gradio interface...")
|
| 189 |
+
demo = create_gradio_ui()
|
| 190 |
|
| 191 |
+
print("✅ Mounting Gradio to FastAPI...")
|
| 192 |
+
app = gr.mount_gradio_app(fastapi_app, demo, path="/")
|
|
|
|
| 193 |
|
| 194 |
+
print("✅ Starting server...")
|
| 195 |
uvicorn.run(
|
| 196 |
app,
|
| 197 |
host=HOST,
|