ndc8
commited on
Commit
Β·
d3ad561
1
Parent(s):
7559c6d
Update space
Browse files- README.md +329 -6
- requirements.txt +10 -1
README.md
CHANGED
@@ -1,12 +1,335 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
colorFrom: yellow
|
5 |
colorTo: purple
|
6 |
-
sdk:
|
7 |
-
sdk_version:
|
8 |
-
app_file:
|
9 |
pinned: false
|
10 |
---
|
11 |
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
+
title: Multimodal AI Backend Service
|
3 |
+
emoji: π
|
4 |
colorFrom: yellow
|
5 |
colorTo: purple
|
6 |
+
sdk: fastapi
|
7 |
+
sdk_version: 0.100.0
|
8 |
+
app_file: backend_service.py
|
9 |
pinned: false
|
10 |
---
|
11 |
|
12 |
+
# firstAI - Multimodal AI Backend π
|
13 |
+
|
14 |
+
A powerful AI backend service with **multimodal capabilities** - supporting both text generation and image analysis using transformers pipelines.
|
15 |
+
|
16 |
+
## π Features
|
17 |
+
|
18 |
+
### π€ Dual AI Models
|
19 |
+
|
20 |
+
- **Text Generation**: Microsoft DialoGPT-medium for conversations
|
21 |
+
- **Image Analysis**: Salesforce BLIP for image captioning and visual Q&A
|
22 |
+
|
23 |
+
### πΌοΈ Multimodal Support
|
24 |
+
|
25 |
+
- Process text-only messages
|
26 |
+
- Analyze images from URLs
|
27 |
+
- Combined image + text conversations
|
28 |
+
- OpenAI Vision API compatible format
|
29 |
+
|
30 |
+
### π§ Production Ready
|
31 |
+
|
32 |
+
- FastAPI backend with automatic docs
|
33 |
+
- Comprehensive error handling
|
34 |
+
- Health checks and monitoring
|
35 |
+
- PyTorch with MPS acceleration (Apple Silicon)
|
36 |
+
|
37 |
+
## π Quick Start
|
38 |
+
|
39 |
+
### 1. Install Dependencies
|
40 |
+
|
41 |
+
```bash
|
42 |
+
pip install -r requirements.txt
|
43 |
+
```
|
44 |
+
|
45 |
+
### 2. Start the Service
|
46 |
+
|
47 |
+
```bash
|
48 |
+
python backend_service.py
|
49 |
+
```
|
50 |
+
|
51 |
+
### 3. Test Multimodal Capabilities
|
52 |
+
|
53 |
+
```bash
|
54 |
+
python test_final.py
|
55 |
+
```
|
56 |
+
|
57 |
+
The service will start on **http://localhost:8001** with both text and vision models loaded.
|
58 |
+
|
59 |
+
## π‘ Usage Examples
|
60 |
+
|
61 |
+
### Text-Only Chat
|
62 |
+
|
63 |
+
```bash
|
64 |
+
curl -X POST http://localhost:8001/v1/chat/completions \
|
65 |
+
-H "Content-Type: application/json" \
|
66 |
+
-d '{
|
67 |
+
"model": "microsoft/DialoGPT-medium",
|
68 |
+
"messages": [{"role": "user", "content": "Hello!"}]
|
69 |
+
}'
|
70 |
+
```
|
71 |
+
|
72 |
+
### Image Analysis
|
73 |
+
|
74 |
+
```bash
|
75 |
+
curl -X POST http://localhost:8001/v1/chat/completions \
|
76 |
+
-H "Content-Type: application/json" \
|
77 |
+
-d '{
|
78 |
+
"model": "Salesforce/blip-image-captioning-base",
|
79 |
+
"messages": [
|
80 |
+
{
|
81 |
+
"role": "user",
|
82 |
+
"content": [
|
83 |
+
{
|
84 |
+
"type": "image",
|
85 |
+
"url": "https://example.com/image.jpg"
|
86 |
+
}
|
87 |
+
]
|
88 |
+
}
|
89 |
+
]
|
90 |
+
}'
|
91 |
+
```
|
92 |
+
|
93 |
+
### Multimodal (Image + Text)
|
94 |
+
|
95 |
+
```bash
|
96 |
+
curl -X POST http://localhost:8001/v1/chat/completions \
|
97 |
+
-H "Content-Type: application/json" \
|
98 |
+
-d '{
|
99 |
+
"model": "Salesforce/blip-image-captioning-base",
|
100 |
+
"messages": [
|
101 |
+
{
|
102 |
+
"role": "user",
|
103 |
+
"content": [
|
104 |
+
{
|
105 |
+
"type": "image",
|
106 |
+
"url": "https://example.com/image.jpg"
|
107 |
+
},
|
108 |
+
{
|
109 |
+
"type": "text",
|
110 |
+
"text": "What do you see in this image?"
|
111 |
+
}
|
112 |
+
]
|
113 |
+
}
|
114 |
+
]
|
115 |
+
}'
|
116 |
+
```
|
117 |
+
|
118 |
+
## π§ Technical Details
|
119 |
+
|
120 |
+
### Architecture
|
121 |
+
|
122 |
+
- **FastAPI** web framework
|
123 |
+
- **Transformers** pipeline for AI models
|
124 |
+
- **PyTorch** backend with GPU/MPS support
|
125 |
+
- **Pydantic** for request/response validation
|
126 |
+
|
127 |
+
### Models
|
128 |
+
|
129 |
+
- **Text**: microsoft/DialoGPT-medium
|
130 |
+
- **Vision**: Salesforce/blip-image-captioning-base
|
131 |
+
|
132 |
+
### API Endpoints
|
133 |
+
|
134 |
+
- `GET /` - Service information
|
135 |
+
- `GET /health` - Health check
|
136 |
+
- `GET /v1/models` - List available models
|
137 |
+
- `POST /v1/chat/completions` - Chat completions (text/multimodal)
|
138 |
+
- `GET /docs` - Interactive API documentation
|
139 |
+
|
140 |
+
## π§ͺ Testing
|
141 |
+
|
142 |
+
Run the comprehensive test suite:
|
143 |
+
|
144 |
+
```bash
|
145 |
+
python test_final.py
|
146 |
+
```
|
147 |
+
|
148 |
+
Test individual components:
|
149 |
+
|
150 |
+
```bash
|
151 |
+
python test_multimodal.py # Basic multimodal tests
|
152 |
+
python test_pipeline.py # Pipeline compatibility
|
153 |
+
```
|
154 |
+
|
155 |
+
## π¦ Dependencies
|
156 |
+
|
157 |
+
Key packages:
|
158 |
+
|
159 |
+
- `fastapi` - Web framework
|
160 |
+
- `transformers` - AI model pipelines
|
161 |
+
- `torch` - PyTorch backend
|
162 |
+
- `Pillow` - Image processing
|
163 |
+
- `accelerate` - Model acceleration
|
164 |
+
- `requests` - HTTP client
|
165 |
+
|
166 |
+
## π― Integration Complete
|
167 |
+
|
168 |
+
This project successfully integrates:
|
169 |
+
β
**Transformers image-text-to-text pipeline**
|
170 |
+
β
**OpenAI Vision API compatibility**
|
171 |
+
β
**Multimodal message processing**
|
172 |
+
β
**Production-ready FastAPI service**
|
173 |
+
|
174 |
+
See `MULTIMODAL_INTEGRATION_COMPLETE.md` for detailed integration documentation.
|
175 |
+
|
176 |
+
- PyTorch with MPS acceleration (Apple Silicon) AI Backend Service
|
177 |
+
emoji: οΏ½
|
178 |
+
colorFrom: yellow
|
179 |
+
colorTo: purple
|
180 |
+
sdk: fastapi
|
181 |
+
sdk_version: 0.100.0
|
182 |
+
app_file: backend_service.py
|
183 |
+
pinned: false
|
184 |
+
|
185 |
+
---
|
186 |
+
|
187 |
+
# AI Backend Service π
|
188 |
+
|
189 |
+
**Status: β
CONVERSION COMPLETE!**
|
190 |
+
|
191 |
+
Successfully converted from a non-functioning Gradio HuggingFace app to a production-ready FastAPI backend service with OpenAI-compatible API endpoints.
|
192 |
+
|
193 |
+
## Quick Start
|
194 |
+
|
195 |
+
### 1. Setup Environment
|
196 |
+
|
197 |
+
```bash
|
198 |
+
# Activate the virtual environment
|
199 |
+
source gradio_env/bin/activate
|
200 |
+
|
201 |
+
# Install dependencies (already done)
|
202 |
+
pip install -r requirements.txt
|
203 |
+
```
|
204 |
+
|
205 |
+
### 2. Start the Backend Service
|
206 |
+
|
207 |
+
```bash
|
208 |
+
python backend_service.py --port 8000 --reload
|
209 |
+
```
|
210 |
+
|
211 |
+
### 3. Test the API
|
212 |
+
|
213 |
+
```bash
|
214 |
+
# Run comprehensive tests
|
215 |
+
python test_api.py
|
216 |
+
|
217 |
+
# Or try usage examples
|
218 |
+
python usage_examples.py
|
219 |
+
```
|
220 |
+
|
221 |
+
## API Endpoints
|
222 |
+
|
223 |
+
| Endpoint | Method | Description |
|
224 |
+
| ---------------------- | ------ | ----------------------------------- |
|
225 |
+
| `/` | GET | Service information |
|
226 |
+
| `/health` | GET | Health check |
|
227 |
+
| `/v1/models` | GET | List available models |
|
228 |
+
| `/v1/chat/completions` | POST | Chat completion (OpenAI compatible) |
|
229 |
+
| `/v1/completions` | POST | Text completion |
|
230 |
+
|
231 |
+
## Example Usage
|
232 |
+
|
233 |
+
### Chat Completion
|
234 |
+
|
235 |
+
```bash
|
236 |
+
curl -X POST http://localhost:8000/v1/chat/completions \
|
237 |
+
-H "Content-Type: application/json" \
|
238 |
+
-d '{
|
239 |
+
"model": "microsoft/DialoGPT-medium",
|
240 |
+
"messages": [
|
241 |
+
{"role": "user", "content": "Hello! How are you?"}
|
242 |
+
],
|
243 |
+
"max_tokens": 150,
|
244 |
+
"temperature": 0.7
|
245 |
+
}'
|
246 |
+
```
|
247 |
+
|
248 |
+
### Streaming Chat
|
249 |
+
|
250 |
+
```bash
|
251 |
+
curl -X POST http://localhost:8000/v1/chat/completions \
|
252 |
+
-H "Content-Type: application/json" \
|
253 |
+
-d '{
|
254 |
+
"model": "microsoft/DialoGPT-medium",
|
255 |
+
"messages": [
|
256 |
+
{"role": "user", "content": "Tell me a joke"}
|
257 |
+
],
|
258 |
+
"stream": true
|
259 |
+
}'
|
260 |
+
```
|
261 |
+
|
262 |
+
## Files
|
263 |
+
|
264 |
+
- **`app.py`** - Original Gradio ChatInterface (still functional)
|
265 |
+
- **`backend_service.py`** - New FastAPI backend service β
|
266 |
+
- **`test_api.py`** - Comprehensive API testing
|
267 |
+
- **`usage_examples.py`** - Simple usage examples
|
268 |
+
- **`requirements.txt`** - Updated dependencies
|
269 |
+
- **`CONVERSION_COMPLETE.md`** - Detailed conversion documentation
|
270 |
+
|
271 |
+
## Features
|
272 |
+
|
273 |
+
β
**OpenAI-Compatible API** - Drop-in replacement for OpenAI API
|
274 |
+
β
**Async FastAPI** - High-performance async architecture
|
275 |
+
β
**Streaming Support** - Real-time response streaming
|
276 |
+
β
**Error Handling** - Robust error handling with fallbacks
|
277 |
+
β
**Production Ready** - CORS, logging, health checks
|
278 |
+
β
**Docker Ready** - Easy containerization
|
279 |
+
β
**Auto-reload** - Development-friendly auto-reload
|
280 |
+
β
**Type Safety** - Full type hints with Pydantic validation
|
281 |
+
|
282 |
+
## Service URLs
|
283 |
+
|
284 |
+
- **Backend Service**: http://localhost:8000
|
285 |
+
- **API Documentation**: http://localhost:8000/docs
|
286 |
+
- **OpenAPI Spec**: http://localhost:8000/openapi.json
|
287 |
+
|
288 |
+
## Model Information
|
289 |
+
|
290 |
+
- **Current Model**: `microsoft/DialoGPT-medium`
|
291 |
+
- **Type**: Conversational AI model
|
292 |
+
- **Provider**: HuggingFace Inference API
|
293 |
+
- **Capabilities**: Text generation, chat completion
|
294 |
+
|
295 |
+
## Architecture
|
296 |
+
|
297 |
+
```
|
298 |
+
βββββββββββββββββββββββ ββββββββββββββββββββββββ βββββββββββββββββββββββ
|
299 |
+
β Client Request βββββΆβ FastAPI Backend βββββΆβ HuggingFace API β
|
300 |
+
β (OpenAI format) β β (backend_service) β β (DialoGPT-medium) β
|
301 |
+
βββββββββββββββββββββββ ββββββββββββββββββββββββ βββββββββββββββββββββββ
|
302 |
+
β
|
303 |
+
βΌ
|
304 |
+
ββββββββββββββββββββββββ
|
305 |
+
β OpenAI Response β
|
306 |
+
β (JSON/Streaming) β
|
307 |
+
ββββββββββββββββββββββββ
|
308 |
+
```
|
309 |
+
|
310 |
+
## Development
|
311 |
+
|
312 |
+
The service includes:
|
313 |
+
|
314 |
+
- **Auto-reload** for development
|
315 |
+
- **Comprehensive logging** for debugging
|
316 |
+
- **Type checking** for code quality
|
317 |
+
- **Test suite** for reliability
|
318 |
+
- **Error handling** for robustness
|
319 |
+
|
320 |
+
## Production Deployment
|
321 |
+
|
322 |
+
Ready for production with:
|
323 |
+
|
324 |
+
- **Environment variables** for configuration
|
325 |
+
- **Health check endpoints** for monitoring
|
326 |
+
- **CORS support** for web applications
|
327 |
+
- **Docker compatibility** for containerization
|
328 |
+
- **Structured logging** for observability
|
329 |
+
|
330 |
+
---
|
331 |
+
|
332 |
+
**π Conversion Status: COMPLETE!**
|
333 |
+
Successfully transformed from broken Gradio app to production-ready AI backend service.
|
334 |
+
|
335 |
+
For detailed conversion documentation, see [`CONVERSION_COMPLETE.md`](CONVERSION_COMPLETE.md).
|
requirements.txt
CHANGED
@@ -1 +1,10 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio>=5.41.0
|
2 |
+
huggingface_hub>=0.34.0
|
3 |
+
transformers>=4.36.0
|
4 |
+
torch>=2.0.0
|
5 |
+
Pillow>=10.0.0
|
6 |
+
requests>=2.31.0
|
7 |
+
accelerate>=0.24.0
|
8 |
+
fastapi>=0.100.0
|
9 |
+
uvicorn[standard]>=0.23.0
|
10 |
+
pydantic>=2.0.0
|