Mohd-Shujath commited on
Commit
c8ce4c2
·
1 Parent(s): 18ec402

Switch to ctransformers for pure glibc compatibility

Browse files
Files changed (2) hide show
  1. app.py +19 -17
  2. requirements.txt +1 -2
app.py CHANGED
@@ -3,20 +3,21 @@ from typing import List, Literal, Optional
3
  from pydantic import BaseModel, Field
4
  from fastapi import FastAPI, HTTPException, Header
5
  import gradio as gr
6
- from llama_cpp import Llama
7
 
8
  # -------------------------------------------------------------
9
- # Configuration & Secret
10
  # -------------------------------------------------------------
11
  MODEL_PATH = "Qwen3.5-0.8B.Q4_K_M.gguf"
12
  API_KEY = os.getenv("API_KEY", "commitcon-secret-2026")
13
 
14
- print("Loading GGUF model via llama-cpp ...")
15
- llm = Llama(
16
- model_path=MODEL_PATH,
17
- n_ctx=4096,
18
- n_threads=2,
19
- verbose=False
 
20
  )
21
  print("Model loaded successfully!")
22
 
@@ -42,18 +43,20 @@ SYSTEM_PROMPT = (
42
  )
43
 
44
  def run_inference(prompt: str, history: List[dict] = None) -> str:
45
- messages = [{"role": "system", "content": SYSTEM_PROMPT}]
 
46
  if history:
47
  for m in history:
48
- messages.append({"role": m["role"], "content": m["content"]})
49
- messages.append({"role": "user", "content": prompt})
50
 
51
- response = llm.create_chat_completion(
52
- messages=messages,
 
53
  temperature=0.7,
54
- max_tokens=512,
55
  )
56
- return response["choices"][0]["message"]["content"]
57
 
58
  @app.get("/")
59
  def root():
@@ -75,7 +78,7 @@ def chat_endpoint(payload: ChatRequest, x_api_key: Optional[str] = Header(None))
75
  }
76
 
77
  # -------------------------------------------------------------
78
- # Gradio UI Mount (allows testing directly on Hugging Face)
79
  # -------------------------------------------------------------
80
  def gradio_chat(user_message, history):
81
  hist_formatted = []
@@ -92,5 +95,4 @@ demo = gr.ChatInterface(
92
  description="Fine-tuned Qwen 0.8B Q4_K_M AI Driver Copilot for ride repositioning."
93
  )
94
 
95
- # Mount Gradio onto the FastAPI app
96
  app = gr.mount_gradio_app(app, demo, path="/")
 
3
  from pydantic import BaseModel, Field
4
  from fastapi import FastAPI, HTTPException, Header
5
  import gradio as gr
6
+ from ctransformers import AutoModelForCausalLM
7
 
8
  # -------------------------------------------------------------
9
+ # Configuration & Load GGUF Model
10
  # -------------------------------------------------------------
11
  MODEL_PATH = "Qwen3.5-0.8B.Q4_K_M.gguf"
12
  API_KEY = os.getenv("API_KEY", "commitcon-secret-2026")
13
 
14
+ print("Loading GGUF model via ctransformers ...")
15
+ llm = AutoModelForCausalLM.from_pretrained(
16
+ ".",
17
+ model_file=MODEL_PATH,
18
+ model_type="qwen2",
19
+ context_length=4096,
20
+ threads=2
21
  )
22
  print("Model loaded successfully!")
23
 
 
43
  )
44
 
45
  def run_inference(prompt: str, history: List[dict] = None) -> str:
46
+ # Build prompt formatted for chat
47
+ formatted_prompt = f"<|im_start|>system\n{SYSTEM_PROMPT}<|im_end|>\n"
48
  if history:
49
  for m in history:
50
+ formatted_prompt += f"<|im_start|>{m['role']}\n{m['content']}<|im_end|>\n"
51
+ formatted_prompt += f"<|im_start|>user\n{prompt}<|im_end|>\n<|im_start|>assistant\n"
52
 
53
+ response = llm(
54
+ formatted_prompt,
55
+ max_new_tokens=512,
56
  temperature=0.7,
57
+ stop=["<|im_end|>", "<|endoftext|>"]
58
  )
59
+ return response.strip()
60
 
61
  @app.get("/")
62
  def root():
 
78
  }
79
 
80
  # -------------------------------------------------------------
81
+ # Gradio UI Mount (playground on Hugging Face)
82
  # -------------------------------------------------------------
83
  def gradio_chat(user_message, history):
84
  hist_formatted = []
 
95
  description="Fine-tuned Qwen 0.8B Q4_K_M AI Driver Copilot for ride repositioning."
96
  )
97
 
 
98
  app = gr.mount_gradio_app(app, demo, path="/")
requirements.txt CHANGED
@@ -1,4 +1,3 @@
1
- --extra-index-url https://abetlen.github.io/llama-cpp-python/whl/cpu
2
- llama-cpp-python==0.2.90
3
  fastapi>=0.115.0
4
  pydantic>=2.0.0
 
1
+ ctransformers>=0.2.27
 
2
  fastapi>=0.115.0
3
  pydantic>=2.0.0