Spaces:
Runtime error
Runtime error
File size: 15,675 Bytes
fcb5a67 |
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 |
import React, { useState } from 'react';
import { Sparkles, BookOpen, Clock, Target, Loader2, CheckCircle, ArrowRight, Settings, Zap, AlertCircle } from 'lucide-react';
import { AIEnhancedWikimedia } from '../utils/ai-enhanced-wikimedia';
import { AIProviderManager } from '../utils/ai-providers';
import { StudyPlan } from '../types';
interface AIStudyPlanGeneratorProps {
onPlanGenerated: (plan: StudyPlan) => void;
}
const AIStudyPlanGenerator: React.FC<AIStudyPlanGeneratorProps> = ({ onPlanGenerated }) => {
const [topic, setTopic] = useState('');
const [difficulty, setDifficulty] = useState<'beginner' | 'intermediate' | 'advanced'>('beginner');
const [loading, setLoading] = useState(false);
const [generatedPlan, setGeneratedPlan] = useState<StudyPlan | null>(null);
const [useAI, setUseAI] = useState(true);
const [aiStatus, setAiStatus] = useState<'checking' | 'available' | 'unavailable'>('checking');
const [showAIConfig, setShowAIConfig] = useState(false);
React.useEffect(() => {
checkAIAvailability();
}, []);
const checkAIAvailability = async () => {
setAiStatus('checking');
try {
const config = AIProviderManager.getConfig();
// Check if any provider is configured
if (!config.selectedProvider) {
setAiStatus('unavailable');
return;
}
// For providers that require API keys, check if key exists
const provider = AIProviderManager.getProviderById(config.selectedProvider);
if (provider?.requiresApiKey) {
const hasApiKey = config.apiKeys && config.apiKeys[config.selectedProvider];
if (!hasApiKey) {
setAiStatus('unavailable');
return;
}
}
// Test actual connection
const isAvailable = await AIProviderManager.testConnection(config.selectedProvider);
setAiStatus(isAvailable ? 'available' : 'unavailable');
} catch (error) {
console.error('AI availability check failed:', error);
setAiStatus('unavailable');
}
};
const handleGenerate = async () => {
if (!topic.trim()) return;
setLoading(true);
try {
let plan: StudyPlan;
if (useAI && aiStatus === 'available') {
// Use AI-enhanced generation
plan = await AIEnhancedWikimedia.generateEnhancedStudyPlan(topic, difficulty);
} else {
// Use standard Wikimedia-based generation
plan = await AIEnhancedWikimedia.generateStudyPlan(topic, difficulty);
}
setGeneratedPlan(plan);
} catch (error) {
console.error('Failed to generate study plan:', error);
} finally {
setLoading(false);
}
};
const handleAcceptPlan = () => {
if (generatedPlan) {
onPlanGenerated(generatedPlan);
setGeneratedPlan(null);
setTopic('');
}
};
const getDifficultyColor = (level: string) => {
switch (level) {
case 'beginner': return 'bg-success-100 text-success-800';
case 'intermediate': return 'bg-warning-100 text-warning-800';
case 'advanced': return 'bg-error-100 text-error-800';
default: return 'bg-gray-100 text-gray-800';
}
};
const getAIStatusMessage = () => {
const config = AIProviderManager.getConfig();
const provider = AIProviderManager.getProviderById(config.selectedProvider);
switch (aiStatus) {
case 'checking':
return 'Testing connection to configured AI provider...';
case 'available':
return `Using ${provider?.name || 'AI'} for enhanced study plan generation`;
case 'unavailable':
if (!config.selectedProvider) {
return 'No AI provider configured. Click "Configure AI" to set up open-source AI.';
}
if (provider?.requiresApiKey && !config.apiKeys?.[config.selectedProvider]) {
return `${provider.name} requires an API key. Click "Configure AI" to add it.`;
}
return `Cannot connect to ${provider?.name || 'AI provider'}. Using Wikimedia content analysis instead.`;
default:
return 'Checking AI availability...';
}
};
return (
<div className="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
<div className="bg-gradient-to-br from-primary-50 to-secondary-50 rounded-2xl p-8 border border-primary-100">
<div className="flex items-center space-x-3 mb-6">
<div className="w-12 h-12 bg-gradient-to-r from-primary-500 to-secondary-500 rounded-xl flex items-center justify-center">
<Sparkles className="w-6 h-6 text-white" />
</div>
<div>
<h2 className="text-2xl font-bold text-gray-900">AI Study Plan Generator</h2>
<p className="text-gray-600">Create personalized learning paths from Wikimedia content</p>
</div>
</div>
{/* AI Status Banner */}
<div className={`mb-6 p-4 rounded-xl border ${
aiStatus === 'available'
? 'bg-green-50 border-green-200'
: aiStatus === 'unavailable'
? 'bg-yellow-50 border-yellow-200'
: 'bg-gray-50 border-gray-200'
}`}>
<div className="flex items-start justify-between">
<div className="flex items-start space-x-3 flex-1">
{aiStatus === 'checking' && <Loader2 className="w-5 h-5 animate-spin text-gray-600 mt-0.5" />}
{aiStatus === 'available' && <Zap className="w-5 h-5 text-green-600 mt-0.5" />}
{aiStatus === 'unavailable' && <AlertCircle className="w-5 h-5 text-yellow-600 mt-0.5" />}
<div className="flex-1">
<div className="font-medium text-gray-900 mb-1">
{aiStatus === 'checking' && 'Checking AI Availability...'}
{aiStatus === 'available' && 'AI Enhancement Available'}
{aiStatus === 'unavailable' && 'AI Enhancement Unavailable'}
</div>
<div className="text-sm text-gray-600">
{getAIStatusMessage()}
</div>
{aiStatus === 'unavailable' && (
<button
onClick={() => setShowAIConfig(true)}
className="mt-2 text-sm text-primary-600 hover:text-primary-700 font-medium underline"
>
Configure AI Provider
</button>
)}
</div>
</div>
<div className="flex items-center space-x-3 ml-4">
{aiStatus === 'available' && (
<label className="flex items-center space-x-2">
<input
type="checkbox"
checked={useAI}
onChange={(e) => setUseAI(e.target.checked)}
className="rounded border-gray-300 text-primary-600 focus:ring-primary-500"
/>
<span className="text-sm font-medium text-gray-700">Use AI Enhancement</span>
</label>
)}
<button
onClick={() => setShowAIConfig(true)}
className="flex items-center space-x-1 px-3 py-1 bg-gray-100 text-gray-700 rounded-lg text-sm hover:bg-gray-200 transition-colors"
>
<Settings className="w-3 h-3" />
<span>Configure</span>
</button>
</div>
</div>
</div>
{!generatedPlan ? (
<div className="space-y-6">
<div>
<label className="block text-sm font-medium text-gray-700 mb-2">
What would you like to learn?
</label>
<input
type="text"
value={topic}
onChange={(e) => setTopic(e.target.value)}
placeholder="e.g., Machine Learning, Ancient History, Climate Science..."
className="w-full px-4 py-3 border border-gray-300 rounded-xl focus:ring-2 focus:ring-primary-500 focus:border-transparent text-lg"
onKeyDown={(e) => e.key === 'Enter' && handleGenerate()}
/>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-3">
Difficulty Level
</label>
<div className="grid grid-cols-3 gap-3">
{(['beginner', 'intermediate', 'advanced'] as const).map((level) => (
<button
key={level}
onClick={() => setDifficulty(level)}
className={`p-4 rounded-xl border-2 transition-all ${
difficulty === level
? 'border-primary-500 bg-primary-50 text-primary-700'
: 'border-gray-200 bg-white text-gray-700 hover:border-gray-300'
}`}
>
<div className="text-center">
<div className="font-medium capitalize">{level}</div>
<div className="text-sm text-gray-500 mt-1">
{level === 'beginner' && '3-5 topics'}
{level === 'intermediate' && '6-8 topics'}
{level === 'advanced' && '9-12 topics'}
</div>
</div>
</button>
))}
</div>
</div>
<button
onClick={handleGenerate}
disabled={loading || !topic.trim()}
className="w-full flex items-center justify-center space-x-2 px-6 py-4 bg-gradient-to-r from-primary-600 to-secondary-600 text-white rounded-xl hover:from-primary-700 hover:to-secondary-700 transition-all duration-200 disabled:opacity-50 disabled:cursor-not-allowed"
>
{loading ? (
<>
<Loader2 className="w-5 h-5 animate-spin" />
<span>
{useAI && aiStatus === 'available'
? 'AI is generating your study plan...'
: 'Generating Study Plan...'
}
</span>
</>
) : (
<>
<Sparkles className="w-5 h-5" />
<span>
{useAI && aiStatus === 'available'
? 'Generate AI-Enhanced Study Plan'
: 'Generate Study Plan'
}
</span>
</>
)}
</button>
{/* Quick Setup Guide */}
{aiStatus === 'unavailable' && (
<div className="bg-blue-50 border border-blue-200 rounded-xl p-4">
<h4 className="font-medium text-blue-900 mb-3">🚀 Quick AI Setup</h4>
<div className="space-y-3 text-sm text-blue-800">
<div className="flex items-start space-x-2">
<span className="font-medium">Option 1 (Privacy):</span>
<div>
<p>Install Ollama locally for complete privacy</p>
<p className="text-blue-600">• Download from ollama.ai</p>
<p className="text-blue-600">• Run: ollama pull llama3.2</p>
</div>
</div>
<div className="flex items-start space-x-2">
<span className="font-medium">Option 2 (Cloud):</span>
<div>
<p>Get free Hugging Face API key</p>
<p className="text-blue-600">• Sign up at huggingface.co</p>
<p className="text-blue-600">• Add key in AI configuration</p>
</div>
</div>
</div>
<button
onClick={checkAIAvailability}
className="mt-3 text-sm text-blue-600 hover:text-blue-700 font-medium"
>
Recheck AI Availability
</button>
</div>
)}
</div>
) : (
<div className="space-y-6">
<div className="bg-white rounded-xl p-6 border border-gray-200">
<div className="flex items-start justify-between mb-4">
<div className="flex-1">
<div className="flex items-center space-x-2 mb-2">
<h3 className="text-xl font-bold text-gray-900">{generatedPlan.title}</h3>
{useAI && aiStatus === 'available' && (
<span className="px-2 py-1 bg-purple-100 text-purple-700 text-xs rounded-full font-medium">
AI Enhanced
</span>
)}
</div>
<p className="text-gray-600 mb-4">{generatedPlan.description}</p>
<div className="flex items-center space-x-4">
<span className={`px-3 py-1 rounded-full text-sm font-medium ${getDifficultyColor(generatedPlan.difficulty)}`}>
{generatedPlan.difficulty}
</span>
<div className="flex items-center text-sm text-gray-600">
<Clock className="w-4 h-4 mr-1" />
{generatedPlan.estimatedTime}
</div>
<div className="flex items-center text-sm text-gray-600">
<BookOpen className="w-4 h-4 mr-1" />
{generatedPlan.topics.length} topics
</div>
</div>
</div>
</div>
<div className="space-y-3">
<h4 className="font-medium text-gray-900">Study Topics:</h4>
{generatedPlan.topics.slice(0, 5).map((topic, index) => (
<div key={topic.id} className="flex items-center space-x-3 p-3 bg-gray-50 rounded-lg">
<div className="w-6 h-6 bg-primary-100 rounded-full flex items-center justify-center text-primary-600 text-sm font-medium">
{index + 1}
</div>
<div className="flex-1">
<div className="font-medium text-gray-900">{topic.title}</div>
<div className="text-sm text-gray-600">{topic.estimatedTime}</div>
</div>
<CheckCircle className="w-5 h-5 text-gray-300" />
</div>
))}
{generatedPlan.topics.length > 5 && (
<div className="text-sm text-gray-500 text-center py-2">
+{generatedPlan.topics.length - 5} more topics...
</div>
)}
</div>
</div>
<div className="flex space-x-3">
<button
onClick={() => setGeneratedPlan(null)}
className="flex-1 px-4 py-3 border border-gray-300 rounded-xl text-gray-700 hover:bg-gray-50 transition-colors"
>
Generate New Plan
</button>
<button
onClick={handleAcceptPlan}
className="flex-1 flex items-center justify-center space-x-2 px-4 py-3 bg-primary-600 text-white rounded-xl hover:bg-primary-700 transition-colors"
>
<span>Accept Plan</span>
<ArrowRight className="w-4 h-4" />
</button>
</div>
</div>
)}
</div>
</div>
);
};
export default AIStudyPlanGenerator; |