Athspi commited on
Commit
1fb8db4
·
verified ·
1 Parent(s): 9c89d97

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +173 -237
app.py CHANGED
@@ -6,7 +6,7 @@ import json
6
  import re
7
  import google.generativeai as genai
8
  from openai import OpenAI
9
- from typing import List, Dict, Tuple
10
 
11
  class CognitiveArchitecture:
12
  def __init__(self):
@@ -18,300 +18,236 @@ class CognitiveArchitecture:
18
  }
19
  self.validate_keys()
20
 
21
- # Initialize all AI models
22
  genai.configure(api_key=self.api_keys["GEMINI"])
23
- self.gemini_model = genai.GenerativeModel(
24
- "gemini-2.0-pro-exp-02-05",
25
- generation_config={"temperature": 0.5, "max_output_tokens": 8192}
26
- )
27
 
28
  self.gpt4o_client = OpenAI(
29
  base_url="https://models.inference.ai.azure.com",
30
  api_key=self.api_keys["AZURE"]
31
  )
32
 
33
- self.models = {
34
- "DeepSeek": "deepseek/deepseek-chat:free",
35
- "Qwen": "qwen/qwen-vl-plus:free",
36
- "Llama": "meta-llama/llama-3.3-70b-instruct:free",
37
- "Mistral": "mistral-large-latest",
38
- "GPT4o": "gpt-4o"
39
  }
40
 
41
  self.headers = {
42
- "OpenRouter": {
43
- "Authorization": f"Bearer {self.api_keys['OPENROUTER']}",
44
- "Content-Type": "application/json"
45
- },
46
- "Mistral": {
47
- "Authorization": f"Bearer {self.api_keys['MISTRAL']}",
48
- "Content-Type": "application/json",
49
- "Accept": "application/json"
50
- }
51
  }
52
-
53
- self.memory = []
54
- self.thinking_steps = []
55
 
56
  def validate_keys(self):
