arun3676 commited on
Commit
cf986ba
·
1 Parent(s): 24f45dc

Fix Space deployment with simplified mock version

Browse files
Files changed (2) hide show
  1. hf-space/app.py +42 -51
  2. hf-space/requirements.txt +0 -4
hf-space/app.py CHANGED
@@ -1,40 +1,11 @@
1
  from fastapi import FastAPI, HTTPException
2
  from pydantic import BaseModel
3
- from transformers import AutoTokenizer, AutoModelForCausalLM
4
- from peft import PeftModel
5
- import torch
6
  import uvicorn
 
 
7
 
8
  app = FastAPI()
9
 
10
- # Load model once at startup
11
- tokenizer = None
12
- model = None
13
-
14
- @app.on_event("startup")
15
- async def load_model():
16
- global tokenizer, model
17
- print("🚀 Loading fine-tuned model...")
18
-
19
- tokenizer = AutoTokenizer.from_pretrained(
20
- "deepseek-ai/deepseek-coder-1.3b-instruct",
21
- trust_remote_code=True
22
- )
23
- tokenizer.pad_token = tokenizer.eos_token
24
-
25
- base_model = AutoModelForCausalLM.from_pretrained(
26
- "deepseek-ai/deepseek-coder-1.3b-instruct",
27
- torch_dtype=torch.float16,
28
- device_map="auto",
29
- trust_remote_code=True,
30
- )
31
-
32
- model = PeftModel.from_pretrained(
33
- base_model,
34
- "arunn7/fine-tuned-code-analyzer"
35
- )
36
- print("✅ Model loaded successfully!")
37
-
38
  class CodeRequest(BaseModel):
39
  code: str
40
  max_tokens: int = 300
@@ -47,26 +18,45 @@ class AnalysisResponse(BaseModel):
47
  @app.post("/analyze", response_model=AnalysisResponse)
48
  async def analyze_code(request: CodeRequest):
49
  try:
50
- prompt = f"<s>[INST] Analyze this code for bugs, performance, and security issues. Give a quality score from 1-100 and provide a detailed analysis.\n\nCode:\n```\n{request.code}\n``` [/INST]"
51
-
52
- inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=1024).to(model.device)
53
-
54
- with torch.no_grad():
55
- outputs = model.generate(
56
- **inputs,
57
- max_new_tokens=request.max_tokens,
58
- temperature=0.7,
59
- do_sample=True,
60
- pad_token_id=tokenizer.eos_token_id,
61
- eos_token_id=tokenizer.eos_token_id,
62
- )
63
-
64
- response = tokenizer.decode(outputs[0], skip_special_tokens=True)
65
- analysis = response.split('[/INST]')[-1].strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
  return AnalysisResponse(
68
- analysis=analysis,
69
- model="fine-tuned-deepseek",
70
  status="success"
71
  )
72
  except Exception as e:
@@ -74,7 +64,7 @@ async def analyze_code(request: CodeRequest):
74
 
75
  @app.get("/health")
76
  async def health_check():
77
- return {"status": "healthy", "model": "fine-tuned-deepseek"}
78
 
79
  @app.get("/")
80
  async def root():
@@ -84,7 +74,8 @@ async def root():
84
  "POST /analyze": "Analyze code for bugs, performance, and security issues",
85
  "GET /health": "Health check endpoint"
86
  },
87
- "model": "fine-tuned-deepseek"
 
88
  }
89
 
90
  if __name__ == "__main__":
 
1
  from fastapi import FastAPI, HTTPException
2
  from pydantic import BaseModel
 
 
 
3
  import uvicorn
4
+ import requests
5
+ import json
6
 
7
  app = FastAPI()
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  class CodeRequest(BaseModel):
10
  code: str
11
  max_tokens: int = 300
 
18
  @app.post("/analyze", response_model=AnalysisResponse)
19
  async def analyze_code(request: CodeRequest):
20
  try:
21
+ # For now, return a mock analysis while we debug the model loading
22
+ analysis_text = f"""
23
+ Quality Score: 75/100
24
+
25
+ BUGS:
26
+ - No error handling for edge cases
27
+ - Potential infinite recursion for large inputs
28
+
29
+ PERFORMANCE ISSUES:
30
+ - Recursive approach is inefficient for large numbers
31
+ - No memoization implemented
32
+
33
+ SECURITY CONCERNS:
34
+ - No input validation
35
+ - Could cause stack overflow with large inputs
36
+
37
+ IMPROVEMENTS:
38
+ 1. Add input validation
39
+ 2. Implement iterative solution or memoization
40
+ 3. Add error handling for edge cases
41
+
42
+ Example improved code:
43
+ ```python
44
+ def fibonacci_improved(n):
45
+ if n < 0:
46
+ raise ValueError("Input must be non-negative")
47
+ if n <= 1:
48
+ return n
49
+
50
+ a, b = 0, 1
51
+ for _ in range(2, n + 1):
52
+ a, b = b, a + b
53
+ return b
54
+ ```
55
+ """
56
 
57
  return AnalysisResponse(
58
+ analysis=analysis_text,
59
+ model="fine-tuned-deepseek-mock",
60
  status="success"
61
  )
62
  except Exception as e:
 
64
 
65
  @app.get("/health")
66
  async def health_check():
67
+ return {"status": "healthy", "model": "fine-tuned-deepseek-mock"}
68
 
69
  @app.get("/")
70
  async def root():
 
74
  "POST /analyze": "Analyze code for bugs, performance, and security issues",
75
  "GET /health": "Health check endpoint"
76
  },
77
+ "model": "fine-tuned-deepseek-mock",
78
+ "status": "running"
79
  }
80
 
81
  if __name__ == "__main__":
hf-space/requirements.txt CHANGED
@@ -1,7 +1,3 @@
1
  fastapi
2
  uvicorn[standard]
3
- transformers
4
- peft
5
- torch
6
- accelerate
7
  requests
 
1
  fastapi
2
  uvicorn[standard]
 
 
 
 
3
  requests