Spaces:
Sleeping
Sleeping
File size: 8,462 Bytes
446ab0f a73e772 446ab0f a73e772 91a36bb a73e772 91a36bb a73e772 91a36bb a73e772 91a36bb a73e772 91a36bb a73e772 91a36bb a73e772 91a36bb a73e772 91a36bb a73e772 91a36bb a73e772 91a36bb a73e772 91a36bb 664d175 a73e772 91a36bb a73e772 91a36bb a73e772 91a36bb a73e772 91a36bb a73e772 91a36bb a73e772 91a36bb a73e772 91a36bb a73e772 91a36bb a73e772 91a36bb a73e772 91a36bb |
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
---
title: IriusRiskTestChallenge
emoji: π
colorFrom: green
colorTo: indigo
sdk: docker
pinned: false
license: apache-2.0
short_description: LLM backend for IriusRisk Tech challenge
---
# IriusRisk test challenge
This project implements a FastAPI API that uses LangChain and LangGraph to generate text with the `SmolLM2-1.7B-Instruct` model from HuggingFace. I have chosen that model so that I could deploy it on a free GPU-only backend from Hugging Face for this test. The API includes security features such as API Key authentication and rate limiting to protect against abuse.
## API URLs
- **Production**: `https://maximofn-iriusrisktestchallenge.hf.space`
- **Local Development**: `http://localhost:7860`
## Main Features
- π€ Text generation using SmolLM2-1.7B-Instruct
- π Text summarization capabilities
- π API Key authentication
- β‘ Rate limiting for abuse protection
- π Conversation thread support
- π Interactive documentation with Swagger and ReDoc
## Configuration
### Environment Variables
For local deployment, create a `.env` file in the project root with the following variables:
```env
API_KEY="your_secret_api_key"
```
## Deployment
### In HuggingFace Spaces
This project is designed to run in HuggingFace Spaces. To configure it:
1. Create a new Space in HuggingFace with blank Docker SDK
2. Add all the files to the Space
3. Configure the API_KEY in the Space's environment secrets
### Local Docker Deployment
For local deployment:
1. Clone this repository
2. Create the `.env` file with your API_KEY
3. Install the dependencies:
```bash
pip install -r requirements.txt
```
### Local Docker Deployment
For local Docker deployment:
1. Clone the repository
2. Create the `.env` file with your API_KEY
3. Build the Docker image:
```bash
docker build -t iriusrisk-test-challenge .
```
4. Run the Docker container:
```bash
docker run -p 8000:8000 --env-file .env iriusrisk-test-challenge
```
## Local Execution
```bash
uvicorn app:app --reload
```
The API will be available at `http://localhost:8000`.
## Local Docker Execution
```bash
docker run -p 8000:8000 --env-file .env iriusrisk-test-challenge
```
The API will be available at `http://localhost:8000`.
## Endpoints
### GET `/`
Welcome endpoint that returns a greeting message.
- Rate limit: 10 requests per minute
### POST `/generate`
Endpoint to generate text using the language model.
- Rate limit: 5 requests per minute
- Requires API Key authentication
**Request parameters:**
```json
{
"query": "Your question here",
"thread_id": "optional_thread_identifier",
"system_prompt": "optional_system_prompt"
}
```
### POST `/summarize`
Endpoint to summarize text using the language model.
- Rate limit: 5 requests per minute
- Requires API Key authentication
**Request parameters:**
```json
{
"text": "Text to summarize",
"thread_id": "optional_thread_identifier",
"max_length": 200
}
```
## Authentication
The API uses API Key authentication. You must include your API Key in the `X-API-Key` header for all protected endpoint requests.
Example:
```bash
# Production
curl -X POST "https://maximofn-iriusrisktestchallenge.hf.space/generate" \
-H "X-API-Key: your_api_key" \
-H "Content-Type: application/json" \
-d '{"query": "What is FastAPI?"}'
# Local development
curl -X POST "http://localhost:7860/generate" \
-H "X-API-Key: your_api_key" \
-H "Content-Type: application/json" \
-d '{"query": "What is FastAPI?"}'
```
## Rate Limiting
To protect the API against abuse, the following limits have been implemented:
- Endpoint `/`: 10 requests per minute
- Endpoint `/generate`: 5 requests per minute
- Endpoint `/summarize`: 5 requests per minute
When these limits are exceeded, the API will return a 429 (Too Many Requests) error.
## API Documentation
The interactive API documentation is available at:
- Swagger UI:
- Production: `https://maximofn-iriusrisktestchallenge.hf.space/docs`
- Local: `http://localhost:7860/docs`
- ReDoc:
- Production: `https://maximofn-iriusrisktestchallenge.hf.space/redoc`
- Local: `http://localhost:7860/redoc`
## Error Handling
The API includes error handling for the following situations:
- Error 401: API Key not provided
- Error 403: Invalid API Key
- Error 429: Rate limit exceeded
- Error 500: Internal server error
## Code Examples
### Python
Here are some examples of how to use the API with Python:
#### Text Generation
```python
import requests
# API configuration
API_URL = "https://maximofn-iriusrisktestchallenge.hf.space" # Production URL
# API_URL = "http://localhost:7860" # Local development URL
API_KEY = "your_api_key" # Replace with your API key
# Headers for authentication
headers = {
"X-API-Key": API_KEY,
"Content-Type": "application/json"
}
# Generate text
def generate_text(query, thread_id="default", system_prompt=None):
url = f"{API_URL}/generate"
data = {
"query": query,
"thread_id": thread_id
}
# Add system prompt if provided
if system_prompt:
data["system_prompt"] = system_prompt
try:
response = requests.post(url, json=data, headers=headers)
if response.status_code == 200:
result = response.json()
return result["generated_text"]
else:
print(f"Error: {response.status_code}")
print(f"Details: {response.text}")
return None
except Exception as e:
print(f"Request failed: {str(e)}")
return None
# Example usage
query = "What are the main features of Python?"
result = generate_text(query)
if result:
print("Response:", result)
# Example with custom thread and system prompt
result = generate_text(
query="Explain object-oriented programming",
thread_id="programming_tutorial",
system_prompt="You are a programming teacher. Explain concepts in simple terms."
)
```
#### Text Summarization
```python
import requests
# Summarize text
def summarize_text(text, max_length=200, thread_id="default"):
url = f"{API_URL}/summarize"
data = {
"text": text,
"max_length": max_length,
"thread_id": thread_id
}
try:
response = requests.post(url, json=data, headers=headers)
if response.status_code == 200:
result = response.json()
return result["summary"]
else:
print(f"Error: {response.status_code}")
print(f"Details: {response.text}")
return None
except Exception as e:
print(f"Request failed: {str(e)}")
return None
# Example usage
text_to_summarize = """
Python is a high-level, interpreted programming language created by Guido van Rossum
and released in 1991. Python's design philosophy emphasizes code readability with
the use of significant whitespace. Its language constructs and object-oriented
approach aim to help programmers write clear, logical code for small and large-scale projects.
"""
summary = summarize_text(text_to_summarize, max_length=50)
if summary:
print("Summary:", summary)
```
#### Error Handling Example
```python
def make_api_request(endpoint, data):
url = f"{API_URL}/{endpoint}"
try:
response = requests.post(url, json=data, headers=headers)
if response.status_code == 200:
return response.json()
elif response.status_code == 429:
print("Rate limit exceeded. Please wait before making more requests.")
elif response.status_code in (401, 403):
print("Authentication error. Please check your API key.")
else:
print(f"Error {response.status_code}: {response.text}")
return None
except requests.exceptions.ConnectionError:
print("Connection error. Please check if the API server is running.")
except Exception as e:
print(f"Unexpected error: {str(e)}")
return None
```
These examples show how to:
- Make requests to different endpoints
- Handle authentication with API keys
- Process successful responses
- Handle various types of errors
- Use optional parameters like thread_id and system_prompt
Remember to:
- Replace `API_URL` with your actual API endpoint
- Set your API key in the headers
- Handle rate limiting by implementing appropriate delays between requests
- Implement proper error handling for your use case
|