57
- for key, value in self.api_keys.items():
58
- if not value:
59
- raise ValueError(f"Missing API key: {key}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
- def call_model(self, model_role: str, prompt: str, context: List[Dict] = None) -> str:
62
- """Universal model router with error handling"""
63
  try:
64
- if model_role == "Gemini":
65
  response = self.gemini_model.generate_content(prompt)
66
- return response.text
67
 
68
- if model_role == "Mistral":
69
- return self._call_mistral(prompt, context)
70
 
71
- if model_role == "GPT4o":
72
- return self._call_gpt4o(prompt, context)
73
 
74
- # Handle OpenRouter models
75
- payload = {
76
- "model": self.models[model_role],
77
- "messages": context if context else [{"role": "user", "content": prompt}],
78
- "temperature": 0.7,
79
- "max_tokens": 3096,
80
- "top_p": 0.9
81
- }
82
 
83
- response = requests.post(
84
- "https://openrouter.ai/api/v1/chat/completions",
85
- headers=self.headers["OpenRouter"],
86
- json=payload,
87
- timeout=30
88
- )
89
-
90
- if response.status_code == 200:
91
- return response.json()['choices'][0]['message']['content']
92
- return f"API Error {response.status_code}: {response.text}"
93
-
94
  except Exception as e:
95
- print(f"Model Error ({model_role}): {str(e)}")
96
- return ""
97
 
98
- def _call_mistral(self, prompt: str, context: List[Dict] = None) -> str:
99
- """Direct Mistral API call"""
100
  try:
101
- payload = {
102
- "model": self.models["Mistral"],
103
- "messages": context if context else [{"role": "user", "content": prompt}],
104
- "temperature": 0.7,
105
- "max_tokens": 4096,
106
- "top_p": 0.9
107
- }
108
-
109
  response = requests.post(
110
  "https://api.mistral.ai/v1/chat/completions",
111
- headers=self.headers["Mistral"],
112
- json=payload,
 
 
 
 
 
113
  timeout=30
114
  )
115
-
116
- return response.json()['choices'][0]['message']['content'] if response.status_code == 200 else ""
117
-
118
  except Exception as e:
119
- print(f"Mistral API Error: {str(e)}")
120
- return ""
121
 
122
- def _call_gpt4o(self, prompt: str, context: List[Dict] = None) -> str:
123
- """Azure Inference API for GPT-4o"""
124
  try:
125
- messages = context if context else [
126
- {"role": "system", "content": "You are an expert analyst."},
127
- {"role": "user", "content": prompt}
128
- ]
129
-
130
  response = self.gpt4o_client.chat.completions.create(
131
- model=self.models["GPT4o"],
132
- messages=messages,
133
- temperature=1.0,
134
- top_p=1.0,
135
- max_tokens=1000
136
  )
137
-
138
- return response.choices[0].message.content
139
-
140
  except Exception as e:
141
- print(f"GPT-4o Error: {str(e)}")
142
- return ""
143
 
144
- def hierarchical_reasoning(self, query: str) -> Tuple[str, dict]:
145
- """Seven-stage AGI reasoning pipeline"""
146
  try:
147
- # Stage 1: Conceptual Decomposition (Mistral)
148
- decomposition = self._call_mistral(
149
- f"""Decompose query into components: "{query}"
150
- Output format:
151
- - Primary Intent
152
- - Implicit Assumptions
153
- - Required Knowledge
154
- - Potential Biases"""
155
- )
156
-
157
- # Stage 2: Deep Analysis (GPT-4o)
158
- analysis = self._call_gpt4o(
159
- f"""Analyze this query using first principles:
160
- {query}
161
- Decomposition: {decomposition}"""
162
- )
163
-
164
- # Stage 3: Contextual Grounding (Qwen)
165
- context = self.call_model(
166
- "Qwen",
167
- f"""Generate multimodal context for: {query}"""
168
- )
169
-
170
- # Stage 4: Critical Evaluation (Mistral)
171
- critique = self._call_mistral(
172
- f"""Critique this analysis for logical soundness:
173
- Query: {query}
174
- Analysis: {analysis}
175
- Context: {context}"""
176
- )
177
-
178
- # Stage 5: Ethical Consideration (Llama)
179
- ethics = self.call_model(
180
- "Llama",
181
- f"""Analyze ethical dimensions of responding to:
182
- {query}
183
- Provide concrete recommendations"""
184
- )
185
-
186
- # Stage 6: Response Synthesis (Gemini)
187
- synthesis = self.call_model(
188
- "Gemini",
189
- f"""Synthesize final response considering:
190
- {json.dumps({
191
- "query": query,
192
- "analysis": analysis,
193
- "critique": critique,
194
- "ethics": ethics
195
- }, indent=2)}"""
196
- )
197
-
198
- # Stage 7: Validation & Refinement (GPT-4o)
199
- validation = self._call_gpt4o(
200
- f"""Validate and refine this response:
201
- Original Query: {query}
202
- Proposed Response: {synthesis}
203
- Provide final optimized version"""
204
  )
205
-
206
- # Store cognitive process
207
- self.thinking_steps = [
208
- ("Decomposition", decomposition),
209
- ("Analysis", analysis),
210
- ("Context", context),
211
- ("Critique", critique),
212
- ("Ethics", ethics),
213
- ("Synthesis", synthesis),
214
- ("Validation", validation)
215
- ]
216
-
217
- return validation, {
218
- "components": self.extract_structured_data(decomposition),
219
- "analysis": self.extract_structured_data(analysis),
220
- "validation": self.extract_structured_data(validation)
221
- }
222
-
223
  except Exception as e:
224
- print(f"Reasoning Error: {str(e)}")
225
- return "Cognitive processing failed", {}
226
 
227
- def extract_structured_data(self, text: str) -> dict:
228
- """Advanced text parsing with fallback"""
229
  try:
230
- # Attempt JSON extraction
231
- json_match = re.search(r'\{.*\}', text, re.DOTALL)
232
- if json_match:
233
- return json.loads(json_match.group(0))
234
-
235
- # Fallback pattern matching
236
- structured_data = {}
237
- patterns = {
238
- "intent": r"Intent:\s*(.*?)\n",
239
- "components": r"Components:\s*(.*?)\n",
240
- "analysis": r"Analysis:\s*(.*?)\n",
241
- "recommendations": r"Recommendations:\s*(.*)"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
242
  }
243
 
244
- for key, pattern in patterns.items():
245
- match = re.search(pattern, text)
246
- if match:
247
- structured_data[key] = match.group(1).strip()
248
-
249
- return structured_data if structured_data else {"content": text}
250
-
251
  except Exception as e:
252
- print(f"Parsing Error: {str(e)}")
253
- return {"error": "Failed to parse response"}
254
 
255
- def visualize_thought_process(self) -> str:
256
- """Interactive process visualization"""
257
- vis = ["## Cognitive Process Breakdown\n"]
258
- for step, content in self.thinking_steps:
259
- vis.append(f"""
260
- <div class="process-step">
261
- <h3>{step}</h3>
262
- <pre>{content}</pre>
263
- </div>
264
- """)
265
- return "\n".join(vis)
266
-
267
- def create_agi_interface():
268
  try:
269
- agi = CognitiveArchitecture()
270
  except ValueError as e:
271
  return gr.Blocks().launch(error_message=str(e))
272
 
273
- with gr.Blocks(title="AGI Thinking Framework", theme=gr.themes.Soft(), css="""
274
- .process-step { margin: 20px 0; padding: 15px; border: 1px solid #e0e0e0; border-radius: 8px; }
275
- .process-step h3 { color: #2b6cb0; margin: 0 0 10px 0; font-size: 1.1em; }
276
- pre {
277
- white-space: pre-wrap;
278
- background: #f8f9fa;
279
- padding: 15px;
280
- border-radius: 6px;
281
- border: 1px solid #eee;
282
- font-family: monospace;
283
- }
284
  """) as demo:
285
- gr.Markdown("# 🧠 Advanced AGI Reasoning System")
 
286
 
287
  with gr.Row():
288
- input_box = gr.Textbox(label="Input Query",
289
- placeholder="Enter your complex request...",
290
- lines=3)
291
- process_btn = gr.Button("Start Cognitive Processing", variant="primary")
292
 
293
- output = gr.Markdown()
294
- process_visual = gr.HTML()
295
 
296
  def process_query(query):
297
  start_time = time.time()
298
- final, _ = agi.hierarchical_reasoning(query)
299
- process_time = time.time() - start_time
300
 
301
- return (
302
- f"## Optimized Response\n{final}\n\n"
303
- f"**Processing Time**: {process_time:.2f}s\n"
304
- f"**Cognitive Steps Executed**: {len(agi.thinking_steps)}",
305
- agi.visualize_thought_process()
306
- )
 
 
 
 
 
 
 
 
 
 
307
 
308
- process_btn.click(
309
- fn=process_query,
310
  inputs=input_box,
311
- outputs=[output, process_visual]
312
  )
313
-
314
  return demo
315
 
316
  if __name__ == "__main__":
317
- create_agi_interface().launch(server_port=7860)
 
6
  import re
7
  import google.generativeai as genai
8
  from openai import OpenAI
9
+ from typing import List, Dict, Optional
10
 
11
  class CognitiveArchitecture:
12
  def __init__(self):
 
18
  }
