1yahoo commited on
Commit
010efa7
·
verified ·
1 Parent(s): aa5b70d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -19
app.py CHANGED
@@ -1,47 +1,90 @@
1
- from fastapi import FastAPI, Request
2
- from fastapi.responses import StreamingResponse
3
- from openai import OpenAI
4
  import os
 
 
5
  import chromadb
 
 
 
 
 
 
 
 
6
 
7
- app = FastAPI()
 
 
 
 
 
 
8
 
9
- # إعدادات الذاكرة (نفس منطق الكود السابق)
10
- STORAGE_PATH = "./neural_memory"
11
  chroma_client = chromadb.PersistentClient(path=STORAGE_PATH)
12
- collection = chroma_client.get_or_create_collection(name="advanced_brain_v6")
 
13
 
14
  client = OpenAI(
15
  base_url="https://router.huggingface.co/hf-inference/v1",
16
  api_key=os.getenv("HF_TOKEN")
17
  )
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  @app.post("/v1/chat/completions")
20
- async def chat_proxy(request: Request):
21
- data = await request.json()
22
- messages = data.get("messages", [])
23
- user_query = messages[-1]["content"]
 
24
 
25
- # البحث في الذاكرة
 
26
  results = collection.query(query_texts=[user_query], n_results=3)
27
  knowledge = "\n".join(results['documents'][0]) if results['documents'] else ""
28
 
29
- # حقن المعرفة في أول رسالة (System Prompt)
30
- messages.insert(0, {"role": "system", "content": f"Context: {knowledge}"})
 
31
 
32
- def stream_response():
33
  response = client.chat.completions.create(
34
  model="huihui-ai/Qwen2.5-72B-Instruct-abliterated",
35
  messages=messages,
 
36
  stream=True
37
  )
38
  for chunk in response:
39
- if chunk.choices[0].delta.content:
40
- yield f"data: {chunk.choices[0].delta.content}\n\n"
 
 
41
  yield "data: [DONE]\n\n"
42
 
43
- return StreamingResponse(stream_response(), media_type="text/event-stream")
44
 
45
  if __name__ == "__main__":
46
  import uvicorn
47
- uvicorn.run(app, host="0.0.0.0", port=8000)
 
 
 
 
 
1
  import os
2
+ import uuid
3
+ import pypdf
4
  import chromadb
5
+ from fastapi import FastAPI, Request, File, UploadFile, BackgroundTasks
6
+ from fastapi.responses import StreamingResponse
7
+ from fastapi.middleware.cors import CORSMiddleware
8
+ from openai import OpenAI
9
+ from chromadb.utils import embedding_functions
10
+
11
+ # --- الإعدادات الفنية ---
12
+ app = FastAPI(title="Neural RAG Engine")
13
 
14
+ # تفعيل CORS للسماح لواجهات Next.js بالاتصال بالخادم
15
+ app.add_middleware(
16
+ CORSMiddleware,
17
+ allow_origins=["*"],
18
+ allow_methods=["*"],
19
+ allow_headers=["*"],
20
+ )
21
 
22
+ STORAGE_PATH = "/data/neural_memory" if os.path.exists("/data") else "./neural_memory"
 
23
  chroma_client = chromadb.PersistentClient(path=STORAGE_PATH)
24
+ default_ef = embedding_functions.DefaultEmbeddingFunction()
25
+ collection = chroma_client.get_or_create_collection(name="advanced_brain_v6", embedding_function=default_ef)
26
 
27
  client = OpenAI(
28
  base_url="https://router.huggingface.co/hf-inference/v1",
29
  api_key=os.getenv("HF_TOKEN")
30
  )
31
 
32
+ # --- وظائف المعالجة ---
33
+ def ingest_document(file_content, filename):
34
+ # (نفس منطق التقطيع الاحترافي السابق مع الـ Overlap)
35
+ text = file_content.decode("utf-8", errors="ignore")
36
+ # إذا كان PDF يحتاج لمكتبة pypdf (يمكن دمجها هنا)
37
+ chunk_size, overlap = 1000, 200
38
+ chunks = [text[i : i + chunk_size] for i in range(0, len(text), chunk_size - overlap)]
39
+ ids = [str(uuid.uuid4()) for _ in chunks]
40
+ metadatas = [{"source": filename} for _ in chunks]
41
+ collection.add(documents=chunks, ids=ids, metadatas=metadatas)
42
+
43
+ # --- الـ Endpoints المتوافقة مع Next.js ---
44
+
45
+ @app.get("/")
46
+ def home():
47
+ return {"status": "Neural Engine is Running", "docs_in_memory": collection.count()}
48
+
49
+ @app.post("/upload")
50
+ async def upload_file(background_tasks: BackgroundTasks, file: UploadFile = File(...)):
51
+ content = await file.read()
52
+ background_tasks.add_task(ingest_document, content, file.filename)
53
+ return {"message": f"Processing {file.filename} in background..."}
54
+
55
  @app.post("/v1/chat/completions")
56
+ async def chat_endpoint(request: Request):
57
+ """هذا المسار يجعل الخادم متوافقاً تماماً مع واجهات Next.js"""
58
+ body = await request.json()
59
+ messages = body.get("messages", [])
60
+ temperature = body.get("temperature", 0.7)
61
 
62
+ # البحث الدلالي (RAG) بناءً على آخر رسالة
63
+ user_query = messages[-1]["content"] if messages else ""
64
  results = collection.query(query_texts=[user_query], n_results=3)
65
  knowledge = "\n".join(results['documents'][0]) if results['documents'] else ""
66
 
67
+ # حقن المعرفة
68
+ system_instruction = f"Context from Memory:\n{knowledge}\n\nAnswer based on this context."
69
+ messages.insert(0, {"role": "system", "content": system_instruction})
70
 
71
+ def stream_gen():
72
  response = client.chat.completions.create(
73
  model="huihui-ai/Qwen2.5-72B-Instruct-abliterated",
74
  messages=messages,
75
+ temperature=temperature,
76
  stream=True
77
  )
78
  for chunk in response:
79
+ content = chunk.choices[0].delta.content
80
+ if content:
81
+ # التنسيق المطلوب لـ Server-Sent Events (SSE)
82
+ yield f"data: {content}\n\n"
83
  yield "data: [DONE]\n\n"
84
 
85
+ return StreamingResponse(stream_gen(), media_type="text/event-stream")
86
 
87
  if __name__ == "__main__":
88
  import uvicorn
89
+ # تشغيل الخادم
90
+ uvicorn.run(app, host="0.0.0.0", port=7860)