Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -465,17 +465,17 @@ class MultimodalRAG:
|
|
| 465 |
logger.error(f"Error during cleanup: {e}")
|
| 466 |
|
| 467 |
class TemplateManager:
|
| 468 |
-
"""Manages Gradio
|
| 469 |
|
| 470 |
-
def __init__(self
|
| 471 |
-
self.
|
| 472 |
-
self.
|
| 473 |
-
self.
|
| 474 |
self.load_templates()
|
| 475 |
-
|
| 476 |
-
def
|
| 477 |
-
|
| 478 |
-
|
| 479 |
"image_classifier": Template(
|
| 480 |
code="""
|
| 481 |
import gradio as gr
|
|
@@ -1482,7 +1482,106 @@ def _get_builtin_templates(self) -> Dict[str, Template]:
|
|
| 1482 |
components=["Slider", "Textbox", "Button", "JSON"],
|
| 1483 |
metadata={"category": "task_automation"}
|
| 1484 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1485 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1486 |
def load_templates(self):
|
| 1487 |
"""Load all templates from directory"""
|
| 1488 |
try:
|
|
|
|
| 465 |
logger.error(f"Error during cleanup: {e}")
|
| 466 |
|
| 467 |
class TemplateManager:
|
| 468 |
+
"""Manages Gradio templates and their metadata"""
|
| 469 |
|
| 470 |
+
def __init__(self):
|
| 471 |
+
self.templates = {}
|
| 472 |
+
self.component_index = {}
|
| 473 |
+
self.category_index = {}
|
| 474 |
self.load_templates()
|
| 475 |
+
|
| 476 |
+
def load_templates(self):
|
| 477 |
+
"""Load built-in templates"""
|
| 478 |
+
self.templates = {
|
| 479 |
"image_classifier": Template(
|
| 480 |
code="""
|
| 481 |
import gradio as gr
|
|
|
|
| 1482 |
components=["Slider", "Textbox", "Button", "JSON"],
|
| 1483 |
metadata={"category": "task_automation"}
|
| 1484 |
)
|
| 1485 |
+
}
|
| 1486 |
+
|
| 1487 |
+
# Build indexes
|
| 1488 |
+
self._build_component_index()
|
| 1489 |
+
self._build_category_index()
|
| 1490 |
+
|
| 1491 |
+
def _build_component_index(self):
|
| 1492 |
+
"""Build index of templates by component"""
|
| 1493 |
+
self.component_index = {}
|
| 1494 |
+
for name, template in self.templates.items():
|
| 1495 |
+
for component in template.components:
|
| 1496 |
+
if component not in self.component_index:
|
| 1497 |
+
self.component_index[component] = []
|
| 1498 |
+
self.component_index[component].append(name)
|
| 1499 |
+
|
| 1500 |
+
def _build_category_index(self):
|
| 1501 |
+
"""Build index of templates by category"""
|
| 1502 |
+
self.category_index = {}
|
| 1503 |
+
for name, template in self.templates.items():
|
| 1504 |
+
category = template.metadata.get("category", "other")
|
| 1505 |
+
if category not in self.category_index:
|
| 1506 |
+
self.category_index[category] = []
|
| 1507 |
+
self.category_index[category].append(name)
|
| 1508 |
+
|
| 1509 |
+
def get_template(self, name: str) -> Optional[Template]:
|
| 1510 |
+
"""Get template by name"""
|
| 1511 |
+
return self.templates.get(name)
|
| 1512 |
+
|
| 1513 |
+
def search(self, query: str, limit: int = 5) -> List[Dict]:
|
| 1514 |
+
"""Search templates by description or metadata"""
|
| 1515 |
+
results = []
|
| 1516 |
+
for name, template in self.templates.items():
|
| 1517 |
+
score = self._calculate_search_score(query, template)
|
| 1518 |
+
if score > 0:
|
| 1519 |
+
results.append({
|
| 1520 |
+
"name": name,
|
| 1521 |
+
"template": template,
|
| 1522 |
+
"score": score
|
| 1523 |
+
})
|
| 1524 |
|
| 1525 |
+
results.sort(key=lambda x: x["score"], reverse=True)
|
| 1526 |
+
return results[:limit]
|
| 1527 |
+
|
| 1528 |
+
def _calculate_search_score(self, query: str, template: Template) -> float:
|
| 1529 |
+
"""Calculate search relevance score"""
|
| 1530 |
+
query = query.lower()
|
| 1531 |
+
score = 0.0
|
| 1532 |
+
|
| 1533 |
+
# Check description match
|
| 1534 |
+
if query in template.description.lower():
|
| 1535 |
+
score += 1.0
|
| 1536 |
+
|
| 1537 |
+
# Check category match
|
| 1538 |
+
category = template.metadata.get("category", "").lower()
|
| 1539 |
+
if query in category:
|
| 1540 |
+
score += 0.5
|
| 1541 |
+
|
| 1542 |
+
# Check component match
|
| 1543 |
+
for component in template.components:
|
| 1544 |
+
if query in component.lower():
|
| 1545 |
+
score += 0.2
|
| 1546 |
+
|
| 1547 |
+
return score
|
| 1548 |
+
|
| 1549 |
+
def get_categories(self) -> List[str]:
|
| 1550 |
+
"""Get list of available categories"""
|
| 1551 |
+
return list(self.category_index.keys())
|
| 1552 |
+
|
| 1553 |
+
def get_components(self) -> List[str]:
|
| 1554 |
+
"""Get list of available components"""
|
| 1555 |
+
return list(self.component_index.keys())
|
| 1556 |
+
|
| 1557 |
+
# Update the Template class if needed
|
| 1558 |
+
@dataclass
|
| 1559 |
+
class Template:
|
| 1560 |
+
"""Template data structure"""
|
| 1561 |
+
code: str
|
| 1562 |
+
description: str
|
| 1563 |
+
components: List[str]
|
| 1564 |
+
metadata: Dict[str, Any] = field(default_factory=dict)
|
| 1565 |
+
version: str = "1.0"
|
| 1566 |
+
|
| 1567 |
+
# Main execution
|
| 1568 |
+
if __name__ == "__main__":
|
| 1569 |
+
# Configure logging
|
| 1570 |
+
logging.basicConfig(
|
| 1571 |
+
level=logging.INFO,
|
| 1572 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
| 1573 |
+
)
|
| 1574 |
+
logger = logging.getLogger(__name__)
|
| 1575 |
+
|
| 1576 |
+
# Initialize GPU configuration
|
| 1577 |
+
setup_gpu_memory()
|
| 1578 |
+
|
| 1579 |
+
try:
|
| 1580 |
+
# Create and run the builder interface
|
| 1581 |
+
demo = create_builder_interface()
|
| 1582 |
+
demo.launch(share=True)
|
| 1583 |
+
except Exception as e:
|
| 1584 |
+
logger.error(f"Error running application: {e}")
|
| 1585 |
def load_templates(self):
|
| 1586 |
"""Load all templates from directory"""
|
| 1587 |
try:
|