Spaces:
Running
Running
enable log
Browse files
app.py
CHANGED
@@ -2,17 +2,24 @@
|
|
2 |
from fastapi import FastAPI
|
3 |
from pydantic import BaseModel
|
4 |
from llama_cpp import Llama
|
5 |
-
import
|
6 |
import os
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
MODEL_REPO = "QuantFactory/Qwen2.5-7B-Instruct-GGUF"
|
9 |
MODEL_FILE = "Qwen2.5-7B-Instruct.Q4_K_M.gguf"
|
10 |
MODEL_PATH = f"/home/user/app/data/cache/{MODEL_FILE}"
|
11 |
|
12 |
-
# تأكد من أن النموذج موجود
|
13 |
if not os.path.exists(MODEL_PATH):
|
14 |
from huggingface_hub import hf_hub_download
|
15 |
os.makedirs("/home/user/app/data/cache", exist_ok=True)
|
|
|
16 |
hf_hub_download(
|
17 |
repo_id=MODEL_REPO,
|
18 |
filename=MODEL_FILE,
|
@@ -20,10 +27,9 @@ if not os.path.exists(MODEL_PATH):
|
|
20 |
)
|
21 |
|
22 |
if os.path.exists(MODEL_PATH):
|
23 |
-
|
24 |
else:
|
25 |
-
|
26 |
-
|
27 |
|
28 |
# تحميل النموذج
|
29 |
llm = Llama(
|
@@ -44,9 +50,13 @@ You are Qwen, created by Alibaba Cloud. You are an AI development assistant. Fol
|
|
44 |
# API setup
|
45 |
app = FastAPI()
|
46 |
|
|
|
|
|
|
|
|
|
47 |
class ChatRequest(BaseModel):
|
48 |
message: str
|
49 |
-
history: list[list[str]] = []
|
50 |
|
51 |
class ChatResponse(BaseModel):
|
52 |
response: str
|
@@ -66,6 +76,7 @@ def format_prompt(messages):
|
|
66 |
|
67 |
@app.post("/chat", response_model=ChatResponse)
|
68 |
def chat(req: ChatRequest):
|
|
|
69 |
messages = [("system", SYSTEM_PROMPT.strip())]
|
70 |
for user_msg, bot_msg in req.history:
|
71 |
messages.append(("user", user_msg))
|
@@ -73,7 +84,11 @@ def chat(req: ChatRequest):
|
|
73 |
messages.append(("user", req.message))
|
74 |
|
75 |
prompt = format_prompt(messages)
|
|
|
|
|
76 |
output = llm(prompt, max_tokens=1024, temperature=0.7, top_p=0.9, repeat_penalty=1.05, stop=["<|im_end|>"])
|
77 |
reply = output["choices"][0]["text"].split("<|im_end|>")[0].strip()
|
|
|
|
|
78 |
req.history.append([req.message, reply])
|
79 |
return ChatResponse(response=reply, updated_history=req.history)
|
|
|
2 |
from fastapi import FastAPI
|
3 |
from pydantic import BaseModel
|
4 |
from llama_cpp import Llama
|
5 |
+
import logging
|
6 |
import os
|
7 |
|
8 |
+
# 🔧 إعداد السجل
|
9 |
+
logging.basicConfig(
|
10 |
+
level=logging.DEBUG,
|
11 |
+
format="🪵 [%(asctime)s] [%(levelname)s] %(message)s",
|
12 |
+
)
|
13 |
+
logger = logging.getLogger(__name__)
|
14 |
+
|
15 |
MODEL_REPO = "QuantFactory/Qwen2.5-7B-Instruct-GGUF"
|
16 |
MODEL_FILE = "Qwen2.5-7B-Instruct.Q4_K_M.gguf"
|
17 |
MODEL_PATH = f"/home/user/app/data/cache/{MODEL_FILE}"
|
18 |
|
|
|
19 |
if not os.path.exists(MODEL_PATH):
|
20 |
from huggingface_hub import hf_hub_download
|
21 |
os.makedirs("/home/user/app/data/cache", exist_ok=True)
|
22 |
+
logger.info("📦 تحميل النموذج من Hugging Face Hub...")
|
23 |
hf_hub_download(
|
24 |
repo_id=MODEL_REPO,
|
25 |
filename=MODEL_FILE,
|
|
|
27 |
)
|
28 |
|
29 |
if os.path.exists(MODEL_PATH):
|
30 |
+
logger.info(f"✅ النموذج موجود: {MODEL_PATH}")
|
31 |
else:
|
32 |
+
logger.error(f"❌ النموذج غير موجود: {MODEL_PATH}")
|
|
|
33 |
|
34 |
# تحميل النموذج
|
35 |
llm = Llama(
|
|
|
50 |
# API setup
|
51 |
app = FastAPI()
|
52 |
|
53 |
+
@app.on_event("startup")
|
54 |
+
async def startup_event():
|
55 |
+
logger.info("🚀 الخادم بدأ بنجاح – /chat جاهز")
|
56 |
+
|
57 |
class ChatRequest(BaseModel):
|
58 |
message: str
|
59 |
+
history: list[list[str]] = []
|
60 |
|
61 |
class ChatResponse(BaseModel):
|
62 |
response: str
|
|
|
76 |
|
77 |
@app.post("/chat", response_model=ChatResponse)
|
78 |
def chat(req: ChatRequest):
|
79 |
+
logger.info(f"📩 طلب جديد: {req.message}")
|
80 |
messages = [("system", SYSTEM_PROMPT.strip())]
|
81 |
for user_msg, bot_msg in req.history:
|
82 |
messages.append(("user", user_msg))
|
|
|
84 |
messages.append(("user", req.message))
|
85 |
|
86 |
prompt = format_prompt(messages)
|
87 |
+
logger.debug(f"📝 prompt المُرسل للنموذج:\n{prompt[:500]}...")
|
88 |
+
|
89 |
output = llm(prompt, max_tokens=1024, temperature=0.7, top_p=0.9, repeat_penalty=1.05, stop=["<|im_end|>"])
|
90 |
reply = output["choices"][0]["text"].split("<|im_end|>")[0].strip()
|
91 |
+
logger.info(f"🤖 رد النموذج: {reply}")
|
92 |
+
|
93 |
req.history.append([req.message, reply])
|
94 |
return ChatResponse(response=reply, updated_history=req.history)
|