Update app.py
Browse files
app.py
CHANGED
|
@@ -1,36 +1,37 @@
|
|
| 1 |
import os
|
| 2 |
from fastapi import FastAPI, Request
|
| 3 |
-
from fastapi.responses import JSONResponse,
|
| 4 |
from fastapi.staticfiles import StaticFiles
|
|
|
|
| 5 |
from huggingface_hub import InferenceClient
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
-
# إعداد
|
| 10 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 11 |
-
MODEL_ID = "
|
| 12 |
client = InferenceClient(token=HF_TOKEN)
|
| 13 |
|
| 14 |
-
# ال
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
@app.get("/")
|
| 18 |
-
async def serve_index():
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
return FileResponse(index_path)
|
| 22 |
-
return JSONResponse({"error": f"Frontend build files not found at {frontend_path}"}, status_code=404)
|
| 23 |
-
|
| 24 |
-
# ربط الملفات الثابتة
|
| 25 |
-
if os.path.exists(frontend_path):
|
| 26 |
-
app.mount("/assets", StaticFiles(directory=os.path.join(frontend_path, "assets")), name="assets")
|
| 27 |
|
| 28 |
@app.post("/chat")
|
| 29 |
async def chat_handler(request: Request):
|
| 30 |
try:
|
| 31 |
body = await request.json()
|
| 32 |
messages = body.get("messages", [])
|
| 33 |
-
response = client.chat_completion(model=MODEL_ID, messages=messages, max_tokens=
|
| 34 |
-
return JSONResponse({
|
|
|
|
|
|
|
| 35 |
except Exception as e:
|
| 36 |
return JSONResponse({"error": str(e)}, status_code=500)
|
|
|
|
| 1 |
import os
|
| 2 |
from fastapi import FastAPI, Request
|
| 3 |
+
from fastapi.responses import JSONResponse, HTMLResponse
|
| 4 |
from fastapi.staticfiles import StaticFiles
|
| 5 |
+
from fastapi.templating import Jinja2Templates
|
| 6 |
from huggingface_hub import InferenceClient
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
+
# إعداد النموذج
|
| 11 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 12 |
+
MODEL_ID = "huihui-ai/Qwen2.5-72B-Instruct-abliterated"
|
| 13 |
client = InferenceClient(token=HF_TOKEN)
|
| 14 |
|
| 15 |
+
# إعداد القوالب والملفات الثابتة بناءً على هيكل مجلد api الخاص بك
|
| 16 |
+
base_path = os.path.dirname(os.path.abspath(__file__))
|
| 17 |
+
# ربط مجلد القوالب (templates) الذي يحتوي على index.html
|
| 18 |
+
templates = Jinja2Templates(directory=os.path.join(base_path, "api", "templates"))
|
| 19 |
+
# ربط مجلد الملفات الثابتة (static)
|
| 20 |
+
app.mount("/static", StaticFiles(directory=os.path.join(base_path, "api", "static")), name="static")
|
| 21 |
|
| 22 |
+
@app.get("/", response_class=HTMLResponse)
|
| 23 |
+
async def serve_index(request: Request):
|
| 24 |
+
# استخدام Jinja2 لعرض ملف index.html من مجلد templates
|
| 25 |
+
return templates.TemplateResponse("index.html", {"request": request})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
@app.post("/chat")
|
| 28 |
async def chat_handler(request: Request):
|
| 29 |
try:
|
| 30 |
body = await request.json()
|
| 31 |
messages = body.get("messages", [])
|
| 32 |
+
response = client.chat_completion(model=MODEL_ID, messages=messages, max_tokens=1500)
|
| 33 |
+
return JSONResponse({
|
| 34 |
+
"choices": [{"message": {"role": "assistant", "content": response.choices[0].message.content}}]
|
| 35 |
+
})
|
| 36 |
except Exception as e:
|
| 37 |
return JSONResponse({"error": str(e)}, status_code=500)
|