File size: 12,754 Bytes
e8434f3
 
25f8aca
 
e8434f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25f8aca
e8434f3
 
25f8aca
e8434f3
 
25f8aca
e8434f3
 
 
 
 
 
 
 
 
25f8aca
 
 
 
e8434f3
25f8aca
e8434f3
 
25f8aca
e8434f3
 
 
25f8aca
acb8402
 
e8434f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25f8aca
e8434f3
 
25f8aca
e8434f3
 
25f8aca
e8434f3
acb8402
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e8434f3
 
 
25f8aca
e8434f3
 
acb8402
e8434f3
25f8aca
 
acb8402
25f8aca
 
 
 
 
 
 
 
 
 
 
 
 
acb8402
25f8aca
 
 
 
 
 
 
 
acb8402
25f8aca
e8434f3
 
25f8aca
 
e8434f3
 
 
 
 
 
25f8aca
 
e8434f3
 
 
 
 
 
acb8402
25f8aca
 
e8434f3
 
 
 
 
 
 
 
 
 
 
acb8402
 
 
 
 
25f8aca
acb8402
 
 
 
 
e8434f3
 
 
 
 
 
25f8aca
e8434f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25f8aca
 
acb8402
 
 
 
 
 
 
 
e8434f3
 
 
25f8aca
 
e8434f3
acb8402
e8434f3
25f8aca
e8434f3
 
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
367
368
369
370
371
372
373
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"]

@app.on_event("startup")
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

@app.get("/", response_model=HealthResponse)
async def root():
    """Health check endpoint"""
    return HealthResponse(
        status="healthy",
        model_loaded=model_loaded,
        timestamp=datetime.now().isoformat()
    )

@app.get("/health", response_model=HealthResponse)
async def health_check():
    """Detailed health check"""
    return HealthResponse(
        status="healthy",
        model_loaded=model_loaded,
        timestamp=datetime.now().isoformat()
    )

@app.post("/chat", response_model=ChatResponse)
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)}"
        )

@app.get("/models")
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
    )