File size: 2,938 Bytes
b3032d5
 
 
 
 
 
 
 
 
 
a35ca58
f3d620b
ca2d733
 
 
 
 
 
2e2c730
ca2d733
b14deb9
a35ca58
b14deb9
a35ca58
 
70c0b4b
081c6c3
70c0b4b
2d7da08
 
ca2d733
2d7da08
a35ca58
 
 
 
 
 
 
70c0b4b
a35ca58
 
b14deb9
a35ca58
f3d620b
a35ca58
 
 
f3d620b
2d7da08
ca2d733
2d7da08
 
ca2d733
f3d620b
a35ca58
 
2d7da08
a35ca58
f3d620b
ca2d733
a35ca58
 
f3d620b
a8e68ee
a35ca58
 
ca2d733
a35ca58
ca2d733
a35ca58
 
2d7da08
a35ca58
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
---
title: CodeEvaluator
emoji: ⚡
colorFrom: gray
colorTo: indigo
sdk: docker
pinned: false
license: mit
---

# Code Evaluator API ⚡

A high-speed code evaluation backend designed for Hugging Face Spaces. External applications send questions, code files, their AI credentials, and service access key to `POST /evaluate`. The service evaluates the code using the model configured in the Hugging Face Web UI, returning the score (0–100) and a concise, human-style review directly back to the caller.

## Security & Access Control

To prevent unauthorized public access and ensure only your applications can send code:
1. Go to **Settings** → **Variables and secrets** in your Hugging Face Space.
2. Add a Secret named `SERVICE_API_KEY` (set this to any secret token you choose).
3. External requests must provide this key in the `X-API-Key` header (or `Authorization: Bearer <key>`). Unauthorized requests are rejected with `401 Unauthorized`.

## Architecture

1. **Hugging Face Web UI (`GET /`)**:
   - Acts as your **Model Control Panel**.
   - Select and save which model the evaluator should use (e.g. `openrouter/free`, `openrouter/auto`, `gemini-2.0-flash`, `gpt-4o-mini`, or custom).
2. **External Evaluation Endpoint (`POST /evaluate`)**:
   - The AI provider key and provider base URL are securely locked in Hugging Face Secrets (`OPENAI_API_KEY` / `OPENROUTER_API_KEY`, `OPENAI_BASE_URL`).
   - The model is locked to the Hugging Face Web UI model switch.
   - External client applications only provide:
     - Header: `X-API-Key: YOUR_SERVICE_ACCESS_KEY`
     - Service Base URL: `https://<your-space>.hf.space/evaluate`
     - `question`: The problem statement or objective.
     - `code` (or `file`): Code text or `.zip`/source file.
   - The server evaluates the code using the currently active model and returns:
     ```json
     {
       "score": 92,
       "feedback": "Your solution handles the core logic well and the iterative approach keeps time complexity to O(n). However, it trips up on negative inputs.",
       "model": "openrouter/free"
     }
     ```

## External API Usage

### Python
```python
import requests

# Base URL of your Hugging Face Space:
url = "https://dhyandevp-codeevaluator.hf.space/evaluate"

# Service API Key configured in your HF Space Secrets:
headers = {"X-API-Key": "YOUR_SERVICE_ACCESS_KEY"}

payload = {
    "question": "Write a function to check if a string is a palindrome.",
    "code": "def is_palindrome(s): return s == s[::-1]"
}

res = requests.post(url, headers=headers, json=payload).json()
print("Score:", res["score"])
print("Feedback:", res["feedback"])
```

### cURL
```bash
curl -X POST "https://dhyandevp-codeevaluator.hf.space/evaluate" \
  -H "Content-Type: application/json" \
  -H "X-API-Key: YOUR_SERVICE_ACCESS_KEY" \
  -d '{
    "question": "Write a function to reverse a list in Python.",
    "code": "def reverse_list(arr): return arr[::-1]"
  }'
```