File size: 1,762 Bytes
814a0d3
ce60d17
 
 
 
814a0d3
 
 
 
 
ce60d17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
title: Personalized Chatbot API
emoji: ๐Ÿค–
colorFrom: blue
colorTo: green
sdk: docker
pinned: false
license: mit
---

# Personalized Chatbot Backend

FastAPI backend for a personalized chatbot with Human-in-the-Loop (HITL) feedback.

## Features

- ๐Ÿš€ Fast inference with Llama 3.2
- ๐Ÿ’พ Feedback collection for continuous learning
- ๐Ÿ“Š Statistics tracking
- ๐Ÿ”ง LoRA adapter support for finetuned models

## API Endpoints

### POST /chat
Generate chatbot response

**Request:**
```json
{
  "message": "Hello, how are you?",
  "history": [],
  "max_length": 200,
  "temperature": 0.7
}
```

**Response:**
```json
{
  "reply": "I'm doing well, thank you!",
  "timestamp": 1234567890.123
}
```

### POST /feedback
Submit correction for model response

**Request:**
```json
{
  "user_input": "What is the capital of France?",
  "model_reply": "The capital is Berlin",
  "user_correction": "The capital is Paris",
  "reason": "incorrect_answer"
}
```

### GET /stats
Get feedback statistics

**Response:**
```json
{
  "total_interactions": 100,
  "corrections": 15,
  "accepted": 85,
  "correction_rate": 0.15
}
```

### GET /health
Health check endpoint

## Configuration

The model is configured in the `startup_event()` function:

```python
model_manager.initialize(
    model_name="meta-llama/Llama-3.2-1B-Instruct",
    adapter_path=None,  # Path to LoRA adapter if finetuned
    use_4bit=True       # Use 4-bit quantization
)
```

## Usage

1. Fork this Space
2. Modify `model_name` and `adapter_path` in `app.py` if needed
3. The API will be available at: `https://YOUR-USERNAME-chatbot-api.hf.space`

## Local Development

```bash
pip install -r requirements.txt
python app.py
```

API will be available at: http://localhost:7860

## License

MIT