last_edit / app.py
Moharek
Fix: Simplify routing with catch-all handler for /app/ prefix
81188cd
"""
Moharek GEO Platform - Hugging Face Space
"""
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent))
import gradio as gr
from fastapi import FastAPI, Request
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
# Import API
from server.api import app as fastapi_app
# Frontend path
frontend_path = Path(__file__).parent / "frontend"
# Serve frontend files - catch-all handler
@fastapi_app.get("/{full_path:path}")
async def serve_frontend(full_path: str):
# Remove /app/ prefix if present
if full_path.startswith('app/'):
full_path = full_path[4:]
# Default to index.html for root
if not full_path or full_path == '/':
full_path = 'index.html'
# Try to serve the file
file_path = frontend_path / full_path
if file_path.exists() and file_path.is_file():
return FileResponse(str(file_path))
# If no extension, try adding .html
if '.' not in full_path:
html_path = frontend_path / f"{full_path}.html"
if html_path.exists():
return FileResponse(str(html_path))
return {"ok": False, "error": "not found"}
# Gradio interface
with gr.Blocks(title="Moharek GEO Platform") as demo:
gr.Markdown("# 🚀 Moharek GEO Platform")
gr.Markdown("## منصة تحسين محركات البحث بالذكاء الاصطناعي")
with gr.Row():
website_url = gr.Textbox(label="رابط الموقع", placeholder="https://example.com")
org_name = gr.Textbox(label="اسم الشركة", placeholder="اسم شركتك")
max_pages = gr.Slider(10, 100, 50, step=10, label="عدد الصفحات")
analyze_btn = gr.Button("🚀 ابدأ التحليل", variant="primary")
output_status = gr.Textbox(label="حالة التحليل", lines=3)
output_json = gr.JSON(label="نتائج التحليل")
gr.Markdown("""
### 🌐 الوصول الكامل
- [لوحة التحكم](/portal.html)
- [الصفحة الرئيسية](/index.html)
- [API Docs](/api/docs)
""")
def analyze(url, name, pages):
if not url or not url.startswith('http'):
return "❌ الرجاء إدخال رابط صحيح", None
try:
import requests
response = requests.post(
"http://localhost:7860/api/jobs",
json={"url": url, "org_name": name or "Unknown", "org_url": url, "max_pages": int(pages), "runs": 1},
timeout=30
)
if response.status_code == 200:
data = response.json()
if data.get("ok"):
return f"✅ تم إنشاء التحليل! Job ID: {data.get('job_id')}", data
return f"❌ خطأ: {data.get('error')}", None
return f"❌ خطأ: {response.status_code}", None
except Exception as e:
return f"❌ خطأ: {str(e)}", None
analyze_btn.click(analyze, [website_url, org_name, max_pages], [output_status, output_json])
# Mount Gradio to FastAPI at /gradio
app = gr.mount_gradio_app(fastapi_app, demo, path="/gradio")
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=7860)