19
  self.validate_keys()
20
 
 
21
  genai.configure(api_key=self.api_keys["GEMINI"])
22
+ self.gemini_model = genai.GenerativeModel("gemini-2.0-pro-exp-02-05")
 
 
 
23
 
24
  self.gpt4o_client = OpenAI(
25
  base_url="https://models.inference.ai.azure.com",
26
  api_key=self.api_keys["AZURE"]
27
  )
28
 
29
+ self.model_config = {
30
+ "decomposition": ["gemini-2.0-pro-exp-02-05", "mistral-large-latest"],
31
+ "analysis": ["gpt-4o"],
32
+ "critique": ["gpt-4o", "meta-llama/llama-3.3-70b-instruct:free"]
 
 
33
  }
34
 
35
  self.headers = {
36
+ "mistral": {"Authorization": f"Bearer {self.api_keys['MISTRAL']}"},
37
+ "openrouter": {"Authorization": f"Bearer {self.api_keys['OPENROUTER']}"}
 
 
 
 
 
 
 
38
  }
 
 
 
39
 
40
  def validate_keys(self):
41
+ for service, key in self.api_keys.items():
42
+ if not key:
43
+ raise ValueError(f"Missing API key: {service}")
44
+
45
+ def safe_json_parse(self, text: str) -> dict:
46
+ """Robust JSON parsing with multiple fallbacks"""
47
+ try:
48
+ return json.loads(text)
49
+ except json.JSONDecodeError:
50
+ try:
51
+ # Try to extract JSON from text
52
+ json_str = re.search(r'\{.*\}', text, re.DOTALL)
53
+ if json_str:
54
+ return json.loads(json_str.group())
55
+ # Fallback to key-value parsing
56
+ return self.parse_kv_text(text)
57
+ except Exception as e:
58
+ return {"error": f"JSON parsing failed: {str(e)}", "raw_response": text}
59
+
60
+ def parse_kv_text(self, text: str) -> dict:
61
+ """Fallback key-value parser"""
62
+ parsed = {}
63
+ current_key = None
64
+ lines = text.split('\n')
65
+
66
+ for line in lines:
67
+ if ':' in line:
68
+ key, value = line.split(':', 1)
69
+ parsed[key.strip()] = value.strip()
70
+ current_key = key.strip()
71
+ elif current_key:
72
+ parsed[current_key] += " " + line.strip()
73
+ return parsed
74
 
