anycoder-0f410020 / utils.py
akhaliq's picture
akhaliq HF Staff
Upload folder using huggingface_hub
4d1fa6f verified
raw
history blame
2.81 kB
import random
import time
from typing import Dict, List, Tuple, Optional
def analyze_ui_code(ui_code: str) -> Dict[str, any]:
"""Analyze UI code and extract metrics"""
return {
"components": ui_code.count('gr.') - ui_code.count('gr.Blocks'),
"has_rows": 'Row' in ui_code,
"has_columns": 'Column' in ui_code,
"has_variants": 'variant=' in ui_code,
"has_interactive_elements": any(comp in ui_code for comp in ['Button', 'Textbox', 'Slider', 'Dropdown']),
"code_lines": len(ui_code.split('\n')),
"complexity": "High" if ui_code.count('gr.') > 10 else "Medium" if ui_code.count('gr.') > 5 else "Low"
}
def get_style_tips(ui_code: str) -> List[str]:
"""Generate style improvement tips based on UI code"""
tips = []
if 'Row' not in ui_code and 'Column' not in ui_code:
tips.append("Consider using Rows and Columns for better layout organization")
if 'variant=' not in ui_code and 'Button' in ui_code:
tips.append("Add variants to buttons for visual hierarchy")
if 'info=' not in ui_code and 'Textbox' in ui_code:
tips.append("Add info text to input components for better UX")
if ui_code.count('gr.') < 5:
tips.append("Add more components for a richer interface")
return tips
def simulate_llm_response_time(model_name: str) -> float:
"""Simulate different response times for different LLMs"""
base_times = {"LLM 1": 0.5, "LLM 2": 0.7}
return base_times.get(model_name, 0.6) + random.uniform(-0.1, 0.2)
def generate_comparison_insights(ui1_code: str, ui2_code: str) -> str:
"""Generate insights comparing two UI codes"""
analysis1 = analyze_ui_code(ui1_code)
analysis2 = analyze_ui_code(ui2_code)
insights = "## πŸ” Comparison Insights\n\n"
if analysis1["components"] > analysis2["components"]:
insights += f"β€’ LLM 1 created a more complex UI with {analysis1['components']} components vs {analysis2['components']}\n"
elif analysis2["components"] > analysis1["components"]:
insights += f"β€’ LLM 2 created a more complex UI with {analysis2['components']} components vs {analysis1['components']}\n"
else:
insights += f"β€’ Both LLMs created UIs with {analysis1['components']} components\n"
if analysis1["has_rows"] and analysis1["has_columns"]:
insights += "β€’ LLM 1 uses advanced layout with Rows and Columns\n"
if analysis2["has_rows"] and analysis2["has_columns"]:
insights += "β€’ LLM 2 uses advanced layout with Rows and Columns\n"
if analysis1["complexity"] != analysis2["complexity"]:
insights += f"β€’ Complexity difference: LLM 1 ({analysis1['complexity']}) vs LLM 2 ({analysis2['complexity']})\n"
return insights