scriptsledge commited on
Commit
ec720bb
·
verified ·
1 Parent(s): ad49d0e

feat: initialize backend for Hugging Face deployment (by antigravity)

Browse files
Files changed (2) hide show
  1. model_service.py +37 -23
  2. verify_server.py +26 -0
model_service.py CHANGED
@@ -12,28 +12,39 @@ print(f"Target Model: {REPO_ID}")
12
 
13
  pipe = None
14
 
15
- try:
16
- print("Loading model...")
17
- # Initialize the pipeline
18
- # device_map="auto" will use GPU if available, otherwise CPU.
19
- # torch_dtype="auto" will use appropriate precision (fp16 on GPU, fp32 on CPU typically)
20
- pipe = pipeline(
21
- "text-generation",
22
- model=REPO_ID,
23
- torch_dtype="auto",
24
- device_map="auto"
25
- )
26
- print("Success: Clarity AI Model loaded.")
27
-
28
- # Warm-up inference
29
- print("Warming up model...")
30
- warmup_msg = [{"role": "user", "content": "print('hello')"}]
31
- pipe(warmup_msg, max_new_tokens=10)
32
- print("Model warmup complete.")
 
 
 
 
 
 
 
 
 
33
 
34
- except Exception as e:
35
- print(f"CRITICAL ERROR: Failed to load model. {e}")
36
- pipe = None
 
 
37
 
38
  def detect_language(code: str) -> dict:
39
  """
@@ -118,7 +129,10 @@ def correct_code_with_ai(code: str) -> dict:
118
  """
119
  detected_lang = detect_language(code)
120
 
121
- if not pipe:
 
 
 
122
  return {
123
  "code": "# Model failed to load. Check server logs.",
124
  "language": detected_lang
@@ -298,7 +312,7 @@ def get_gemini_models(api_key: str = None) -> list:
298
  final_key = api_key if api_key else os.environ.get("GOOGLE_API_KEY")
299
 
300
  if not final_key:
301
- return ["Error: No API Key"]
302
 
303
  try:
304
  genai.configure(api_key=final_key)
 
12
 
13
  pipe = None
14
 
15
+ def load_model():
16
+ """
17
+ Lazy-loads the model pipeline.
18
+ """
19
+ global pipe
20
+ if pipe is not None:
21
+ return pipe
22
+
23
+ print(f"Initializing Clarity AI Engine (Transformers)...")
24
+ print(f"Target Model: {REPO_ID}")
25
+
26
+ try:
27
+ print("Loading model...")
28
+ # Initialize the pipeline
29
+ pipe = pipeline(
30
+ "text-generation",
31
+ model=REPO_ID,
32
+ torch_dtype="auto",
33
+ device_map="auto"
34
+ )
35
+ print("Success: Clarity AI Model loaded.")
36
+
37
+ # Warm-up inference
38
+ print("Warming up model...")
39
+ warmup_msg = [{"role": "user", "content": "print('hello')"}]
40
+ pipe(warmup_msg, max_new_tokens=10)
41
+ print("Model warmup complete.")
42
 
43
+ except Exception as e:
44
+ print(f"CRITICAL ERROR: Failed to load model. {e}")
45
+ pipe = None
46
+
47
+ return pipe
48
 
49
  def detect_language(code: str) -> dict:
50
  """
 
129
  """
130
  detected_lang = detect_language(code)
131
 
132
+ # Lazy Load
133
+ current_pipe = load_model()
134
+
135
+ if not current_pipe:
136
  return {
137
  "code": "# Model failed to load. Check server logs.",
138
  "language": detected_lang
 
312
  final_key = api_key if api_key else os.environ.get("GOOGLE_API_KEY")
313
 
314
  if not final_key:
315
+ return []
316
 
317
  try:
318
  genai.configure(api_key=final_key)
verify_server.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import time
3
+ import sys
4
+
5
+ def verify():
6
+ print("Verifying Backend Health...")
7
+ url = "http://127.0.0.1:7860/api/health"
8
+ try:
9
+ # Retry logic for startup
10
+ for i in range(5):
11
+ try:
12
+ res = requests.get(url)
13
+ if res.status_code == 200:
14
+ print("✅ Backend is Online")
15
+ return
16
+ except:
17
+ print(f"Waiting for server... ({i+1}/5)")
18
+ time.sleep(2)
19
+ print("❌ Backend failed to start")
20
+ sys.exit(1)
21
+ except Exception as e:
22
+ print(f"Error: {e}")
23
+ sys.exit(1)
24
+
25
+ if __name__ == "__main__":
26
+ verify()