75
+ def call_model(self, model: str, prompt: str) -> dict:
76
+ """Robust model caller with error handling"""
77
  try:
78
+ if "gemini" in model:
79
  response = self.gemini_model.generate_content(prompt)
80
+ return {"success": True, "content": response.text}
81
 
82
+ if "mistral" in model:
83
+ return self.call_mistral(model, prompt)
84
 
85
+ if "gpt-4o" in model:
86
+ return self.call_azure_gpt4o(prompt)
87
 
88
+ return self.call_openrouter(model, prompt)
 
 
 
 
 
 
 
89
 
 
 
 
 
 
 
 
 
 
 
 
90
  except Exception as e:
91
+ return {"success": False, "error": str(e)}
 
92
 
93
+ def call_mistral(self, model: str, prompt: str) -> dict:
94
+ """Mistral API caller"""
95
  try:
 
 
 
 
 
 
 
 
96
  response = requests.post(
97
  "https://api.mistral.ai/v1/chat/completions",
98
+ headers=self.headers["mistral"],
99
+ json={
100
+ "model": model,
101
+ "messages": [{"role": "user", "content": prompt}],
102
+ "temperature": 0.7,
103
+ "max_tokens": 3000
104
+ },
105
  timeout=30
106
  )
107
+ if response.status_code == 200:
108
+ return {"success": True, "content": response.json()['choices'][0]['message']['content']}
109
+ return {"success": False, "error": f"API Error {response.status_code}"}
110
  except Exception as e:
111
+ return {"success": False, "error": str(e)}
 
112
 
113
+ def call_azure_gpt4o(self, prompt: str) -> dict:
114
+ """Azure GPT-4o caller"""
115
  try:
 
 
 
 
 
116
  response = self.gpt4o_client.chat.completions.create(
117
+ model="gpt-4o",
118
+ messages=[{"role": "user", "content": prompt}],
119
+ temperature=0.7,
120
+ max_tokens=2000
 
121
  )
122
+ return {"success": True, "content": response.choices[0].message.content}
 
 
123
  except Exception as e:
124
+ return {"success": False, "error": str(e)}
 
125
 
126
+ def call_openrouter(self, model: str, prompt: str) -> dict:
127
+ """OpenRouter caller"""
128
  try:
129
+ response = requests.post(
130
+ "https://openrouter.ai/api/v1/chat/completions",
131
+ headers=self.headers["openrouter"],
132
+ json={
133
+ "model": model,
134
+ "messages": [{"role": "user", "content": prompt}],
135
+ "temperature": 0.7,
136
+ "max_tokens": 3000
137
+ },
138
+ timeout=30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
  )
