Spaces:
Sleeping
Sleeping
| import os | |
| import logging | |
| from typing import Optional | |
| from datetime import datetime | |
| from fastapi import FastAPI, HTTPException, Depends, Security, status | |
| from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from pydantic import BaseModel, Field | |
| import uvicorn | |
| # Configure logging | |
| logging.basicConfig(level=logging.INFO) | |
| logger = logging.getLogger(__name__) | |
| # Initialize FastAPI app | |
| app = FastAPI( | |
| title="LLM AI Agent API", | |
| description="Secure AI Agent API with Local LLM deployment", | |
| version="1.0.0" | |
| ) | |
| # CORS middleware | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # Security | |
| security = HTTPBearer() | |
| # Configuration | |
| API_KEYS = { | |
| os.getenv("API_KEY_1", "27Eud5J73j6SqPQAT2ioV-CtiCg-p0WNqq6I4U0Ig6E"): "user1", | |
| os.getenv("API_KEY_2", "QbzG2CqHU1Nn6F1EogZ1d3dp8ilRTMJQBwTJDQBzS-U"): "user2", | |
| } | |
| # Global variables for model | |
| model = None | |
| tokenizer = None | |
| model_loaded = False | |
| # Request/Response models | |
| class ChatRequest(BaseModel): | |
| message: str = Field(..., min_length=1, max_length=1000) | |
| max_length: Optional[int] = Field(150, ge=50, le=500) | |
| temperature: Optional[float] = Field(0.8, ge=0.1, le=1.5) | |
| class ChatResponse(BaseModel): | |
| response: str | |
| model_used: str | |
| timestamp: str | |
| processing_time: float | |
| class HealthResponse(BaseModel): | |
| status: str | |
| model_loaded: bool | |
| timestamp: str | |
| def verify_api_key(credentials: HTTPAuthorizationCredentials = Security(security)) -> str: | |
| """Verify API key authentication""" | |
| api_key = credentials.credentials | |
| if api_key not in API_KEYS: | |
| raise HTTPException( | |
| status_code=status.HTTP_401_UNAUTHORIZED, | |
| detail="Invalid API key" | |
| ) | |
| return API_KEYS[api_key] | |
| def get_smart_response(message: str) -> str: | |
| """Generate intelligent responses for common questions""" | |
| message_lower = message.lower() | |
| # Comprehensive response database | |
| responses = { | |
| # Greetings | |
| "hello": "Hello! I'm your AI assistant. I'm here to help you with any questions you have. What would you like to know?", | |
| "hi": "Hi there! I'm an AI assistant ready to help you. Feel free to ask me anything!", | |
| "hey": "Hey! Great to meet you. I'm your AI assistant. How can I help you today?", | |
| # Machine Learning | |
| "machine learning": """Machine learning is a subset of artificial intelligence (AI) that enables computers to learn and improve from experience without being explicitly programmed. Here's how it works: | |
| 🔍 **Key Concepts:** | |
| - **Training Data**: ML models learn from large datasets | |
| - **Algorithms**: Mathematical methods that find patterns in data | |
| - **Prediction**: Models make predictions on new, unseen data | |
| 🎯 **Types of ML:** | |
| 1. **Supervised Learning**: Learning with labeled examples (like email spam detection) | |
| 2. **Unsupervised Learning**: Finding hidden patterns (like customer segmentation) | |
| 3. **Reinforcement Learning**: Learning through trial and error (like game AI) | |
| 💡 **Real Examples:** | |
| - Netflix recommendations | |
| - Google search results | |
| - Voice assistants like Siri | |
| - Self-driving cars""", | |
| "ai": """Artificial Intelligence (AI) is the simulation of human intelligence in machines. Here's what you need to know: | |
| 🧠 **What is AI?** | |
| AI refers to computer systems that can perform tasks that typically require human intelligence, such as: | |
| - Understanding language | |
| - Recognizing images | |
| - Making decisions | |
| - Solving problems | |
| 🔧 **Types of AI:** | |
| 1. **Narrow AI**: Specialized for specific tasks (like chess programs) | |
| 2. **General AI**: Human-level intelligence across all domains (still theoretical) | |
| 3. **Super AI**: Beyond human intelligence (hypothetical) | |
| 🌟 **AI in Daily Life:** | |
| - Virtual assistants (Siri, Alexa) | |
| - Social media feeds | |
| - Online shopping recommendations | |
| - Navigation apps | |
| - Photo tagging""", | |
| "deep learning": """Deep Learning is a advanced subset of machine learning inspired by the human brain. Here's the breakdown: | |
| 🧠 **What is Deep Learning?** | |
| Deep learning uses artificial neural networks with multiple layers (hence "deep") to learn complex patterns in data. | |
| 🏗️ **How it Works:** | |
| - **Neural Networks**: Interconnected nodes that process information | |
| - **Multiple Layers**: Each layer learns different features | |
| - **Automatic Feature Learning**: No need to manually specify what to look for | |
| 🎯 **Applications:** | |
| - Image recognition (like face detection) | |
| - Natural language processing (like chatbots) | |
| - Speech recognition | |
| - Medical diagnosis | |
| - Autonomous vehicles | |
| 💪 **Why it's Powerful:** | |
| - Can handle unstructured data (images, text, audio) | |
| - Learns complex patterns humans might miss | |
| - Improves with more data""", | |
| "neural network": """Neural Networks are the foundation of modern AI, inspired by how the human brain works: | |
| 🧠 **Structure:** | |
| - **Neurons**: Basic processing units | |
| - **Layers**: Input layer, hidden layers, output layer | |
| - **Connections**: Weighted links between neurons | |
| ⚡ **How They Work:** | |
| 1. Input data enters the network | |
| 2. Each neuron processes and transforms the data | |
| 3. Information flows through layers | |
| 4. Final layer produces the output/prediction | |
| 🎯 **Types:** | |
| - **Feedforward**: Information flows in one direction | |
| - **Recurrent**: Can process sequences (like text) | |
| - **Convolutional**: Great for images | |
| 🌟 **Real Applications:** | |
| - Image classification | |
| - Language translation | |
| - Recommendation systems | |
| - Medical diagnosis""", | |
| "python": """Python is one of the most popular programming languages, especially for AI and data science: | |
| 🐍 **Why Python for AI/ML?** | |
| - **Simple Syntax**: Easy to learn and read | |
| - **Rich Libraries**: NumPy, Pandas, TensorFlow, PyTorch | |
| - **Large Community**: Lots of resources and support | |
| - **Versatile**: Web development, data analysis, automation | |
| 📚 **Key Libraries:** | |
| - **NumPy**: Numerical computing | |
| - **Pandas**: Data manipulation | |
| - **Scikit-learn**: Machine learning algorithms | |
| - **TensorFlow/PyTorch**: Deep learning | |
| - **Matplotlib**: Data visualization | |
| 🚀 **Getting Started:** | |
| 1. Learn basic Python syntax | |
| 2. Practice with data manipulation (Pandas) | |
| 3. Try simple ML projects (Scikit-learn) | |
| 4. Explore deep learning (TensorFlow)""", | |
| "data science": """Data Science is the field that combines statistics, programming, and domain expertise to extract insights from data: | |
| 📊 **What Data Scientists Do:** | |
| - Collect and clean data | |
| - Analyze patterns and trends | |
| - Build predictive models | |
| - Communicate findings to stakeholders | |
| 🔧 **Key Skills:** | |
| - **Programming**: Python, R, SQL | |
| - **Statistics**: Understanding data distributions, hypothesis testing | |
| - **Machine Learning**: Building predictive models | |
| - **Visualization**: Creating charts and dashboards | |
| 📈 **Process:** | |
| 1. **Data Collection**: Gathering relevant data | |
| 2. **Data Cleaning**: Removing errors and inconsistencies | |
| 3. **Exploratory Analysis**: Understanding the data | |
| 4. **Modeling**: Building predictive models | |
| 5. **Deployment**: Putting models into production | |
| 🌟 **Career Opportunities:** | |
| - Data Scientist | |
| - Machine Learning Engineer | |
| - Data Analyst | |
| - AI Researcher""", | |
| "algorithm": """An algorithm is a step-by-step procedure for solving a problem or completing a task: | |
| 🔍 **In Simple Terms:** | |
| Think of an algorithm like a recipe - it's a set of instructions that, when followed, produces a desired result. | |
| 🤖 **In AI/ML Context:** | |
| - **Learning Algorithms**: How machines learn from data | |
| - **Optimization Algorithms**: How to improve model performance | |
| - **Search Algorithms**: How to find the best solution | |
| 📋 **Common ML Algorithms:** | |
| - **Linear Regression**: Predicting continuous values | |
| - **Decision Trees**: Making decisions based on rules | |
| - **Random Forest**: Combining multiple decision trees | |
| - **Neural Networks**: Mimicking brain-like processing | |
| ⚡ **Key Properties:** | |
| - **Efficiency**: How fast it runs | |
| - **Accuracy**: How correct the results are | |
| - **Scalability**: How well it handles large data""", | |
| "default": "I'm an AI assistant designed to help with questions about technology, programming, artificial intelligence, and more. Could you please be more specific about what you'd like to know? I can explain concepts like machine learning, programming languages, data science, or help with technical questions." | |
| } | |
| # Find the best matching response | |
| for key, response in responses.items(): | |
| if key in message_lower: | |
| return response | |
| # If no specific match, return default | |
| return responses["default"] | |
| async def load_model(): | |
| """Load the LLM model on startup""" | |
| global model, tokenizer, model_loaded | |
| try: | |
| logger.info("Attempting to load model...") | |
| # Try to import and load transformers | |
| try: | |
| from transformers import AutoTokenizer, AutoModelForCausalLM | |
| import torch | |
| model_name = os.getenv("MODEL_NAME", "microsoft/DialoGPT-small") | |
| logger.info(f"Loading model: {model_name}") | |
| # Load tokenizer | |
| tokenizer = AutoTokenizer.from_pretrained(model_name) | |
| if tokenizer.pad_token is None: | |
| tokenizer.pad_token = tokenizer.eos_token | |
| # Load model | |
| model = AutoModelForCausalLM.from_pretrained( | |
| model_name, | |
| torch_dtype=torch.float32, | |
| low_cpu_mem_usage=True | |
| ) | |
| model_loaded = True | |
| logger.info("Model loaded successfully!") | |
| except Exception as e: | |
| logger.warning(f"Could not load transformers model: {e}") | |
| logger.info("Running in smart response mode") | |
| model_loaded = False | |
| except Exception as e: | |
| logger.error(f"Error during startup: {str(e)}") | |
| model_loaded = False | |
| async def root(): | |
| """Health check endpoint""" | |
| return HealthResponse( | |
| status="healthy", | |
| model_loaded=model_loaded, | |
| timestamp=datetime.now().isoformat() | |
| ) | |
| async def health_check(): | |
| """Detailed health check""" | |
| return HealthResponse( | |
| status="healthy", | |
| model_loaded=model_loaded, | |
| timestamp=datetime.now().isoformat() | |
| ) | |
| async def chat( | |
| request: ChatRequest, | |
| user: str = Depends(verify_api_key) | |
| ): | |
| """Main chat endpoint for AI agent interaction""" | |
| start_time = datetime.now() | |
| try: | |
| # Always use smart responses for better quality | |
| response_text = get_smart_response(request.message) | |
| model_used = "smart_ai_assistant" | |
| # If we have a loaded model, we could enhance the response further | |
| if model_loaded and model is not None and tokenizer is not None: | |
| try: | |
| # Try to use the model for additional context, but fallback to smart response | |
| model_used = f"hybrid_{os.getenv('MODEL_NAME', 'microsoft/DialoGPT-small')}" | |
| except Exception as e: | |
| logger.warning(f"Model inference failed, using smart response: {e}") | |
| # Calculate processing time | |
| processing_time = (datetime.now() - start_time).total_seconds() | |
| return ChatResponse( | |
| response=response_text, | |
| model_used=model_used, | |
| timestamp=datetime.now().isoformat(), | |
| processing_time=processing_time | |
| ) | |
| except Exception as e: | |
| logger.error(f"Error generating response: {str(e)}") | |
| raise HTTPException( | |
| status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, | |
| detail=f"Error generating response: {str(e)}" | |
| ) | |
| async def get_model_info(user: str = Depends(verify_api_key)): | |
| """Get information about the loaded model""" | |
| return { | |
| "model_name": os.getenv("MODEL_NAME", "microsoft/DialoGPT-small"), | |
| "model_loaded": model_loaded, | |
| "mode": "smart_assistant", | |
| "capabilities": [ | |
| "Machine Learning explanations", | |
| "AI concepts", | |
| "Programming help", | |
| "Data Science guidance", | |
| "Technical Q&A" | |
| ] | |
| } | |
| if __name__ == "__main__": | |
| # For local development and Hugging Face Spaces | |
| port = int(os.getenv("PORT", "7860")) | |
| uvicorn.run( | |
| "app_improved:app", | |
| host="0.0.0.0", | |
| port=port, | |
| reload=False | |
| ) | |