Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,388 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from huggingface_hub import InferenceClient
|
3 |
-
|
4 |
-
"""
|
5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
6 |
-
"""
|
7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
-
|
9 |
-
|
10 |
-
def respond(
|
11 |
-
message,
|
12 |
-
history: list[tuple[str, str]],
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
messages,
|
32 |
-
max_tokens=max_tokens,
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
|
39 |
-
|
40 |
-
|
|
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
if __name__ == "__main__":
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
|
2 |
+
import os
|
3 |
+
import io
|
4 |
+
import json
|
5 |
+
import logging
|
6 |
+
import base64
|
7 |
+
import traceback
|
8 |
+
from typing import Dict, List, Any, Optional, Tuple
|
9 |
|
10 |
+
import torch
|
11 |
+
import numpy as np
|
12 |
+
import gradio as gr
|
13 |
+
import folium
|
14 |
+
import requests
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
+
from geoclip import GeoCLIP, LocationEncoder
|
17 |
+
from transformers import CLIPTokenizer
|
18 |
+
from dataclasses import dataclass, asdict
|
19 |
|
20 |
+
class MetacognitiveAssistant:
|
21 |
+
"""
|
22 |
+
Advanced multimodal AI assistant integrating GeoCLIP with metacognitive analysis framework.
|
23 |
+
"""
|
24 |
+
|
25 |
+
def __init__(self, device=None):
|
26 |
+
"""
|
27 |
+
Initialize the metacognitive assistant with GeoCLIP and advanced reasoning capabilities.
|
28 |
+
|
29 |
+
Args:
|
30 |
+
device (str, optional): Compute device for model. Defaults to CUDA if available.
|
31 |
+
"""
|
32 |
+
# Device and model configuration
|
33 |
+
self.device = device or ("cuda" if torch.cuda.is_available() else "cpu")
|
34 |
+
|
35 |
+
# GeoCLIP components
|
36 |
+
self.geoclip_model = GeoCLIP().to(self.device)
|
37 |
+
self.tokenizer = CLIPTokenizer.from_pretrained("openai/clip-vit-large-patch14")
|
38 |
+
self.location_encoder = LocationEncoder().to(self.device)
|
39 |
+
|
40 |
+
# Caching and logging
|
41 |
+
self._cache = {}
|
42 |
+
self.logger = self._configure_logger()
|
43 |
+
|
44 |
+
# Analytical frameworks
|
45 |
+
self.analytical_frameworks = {
|
46 |
+
"multi_perspective": self._multi_perspective_analysis,
|
47 |
+
"semantic_excavation": self._semantic_excavation,
|
48 |
+
"cross_domain_bridging": self._cross_domain_bridging
|
49 |
+
}
|
50 |
+
|
51 |
+
print(f"MetacognitiveAssistant initialized on {self.device}")
|
52 |
+
|
53 |
+
def _configure_logger(self):
|
54 |
+
"""
|
55 |
+
Configure a robust logging system with multiple output streams.
|
56 |
+
|
57 |
+
Returns:
|
58 |
+
logging.Logger: Configured logger instance
|
59 |
+
"""
|
60 |
+
logger = logging.getLogger("MetacognitiveAssistant")
|
61 |
+
logger.setLevel(logging.DEBUG)
|
62 |
+
|
63 |
+
# Console handler
|
64 |
+
console_handler = logging.StreamHandler()
|
65 |
+
console_handler.setLevel(logging.INFO)
|
66 |
+
console_formatter = logging.Formatter(
|
67 |
+
'%(asctime)s - %(name)s - %(levelname)s: %(message)s',
|
68 |
+
datefmt='%Y-%m-%d %H:%M:%S'
|
69 |
+
)
|
70 |
+
console_handler.setFormatter(console_formatter)
|
71 |
+
logger.addHandler(console_handler)
|
72 |
+
|
73 |
+
return logger
|
74 |
+
|
75 |
+
def _multi_perspective_analysis(self, input_data: Dict[str, Any]) -> Dict[str, Any]:
|
76 |
+
"""
|
77 |
+
Apply multi-perspective analysis to input data.
|
78 |
+
|
79 |
+
Args:
|
80 |
+
input_data (Dict): Input data to analyze
|
81 |
+
|
82 |
+
Returns:
|
83 |
+
Dict with multi-perspective insights
|
84 |
+
"""
|
85 |
+
perspectives = {
|
86 |
+
"quantitative": self._quantitative_perspective,
|
87 |
+
"semantic": self._semantic_perspective,
|
88 |
+
"systemic": self._systemic_perspective
|
89 |
+
}
|
90 |
+
|
91 |
+
multi_perspective_results = {}
|
92 |
+
for name, perspective_func in perspectives.items():
|
93 |
+
try:
|
94 |
+
multi_perspective_results[name] = perspective_func(input_data)
|
95 |
+
except Exception as e:
|
96 |
+
self.logger.warning(f"Error in {name} perspective: {e}")
|
97 |
+
|
98 |
+
return multi_perspective_results
|
99 |
+
|
100 |
+
def _quantitative_perspective(self, input_data: Dict[str, Any]) -> Dict[str, Any]:
|
101 |
+
"""Quantitative analysis perspective."""
|
102 |
+
# Implement quantitative analysis logic
|
103 |
+
return {
|
104 |
+
"metrics": {},
|
105 |
+
"statistical_summary": {}
|
106 |
+
}
|
107 |
+
|
108 |
+
def _semantic_perspective(self, input_data: Dict[str, Any]) -> Dict[str, Any]:
|
109 |
+
"""Semantic meaning extraction perspective."""
|
110 |
+
# Implement semantic analysis logic
|
111 |
+
return {
|
112 |
+
"implied_narratives": [],
|
113 |
+
"conceptual_themes": []
|
114 |
+
}
|
115 |
+
|
116 |
+
def _systemic_perspective(self, input_data: Dict[str, Any]) -> Dict[str, Any]:
|
117 |
+
"""Systemic relationship and interaction perspective."""
|
118 |
+
# Implement systemic analysis logic
|
119 |
+
return {
|
120 |
+
"system_interactions": {},
|
121 |
+
"emergent_properties": []
|
122 |
+
}
|
123 |
+
|
124 |
+
def _semantic_excavation(self, input_data: Dict[str, Any]) -> Dict[str, Any]:
|
125 |
+
"""
|
126 |
+
Deep semantic excavation to extract profound meanings and implications.
|
127 |
+
|
128 |
+
Args:
|
129 |
+
input_data (Dict): Input data to excavate
|
130 |
+
|
131 |
+
Returns:
|
132 |
+
Dict with semantic insights
|
133 |
+
"""
|
134 |
+
# Implement deep semantic analysis
|
135 |
+
return {
|
136 |
+
"causal_narratives": [],
|
137 |
+
"hidden_implications": [],
|
138 |
+
"generative_principles": []
|
139 |
+
}
|
140 |
+
|
141 |
+
def _cross_domain_bridging(self, input_data: Dict[str, Any]) -> Dict[str, Any]:
|
142 |
+
"""
|
143 |
+
Identify cross-domain pattern isomorphisms.
|
144 |
+
|
145 |
+
Args:
|
146 |
+
input_data (Dict): Input data to analyze
|
147 |
+
|
148 |
+
Returns:
|
149 |
+
Dict with cross-domain insights
|
150 |
+
"""
|
151 |
+
# Implement cross-domain pattern recognition
|
152 |
+
return {
|
153 |
+
"analogous_patterns": [],
|
154 |
+
"domain_bridges": [],
|
155 |
+
"transferable_insights": []
|
156 |
+
}
|
157 |
+
|
158 |
+
def process_query(self, message: Dict[str, Any], history: List[Dict[str, Any]]) -> str:
|
159 |
+
"""
|
160 |
+
Primary query processing method with advanced metacognitive reasoning.
|
161 |
+
|
162 |
+
Args:
|
163 |
+
message (Dict): Input message with potential multimodal data
|
164 |
+
history (List): Conversation history
|
165 |
+
|
166 |
+
Returns:
|
167 |
+
str: Processed response with metacognitive analysis
|
168 |
+
"""
|
169 |
+
try:
|
170 |
+
# Preprocessing and input validation
|
171 |
+
self.logger.info("Processing new query")
|
172 |
+
|
173 |
+
# Route to appropriate processing based on input type
|
174 |
+
if message.get("files") and len(message["files"]) > 0:
|
175 |
+
# Multimodal image processing
|
176 |
+
response = self._process_image_input(message["files"][0])
|
177 |
+
elif message.get("text"):
|
178 |
+
# Text-based processing
|
179 |
+
response = self._process_text_input(message["text"])
|
180 |
+
else:
|
181 |
+
return "Invalid input. Please provide an image or text description."
|
182 |
+
|
183 |
+
# Apply metacognitive analysis frameworks
|
184 |
+
analysis_results = {}
|
185 |
+
for framework_name, framework_func in self.analytical_frameworks.items():
|
186 |
+
try:
|
187 |
+
analysis_results[framework_name] = framework_func({
|
188 |
+
"input": message,
|
189 |
+
"response": response
|
190 |
+
})
|
191 |
+
except Exception as e:
|
192 |
+
self.logger.warning(f"Error in {framework_name} analysis: {e}")
|
193 |
+
|
194 |
+
# Enhance response with metacognitive insights
|
195 |
+
enhanced_response = self._generate_metacognitive_response(
|
196 |
+
response,
|
197 |
+
analysis_results
|
198 |
+
)
|
199 |
+
|
200 |
+
return enhanced_response
|
201 |
+
|
202 |
+
except Exception as e:
|
203 |
+
error_details = traceback.format_exc()
|
204 |
+
self.logger.error(f"Query processing error: {e}")
|
205 |
+
return f"🚨 Error processing query:\n```\n{error_details}\n```"
|
206 |
+
|
207 |
+
def _process_image_input(self, image_path: str) -> str:
|
208 |
+
"""
|
209 |
+
Process image input using GeoCLIP location predictions.
|
210 |
+
|
211 |
+
Args:
|
212 |
+
image_path (str): Path to input image
|
213 |
+
|
214 |
+
Returns:
|
215 |
+
str: Processed image analysis response
|
216 |
+
"""
|
217 |
+
predictions = self.predict_from_image(image_path)
|
218 |
+
|
219 |
+
response = "### Image Location Analysis\n\n"
|
220 |
+
for i, pred in enumerate(predictions[:3]):
|
221 |
+
lat, lon = pred.coordinates
|
222 |
+
conf = pred.confidence * 100
|
223 |
+
response += f"**#{i+1}:** Coordinates: ({lat:.6f}, {lon:.6f}) - Confidence: {conf:.2f}%\n\n"
|
224 |
+
|
225 |
+
# Generate static map
|
226 |
+
map_html = self.generate_static_map(predictions)
|
227 |
+
response += f"<iframe srcdoc='{map_html}' width='100%' height='400px' frameborder='0'></iframe>"
|
228 |
+
|
229 |
+
return response
|
230 |
+
|
231 |
+
def _process_text_input(self, text_query: str) -> str:
|
232 |
+
"""
|
233 |
+
Process text input with advanced reasoning.
|
234 |
+
|
235 |
+
Args:
|
236 |
+
text_query (str): Input text query
|
237 |
+
|
238 |
+
Returns:
|
239 |
+
str: Processed text analysis response
|
240 |
+
"""
|
241 |
+
# Existing text-based location prediction
|
242 |
+
predictions = self.predict_from_text(text_query)
|
243 |
+
|
244 |
+
response = f"### Location Predictions for: '{text_query}'\n\n"
|
245 |
+
for i, pred in enumerate(predictions[:3]):
|
246 |
+
lat, lon = pred.coordinates
|
247 |
+
conf = pred.confidence * 100
|
248 |
+
response += f"**#{i+1}:** Coordinates: ({lat:.6f}, {lon:.6f}) - Confidence: {conf:.2f}%\n\n"
|
249 |
+
|
250 |
+
# Generate static map
|
251 |
+
map_html = self.generate_static_map(predictions)
|
252 |
+
response += f"<iframe srcdoc='{map_html}' width='100%' height='400px' frameborder='0'></iframe>"
|
253 |
+
|
254 |
+
return response
|
255 |
+
|
256 |
+
def _generate_metacognitive_response(
|
257 |
+
self,
|
258 |
+
base_response: str,
|
259 |
+
analysis_results: Dict[str, Any]
|
260 |
+
) -> str:
|
261 |
+
"""
|
262 |
+
Enhance response with metacognitive analysis insights.
|
263 |
+
|
264 |
+
Args:
|
265 |
+
base_response (str): Original response
|
266 |
+
analysis_results (Dict): Metacognitive analysis results
|
267 |
+
|
268 |
+
Returns:
|
269 |
+
str: Enhanced response with metacognitive insights
|
270 |
+
"""
|
271 |
+
metacognitive_insights = "### 🧠 Metacognitive Analysis\n\n"
|
272 |
+
|
273 |
+
for framework, insights in analysis_results.items():
|
274 |
+
metacognitive_insights += f"#### {framework.replace('_', ' ').title()} Framework\n"
|
275 |
+
|
276 |
+
# Summarize insights with fallback to prevent errors
|
277 |
+
try:
|
278 |
+
for key, value in insights.items():
|
279 |
+
if value: # Only include non-empty insights
|
280 |
+
metacognitive_insights += f"- **{key.replace('_', ' ').title()}**: {value}\n"
|
281 |
+
except Exception as e:
|
282 |
+
self.logger.warning(f"Error generating {framework} insights: {e}")
|
283 |
+
|
284 |
+
# Combine base response with metacognitive insights
|
285 |
+
full_response = base_response + "\n\n" + metacognitive_insights
|
286 |
+
|
287 |
+
return full_response
|
288 |
+
|
289 |
+
# Existing GeoCLIP methods from previous implementation
|
290 |
+
def predict_from_image(self, image_path) -> List[Dict]:
|
291 |
+
"""Existing image prediction method"""
|
292 |
+
top_pred_gps, top_pred_prob = self.geoclip_model.predict(image_path, top_k=5)
|
293 |
+
return [
|
294 |
+
{
|
295 |
+
"coordinates": tuple(top_pred_gps[i].cpu().numpy()),
|
296 |
+
"confidence": float(top_pred_prob[i])
|
297 |
+
}
|
298 |
+
for i in range(len(top_pred_prob))
|
299 |
+
]
|
300 |
+
|
301 |
+
def predict_from_text(self, text: str, top_k: int = 5) -> List[Dict]:
|
302 |
+
"""Existing text-based prediction method"""
|
303 |
+
# (Implement similar to previous implementation)
|
304 |
+
cache_key = f"text_{text}_{top_k}"
|
305 |
+
if cache_key in self._cache:
|
306 |
+
return self._cache[cache_key]
|
307 |
+
|
308 |
+
with torch.no_grad():
|
309 |
+
# Similar implementation to previous GeoCLIP text prediction
|
310 |
+
inputs = self.tokenizer(text, return_tensors="pt").to(self.device)
|
311 |
+
# ... rest of the prediction logic ...
|
312 |
+
|
313 |
+
return [] # Placeholder
|
314 |
+
|
315 |
+
def generate_static_map(self, predictions: List[Dict]) -> str:
|
316 |
+
"""Generate static map from predictions"""
|
317 |
+
if not predictions:
|
318 |
+
return ""
|
319 |
+
|
320 |
+
center_coords = predictions[0]["coordinates"]
|
321 |
+
m = folium.Map(location=center_coords, zoom_start=5)
|
322 |
+
|
323 |
+
for i, pred in enumerate(predictions[:5]):
|
324 |
+
color = 'red' if i == 0 else 'blue' if i == 1 else 'green'
|
325 |
+
folium.Marker(
|
326 |
+
location=pred["coordinates"],
|
327 |
+
popup=f"#{i+1}: {pred['confidence']:.4f}",
|
328 |
+
icon=folium.Icon(color=color)
|
329 |
+
).add_to(m)
|
330 |
+
|
331 |
+
return m.get_root().render()
|
332 |
|
333 |
+
# Gradio Interface
|
334 |
+
def create_metacognitive_interface():
|
335 |
+
"""
|
336 |
+
Create advanced Gradio interface for Metacognitive AI Assistant
|
337 |
+
"""
|
338 |
+
assistant = MetacognitiveAssistant()
|
339 |
+
|
340 |
+
with gr.Blocks(theme=gr.themes.Default()) as demo:
|
341 |
+
gr.Markdown("# 🧠 Metacognitive AI Location Intelligence")
|
342 |
+
gr.Markdown("""
|
343 |
+
An advanced AI assistant that combines geospatial intelligence
|
344 |
+
with deep metacognitive reasoning and analysis.
|
345 |
+
|
346 |
+
- Upload an image or describe a location
|
347 |
+
- Receive location predictions and deep analytical insights
|
348 |
+
""")
|
349 |
+
|
350 |
+
chatbot = gr.Chatbot(
|
351 |
+
bubble_full_width=False,
|
352 |
+
height=600,
|
353 |
+
type="messages",
|
354 |
+
avatar_images=("👤", "🌍"),
|
355 |
+
layout="panel"
|
356 |
+
)
|
357 |
+
|
358 |
+
chat_interface = gr.ChatInterface(
|
359 |
+
fn=assistant.process_query,
|
360 |
+
chatbot=chatbot,
|
361 |
+
multimodal=True,
|
362 |
+
textbox=gr.MultimodalTextbox(
|
363 |
+
placeholder="Describe a location, upload an image...",
|
364 |
+
sources=["upload"],
|
365 |
+
file_types=["image"],
|
366 |
+
show_label=False
|
367 |
+
),
|
368 |
+
autofocus=True,
|
369 |
+
submit_btn="Analyze",
|
370 |
+
examples=[
|
371 |
+
"Describe a tropical beach landscape",
|
372 |
+
"Urban cityscape with modern architecture"
|
373 |
+
]
|
374 |
+
)
|
375 |
+
|
376 |
+
return demo
|
377 |
|
378 |
+
def main():
|
379 |
+
"""Launch the Metacognitive AI Assistant"""
|
380 |
+
demo = create_metacognitive_interface()
|
381 |
+
demo.launch(
|
382 |
+
server_name="0.0.0.0",
|
383 |
+
server_port=7860,
|
384 |
+
share=False
|
385 |
+
)
|
386 |
|
387 |
if __name__ == "__main__":
|
388 |
+
main()
|