1Egyb commited on
Commit
67e4a69
·
verified ·
1 Parent(s): a300a80

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -17
app.py CHANGED
@@ -1,36 +1,37 @@
1
  import os
2
  from fastapi import FastAPI, Request
3
- from fastapi.responses import JSONResponse, FileResponse
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 = "uihui-ai/Qwen2.5-72B-Instruct-abliterated"
12
  client = InferenceClient(token=HF_TOKEN)
13
 
14
- # المسار الذي ستوضع فيه الملفات
15
- frontend_path = os.path.join(os.path.dirname(__file__), "temp_unzip", "dist")
 
 
 
 
16
 
17
- @app.get("/")
18
- async def serve_index():
19
- index_path = os.path.join(frontend_path, "index.html")
20
- if os.path.exists(index_path):
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=1000)
34
- return JSONResponse({"choices": [{"message": {"role": "assistant", "content": response.choices[0].message.content}}]})
 
 
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)