140
+ if response.status_code == 200:
141
+ return {"success": True, "content": response.json()['choices'][0]['message']['content']}
142
+ return {"success": False, "error": f"API Error {response.status_code}"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143
  except Exception as e:
144
+ return {"success": False, "error": str(e)}
 
145
 
146
+ def analyze_query(self, query: str) -> dict:
147
+ """Full analysis workflow with error resilience"""
148
  try:
149
+ # Step 1: Multi-model decomposition
150
+ decompositions = {}
151
+ for model in self.model_config["decomposition"]:
152
+ result = self.call_model(model, f"Decompose this query: {query}")
153
+ if result["success"]:
154
+ decompositions[model] = self.safe_json_parse(result["content"])
155
+
156
+ # Step 2: GPT-4 analysis
157
+ analysis_prompt = f"""Analyze this query based on decompositions:
158
+ {json.dumps(decompositions, indent=2)}
159
+
160
+ Query: {query}
161
+ Provide detailed analysis in JSON format."""
162
+
163
+ analysis = self.call_model("gpt-4o", analysis_prompt)
164
+ if not analysis["success"]:
165
+ return {"error": "Analysis failed", "details": analysis["error"]}
166
+
167
+ parsed_analysis = self.safe_json_parse(analysis["content"])
168
+
169
+ # Step 3: Cross-model critique
170
+ critiques = {}
171
+ for model in self.model_config["critique"]:
172
+ critique_prompt = f"""Critique this analysis:
173
+ {json.dumps(parsed_analysis, indent=2)}
174
+
175
+ Provide structured feedback in JSON format."""
176
+
177
+ result = self.call_model(model, critique_prompt)
178
+ if result["success"]:
179
+ critiques[model] = self.safe_json_parse(result["content"])
180
+
181
+ # Step 4: Final synthesis
182
+ synthesis_prompt = f"""Synthesize final response considering:
183
+ Analysis: {json.dumps(parsed_analysis, indent=2)}
184
+ Critiques: {json.dumps(critiques, indent=2)}
185
+
186
+ Provide comprehensive response with confidence score."""
187
+
188
+ synthesis = self.call_model("gemini-2.0-pro-exp-02-05", synthesis_prompt)
189
+
190
+ return {
191
+ "decompositions": decompositions,
192
+ "analysis": parsed_analysis,
193
+ "critiques": critiques,
194
+ "synthesis": synthesis["content"] if synthesis["success"] else "Synthesis failed",
195
+ "success": True
196
  }
197
 
 
 
 
 
 
 
 
198
  except Exception as e:
199
+ return {"success": False, "error": str(e)}
 
200
 
201
+ def create_interface():
 
 
 
 
 
 
 
 
 
 
 
 
202
  try:
203
+ analyzer = CognitiveArchitecture()
204
  except ValueError as e:
205
  return gr.Blocks().launch(error_message=str(e))
206
 
207
+ with gr.Blocks(title="AI Analysis Suite", theme=gr.themes.Soft(), css="""
208
+ .analysis-section { margin: 15px; padding: 15px; border: 1px solid #eee; border-radius: 8px; }
209
+ pre { white-space: pre-wrap; background: #f8f9fa; padding: 10px; }
210
+ .error { color: #dc3545; background: #ffeef0; padding: 10px; border-radius: 5px; }
 
 
 
 
 
 
 
211
  """) as demo:
212
+
213
+ gr.Markdown("# 🧠 Advanced AI Analysis System")
214
 
215
  with gr.Row():
216
+ input_box = gr.Textbox(label="Input Query", placeholder="Enter your query...", lines=3)
217
+ submit_btn = gr.Button("Analyze", variant="primary")
 
 
218
 
219
+ output_area = gr.Markdown()
220
+ debug_info = gr.JSON(label="Analysis Details")
221
 
222
  def process_query(query):
223
  start_time = time.time()
224
+ result = analyzer.analyze_query(query)
225
+ duration = time.time() - start_time
226
 
227
+ if not result.get("success"):
228
+ return f"## Analysis Error\n{result.get('error', 'Unknown error')}", {}
229
+
230
+ formatted_output = f"""
231
+ ## Analysis Result
232
+ {result.get('synthesis', 'No synthesis available')}
233
+
234
+ **Processing Time**: {duration:.2f}s
235
+ **Models Used**: {len(result['decompositions'])} decomposition, {len(result['critiques'])} critique
236
+ """
237
+
238
+ return formatted_output, {
239
+ "decompositions": result["decompositions"],
240
+ "analysis": result["analysis"],
241
+ "critiques": result["critiques"]
242
+ }
243
 
244
+ submit_btn.click(
245
+ process_query,
246
  inputs=input_box,
247
+ outputs=[output_area, debug_info]
248
  )
249
+
250
  return demo
251
 
252
  if __name__ == "__main__":
253
+ create_interface().launch(server_port=7860)