Awesome-Developer commited on
Commit
7357efd
·
verified ·
1 Parent(s): 048bd6d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -5
app.py CHANGED
@@ -2,6 +2,9 @@ import os
2
  import sys
3
  import subprocess
4
  import site
 
 
 
5
 
6
  # 1. BIND TO THE PERSISTENT COMPILATION REGISTRY
7
  PERSISTENT_PACKAGES = "/data/compiled_cache"
@@ -34,7 +37,7 @@ import gradio as gr
34
  from huggingface_hub import hf_hub_download
35
  import spaces
36
 
37
- # 2. UPDATED MODEL WORKSPACE INDEXES (Uses your exact link maps!)
38
  print("Checking persistent storage for AI model weights...")
39
 
40
  # Model 1: The Main 27B Monster for GPU (3.9 GB)
@@ -44,8 +47,7 @@ path_27b = hf_hub_download(
44
  local_dir="/data"
45
  )
46
 
47
- # Model 2: YOUR EXACT WORKING LINK SWAP
48
- # Points precisely to the repo and file schema you provided to eliminate the 404 crash!
49
  path_moe = hf_hub_download(
50
  repo_id="LiquidAI/LFM2-8B-A1B-GGUF",
51
  filename="LFM2-8B-A1B-Q4_K_M.gguf",
@@ -74,7 +76,6 @@ def generate_moe_cpu(prompt):
74
  clean_prompt = str(prompt).strip()
75
  if not clean_prompt: return "Empty prompt."
76
 
77
- # Liquid AI specific instruction tags
78
  system_tool_prompt = "You are an advanced AI agent with Tool Calling capabilities."
79
  formatted = f"<|im_start|>system\n{system_tool_prompt}<|im_end|>\n<|im_start|>user\n{clean_prompt}<|im_end|>\n<|im_start|>assistant\n"
80
 
@@ -98,5 +99,45 @@ with gr.Blocks(title="Resilient AI Hub") as demo:
98
  btn_moe = gr.Button("Submit to MoE Engine")
99
  btn_moe.click(fn=generate_moe_cpu, inputs=input_moe, outputs=output_moe, api_name="chat_backup")
100
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  if __name__ == "__main__":
102
- demo.launch()
 
2
  import sys
3
  import subprocess
4
  import site
5
+ from fastapi import FastAPI, Request
6
+ from fastapi.responses import JSONResponse
7
+ import uvicorn
8
 
9
  # 1. BIND TO THE PERSISTENT COMPILATION REGISTRY
10
  PERSISTENT_PACKAGES = "/data/compiled_cache"
 
37
  from huggingface_hub import hf_hub_download
38
  import spaces
39
 
40
+ # 2. MODEL WORKSPACE INDEXES
41
  print("Checking persistent storage for AI model weights...")
42
 
43
  # Model 1: The Main 27B Monster for GPU (3.9 GB)
 
47
  local_dir="/data"
48
  )
49
 
50
+ # Model 2: Verified LiquidAI repo and file path
 
51
  path_moe = hf_hub_download(
52
  repo_id="LiquidAI/LFM2-8B-A1B-GGUF",
53
  filename="LFM2-8B-A1B-Q4_K_M.gguf",
 
76
  clean_prompt = str(prompt).strip()
77
  if not clean_prompt: return "Empty prompt."
78
 
 
79
  system_tool_prompt = "You are an advanced AI agent with Tool Calling capabilities."
80
  formatted = f"<|im_start|>system\n{system_tool_prompt}<|im_end|>\n<|im_start|>user\n{clean_prompt}<|im_end|>\n<|im_start|>assistant\n"
81
 
 
99
  btn_moe = gr.Button("Submit to MoE Engine")
100
  btn_moe.click(fn=generate_moe_cpu, inputs=input_moe, outputs=output_moe, api_name="chat_backup")
101
 
102
+ # ==========================================
103
+ # 5. FASTAPI /V1 OPENAI COMPATIBILITY MOUNT
104
+ # ==========================================
105
+ # This acts as a background translator server for incoming OpenCode requests!
106
+ fastapi_app = FastAPI()
107
+
108
+ @fastapi_app.post("/v1/chat/completions")
109
+ async def openai_endpoints_router(request: Request):
110
+ try:
111
+ json_data = await request.json()
112
+ messages = json_data.get("messages", [])
113
+ user_prompt = messages[-1]["content"] if messages else ""
114
+ chosen_model = json_data.get("model", "bonsai-27b")
115
+ except Exception:
116
+ return JSONResponse({"error": "Invalid JSON context formatting payload"}, status_code=400)
117
+
118
+ # Route input context arrays directly to the right execution model function
119
+ if "liquid" in chosen_model or "cpu" in chosen_model or "backup" in chosen_model:
120
+ model_reply = generate_moe_cpu(user_prompt)
121
+ else:
122
+ model_reply = generate_27b(user_prompt)
123
+
124
+ # Standard OpenAI JSON dictionary schema response structure format
125
+ return JSONResponse({
126
+ "id": "hf-split-brain-chat",
127
+ "object": "chat.completion",
128
+ "model": chosen_model,
129
+ "choices": [{
130
+ "index": 0,
131
+ "message": {
132
+ "role": "assistant",
133
+ "content": model_reply
134
+ },
135
+ "finish_reason": "stop"
136
+ }]
137
+ })
138
+
139
+ # Bind Gradio web app structure paths directly to the root of the server instance
140
+ app = gr.mount_gradio_app(fastapi_app, demo, path="/")
141
+
142
  if __name__ == "__main__":
143
+ uvicorn.run(app, host="0.0.0.0", port=7860)