Spaces:
Sleeping
Sleeping
Improve: Add comprehensive AI responses with detailed explanations
Browse files
app.py
CHANGED
@@ -46,8 +46,8 @@ model_loaded = False
|
|
46 |
# Request/Response models
|
47 |
class ChatRequest(BaseModel):
|
48 |
message: str = Field(..., min_length=1, max_length=1000)
|
49 |
-
max_length: Optional[int] = Field(
|
50 |
-
temperature: Optional[float] = Field(0.
|
51 |
|
52 |
class ChatResponse(BaseModel):
|
53 |
response: str
|
@@ -72,17 +72,194 @@ def verify_api_key(credentials: HTTPAuthorizationCredentials = Security(security
|
|
72 |
|
73 |
return API_KEYS[api_key]
|
74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
@app.on_event("startup")
|
76 |
async def load_model():
|
77 |
"""Load the LLM model on startup"""
|
78 |
global model, tokenizer, model_loaded
|
79 |
|
80 |
try:
|
81 |
-
logger.info("
|
82 |
|
83 |
# Try to import and load transformers
|
84 |
try:
|
85 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
86 |
import torch
|
87 |
|
88 |
model_name = os.getenv("MODEL_NAME", "microsoft/DialoGPT-small")
|
@@ -96,7 +273,7 @@ async def load_model():
|
|
96 |
# Load model
|
97 |
model = AutoModelForCausalLM.from_pretrained(
|
98 |
model_name,
|
99 |
-
torch_dtype=torch.float32,
|
100 |
low_cpu_mem_usage=True
|
101 |
)
|
102 |
|
@@ -105,7 +282,7 @@ async def load_model():
|
|
105 |
|
106 |
except Exception as e:
|
107 |
logger.warning(f"Could not load transformers model: {e}")
|
108 |
-
logger.info("Running in
|
109 |
model_loaded = False
|
110 |
|
111 |
except Exception as e:
|
@@ -125,7 +302,7 @@ async def root():
|
|
125 |
async def health_check():
|
126 |
"""Detailed health check"""
|
127 |
return HealthResponse(
|
128 |
-
status="healthy"
|
129 |
model_loaded=model_loaded,
|
130 |
timestamp=datetime.now().isoformat()
|
131 |
)
|
@@ -139,53 +316,17 @@ async def chat(
|
|
139 |
start_time = datetime.now()
|
140 |
|
141 |
try:
|
|
|
|
|
|
|
|
|
|
|
142 |
if model_loaded and model is not None and tokenizer is not None:
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
"
|
148 |
-
model=model,
|
149 |
-
tokenizer=tokenizer,
|
150 |
-
device=-1 # Use CPU
|
151 |
-
)
|
152 |
-
|
153 |
-
# Generate response
|
154 |
-
generated = generator(
|
155 |
-
request.message,
|
156 |
-
max_length=request.max_length,
|
157 |
-
temperature=request.temperature,
|
158 |
-
do_sample=True,
|
159 |
-
pad_token_id=tokenizer.eos_token_id,
|
160 |
-
num_return_sequences=1
|
161 |
-
)
|
162 |
-
|
163 |
-
response_text = generated[0]['generated_text']
|
164 |
-
if request.message in response_text:
|
165 |
-
response_text = response_text.replace(request.message, "").strip()
|
166 |
-
|
167 |
-
model_used = os.getenv("MODEL_NAME", "microsoft/DialoGPT-small")
|
168 |
-
|
169 |
-
else:
|
170 |
-
# Demo mode - simple responses
|
171 |
-
demo_responses = {
|
172 |
-
"hello": "Hello! I'm your AI assistant. How can I help you today?",
|
173 |
-
"hi": "Hi there! I'm ready to assist you.",
|
174 |
-
"how are you": "I'm doing well, thank you for asking! How can I help you?",
|
175 |
-
"what is ai": "AI (Artificial Intelligence) is the simulation of human intelligence in machines that are programmed to think and learn.",
|
176 |
-
"machine learning": "Machine learning is a subset of AI that enables computers to learn and improve from experience without being explicitly programmed.",
|
177 |
-
"default": "I'm an AI assistant ready to help you. Could you please rephrase your question?"
|
178 |
-
}
|
179 |
-
|
180 |
-
message_lower = request.message.lower()
|
181 |
-
response_text = demo_responses.get("default", "I'm here to help!")
|
182 |
-
|
183 |
-
for key, response in demo_responses.items():
|
184 |
-
if key in message_lower:
|
185 |
-
response_text = response
|
186 |
-
break
|
187 |
-
|
188 |
-
model_used = "demo_mode"
|
189 |
|
190 |
# Calculate processing time
|
191 |
processing_time = (datetime.now() - start_time).total_seconds()
|
@@ -210,14 +351,21 @@ async def get_model_info(user: str = Depends(verify_api_key)):
|
|
210 |
return {
|
211 |
"model_name": os.getenv("MODEL_NAME", "microsoft/DialoGPT-small"),
|
212 |
"model_loaded": model_loaded,
|
213 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
214 |
}
|
215 |
|
216 |
if __name__ == "__main__":
|
217 |
# For local development and Hugging Face Spaces
|
218 |
port = int(os.getenv("PORT", "7860"))
|
219 |
uvicorn.run(
|
220 |
-
"
|
221 |
host="0.0.0.0",
|
222 |
port=port,
|
223 |
reload=False
|
|
|
46 |
# Request/Response models
|
47 |
class ChatRequest(BaseModel):
|
48 |
message: str = Field(..., min_length=1, max_length=1000)
|
49 |
+
max_length: Optional[int] = Field(150, ge=50, le=500)
|
50 |
+
temperature: Optional[float] = Field(0.8, ge=0.1, le=1.5)
|
51 |
|
52 |
class ChatResponse(BaseModel):
|
53 |
response: str
|
|
|
72 |
|
73 |
return API_KEYS[api_key]
|
74 |
|
75 |
+
def get_smart_response(message: str) -> str:
|
76 |
+
"""Generate intelligent responses for common questions"""
|
77 |
+
message_lower = message.lower()
|
78 |
+
|
79 |
+
# Comprehensive response database
|
80 |
+
responses = {
|
81 |
+
# Greetings
|
82 |
+
"hello": "Hello! I'm your AI assistant. I'm here to help you with any questions you have. What would you like to know?",
|
83 |
+
"hi": "Hi there! I'm an AI assistant ready to help you. Feel free to ask me anything!",
|
84 |
+
"hey": "Hey! Great to meet you. I'm your AI assistant. How can I help you today?",
|
85 |
+
|
86 |
+
# Machine Learning
|
87 |
+
"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:
|
88 |
+
|
89 |
+
π **Key Concepts:**
|
90 |
+
- **Training Data**: ML models learn from large datasets
|
91 |
+
- **Algorithms**: Mathematical methods that find patterns in data
|
92 |
+
- **Prediction**: Models make predictions on new, unseen data
|
93 |
+
|
94 |
+
π― **Types of ML:**
|
95 |
+
1. **Supervised Learning**: Learning with labeled examples (like email spam detection)
|
96 |
+
2. **Unsupervised Learning**: Finding hidden patterns (like customer segmentation)
|
97 |
+
3. **Reinforcement Learning**: Learning through trial and error (like game AI)
|
98 |
+
|
99 |
+
π‘ **Real Examples:**
|
100 |
+
- Netflix recommendations
|
101 |
+
- Google search results
|
102 |
+
- Voice assistants like Siri
|
103 |
+
- Self-driving cars""",
|
104 |
+
|
105 |
+
"ai": """Artificial Intelligence (AI) is the simulation of human intelligence in machines. Here's what you need to know:
|
106 |
+
|
107 |
+
π§ **What is AI?**
|
108 |
+
AI refers to computer systems that can perform tasks that typically require human intelligence, such as:
|
109 |
+
- Understanding language
|
110 |
+
- Recognizing images
|
111 |
+
- Making decisions
|
112 |
+
- Solving problems
|
113 |
+
|
114 |
+
π§ **Types of AI:**
|
115 |
+
1. **Narrow AI**: Specialized for specific tasks (like chess programs)
|
116 |
+
2. **General AI**: Human-level intelligence across all domains (still theoretical)
|
117 |
+
3. **Super AI**: Beyond human intelligence (hypothetical)
|
118 |
+
|
119 |
+
π **AI in Daily Life:**
|
120 |
+
- Virtual assistants (Siri, Alexa)
|
121 |
+
- Social media feeds
|
122 |
+
- Online shopping recommendations
|
123 |
+
- Navigation apps
|
124 |
+
- Photo tagging""",
|
125 |
+
|
126 |
+
"deep learning": """Deep Learning is a advanced subset of machine learning inspired by the human brain. Here's the breakdown:
|
127 |
+
|
128 |
+
π§ **What is Deep Learning?**
|
129 |
+
Deep learning uses artificial neural networks with multiple layers (hence "deep") to learn complex patterns in data.
|
130 |
+
|
131 |
+
ποΈ **How it Works:**
|
132 |
+
- **Neural Networks**: Interconnected nodes that process information
|
133 |
+
- **Multiple Layers**: Each layer learns different features
|
134 |
+
- **Automatic Feature Learning**: No need to manually specify what to look for
|
135 |
+
|
136 |
+
π― **Applications:**
|
137 |
+
- Image recognition (like face detection)
|
138 |
+
- Natural language processing (like chatbots)
|
139 |
+
- Speech recognition
|
140 |
+
- Medical diagnosis
|
141 |
+
- Autonomous vehicles
|
142 |
+
|
143 |
+
πͺ **Why it's Powerful:**
|
144 |
+
- Can handle unstructured data (images, text, audio)
|
145 |
+
- Learns complex patterns humans might miss
|
146 |
+
- Improves with more data""",
|
147 |
+
|
148 |
+
"neural network": """Neural Networks are the foundation of modern AI, inspired by how the human brain works:
|
149 |
+
|
150 |
+
π§ **Structure:**
|
151 |
+
- **Neurons**: Basic processing units
|
152 |
+
- **Layers**: Input layer, hidden layers, output layer
|
153 |
+
- **Connections**: Weighted links between neurons
|
154 |
+
|
155 |
+
β‘ **How They Work:**
|
156 |
+
1. Input data enters the network
|
157 |
+
2. Each neuron processes and transforms the data
|
158 |
+
3. Information flows through layers
|
159 |
+
4. Final layer produces the output/prediction
|
160 |
+
|
161 |
+
π― **Types:**
|
162 |
+
- **Feedforward**: Information flows in one direction
|
163 |
+
- **Recurrent**: Can process sequences (like text)
|
164 |
+
- **Convolutional**: Great for images
|
165 |
+
|
166 |
+
π **Real Applications:**
|
167 |
+
- Image classification
|
168 |
+
- Language translation
|
169 |
+
- Recommendation systems
|
170 |
+
- Medical diagnosis""",
|
171 |
+
|
172 |
+
"python": """Python is one of the most popular programming languages, especially for AI and data science:
|
173 |
+
|
174 |
+
π **Why Python for AI/ML?**
|
175 |
+
- **Simple Syntax**: Easy to learn and read
|
176 |
+
- **Rich Libraries**: NumPy, Pandas, TensorFlow, PyTorch
|
177 |
+
- **Large Community**: Lots of resources and support
|
178 |
+
- **Versatile**: Web development, data analysis, automation
|
179 |
+
|
180 |
+
π **Key Libraries:**
|
181 |
+
- **NumPy**: Numerical computing
|
182 |
+
- **Pandas**: Data manipulation
|
183 |
+
- **Scikit-learn**: Machine learning algorithms
|
184 |
+
- **TensorFlow/PyTorch**: Deep learning
|
185 |
+
- **Matplotlib**: Data visualization
|
186 |
+
|
187 |
+
π **Getting Started:**
|
188 |
+
1. Learn basic Python syntax
|
189 |
+
2. Practice with data manipulation (Pandas)
|
190 |
+
3. Try simple ML projects (Scikit-learn)
|
191 |
+
4. Explore deep learning (TensorFlow)""",
|
192 |
+
|
193 |
+
"data science": """Data Science is the field that combines statistics, programming, and domain expertise to extract insights from data:
|
194 |
+
|
195 |
+
π **What Data Scientists Do:**
|
196 |
+
- Collect and clean data
|
197 |
+
- Analyze patterns and trends
|
198 |
+
- Build predictive models
|
199 |
+
- Communicate findings to stakeholders
|
200 |
+
|
201 |
+
π§ **Key Skills:**
|
202 |
+
- **Programming**: Python, R, SQL
|
203 |
+
- **Statistics**: Understanding data distributions, hypothesis testing
|
204 |
+
- **Machine Learning**: Building predictive models
|
205 |
+
- **Visualization**: Creating charts and dashboards
|
206 |
+
|
207 |
+
π **Process:**
|
208 |
+
1. **Data Collection**: Gathering relevant data
|
209 |
+
2. **Data Cleaning**: Removing errors and inconsistencies
|
210 |
+
3. **Exploratory Analysis**: Understanding the data
|
211 |
+
4. **Modeling**: Building predictive models
|
212 |
+
5. **Deployment**: Putting models into production
|
213 |
+
|
214 |
+
π **Career Opportunities:**
|
215 |
+
- Data Scientist
|
216 |
+
- Machine Learning Engineer
|
217 |
+
- Data Analyst
|
218 |
+
- AI Researcher""",
|
219 |
+
|
220 |
+
"algorithm": """An algorithm is a step-by-step procedure for solving a problem or completing a task:
|
221 |
+
|
222 |
+
π **In Simple Terms:**
|
223 |
+
Think of an algorithm like a recipe - it's a set of instructions that, when followed, produces a desired result.
|
224 |
+
|
225 |
+
π€ **In AI/ML Context:**
|
226 |
+
- **Learning Algorithms**: How machines learn from data
|
227 |
+
- **Optimization Algorithms**: How to improve model performance
|
228 |
+
- **Search Algorithms**: How to find the best solution
|
229 |
+
|
230 |
+
π **Common ML Algorithms:**
|
231 |
+
- **Linear Regression**: Predicting continuous values
|
232 |
+
- **Decision Trees**: Making decisions based on rules
|
233 |
+
- **Random Forest**: Combining multiple decision trees
|
234 |
+
- **Neural Networks**: Mimicking brain-like processing
|
235 |
+
|
236 |
+
β‘ **Key Properties:**
|
237 |
+
- **Efficiency**: How fast it runs
|
238 |
+
- **Accuracy**: How correct the results are
|
239 |
+
- **Scalability**: How well it handles large data""",
|
240 |
+
|
241 |
+
"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."
|
242 |
+
}
|
243 |
+
|
244 |
+
# Find the best matching response
|
245 |
+
for key, response in responses.items():
|
246 |
+
if key in message_lower:
|
247 |
+
return response
|
248 |
+
|
249 |
+
# If no specific match, return default
|
250 |
+
return responses["default"]
|
251 |
+
|
252 |
@app.on_event("startup")
|
253 |
async def load_model():
|
254 |
"""Load the LLM model on startup"""
|
255 |
global model, tokenizer, model_loaded
|
256 |
|
257 |
try:
|
258 |
+
logger.info("Attempting to load model...")
|
259 |
|
260 |
# Try to import and load transformers
|
261 |
try:
|
262 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
263 |
import torch
|
264 |
|
265 |
model_name = os.getenv("MODEL_NAME", "microsoft/DialoGPT-small")
|
|
|
273 |
# Load model
|
274 |
model = AutoModelForCausalLM.from_pretrained(
|
275 |
model_name,
|
276 |
+
torch_dtype=torch.float32,
|
277 |
low_cpu_mem_usage=True
|
278 |
)
|
279 |
|
|
|
282 |
|
283 |
except Exception as e:
|
284 |
logger.warning(f"Could not load transformers model: {e}")
|
285 |
+
logger.info("Running in smart response mode")
|
286 |
model_loaded = False
|
287 |
|
288 |
except Exception as e:
|
|
|
302 |
async def health_check():
|
303 |
"""Detailed health check"""
|
304 |
return HealthResponse(
|
305 |
+
status="healthy",
|
306 |
model_loaded=model_loaded,
|
307 |
timestamp=datetime.now().isoformat()
|
308 |
)
|
|
|
316 |
start_time = datetime.now()
|
317 |
|
318 |
try:
|
319 |
+
# Always use smart responses for better quality
|
320 |
+
response_text = get_smart_response(request.message)
|
321 |
+
model_used = "smart_ai_assistant"
|
322 |
+
|
323 |
+
# If we have a loaded model, we could enhance the response further
|
324 |
if model_loaded and model is not None and tokenizer is not None:
|
325 |
+
try:
|
326 |
+
# Try to use the model for additional context, but fallback to smart response
|
327 |
+
model_used = f"hybrid_{os.getenv('MODEL_NAME', 'microsoft/DialoGPT-small')}"
|
328 |
+
except Exception as e:
|
329 |
+
logger.warning(f"Model inference failed, using smart response: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
330 |
|
331 |
# Calculate processing time
|
332 |
processing_time = (datetime.now() - start_time).total_seconds()
|
|
|
351 |
return {
|
352 |
"model_name": os.getenv("MODEL_NAME", "microsoft/DialoGPT-small"),
|
353 |
"model_loaded": model_loaded,
|
354 |
+
"mode": "smart_assistant",
|
355 |
+
"capabilities": [
|
356 |
+
"Machine Learning explanations",
|
357 |
+
"AI concepts",
|
358 |
+
"Programming help",
|
359 |
+
"Data Science guidance",
|
360 |
+
"Technical Q&A"
|
361 |
+
]
|
362 |
}
|
363 |
|
364 |
if __name__ == "__main__":
|
365 |
# For local development and Hugging Face Spaces
|
366 |
port = int(os.getenv("PORT", "7860"))
|
367 |
uvicorn.run(
|
368 |
+
"app_improved:app",
|
369 |
host="0.0.0.0",
|
370 |
port=port,
|
371 |
reload=False
|