File size: 25,291 Bytes
b190b45 |
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 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 |
#!/usr/bin/env python3
"""
Model Catalog API Router
API برای دسترسی به کاتالوگ مدلهای AI
"""
from fastapi import APIRouter, Query, HTTPException
from fastapi.responses import HTMLResponse, FileResponse
from typing import List, Dict, Any, Optional
import sys
import os
# اضافه کردن مسیر root
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
from backend.services.advanced_model_manager import get_model_manager, ModelInfo
router = APIRouter(prefix="/api/models", tags=["Model Catalog"])
@router.get("/catalog", response_model=List[Dict[str, Any]])
async def get_model_catalog(
category: Optional[str] = Query(None, description="Filter by category"),
size: Optional[str] = Query(None, description="Filter by size"),
max_size_mb: Optional[int] = Query(None, description="Max size in MB"),
language: Optional[str] = Query(None, description="Filter by language"),
free_only: bool = Query(True, description="Free models only"),
no_auth: bool = Query(True, description="No authentication required"),
min_performance: float = Query(0.0, description="Minimum performance score"),
limit: int = Query(100, description="Max results")
):
"""
دریافت لیست مدلها با فیلترهای مختلف
### مثال:
```
GET /api/models/catalog?category=sentiment&max_size_mb=500&limit=10
```
"""
manager = get_model_manager()
models = manager.filter_models(
category=category,
size=size,
max_size_mb=max_size_mb,
language=language,
free_only=free_only,
no_auth=no_auth,
min_performance=min_performance
)
# Convert to dict و محدود کردن به limit
return [model.to_dict() for model in models[:limit]]
@router.get("/model/{model_id}", response_model=Dict[str, Any])
async def get_model_details(model_id: str):
"""
دریافت جزئیات کامل یک مدل
### مثال:
```
GET /api/models/model/cryptobert
```
"""
manager = get_model_manager()
model = manager.get_model_by_id(model_id)
if not model:
raise HTTPException(status_code=404, detail=f"Model {model_id} not found")
return model.to_dict()
@router.get("/search")
async def search_models(
q: str = Query(..., description="Search query"),
limit: int = Query(10, description="Max results")
):
"""
جستجو در مدلها
### مثال:
```
GET /api/models/search?q=crypto&limit=5
```
"""
manager = get_model_manager()
results = manager.search_models(q)
return {
"query": q,
"total": len(results),
"results": [model.to_dict() for model in results[:limit]]
}
@router.get("/best/{category}")
async def get_best_models(
category: str,
top_n: int = Query(3, description="Number of top models"),
max_size_mb: Optional[int] = Query(None, description="Max size in MB")
):
"""
دریافت بهترین مدلها در یک category
### مثال:
```
GET /api/models/best/sentiment?top_n=5&max_size_mb=500
```
"""
manager = get_model_manager()
try:
models = manager.get_best_models(
category=category,
top_n=top_n,
max_size_mb=max_size_mb
)
return {
"category": category,
"count": len(models),
"models": [model.to_dict() for model in models]
}
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
@router.get("/recommend")
async def recommend_models(
use_case: str = Query(..., description="Use case (e.g., twitter, news, trading)"),
max_models: int = Query(5, description="Max recommendations"),
max_size_mb: Optional[int] = Query(None, description="Max size in MB")
):
"""
توصیه مدلها بر اساس use case
### مثال:
```
GET /api/models/recommend?use_case=twitter&max_models=3
```
"""
manager = get_model_manager()
models = manager.recommend_models(
use_case=use_case,
max_models=max_models,
max_size_mb=max_size_mb
)
return {
"use_case": use_case,
"count": len(models),
"recommendations": [model.to_dict() for model in models]
}
@router.get("/stats")
async def get_catalog_stats():
"""
آمار کامل کاتالوگ مدلها
### مثال:
```
GET /api/models/stats
```
"""
manager = get_model_manager()
return manager.get_model_stats()
@router.get("/categories")
async def get_categories():
"""
لیست categories با آمار
### مثال:
```
GET /api/models/categories
```
"""
manager = get_model_manager()
return {
"categories": manager.get_categories()
}
@router.get("/ui", response_class=HTMLResponse)
async def model_catalog_ui():
"""
رابط کاربری HTML برای مرور مدلها
"""
return """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>🤖 AI Models Catalog</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
padding: 20px;
}
.container {
max-width: 1400px;
margin: 0 auto;
background: white;
border-radius: 20px;
box-shadow: 0 20px 60px rgba(0,0,0,0.3);
overflow: hidden;
}
.header {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 40px;
text-align: center;
}
.header h1 {
font-size: 2.5rem;
margin-bottom: 10px;
}
.header p {
font-size: 1.1rem;
opacity: 0.9;
}
.stats-bar {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 20px;
padding: 30px 40px;
background: #f8f9fa;
border-bottom: 1px solid #e9ecef;
}
.stat-card {
background: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
text-align: center;
}
.stat-card .value {
font-size: 2rem;
font-weight: bold;
color: #667eea;
margin-bottom: 5px;
}
.stat-card .label {
font-size: 0.9rem;
color: #6c757d;
}
.filters {
padding: 30px 40px;
background: #f8f9fa;
border-bottom: 1px solid #e9ecef;
}
.filter-row {
display: flex;
gap: 15px;
flex-wrap: wrap;
margin-bottom: 15px;
}
.filter-group {
flex: 1;
min-width: 200px;
}
.filter-group label {
display: block;
margin-bottom: 5px;
font-weight: 500;
color: #495057;
}
.filter-group select,
.filter-group input {
width: 100%;
padding: 10px 15px;
border: 1px solid #ced4da;
border-radius: 8px;
font-size: 1rem;
}
.search-box {
position: relative;
flex: 2;
min-width: 300px;
}
.search-box input {
width: 100%;
padding: 12px 45px 12px 15px;
border: 2px solid #667eea;
border-radius: 10px;
font-size: 1rem;
}
.search-box button {
position: absolute;
right: 5px;
top: 50%;
transform: translateY(-50%);
background: #667eea;
color: white;
border: none;
padding: 8px 15px;
border-radius: 8px;
cursor: pointer;
font-weight: 500;
}
.search-box button:hover {
background: #5568d3;
}
.content {
padding: 40px;
}
.models-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
gap: 25px;
}
.model-card {
background: white;
border: 1px solid #e9ecef;
border-radius: 15px;
padding: 25px;
transition: all 0.3s ease;
position: relative;
overflow: hidden;
}
.model-card:hover {
box-shadow: 0 10px 30px rgba(0,0,0,0.15);
transform: translateY(-5px);
border-color: #667eea;
}
.model-card::before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 4px;
background: linear-gradient(90deg, #667eea, #764ba2);
}
.model-header {
margin-bottom: 15px;
}
.model-name {
font-size: 1.3rem;
font-weight: bold;
color: #212529;
margin-bottom: 5px;
}
.model-id {
font-size: 0.85rem;
color: #6c757d;
font-family: 'Courier New', monospace;
}
.model-description {
color: #495057;
line-height: 1.6;
margin-bottom: 15px;
}
.model-meta {
display: flex;
gap: 15px;
margin-bottom: 15px;
flex-wrap: wrap;
}
.meta-item {
display: flex;
align-items: center;
gap: 5px;
font-size: 0.9rem;
color: #6c757d;
}
.meta-item .icon {
font-size: 1.1rem;
}
.tags {
display: flex;
flex-wrap: wrap;
gap: 8px;
margin-bottom: 15px;
}
.tag {
background: #e7f0ff;
color: #0056b3;
padding: 5px 12px;
border-radius: 20px;
font-size: 0.85rem;
font-weight: 500;
}
.category-badge {
background: #667eea;
color: white;
padding: 4px 10px;
border-radius: 15px;
font-size: 0.8rem;
font-weight: 500;
}
.performance-bar {
margin-top: 15px;
}
.performance-label {
font-size: 0.85rem;
color: #6c757d;
margin-bottom: 5px;
}
.progress-bar {
height: 6px;
background: #e9ecef;
border-radius: 3px;
overflow: hidden;
}
.progress-fill {
height: 100%;
background: linear-gradient(90deg, #667eea, #764ba2);
border-radius: 3px;
transition: width 0.3s ease;
}
.model-actions {
display: flex;
gap: 10px;
margin-top: 15px;
}
.btn {
flex: 1;
padding: 10px;
border: none;
border-radius: 8px;
cursor: pointer;
font-weight: 500;
transition: all 0.3s ease;
}
.btn-primary {
background: #667eea;
color: white;
}
.btn-primary:hover {
background: #5568d3;
}
.btn-secondary {
background: #f8f9fa;
color: #495057;
border: 1px solid #dee2e6;
}
.btn-secondary:hover {
background: #e9ecef;
}
.loading {
text-align: center;
padding: 60px;
color: #6c757d;
font-size: 1.2rem;
}
.no-results {
text-align: center;
padding: 60px;
color: #6c757d;
}
.no-results-icon {
font-size: 4rem;
margin-bottom: 20px;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>🤖 AI Models Catalog</h1>
<p>Comprehensive catalog of 25+ AI models for crypto & finance</p>
</div>
<div class="stats-bar" id="stats-bar">
<div class="stat-card">
<div class="value" id="stat-total">-</div>
<div class="label">Total Models</div>
</div>
<div class="stat-card">
<div class="value" id="stat-free">-</div>
<div class="label">Free Models</div>
</div>
<div class="stat-card">
<div class="value" id="stat-api">-</div>
<div class="label">API Compatible</div>
</div>
<div class="stat-card">
<div class="value" id="stat-performance">-</div>
<div class="label">Avg Performance</div>
</div>
</div>
<div class="filters">
<div class="filter-row">
<div class="search-box">
<input type="text" id="search-input" placeholder="Search models by name, description, or tags...">
<button onclick="searchModels()">🔍 Search</button>
</div>
</div>
<div class="filter-row">
<div class="filter-group">
<label>Category</label>
<select id="filter-category" onchange="applyFilters()">
<option value="">All Categories</option>
<option value="sentiment">Sentiment</option>
<option value="generation">Generation</option>
<option value="trading">Trading</option>
<option value="summarization">Summarization</option>
<option value="ner">NER</option>
<option value="question_answering">Q&A</option>
<option value="classification">Classification</option>
<option value="embedding">Embedding</option>
</select>
</div>
<div class="filter-group">
<label>Size</label>
<select id="filter-size" onchange="applyFilters()">
<option value="">All Sizes</option>
<option value="tiny">Tiny (<100 MB)</option>
<option value="small">Small (100-500 MB)</option>
<option value="medium">Medium (500MB-1GB)</option>
<option value="large">Large (1-3GB)</option>
</select>
</div>
<div class="filter-group">
<label>Max Size (MB)</label>
<input type="number" id="filter-max-size" placeholder="e.g., 500" onchange="applyFilters()">
</div>
<div class="filter-group">
<label>Min Performance</label>
<input type="number" id="filter-min-perf" placeholder="0.0-1.0" step="0.1" min="0" max="1" onchange="applyFilters()">
</div>
</div>
</div>
<div class="content">
<div id="loading" class="loading">Loading models...</div>
<div id="models-container" class="models-grid" style="display: none;"></div>
<div id="no-results" class="no-results" style="display: none;">
<div class="no-results-icon">🔍</div>
<h2>No models found</h2>
<p>Try adjusting your filters or search query</p>
</div>
</div>
</div>
<script>
let allModels = [];
// Load stats
async function loadStats() {
try {
const response = await fetch('/api/models/stats');
const stats = await response.json();
document.getElementById('stat-total').textContent = stats.total_models;
document.getElementById('stat-free').textContent = stats.free_models;
document.getElementById('stat-api').textContent = stats.api_compatible;
document.getElementById('stat-performance').textContent = stats.avg_performance.toFixed(2);
} catch (error) {
console.error('Error loading stats:', error);
}
}
// Load models
async function loadModels() {
try {
const response = await fetch('/api/models/catalog?limit=100');
allModels = await response.json();
document.getElementById('loading').style.display = 'none';
displayModels(allModels);
} catch (error) {
console.error('Error loading models:', error);
document.getElementById('loading').innerHTML = '❌ Error loading models';
}
}
// Display models
function displayModels(models) {
const container = document.getElementById('models-container');
if (models.length === 0) {
container.style.display = 'none';
document.getElementById('no-results').style.display = 'block';
return;
}
document.getElementById('no-results').style.display = 'none';
container.style.display = 'grid';
container.innerHTML = models.map(model => `
<div class="model-card">
<div class="model-header">
<div class="model-name">${model.name}</div>
<div class="model-id">${model.hf_id}</div>
</div>
<div class="category-badge">${model.category}</div>
<p class="model-description">${model.description}</p>
<div class="model-meta">
<div class="meta-item">
<span class="icon">💾</span>
<span>${model.size_mb} MB</span>
</div>
<div class="meta-item">
<span class="icon">🌍</span>
<span>${model.languages.join(', ')}</span>
</div>
${model.free ? '<div class="meta-item"><span class="icon">✅</span><span>Free</span></div>' : ''}
${model.api_compatible ? '<div class="meta-item"><span class="icon">🔌</span><span>API</span></div>' : ''}
</div>
<div class="tags">
${model.tags.slice(0, 3).map(tag => `<span class="tag">${tag}</span>`).join('')}
</div>
<div class="performance-bar">
<div class="performance-label">Performance: ${(model.performance_score * 100).toFixed(0)}%</div>
<div class="progress-bar">
<div class="progress-fill" style="width: ${model.performance_score * 100}%"></div>
</div>
</div>
<div class="model-actions">
<button class="btn btn-primary" onclick="tryModel('${model.id}')">
Try Model
</button>
<button class="btn btn-secondary" onclick="viewDetails('${model.id}')">
Details
</button>
</div>
</div>
`).join('');
}
// Apply filters
function applyFilters() {
const category = document.getElementById('filter-category').value;
const size = document.getElementById('filter-size').value;
const maxSize = document.getElementById('filter-max-size').value;
const minPerf = document.getElementById('filter-min-perf').value;
let filtered = allModels;
if (category) {
filtered = filtered.filter(m => m.category === category);
}
if (size) {
filtered = filtered.filter(m => m.size === size);
}
if (maxSize) {
filtered = filtered.filter(m => m.size_mb <= parseInt(maxSize));
}
if (minPerf) {
filtered = filtered.filter(m => m.performance_score >= parseFloat(minPerf));
}
displayModels(filtered);
}
// Search models
async function searchModels() {
const query = document.getElementById('search-input').value;
if (!query) {
displayModels(allModels);
return;
}
try {
const response = await fetch(`/api/models/search?q=${encodeURIComponent(query)}&limit=50`);
const data = await response.json();
displayModels(data.results);
} catch (error) {
console.error('Error searching:', error);
}
}
// Try model
function tryModel(modelId) {
// Redirect to sentiment analysis page with model pre-selected
window.location.href = `/api/ai/sentiment/quick?model=${modelId}`;
}
// View details
async function viewDetails(modelId) {
try {
const response = await fetch(`/api/models/model/${modelId}`);
const model = await response.json();
alert(`
Model: ${model.name}
HuggingFace ID: ${model.hf_id}
Category: ${model.category}
Size: ${model.size_mb} MB
Description: ${model.description}
Use Cases: ${model.use_cases.join(', ')}
Performance: ${(model.performance_score * 100).toFixed(0)}%
Popularity: ${(model.popularity_score * 100).toFixed(0)}%
`.trim());
} catch (error) {
console.error('Error loading details:', error);
}
}
// Initialize
window.addEventListener('DOMContentLoaded', () => {
loadStats();
loadModels();
});
// Enter key for search
document.addEventListener('DOMContentLoaded', () => {
document.getElementById('search-input').addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
searchModels();
}
});
});
</script>
</body>
</html>
"""
# ===== Integration with production_server.py =====
"""
# در production_server.py:
from backend.routers.model_catalog import router as catalog_router
app = FastAPI()
app.include_router(catalog_router)
# حالا در دسترس است:
# - GET /api/models/catalog
# - GET /api/models/model/{model_id}
# - GET /api/models/search?q=...
# - GET /api/models/best/{category}
# - GET /api/models/recommend?use_case=...
# - GET /api/models/stats
# - GET /api/models/categories
# - GET /api/models/ui (صفحه HTML)
"""
|