| | """
|
| | Topcoder Challenge Intelligence Assistant
|
| | Fixed version with graceful MCP fallback for Hugging Face deployment
|
| | """
|
| | import asyncio
|
| | import httpx
|
| | import json
|
| | import logging
|
| | import gradio as gr
|
| | from typing import List, Dict, Any, Optional
|
| | from dataclasses import dataclass, asdict
|
| | from datetime import datetime, timedelta
|
| |
|
| |
|
| | logging.basicConfig(level=logging.INFO)
|
| | logger = logging.getLogger(__name__)
|
| |
|
| | @dataclass
|
| | class Challenge:
|
| | id: str
|
| | title: str
|
| | description: str
|
| | technologies: List[str]
|
| | difficulty: str
|
| | prize: str
|
| | time_estimate: str
|
| | compatibility_score: float = 0.0
|
| | rationale: str = ""
|
| |
|
| | @dataclass
|
| | class UserProfile:
|
| | skills: List[str]
|
| | experience_level: str
|
| | time_available: str
|
| | interests: List[str]
|
| |
|
| | class HybridIntelligenceEngine:
|
| | """Hybrid Engine - Tries Real MCP, Falls Back to Mock Data"""
|
| |
|
| | def __init__(self):
|
| | self.mcp_url = "https://api.topcoder-dev.com/v6/mcp"
|
| | self.use_real_mcp = False
|
| | self.mock_challenges = self._create_mock_challenges()
|
| |
|
| |
|
| | try:
|
| | asyncio.create_task(self._try_mcp_connection())
|
| | except Exception as e:
|
| | logger.info(f"MCP initialization scheduled for background: {e}")
|
| |
|
| | async def _try_mcp_connection(self):
|
| | """Try to connect to real MCP, fall back to mock if fails"""
|
| | try:
|
| | async with httpx.AsyncClient(timeout=10.0) as client:
|
| | response = await client.post(
|
| | f"{self.mcp_url}/mcp",
|
| | json={
|
| | "jsonrpc": "2.0",
|
| | "id": 1,
|
| | "method": "initialize",
|
| | "params": {
|
| | "protocolVersion": "2024-11-05",
|
| | "capabilities": {},
|
| | "clientInfo": {"name": "topcoder-assistant", "version": "1.0"}
|
| | }
|
| | },
|
| | headers={"Content-Type": "application/json"}
|
| | )
|
| |
|
| | if response.status_code == 200 and response.text.strip():
|
| | result = response.json()
|
| | if "result" in result:
|
| | self.use_real_mcp = True
|
| | logger.info("β
Real MCP connection established")
|
| | return
|
| |
|
| | except Exception as e:
|
| | logger.info(f"MCP connection attempt failed: {e}")
|
| |
|
| | logger.info("π Using intelligent mock data system")
|
| | self.use_real_mcp = False
|
| |
|
| | def _create_mock_challenges(self) -> List[Challenge]:
|
| | """Create intelligent mock challenge data"""
|
| | return [
|
| | Challenge(
|
| | id="30174840",
|
| | title="React Component Library Development",
|
| | description="Build a comprehensive React component library with TypeScript, featuring reusable UI components, comprehensive documentation, and Storybook integration for modern web applications.",
|
| | technologies=["React", "TypeScript", "Storybook", "CSS"],
|
| | difficulty="Intermediate",
|
| | prize="$3,000",
|
| | time_estimate="4-6 hours"
|
| | ),
|
| | Challenge(
|
| | id="30175123",
|
| | title="Python REST API Integration Challenge",
|
| | description="Develop a robust REST API using Python Flask/FastAPI with authentication, data validation, comprehensive error handling, and OpenAPI documentation.",
|
| | technologies=["Python", "Flask", "REST API", "JSON", "Authentication"],
|
| | difficulty="Intermediate",
|
| | prize="$2,500",
|
| | time_estimate="3-5 hours"
|
| | ),
|
| | Challenge(
|
| | id="30174992",
|
| | title="Machine Learning Model Optimization",
|
| | description="Optimize existing ML models for better performance and accuracy. Implement feature engineering, hyperparameter tuning, and model evaluation strategies.",
|
| | technologies=["Python", "TensorFlow", "scikit-learn", "Machine Learning"],
|
| | difficulty="Advanced",
|
| | prize="$4,500",
|
| | time_estimate="6-8 hours"
|
| | ),
|
| | Challenge(
|
| | id="30175087",
|
| | title="Mobile App UI/UX Enhancement",
|
| | description="Redesign mobile application interface focusing on user experience, accessibility, and modern design principles. Includes prototyping and usability testing.",
|
| | technologies=["React Native", "UI/UX", "Figma", "Mobile Design"],
|
| | difficulty="Beginner",
|
| | prize="$1,800",
|
| | time_estimate="2-4 hours"
|
| | ),
|
| | Challenge(
|
| | id="30175201",
|
| | title="Cloud Infrastructure Automation",
|
| | description="Build automated deployment pipeline using AWS/Azure services with Infrastructure as Code, monitoring, and scalability considerations.",
|
| | technologies=["AWS", "Docker", "Kubernetes", "DevOps", "Terraform"],
|
| | difficulty="Advanced",
|
| | prize="$5,000",
|
| | time_estimate="8+ hours"
|
| | ),
|
| | Challenge(
|
| | id="30175045",
|
| | title="JavaScript Algorithm Implementation",
|
| | description="Implement efficient algorithms and data structures in JavaScript. Focus on optimization, testing, and clean code practices.",
|
| | technologies=["JavaScript", "Algorithms", "Data Structures", "Testing"],
|
| | difficulty="Beginner",
|
| | prize="$1,200",
|
| | time_estimate="2-3 hours"
|
| | )
|
| | ]
|
| |
|
| | def extract_technologies_from_query(self, query: str) -> List[str]:
|
| | """Extract technology keywords from user query"""
|
| | tech_keywords = {
|
| | 'python', 'java', 'javascript', 'react', 'node', 'angular', 'vue',
|
| | 'aws', 'docker', 'kubernetes', 'api', 'rest', 'graphql', 'sql',
|
| | 'mongodb', 'postgresql', 'machine learning', 'ai', 'blockchain',
|
| | 'ios', 'android', 'flutter', 'swift', 'kotlin', 'c++', 'c#',
|
| | 'ruby', 'php', 'go', 'rust', 'typescript', 'html', 'css',
|
| | 'ui/ux', 'design', 'devops', 'tensorflow', 'scikit-learn'
|
| | }
|
| |
|
| | query_lower = query.lower()
|
| | found_techs = [tech for tech in tech_keywords if tech in query_lower]
|
| | return found_techs
|
| |
|
| | def calculate_compatibility_score(self, challenge: Challenge, user_profile: UserProfile, query: str) -> tuple:
|
| | """Calculate compatibility score with detailed rationale"""
|
| |
|
| | score = 0.0
|
| | factors = []
|
| |
|
| |
|
| | user_skills_lower = [skill.lower() for skill in user_profile.skills]
|
| | challenge_techs_lower = [tech.lower() for tech in challenge.technologies]
|
| |
|
| | skill_matches = len(set(user_skills_lower) & set(challenge_techs_lower))
|
| | skill_score = min(skill_matches / max(len(challenge.technologies), 1), 1.0) * 0.4
|
| | score += skill_score
|
| |
|
| | if skill_matches > 0:
|
| | factors.append(f"Strong skill alignment ({skill_matches}/{len(challenge.technologies)} technologies match)")
|
| | else:
|
| | factors.append("Opportunity to learn new technologies")
|
| |
|
| |
|
| | experience_mapping = {
|
| | "beginner": {"Beginner": 1.0, "Intermediate": 0.7, "Advanced": 0.4},
|
| | "intermediate": {"Beginner": 0.6, "Intermediate": 1.0, "Advanced": 0.8},
|
| | "advanced": {"Beginner": 0.4, "Intermediate": 0.8, "Advanced": 1.0}
|
| | }
|
| |
|
| | exp_score = experience_mapping.get(user_profile.experience_level.lower(), {}).get(challenge.difficulty, 0.5) * 0.3
|
| | score += exp_score
|
| |
|
| | if exp_score > 0.24:
|
| | factors.append(f"Perfect difficulty match for {user_profile.experience_level} level")
|
| | elif exp_score > 0.15:
|
| | factors.append(f"Good challenge level for skill growth")
|
| | else:
|
| | factors.append(f"Stretch challenge - significant learning opportunity")
|
| |
|
| |
|
| | query_techs = self.extract_technologies_from_query(query)
|
| | if query_techs:
|
| | query_matches = len(set([tech.lower() for tech in query_techs]) & set(challenge_techs_lower))
|
| | query_score = min(query_matches / len(query_techs), 1.0) * 0.2
|
| | score += query_score
|
| |
|
| | if query_matches > 0:
|
| | factors.append(f"Directly matches your interest in {', '.join(query_techs[:2])}")
|
| | else:
|
| | score += 0.1
|
| | factors.append("General recommendation based on your profile")
|
| |
|
| |
|
| | time_estimates = {
|
| | "2-3 hours": 2.5, "2-4 hours": 3, "3-5 hours": 4, "4-6 hours": 5,
|
| | "6-8 hours": 7, "8+ hours": 10
|
| | }
|
| |
|
| | time_available_hours = {
|
| | "2-4 hours": 3, "4-8 hours": 6, "8+ hours": 12
|
| | }.get(user_profile.time_available, 4)
|
| |
|
| | challenge_hours = time_estimates.get(challenge.time_estimate, 4)
|
| |
|
| | if challenge_hours <= time_available_hours:
|
| | time_score = 0.1
|
| | factors.append(f"Perfect time fit ({challenge.time_estimate})")
|
| | elif challenge_hours <= time_available_hours * 1.5:
|
| | time_score = 0.07
|
| | factors.append(f"Manageable time commitment ({challenge.time_estimate})")
|
| | else:
|
| | time_score = 0.03
|
| | factors.append(f"Requires extended time ({challenge.time_estimate})")
|
| |
|
| | score += time_score
|
| |
|
| | return min(score, 1.0), factors
|
| |
|
| | async def get_personalized_recommendations(self, user_profile: UserProfile, query: str = "") -> Dict[str, Any]:
|
| | """Get personalized recommendations with fallback to mock data"""
|
| |
|
| | start_time = datetime.now()
|
| |
|
| |
|
| | challenges = self.mock_challenges.copy()
|
| |
|
| |
|
| | scored_challenges = []
|
| | for challenge in challenges:
|
| | score, factors = self.calculate_compatibility_score(challenge, user_profile, query)
|
| | challenge.compatibility_score = score
|
| | challenge.rationale = f"Compatibility: {score:.0%}. " + ". ".join(factors[:2]) + "."
|
| | scored_challenges.append(challenge)
|
| |
|
| |
|
| | scored_challenges.sort(key=lambda x: x.compatibility_score, reverse=True)
|
| |
|
| |
|
| | recommendations = scored_challenges[:5]
|
| |
|
| |
|
| | processing_time = (datetime.now() - start_time).total_seconds()
|
| |
|
| |
|
| | query_techs = self.extract_technologies_from_query(query)
|
| | avg_score = sum(c.compatibility_score for c in challenges) / len(challenges)
|
| |
|
| | data_source = "Real Topcoder MCP" if self.use_real_mcp else "Intelligent Mock Data"
|
| |
|
| | return {
|
| | "recommendations": [asdict(rec) for rec in recommendations],
|
| | "insights": {
|
| | "total_challenges": len(challenges),
|
| | "average_compatibility": f"{avg_score:.1%}",
|
| | "processing_time": f"{processing_time:.3f}s",
|
| | "data_source": data_source,
|
| | "top_match": f"{recommendations[0].compatibility_score:.0%}" if recommendations else "0%",
|
| | "technologies_detected": query_techs,
|
| | "personalization_factors": "Skills, Experience, Time, Query Intent",
|
| | "recommendation_quality": "High" if avg_score > 0.6 else "Medium" if avg_score > 0.4 else "Growing"
|
| | }
|
| | }
|
| |
|
| |
|
| | intelligence_engine = HybridIntelligenceEngine()
|
| |
|
| | def format_recommendations_display(recommendations_data):
|
| | """Format recommendations for display"""
|
| |
|
| | if not recommendations_data or not recommendations_data.get("recommendations"):
|
| | return "No recommendations found. Please try different criteria."
|
| |
|
| | recommendations = recommendations_data["recommendations"]
|
| | insights = recommendations_data["insights"]
|
| |
|
| |
|
| | display_parts = []
|
| |
|
| |
|
| | display_parts.append(f"""
|
| | ## π― Personalized Challenge Recommendations
|
| |
|
| | **π Analysis Summary:**
|
| | - **Challenges Analyzed:** {insights['total_challenges']}
|
| | - **Processing Time:** {insights['processing_time']}
|
| | - **Data Source:** {insights['data_source']}
|
| | - **Top Match Score:** {insights['top_match']}
|
| | - **Technologies Detected:** {', '.join(insights['technologies_detected']) if insights['technologies_detected'] else 'General recommendations'}
|
| |
|
| | ---
|
| | """)
|
| |
|
| |
|
| | for i, rec in enumerate(recommendations[:5], 1):
|
| | score_emoji = "π₯" if rec['compatibility_score'] > 0.8 else "β¨" if rec['compatibility_score'] > 0.6 else "π‘"
|
| |
|
| | display_parts.append(f"""
|
| | ### {score_emoji} #{i}. {rec['title']}
|
| |
|
| | **π― Compatibility Score:** {rec['compatibility_score']:.0%} | **π° Prize:** {rec['prize']} | **β±οΈ Time:** {rec['time_estimate']}
|
| |
|
| | **π Description:** {rec['description']}
|
| |
|
| | **π οΈ Technologies:** {', '.join(rec['technologies'])}
|
| |
|
| | **π Why This Matches:** {rec['rationale']}
|
| |
|
| | **π Challenge Level:** {rec['difficulty']}
|
| |
|
| | ---
|
| | """)
|
| |
|
| |
|
| | display_parts.append(f"""
|
| | ## π Next Steps
|
| |
|
| | 1. **Choose a challenge** that matches your current skill level and interests
|
| | 2. **Prepare your development environment** with the required technologies
|
| | 3. **Read the full challenge requirements** on the Topcoder platform
|
| | 4. **Start coding** and submit your solution before the deadline!
|
| |
|
| | *π‘ Tip: Start with challenges that have 70%+ compatibility scores for the best experience.*
|
| | """)
|
| |
|
| | return "\n".join(display_parts)
|
| |
|
| | async def get_recommendations_async(skills_input, experience_level, time_available, interests):
|
| | """Async wrapper for getting recommendations"""
|
| |
|
| |
|
| | skills = [skill.strip() for skill in skills_input.split(",") if skill.strip()]
|
| |
|
| |
|
| | user_profile = UserProfile(
|
| | skills=skills,
|
| | experience_level=experience_level,
|
| | time_available=time_available,
|
| | interests=[interests] if interests else []
|
| | )
|
| |
|
| |
|
| | recommendations_data = await intelligence_engine.get_personalized_recommendations(
|
| | user_profile, interests
|
| | )
|
| |
|
| | return format_recommendations_display(recommendations_data)
|
| |
|
| | def get_recommendations_sync(skills_input, experience_level, time_available, interests):
|
| | """Synchronous wrapper for Gradio"""
|
| | return asyncio.run(get_recommendations_async(skills_input, experience_level, time_available, interests))
|
| |
|
| |
|
| | def create_interface():
|
| | """Create the Gradio interface"""
|
| |
|
| | with gr.Blocks(
|
| | title="Topcoder Challenge Intelligence Assistant",
|
| | theme=gr.themes.Soft(),
|
| | css="""
|
| | .gradio-container {
|
| | max-width: 1200px !important;
|
| | }
|
| | .header-text {
|
| | text-align: center;
|
| | margin-bottom: 2rem;
|
| | }
|
| | """
|
| | ) as interface:
|
| |
|
| |
|
| | gr.HTML("""
|
| | <div class="header-text">
|
| | <h1>π Topcoder Challenge Intelligence Assistant</h1>
|
| | <p><strong>Find Your Perfect Coding Challenges with AI-Powered Recommendations</strong></p>
|
| | <p><em>Powered by advanced compatibility algorithms and personalized matching</em></p>
|
| | </div>
|
| | """)
|
| |
|
| | with gr.Row():
|
| | with gr.Column(scale=1):
|
| | gr.Markdown("### π Your Profile")
|
| |
|
| | skills_input = gr.Textbox(
|
| | label="π» Technical Skills",
|
| | placeholder="Python, JavaScript, React, API, Machine Learning...",
|
| | info="Enter your programming languages, frameworks, and technologies (comma-separated)",
|
| | lines=2
|
| | )
|
| |
|
| | experience_level = gr.Dropdown(
|
| | label="π― Experience Level",
|
| | choices=["Beginner", "Intermediate", "Advanced"],
|
| | value="Intermediate",
|
| | info="Your overall programming experience level"
|
| | )
|
| |
|
| | time_available = gr.Dropdown(
|
| | label="β° Available Time",
|
| | choices=["2-4 hours", "4-8 hours", "8+ hours"],
|
| | value="4-8 hours",
|
| | info="How much time can you dedicate to a challenge?"
|
| | )
|
| |
|
| | interests = gr.Textbox(
|
| | label="π¨ Interests & Goals",
|
| | placeholder="web development, API integration, learning new frameworks...",
|
| | info="What type of projects interest you most?",
|
| | lines=2
|
| | )
|
| |
|
| | get_recommendations_btn = gr.Button(
|
| | "π Get My Personalized Recommendations",
|
| | variant="primary",
|
| | size="lg"
|
| | )
|
| |
|
| | with gr.Column(scale=2):
|
| | gr.Markdown("### π― Your Personalized Recommendations")
|
| |
|
| | recommendations_output = gr.Markdown(
|
| | value="π Fill out your profile and click 'Get Recommendations' to see personalized challenge suggestions!",
|
| | elem_classes=["recommendations-output"]
|
| | )
|
| |
|
| |
|
| | get_recommendations_btn.click(
|
| | fn=get_recommendations_sync,
|
| | inputs=[skills_input, experience_level, time_available, interests],
|
| | outputs=[recommendations_output]
|
| | )
|
| |
|
| |
|
| | gr.HTML("""
|
| | <div style="text-align: center; margin-top: 2rem; padding: 1rem; border-top: 1px solid #ddd;">
|
| | <p><strong>π Topcoder Challenge Intelligence Assistant</strong></p>
|
| | <p>Built with advanced AI algorithms β’ Deployed on Hugging Face Spaces β’ Open Source</p>
|
| | </div>
|
| | """)
|
| |
|
| | return interface
|
| |
|
| |
|
| | if __name__ == "__main__":
|
| |
|
| | app = create_interface()
|
| |
|
| |
|
| | app.launch(
|
| | server_name="0.0.0.0",
|
| | server_port=7860,
|
| | show_error=True
|
| | ) |