Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,25 @@
|
|
| 1 |
import os
|
| 2 |
-
import uuid
|
| 3 |
import gradio as gr
|
| 4 |
from fastapi import FastAPI, Request, File, UploadFile
|
| 5 |
from fastapi.responses import StreamingResponse, HTMLResponse
|
| 6 |
from openai import OpenAI
|
| 7 |
-
from gradio.oauth import get_space_authorized_header
|
| 8 |
|
| 9 |
-
# إعداد FastAPI
|
| 10 |
app = FastAPI()
|
| 11 |
|
| 12 |
-
# واجهة المستخدم (HTML/React) - م
|
| 13 |
HTML_UI = """
|
| 14 |
<!DOCTYPE html>
|
| 15 |
<html lang="ar" dir="rtl">
|
| 16 |
<head>
|
| 17 |
<meta charset="UTF-8">
|
| 18 |
-
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no
|
| 19 |
<title>Neural Vault</title>
|
| 20 |
<script src="https://cdn.tailwindcss.com"></script>
|
| 21 |
<script src="https://unpkg.com/react@18/umd/react.production.min.js"></script>
|
| 22 |
<script src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"></script>
|
| 23 |
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
|
| 24 |
<style>
|
| 25 |
-
body { background: #020617; color: #f8fafc; height: 100dvh; overflow: hidden; margin: 0; position: fixed; width: 100vw;
|
| 26 |
.chat-area { height: calc(100dvh - 160px); overflow-y: auto; padding: 20px; -webkit-overflow-scrolling: touch; }
|
| 27 |
.sidebar { position: fixed; top: 0; right: -100%; width: 80%; max-width: 300px; height: 100%; background: #0f172a; z-index: 2000; transition: 0.3s ease; border-left: 1px solid #1e293b; }
|
| 28 |
.sidebar.open { right: 0; }
|
|
@@ -59,8 +56,6 @@ HTML_UI = """
|
|
| 59 |
body: JSON.stringify({ messages: [...messages, userMsg] })
|
| 60 |
});
|
| 61 |
|
| 62 |
-
if (res.status === 401) { window.location.href = "/auth"; return; }
|
| 63 |
-
|
| 64 |
const reader = res.body.getReader();
|
| 65 |
let botMsg = { role: 'assistant', content: "" };
|
| 66 |
setMessages(p => [...p, botMsg]);
|
|
@@ -77,17 +72,6 @@ HTML_UI = """
|
|
| 77 |
setStatus("idle");
|
| 78 |
};
|
| 79 |
|
| 80 |
-
const uploadFile = async (e) => {
|
| 81 |
-
const file = e.target.files[0];
|
| 82 |
-
if(!file) return;
|
| 83 |
-
const fd = new FormData();
|
| 84 |
-
fd.append('file', file);
|
| 85 |
-
setStatus("uploading");
|
| 86 |
-
await fetch('/upload', { method: 'POST', body: fd });
|
| 87 |
-
setStatus("idle");
|
| 88 |
-
alert("تمت إضافة الملف للذاكرة");
|
| 89 |
-
};
|
| 90 |
-
|
| 91 |
return (
|
| 92 |
<div className="flex flex-col h-full">
|
| 93 |
<header className="p-4 border-b border-white/5 flex justify-between items-center bg-slate-900/50">
|
|
@@ -106,9 +90,6 @@ HTML_UI = """
|
|
| 106 |
|
| 107 |
<div className="input-bar">
|
| 108 |
<div className="flex gap-2 max-w-xl mx-auto">
|
| 109 |
-
<label className="bg-slate-800 p-3 rounded-xl cursor-pointer">
|
| 110 |
-
📎 <input type="file" className="hidden" onChange={uploadFile} />
|
| 111 |
-
</label>
|
| 112 |
<input
|
| 113 |
className="flex-1 bg-slate-900 border border-white/10 rounded-xl px-4 py-2 outline-none text-sm"
|
| 114 |
placeholder="اكتب سؤالك..."
|
|
@@ -125,12 +106,9 @@ HTML_UI = """
|
|
| 125 |
<h2 className="font-bold">الإعدادات</h2>
|
| 126 |
<button onClick={() => setSideOpen(false)}>✕</button>
|
| 127 |
</div>
|
| 128 |
-
<div className="
|
| 129 |
-
<
|
| 130 |
-
|
| 131 |
-
<p className="mt-2 text-emerald-400">الحالة: متصل عبر الرصيد الشخصي</p>
|
| 132 |
-
</div>
|
| 133 |
-
<button className="w-full py-3 bg-red-500/10 text-red-500 rounded-lg" onClick={() => window.location.href='/logout'}>تسجيل الخروج</button>
|
| 134 |
</div>
|
| 135 |
</div>
|
| 136 |
</div>
|
|
@@ -144,29 +122,19 @@ HTML_UI = """
|
|
| 144 |
"""
|
| 145 |
|
| 146 |
@app.get("/")
|
| 147 |
-
async def ui(
|
| 148 |
-
# إذا لم يسجل دخوله، نوجهه لصفحة الدخول في Gradio
|
| 149 |
-
if not get_space_authorized_header(request):
|
| 150 |
-
return HTMLResponse("<html><script>window.location.href='/auth'</script></html>")
|
| 151 |
return HTMLResponse(HTML_UI)
|
| 152 |
|
| 153 |
-
@app.post("/upload")
|
| 154 |
-
async def handle_upload(file: UploadFile = File(...)):
|
| 155 |
-
# هنا يتم استقبال الملفات وإضافتها للذاكرة
|
| 156 |
-
return {"status": "success"}
|
| 157 |
-
|
| 158 |
@app.post("/v1/chat/completions")
|
| 159 |
async def chat_api(request: Request):
|
| 160 |
-
auth_header = get_space_authorized_header(request)
|
| 161 |
-
if not auth_header: return StreamingResponse(iter(["data: [401 Unauthorized]\n\n"]), status_code=401)
|
| 162 |
-
|
| 163 |
-
# استخراج التوكن الخاص بالمستخدم للخصم من رصيده
|
| 164 |
-
user_token = auth_header.get("Authorization").replace("Bearer ", "")
|
| 165 |
body = await request.json()
|
| 166 |
|
|
|
|
|
|
|
|
|
|
| 167 |
client = OpenAI(
|
| 168 |
base_url="https://router.huggingface.co/hf-inference/v1",
|
| 169 |
-
api_key=
|
| 170 |
)
|
| 171 |
|
| 172 |
def stream_gen():
|
|
@@ -182,9 +150,10 @@ async def chat_api(request: Request):
|
|
| 182 |
|
| 183 |
return StreamingResponse(stream_gen(), media_type="text/event-stream")
|
| 184 |
|
| 185 |
-
# إعداد واجهة
|
| 186 |
with gr.Blocks() as auth_interface:
|
| 187 |
-
gr.Markdown("# مرحبا بك في الدهليز
|
|
|
|
| 188 |
gr.LoginButton("الدخول عبر Hugging Face")
|
| 189 |
|
| 190 |
app = gr.mount_gradio_app(app, auth_interface, path="/auth")
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from fastapi import FastAPI, Request, File, UploadFile
|
| 4 |
from fastapi.responses import StreamingResponse, HTMLResponse
|
| 5 |
from openai import OpenAI
|
|
|
|
| 6 |
|
|
|
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
+
# واجهة المستخدم (HTML/React) - متجاوبة تماماً للهواتف
|
| 10 |
HTML_UI = """
|
| 11 |
<!DOCTYPE html>
|
| 12 |
<html lang="ar" dir="rtl">
|
| 13 |
<head>
|
| 14 |
<meta charset="UTF-8">
|
| 15 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
|
| 16 |
<title>Neural Vault</title>
|
| 17 |
<script src="https://cdn.tailwindcss.com"></script>
|
| 18 |
<script src="https://unpkg.com/react@18/umd/react.production.min.js"></script>
|
| 19 |
<script src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"></script>
|
| 20 |
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
|
| 21 |
<style>
|
| 22 |
+
body { background: #020617; color: #f8fafc; height: 100dvh; overflow: hidden; margin: 0; position: fixed; width: 100vw; }
|
| 23 |
.chat-area { height: calc(100dvh - 160px); overflow-y: auto; padding: 20px; -webkit-overflow-scrolling: touch; }
|
| 24 |
.sidebar { position: fixed; top: 0; right: -100%; width: 80%; max-width: 300px; height: 100%; background: #0f172a; z-index: 2000; transition: 0.3s ease; border-left: 1px solid #1e293b; }
|
| 25 |
.sidebar.open { right: 0; }
|
|
|
|
| 56 |
body: JSON.stringify({ messages: [...messages, userMsg] })
|
| 57 |
});
|
| 58 |
|
|
|
|
|
|
|
| 59 |
const reader = res.body.getReader();
|
| 60 |
let botMsg = { role: 'assistant', content: "" };
|
| 61 |
setMessages(p => [...p, botMsg]);
|
|
|
|
| 72 |
setStatus("idle");
|
| 73 |
};
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
return (
|
| 76 |
<div className="flex flex-col h-full">
|
| 77 |
<header className="p-4 border-b border-white/5 flex justify-between items-center bg-slate-900/50">
|
|
|
|
| 90 |
|
| 91 |
<div className="input-bar">
|
| 92 |
<div className="flex gap-2 max-w-xl mx-auto">
|
|
|
|
|
|
|
|
|
|
| 93 |
<input
|
| 94 |
className="flex-1 bg-slate-900 border border-white/10 rounded-xl px-4 py-2 outline-none text-sm"
|
| 95 |
placeholder="اكتب سؤالك..."
|
|
|
|
| 106 |
<h2 className="font-bold">الإعدادات</h2>
|
| 107 |
<button onClick={() => setSideOpen(false)}>✕</button>
|
| 108 |
</div>
|
| 109 |
+
<div className="p-4 bg-slate-800/50 rounded-xl text-xs">
|
| 110 |
+
<p>الموديل: Mistral-Small-24B</p>
|
| 111 |
+
<p className="mt-2 text-emerald-400">الحالة: متصل</p>
|
|
|
|
|
|
|
|
|
|
| 112 |
</div>
|
| 113 |
</div>
|
| 114 |
</div>
|
|
|
|
| 122 |
"""
|
| 123 |
|
| 124 |
@app.get("/")
|
| 125 |
+
async def ui():
|
|
|
|
|
|
|
|
|
|
| 126 |
return HTMLResponse(HTML_UI)
|
| 127 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
@app.post("/v1/chat/completions")
|
| 129 |
async def chat_api(request: Request):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
body = await request.json()
|
| 131 |
|
| 132 |
+
# استخدام توكن النظام كاحتياطي أو توكن المستخدم إذا توفر
|
| 133 |
+
api_key = os.getenv("HF_TOKEN")
|
| 134 |
+
|
| 135 |
client = OpenAI(
|
| 136 |
base_url="https://router.huggingface.co/hf-inference/v1",
|
| 137 |
+
api_key=api_key
|
| 138 |
)
|
| 139 |
|
| 140 |
def stream_gen():
|
|
|
|
| 150 |
|
| 151 |
return StreamingResponse(stream_gen(), media_type="text/event-stream")
|
| 152 |
|
| 153 |
+
# إعداد واجهة Gradio كمدخل
|
| 154 |
with gr.Blocks() as auth_interface:
|
| 155 |
+
gr.Markdown("# مرحبا بك في الدهليز")
|
| 156 |
+
# زر الدخول المذكور في ملفك
|
| 157 |
gr.LoginButton("الدخول عبر Hugging Face")
|
| 158 |
|
| 159 |
app = gr.mount_gradio_app(app, auth_interface, path="/auth")
|