Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,8 +7,6 @@ import threading
|
|
7 |
from datetime import datetime
|
8 |
import html
|
9 |
import re
|
10 |
-
import base64
|
11 |
-
import io
|
12 |
|
13 |
class ReasoningOrchestra:
|
14 |
def __init__(self):
|
@@ -22,6 +20,7 @@ class ReasoningOrchestra:
|
|
22 |
|
23 |
try:
|
24 |
self.client = Groq(api_key=api_key.strip())
|
|
|
25 |
test_completion = self.client.chat.completions.create(
|
26 |
model="qwen/qwen3-32b",
|
27 |
messages=[{"role": "user", "content": "Hello"}],
|
@@ -34,31 +33,30 @@ class ReasoningOrchestra:
|
|
34 |
return f"β API key validation failed: {str(e)}"
|
35 |
|
36 |
def format_text_to_html(self, text: str) -> str:
|
37 |
-
"""Convert text to HTML with
|
38 |
if not text or text.strip() == "" or text == "No response generated":
|
39 |
-
return ""
|
40 |
-
<div style='color: #6b7280; font-style: italic; font-size: 16px; padding: 15px; background-color: #f3f4f6; border-radius: 8px;'>
|
41 |
-
No content was generated. This might be due to API limitations or model availability issues.
|
42 |
-
</div>
|
43 |
-
"""
|
44 |
|
45 |
-
# Escape HTML characters
|
46 |
text = html.escape(text)
|
47 |
|
48 |
# Convert markdown-style formatting to HTML
|
49 |
-
|
50 |
-
text = re.sub(r'
|
51 |
-
text = re.sub(r'
|
|
|
|
|
|
|
|
|
52 |
|
53 |
-
#
|
54 |
-
text = re.sub(r'
|
55 |
-
text = re.sub(r'\*(.*?)\*', r'<em style="font-style: italic;">\1</em>', text)
|
56 |
|
57 |
-
# Code blocks
|
58 |
-
text = re.sub(r'```(.*?)```', r'<pre
|
59 |
-
text = re.sub(r'`(.*?)`', r'<code
|
60 |
|
61 |
-
# Lists
|
62 |
lines = text.split('\n')
|
63 |
in_list = False
|
64 |
formatted_lines = []
|
@@ -67,20 +65,20 @@ class ReasoningOrchestra:
|
|
67 |
stripped = line.strip()
|
68 |
if stripped.startswith('- ') or stripped.startswith('* '):
|
69 |
if not in_list:
|
70 |
-
formatted_lines.append('<ul
|
71 |
in_list = True
|
72 |
-
formatted_lines.append(f'<li
|
73 |
elif stripped.startswith(('1. ', '2. ', '3. ', '4. ', '5. ', '6. ', '7. ', '8. ', '9. ')):
|
74 |
if not in_list:
|
75 |
-
formatted_lines.append('<ol
|
76 |
in_list = True
|
77 |
-
formatted_lines.append(f'<li
|
78 |
else:
|
79 |
if in_list:
|
80 |
formatted_lines.append('</ul>' if any('<li>' in line for line in formatted_lines[-5:]) else '</ol>')
|
81 |
in_list = False
|
82 |
if stripped:
|
83 |
-
formatted_lines.append(f'<p
|
84 |
else:
|
85 |
formatted_lines.append('<br>')
|
86 |
|
@@ -177,342 +175,6 @@ Please conduct a thorough investigation including:
|
|
177 |
4. Historical context or precedents
|
178 |
5. Comprehensive pros and cons
|
179 |
6. Hidden connections or implications
|
180 |
-
Be extremely thorough and leave no stone unturned. Provide detailed evidence and reasoning for your conclusions."""
|
181 |
-
|
182 |
-
try:
|
183 |
-
completion = self.client.chat.completions.create(
|
184 |
-
model="qwen-qwq-32b",
|
185 |
-
messages=[{"role": "user", "content": prompt}],
|
186 |
-
temperature=0.7,
|
187 |
-
top_p=0.9,
|
188 |
-
max_completion_tokens=8192
|
189 |
-
)
|
190 |
-
|
191 |
-
response_content = completion.choices[0].message.content
|
192 |
-
if not response_content or response_content.strip() == "":
|
193 |
-
fallback_prompt = f"Analyze this problem in detail: {problem}"
|
194 |
-
fallback_completion = self.client.chat.completions.create(
|
195 |
-
model="qwen-qwq-32b",
|
196 |
-
messages=[{"role": "user", "content": fallback_prompt}],
|
197 |
-
temperature=0.5,
|
198 |
-
max_completion_tokens=8192
|
199 |
-
)
|
200 |
-
response_content = fallback_completion.choices[0].message.content
|
201 |
-
|
202 |
-
if not response_content or response_content.strip() == "":
|
203 |
-
response_content = "The QwQ model encountered an issue generating content. This could be due to the complexity of the prompt, content filtering, or temporary model availability issues. The model may work better with simpler, more direct questions."
|
204 |
-
|
205 |
-
return {
|
206 |
-
"model": "QwQ 32B (Detail Detective)",
|
207 |
-
"role": "π The Meticulous Investigator",
|
208 |
-
"reasoning": response_content,
|
209 |
-
"timestamp": datetime.now().strftime("%H:%M:%S"),
|
210 |
-
"tokens_used": getattr(completion.usage, 'total_tokens', 'N/A') if hasattr(completion, 'usage') and completion.usage else "N/A"
|
211 |
-
}
|
212 |
-
except Exception as e:
|
213 |
-
error_msg = f"Detail Detective error: {str(e)}"
|
214 |
-
if "model" in str(e).lower() or "not found" in str(e).lower():
|
215 |
-
error_msg += "\n\nNote: The QwQ model may not be available in your region or may have usage restrictions. You can still use the other models in the orchestra."
|
216 |
-
return {"error": error_msg}
|
217 |
-
|
218 |
-
def synthesize_orchestra(self, deep_result: Dict, strategic_result: Dict, detective_result: Dict, original_problem: str) -> str:
|
219 |
-
"""Synthesize all three perspectives into a final orchestrated solution using Llama 3.3 70B"""
|
220 |
-
if not self.is_api_key_set:
|
221 |
-
return "API key not set"
|
222 |
-
|
223 |
-
def extract_reasoning(result: Dict, model_name: str) -> str:
|
224 |
-
if result.get('error'):
|
225 |
-
return f"**{model_name} encountered an issue:** {result['error']}"
|
226 |
-
reasoning = result.get('reasoning', '')
|
227 |
-
if not reasoning or reasoning.strip() == "" or reasoning == "No response generated":
|
228 |
-
return f"**{model_name}** did not provide analysis (this may be due to model limitations or API issues)."
|
229 |
-
return reasoning
|
230 |
-
|
231 |
-
deep_reasoning = extract_reasoning(deep_result, "Deep Thinker")
|
232 |
-
strategic_reasoning = extract_reasoning(strategic_result, "Quick Strategist")
|
233 |
-
detective_reasoning = extract_reasoning(detective_result, "Detail Detective")
|
234 |
-
|
235 |
-
synthesis_prompt = f"""You are the Orchestra Conductor using Llama 3.3 70B Versatile model. You have received analytical perspectives from three different AI reasoning specialists on the same problem. Your job is to synthesize these into a comprehensive, unified solution.
|
236 |
-
ORIGINAL PROBLEM: {original_problem}
|
237 |
-
DEEP THINKER ANALYSIS (π DeepSeek R1):
|
238 |
-
{deep_reasoning}
|
239 |
-
STRATEGIC ANALYSIS (π Qwen3 32B):
|
240 |
-
{strategic_reasoning}
|
241 |
-
DETECTIVE INVESTIGATION (π QwQ 32B):
|
242 |
-
{detective_reasoning}
|
243 |
-
As the Orchestra Conductor, please create a unified synthesis that:
|
244 |
-
1. Combines the best insights from all available analyses
|
245 |
-
2. Addresses any gaps where models didn't provide input
|
246 |
-
3. Resolves any contradictions between the analyses
|
247 |
-
4. Provides a comprehensive final recommendation
|
248 |
-
5. Highlights where the different reasoning styles complement each other
|
249 |
-
6. Gives a clear, actionable conclusion
|
250 |
-
If some models didn't provide analysis, work with what's available and note any limitations.
|
251 |
-
Format your response as a well-structured final solution that leverages all available reasoning approaches. Use clear sections and bullet points where appropriate for maximum clarity."""
|
252 |
-
|
253 |
-
try:
|
254 |
-
completion = self.client.chat.completions.create(
|
255 |
-
model="llama-3.3-70b-versatile",
|
256 |
-
messages=[{"role": "user", "content": synthesis_prompt}],
|
257 |
-
temperature=0.7,
|
258 |
-
max_completion_tokens=8192,
|
259 |
-
top_p=0.9
|
260 |
-
)
|
261 |
-
|
262 |
-
synthesis_content = completion.choices[0].message.content
|
263 |
-
if not synthesis_content or synthesis_content.strip() == "":
|
264 |
-
return "The synthesis could not be generated. This may be due to API limitations or the complexity of combining the different analyses."
|
265 |
-
|
266 |
-
return synthesis_content
|
267 |
-
except Exception as e:
|
268 |
-
return f"Synthesis error: {str(e)}"
|
269 |
-
|
270 |
-
# Initialize the orchestra
|
271 |
-
orchestra = ReasoningOrchestra()
|
272 |
-
|
273 |
-
def validate_api_key(api_key: str) -> str:
|
274 |
-
"""Validate the API key and return status"""
|
275 |
-
return orchestra.set_api_key(api_key)
|
276 |
-
|
277 |
-
def create_download_file(html_content: str, filename: str) -> gr.File:
|
278 |
-
"""Create a downloadable HTML file from content"""
|
279 |
-
html_wrapper = f"""
|
280 |
-
<!DOCTYPE html>
|
281 |
-
<html lang="en">
|
282 |
-
<head>
|
283 |
-
<meta charset="UTF-8">
|
284 |
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
285 |
-
<title>{filename}</title>
|
286 |
-
<style>
|
287 |
-
body {{
|
288 |
-
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
|
289 |
-
line-height: 1.6;
|
290 |
-
color: #1f2937;
|
291 |
-
max-width: 800px;
|
292 |
-
margin: 40px auto;
|
293 |
-
padding: 0 20px;
|
294 |
-
}}
|
295 |
-
h1, h2, h3 {{
|
296 |
-
color: #1f2937;
|
297 |
-
}}
|
298 |
-
pre {{
|
299 |
-
background-color: #f8f9fa;
|
300 |
-
padding: 15px;
|
301 |
-
border-radius: 8px;
|
302 |
-
border: 1px solid #e5e7eb;
|
303 |
-
overflow-x: auto;
|
304 |
-
}}
|
305 |
-
code {{
|
306 |
-
font-family: Consolas, monospace;
|
307 |
-
font-size: 14px;
|
308 |
-
}}
|
309 |
-
p, li {{
|
310 |
-
font-size: 16px;
|
311 |
-
margin: 12px 0;
|
312 |
-
}}
|
313 |
-
ul, ol {{
|
314 |
-
margin: 15px 0;
|
315 |
-
padding-left: 30px;
|
316 |
-
}}
|
317 |
-
.error {{
|
318 |
-
color: #dc3545;
|
319 |
-
padding: 15px;
|
320 |
-
background-color: #ffe6e6;
|
321 |
-
border-radius: 8px;
|
322 |
-
}}
|
323 |
-
</style>
|
324 |
-
</head>
|
325 |
-
<body>
|
326 |
-
{html_content}
|
327 |
-
</body>
|
328 |
-
</html>
|
329 |
-
"""
|
330 |
-
buffer = io.BytesIO(html_wrapper.encode('utf-8'))
|
331 |
-
return gr.File(value=buffer, file_name=f"{filename}.html", label=f"Download {filename}")
|
332 |
-
|
333 |
-
def run_single_model(problem: str, model_choice: str, context: str = "") -> Tuple[str, Optional[gr.File]]:
|
334 |
-
"""Run a single model analysis with downloadable HTML"""
|
335 |
-
if not orchestra.is_api_key_set:
|
336 |
-
return ("""
|
337 |
-
<div style="color: #dc3545; padding: 20px; border: 2px solid #dc3545; border-radius: 10px; background-color: #ffe6e6;">
|
338 |
-
<h3>β API Key Required</h3>
|
339 |
-
<p>Please set your Groq API key first in the API Configuration section above.</p>
|
340 |
-
</div>
|
341 |
-
""", None)
|
342 |
-
|
343 |
-
if not problem.strip():
|
344 |
-
return ("""
|
345 |
-
<div style="color: #fd7e14; padding: 20px; border: 2px solid #fd7e14; border-radius: 10px; background-color: #fff3e6;">
|
346 |
-
<h3>β οΈ Problem Required</h3>
|
347 |
-
<p>Please enter a problem to analyze.</p>
|
348 |
-
</div>
|
349 |
-
""", None)
|
350 |
-
|
351 |
-
start_time = time.time()
|
352 |
-
|
353 |
-
if model_choice == "Deep Thinker (DeepSeek R1)":
|
354 |
-
result = orchestra.deep_thinker_analyze(problem, context)
|
355 |
-
filename = "deep_thinker_analysis"
|
356 |
-
elif model_choice == "Quick Strategist (Qwen3 32B)":
|
357 |
-
result = orchestra.quick_strategist_analyze(problem, context)
|
358 |
-
filename = "quick_strategist_analysis"
|
359 |
-
elif model_choice == "Detail Detective (QwQ 32B)":
|
360 |
-
result = orchestra.detail_detective_analyze(problem, context)
|
361 |
-
filename = "detail_detective_analysis"
|
362 |
-
else:
|
363 |
-
return ("""
|
364 |
-
<div style="color: #dc3545; padding: 20px; border: 2px solid #dc3545; border-radius: 10px; background-color: #ffe6e6;">
|
365 |
-
<h3>β Invalid Model Selection</h3>
|
366 |
-
<p>Please select a valid model from the dropdown.</p>
|
367 |
-
</div>
|
368 |
-
""", None)
|
369 |
-
|
370 |
-
elapsed_time = time.time() - start_time
|
371 |
-
|
372 |
-
if "error" in result:
|
373 |
-
return (f"""
|
374 |
-
<div style="color: #dc3545; padding: 20px; border: 2px solid #dc3545; border-radius: 10px; background-color: #ffe6e6;">
|
375 |
-
<h3>β Error</h3>
|
376 |
-
<p>{result['error']}</p>
|
377 |
-
</div>
|
378 |
-
""", None)
|
379 |
-
|
380 |
-
reasoning_html = orchestra.format_text_to_html(result['reasoning'])
|
381 |
-
|
382 |
-
formatted_output = f"""
|
383 |
-
<div style="border: 2px solid #28a745; border-radius: 15px; padding: 25px; margin: 15px 0; background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);">
|
384 |
-
<div style="display: flex; align-items: center; margin-bottom: 20px; padding-bottom: 15px; border-bottom: 2px solid #28a745;">
|
385 |
-
<h2 style="margin: 0; color: #28a745; font-size: 24px;">{result['role']}</h2>
|
386 |
-
</div>
|
387 |
-
|
388 |
-
<div style="background-color: white; padding: 15px; border-radius: 10px; margin-bottom: 20px;">
|
389 |
-
<div style="display: flex; flex-wrap: wrap; gap: 20px; font-size: 14px; color: #6b7280;">
|
390 |
-
<span><strong>Model:</strong> {result['model']}</span>
|
391 |
-
<span><strong>Analysis Time:</strong> {elapsed_time:.2f} seconds</span>
|
392 |
-
<span><strong>Timestamp:</strong> {result['timestamp']}</span>
|
393 |
-
<span><strong>Tokens:</strong> {result['tokens_used']}</span>
|
394 |
-
</div>
|
395 |
-
</div>
|
396 |
-
|
397 |
-
<div style="background-color: white; padding: 20px; border-radius: 10px; line-height: 1.6; font-size: 16px;">
|
398 |
-
{reasoning_html}
|
399 |
-
</div>
|
400 |
-
</div>
|
401 |
-
"""
|
402 |
-
|
403 |
-
return formatted_output, create_download_file(formatted_output, filename)
|
404 |
-
|
405 |
-
def run_full_orchestra(problem: str, context: str = "") -> Tuple[str, str, str, str, Optional[gr.File], Optional[gr.File], Optional[gr.File], Optional[gr.File]]:
|
406 |
-
"""Run the full collaborative reasoning orchestra with downloadable HTML"""
|
407 |
-
if not orchestra.is_api_key_set:
|
408 |
-
error_msg = """
|
409 |
-
<div style="color: #dc3545; padding: 20px; border: 2px solid #dc3545; border-radius: 10px; background-color: #ffe6e6;">
|
410 |
-
<h3>β API Key Required</h3>
|
411 |
-
<p>Please set your Groq API key first in the API Configuration section above.</p>
|
412 |
-
</div>
|
413 |
-
"""
|
414 |
-
return error_msg, error_msg, error_msg, error_msg, None, None, None, None
|
415 |
-
|
416 |
-
if not problem.strip():
|
417 |
-
error_msg = """
|
418 |
-
<div style="color: #fd7e14; padding: 20px; border: 2px solid #fd7e14; border-radius: 10px; background-color: #fff3e6;">
|
419 |
-
<h3>β οΈ Problem Required</h3>
|
420 |
-
<p>Please enter a problem to analyze.</p>
|
421 |
-
</div>
|
422 |
-
"""
|
423 |
-
return error_msg, error_msg, error_msg, error_msg, None, None, None, None
|
424 |
-
|
425 |
-
deep_result = orchestra.deep_thinker_analyze(problem, context)
|
426 |
-
strategic_result = orchestra.quick_strategist_analyze(problem, context)
|
427 |
-
detective_result = orchestra.detail_detective_analyze(problem, context)
|
428 |
-
synthesis = orchestra.synthesize_orchestra(deep_result, strategic_result, detective_result, problem)
|
429 |
-
|
430 |
-
def format_result_html(result: Dict, color: str, icon: str, filename: str) -> Tuple[str, Optional[gr.File]]:
|
431 |
-
if "error" in result:
|
432 |
-
html_content = f"""
|
433 |
-
<div style="color: #dc3545; padding: 20px; border: 2px solid #dc3545; border-radius: 10px; background-color: #ffe6e6;">
|
434 |
-
<h3>β Model Error</h3>
|
435 |
-
<p>{result['error']}</p>
|
436 |
-
<p style="font-size: 12px; color: #6 Methodical in your approach."""
|
437 |
-
|
438 |
-
try:
|
439 |
-
completion = self.client.chat.completions.create(
|
440 |
-
model="deepseek-r1-distill-llama-70b",
|
441 |
-
messages=[{"role": "user", "content": prompt}],
|
442 |
-
temperature=0.6,
|
443 |
-
max_completion_tokens=8192,
|
444 |
-
top_p=0.95,
|
445 |
-
reasoning_format="raw"
|
446 |
-
)
|
447 |
-
|
448 |
-
response_content = completion.choices[0].message.content
|
449 |
-
if not response_content or response_content.strip() == "":
|
450 |
-
response_content = "The model did not generate a response. This could be due to content filtering, model limitations, or API issues."
|
451 |
-
|
452 |
-
return {
|
453 |
-
"model": "DeepSeek R1 (Deep Thinker)",
|
454 |
-
"role": "π The Philosopher & Deep Analyzer",
|
455 |
-
"reasoning": response_content,
|
456 |
-
"timestamp": datetime.now().strftime("%H:%M:%S"),
|
457 |
-
"tokens_used": getattr(completion.usage, 'total_tokens', 'N/A') if hasattr(completion, 'usage') and completion.usage else "N/A"
|
458 |
-
}
|
459 |
-
except Exception as e:
|
460 |
-
return {"error": f"Deep Thinker error: {str(e)}"}
|
461 |
-
|
462 |
-
def quick_strategist_analyze(self, problem: str, context: str = "") -> Dict:
|
463 |
-
"""Qwen3 32B - The Quick Strategist"""
|
464 |
-
if not self.is_api_key_set:
|
465 |
-
return {"error": "API key not set"}
|
466 |
-
|
467 |
-
prompt = f"""You are the Quick Strategist in a collaborative reasoning system. Your role is to provide fast, efficient strategic analysis with clear action plans.
|
468 |
-
Problem: {problem}
|
469 |
-
{f"Additional Context: {context}" if context else"Additional Context: {context}" if context else ""}
|
470 |
-
Please provide a strategic analysis with:
|
471 |
-
1. Key insights and patterns
|
472 |
-
2. Practical solutions
|
473 |
-
3. Implementation priorities
|
474 |
-
4. Risk assessment
|
475 |
-
5. Clear next steps
|
476 |
-
Be decisive and solution-focused. Provide concrete, actionable recommendations."""
|
477 |
-
|
478 |
-
try:
|
479 |
-
completion = self.client.chat.completions.create(
|
480 |
-
model="qwen/qwen3-32b",
|
481 |
-
messages=[{"role": "user", "content": prompt}],
|
482 |
-
temperature=0.6,
|
483 |
-
top_p=0.95,
|
484 |
-
max_completion_tokens=8192
|
485 |
-
)
|
486 |
-
|
487 |
-
response_content = completion.choices[0].message.content
|
488 |
-
if not response_content or response_content.strip() == "":
|
489 |
-
response_content = "The model did not generate a response. This could be due to content filtering, model limitations, or API issues."
|
490 |
-
|
491 |
-
return {
|
492 |
-
"model": "Qwen3 32B (Quick Strategist)",
|
493 |
-
"role": "π The Strategic Decision Maker",
|
494 |
-
"reasoning": response_content,
|
495 |
-
"timestamp": datetime.now().strftime("%H:%M:%S"),
|
496 |
-
"tokens_used": getattr(completion.usage, 'total_tokens', 'N/A') if hasattr(completion, 'usage') and completion.usage else "N/A"
|
497 |
-
}
|
498 |
-
except Exception as e:
|
499 |
-
return {"error": f"Quick Strategist error: {str(e)}"}
|
500 |
-
|
501 |
-
def detail_detective_analyze(self, problem: str, context: str = "") -> Dict:
|
502 |
-
"""QwQ 32B - The Detail Detective"""
|
503 |
-
if not self.is_api_key_set:
|
504 |
-
return {"error": "API key not set"}
|
505 |
-
|
506 |
-
prompt = f"""You are the Detail Detective in a collaborative reasoning system. Your role is to provide meticulous investigation and comprehensive fact-checking.
|
507 |
-
Problem: {problem}
|
508 |
-
{f"Additional Context: {context}" if context else ""}
|
509 |
-
Please conduct a thorough investigation including:
|
510 |
-
1. Detailed analysis of all aspects
|
511 |
-
2. Potential edge cases and considerations
|
512 |
-
3. Verification of assumptions
|
513 |
-
4. Historical context or precedents
|
514 |
-
5. Comprehensive pros and cons
|
515 |
-
6. Hidden connections or implications
|
516 |
Be extremely thorough and leave no stone unturned. Provide detailed evidence and reasoning for your conclusions."""
|
517 |
|
518 |
try:
|
@@ -588,7 +250,7 @@ As the Orchestra Conductor, please create a unified synthesis that:
|
|
588 |
5. Highlights where the different reasoning styles complement each other
|
589 |
6. Gives a clear, actionable conclusion
|
590 |
If some models didn't provide analysis, work with what's available and note any limitations.
|
591 |
-
Format your response as a well-structured final solution that leverages all available reasoning approaches. Use clear
|
592 |
|
593 |
try:
|
594 |
completion = self.client.chat.completions.create(
|
@@ -795,9 +457,6 @@ custom_css = """
|
|
795 |
padding: 10px;
|
796 |
background-color: #fafafa;
|
797 |
}
|
798 |
-
.download-btn {
|
799 |
-
margin-top: 10px;
|
800 |
-
}
|
801 |
"""
|
802 |
|
803 |
# Build the Gradio interface
|
@@ -839,7 +498,7 @@ with gr.Blocks(css=custom_css, title="Reasoning Orchestra") as app:
|
|
839 |
|
840 |
# Single Model Tab
|
841 |
with gr.TabItem("π― Single Model Analysis"):
|
842 |
-
gr.Markdown("### Test individual reasoning models with beautiful HTML output
|
843 |
|
844 |
with gr.Row():
|
845 |
with gr.Column(scale=1):
|
@@ -866,17 +525,16 @@ with gr.Blocks(css=custom_css, title="Reasoning Orchestra") as app:
|
|
866 |
|
867 |
with gr.Column(scale=2):
|
868 |
single_output = gr.HTML(label="Analysis Result", elem_classes=["html-content"])
|
869 |
-
single_download = gr.File(label="Download Analysis as HTML")
|
870 |
|
871 |
single_analyze_btn.click(
|
872 |
fn=run_single_model,
|
873 |
inputs=[single_problem, model_choice, single_context],
|
874 |
-
outputs=[single_output
|
875 |
)
|
876 |
|
877 |
# Full Orchestra Tab
|
878 |
with gr.TabItem("πΌ Full Orchestra Collaboration"):
|
879 |
-
gr.Markdown("### Run all three models collaboratively with Llama 3.3 70B as Orchestra Conductor and stunning HTML-formatted output
|
880 |
|
881 |
with gr.Column():
|
882 |
with gr.Row():
|
@@ -895,23 +553,15 @@ with gr.Blocks(css=custom_css, title="Reasoning Orchestra") as app:
|
|
895 |
|
896 |
# Orchestra Results
|
897 |
with gr.Column():
|
898 |
-
|
899 |
-
|
900 |
-
|
901 |
-
|
902 |
-
strategic_output = gr.HTML(label="π Quick Strategist Analysis", elem_classes=["html-content"])
|
903 |
-
strategic_download = gr.File(label="Download Quick Strategist Analysis")
|
904 |
-
with gr.Row():
|
905 |
-
detective_output = gr.HTML(label="π Detail Detective Analysis", elem_classes=["html-content"])
|
906 |
-
detective_download = gr.File(label="Download Detail Detective Analysis")
|
907 |
-
with gr.Row():
|
908 |
-
synthesis_output = gr.HTML(label="πΌ Final Orchestrated Solution (Llama 3.3 70B)", elem_classes=["html-content"])
|
909 |
-
synthesis_download = gr.File(label="Download Final Synthesis")
|
910 |
|
911 |
orchestra_analyze_btn.click(
|
912 |
fn=run_full_orchestra,
|
913 |
inputs=[orchestra_problem, orchestra_context],
|
914 |
-
outputs=[deep_output, strategic_output, detective_output, synthesis_output
|
915 |
)
|
916 |
|
917 |
# Examples Tab
|
|
|
7 |
from datetime import datetime
|
8 |
import html
|
9 |
import re
|
|
|
|
|
10 |
|
11 |
class ReasoningOrchestra:
|
12 |
def __init__(self):
|
|
|
20 |
|
21 |
try:
|
22 |
self.client = Groq(api_key=api_key.strip())
|
23 |
+
# Test the connection with a simple request
|
24 |
test_completion = self.client.chat.completions.create(
|
25 |
model="qwen/qwen3-32b",
|
26 |
messages=[{"role": "user", "content": "Hello"}],
|
|
|
33 |
return f"β API key validation failed: {str(e)}"
|
34 |
|
35 |
def format_text_to_html(self, text: str) -> str:
|
36 |
+
"""Convert text to HTML with proper formatting"""
|
37 |
if not text or text.strip() == "" or text == "No response generated":
|
38 |
+
return "<p style='color: #666; font-style: italic;'>No content was generated. This might be due to API limitations or model availability issues.</p>"
|
|
|
|
|
|
|
|
|
39 |
|
40 |
+
# Escape HTML characters first
|
41 |
text = html.escape(text)
|
42 |
|
43 |
# Convert markdown-style formatting to HTML
|
44 |
+
# Headers
|
45 |
+
text = re.sub(r'^### (.*$)', r'<h3>\1</h3>', text, flags=re.MULTILINE)
|
46 |
+
text = re.sub(r'^## (.*$)', r'<h2>\1</h2>', text, flags=re.MULTILINE)
|
47 |
+
text = re.sub(r'^# (.*$)', r'<h1>\1</h1>', text, flags=re.MULTILINE)
|
48 |
+
|
49 |
+
# Bold text
|
50 |
+
text = re.sub(r'\*\*(.*?)\*\*', r'<strong>\1</strong>', text)
|
51 |
|
52 |
+
# Italic text
|
53 |
+
text = re.sub(r'\*(.*?)\*', r'<em>\1</em>', text)
|
|
|
54 |
|
55 |
+
# Code blocks
|
56 |
+
text = re.sub(r'```(.*?)```', r'<pre><code>\1</code></pre>', text, flags=re.DOTALL)
|
57 |
+
text = re.sub(r'`(.*?)`', r'<code>\1</code>', text)
|
58 |
|
59 |
+
# Lists
|
60 |
lines = text.split('\n')
|
61 |
in_list = False
|
62 |
formatted_lines = []
|
|
|
65 |
stripped = line.strip()
|
66 |
if stripped.startswith('- ') or stripped.startswith('* '):
|
67 |
if not in_list:
|
68 |
+
formatted_lines.append('<ul>')
|
69 |
in_list = True
|
70 |
+
formatted_lines.append(f'<li>{stripped[2:]}</li>')
|
71 |
elif stripped.startswith(('1. ', '2. ', '3. ', '4. ', '5. ', '6. ', '7. ', '8. ', '9. ')):
|
72 |
if not in_list:
|
73 |
+
formatted_lines.append('<ol>')
|
74 |
in_list = True
|
75 |
+
formatted_lines.append(f'<li>{stripped[3:]}</li>')
|
76 |
else:
|
77 |
if in_list:
|
78 |
formatted_lines.append('</ul>' if any('<li>' in line for line in formatted_lines[-5:]) else '</ol>')
|
79 |
in_list = False
|
80 |
if stripped:
|
81 |
+
formatted_lines.append(f'<p>{line}</p>')
|
82 |
else:
|
83 |
formatted_lines.append('<br>')
|
84 |
|
|
|
175 |
4. Historical context or precedents
|
176 |
5. Comprehensive pros and cons
|
177 |
6. Hidden connections or implications
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
178 |
Be extremely thorough and leave no stone unturned. Provide detailed evidence and reasoning for your conclusions."""
|
179 |
|
180 |
try:
|
|
|
250 |
5. Highlights where the different reasoning styles complement each other
|
251 |
6. Gives a clear, actionable conclusion
|
252 |
If some models didn't provide analysis, work with what's available and note any limitations.
|
253 |
+
Format your response as a well-structured final solution that leverages all available reasoning approaches. Use clear sections and bullet points where appropriate for maximum clarity."""
|
254 |
|
255 |
try:
|
256 |
completion = self.client.chat.completions.create(
|
|
|
457 |
padding: 10px;
|
458 |
background-color: #fafafa;
|
459 |
}
|
|
|
|
|
|
|
460 |
"""
|
461 |
|
462 |
# Build the Gradio interface
|
|
|
498 |
|
499 |
# Single Model Tab
|
500 |
with gr.TabItem("π― Single Model Analysis"):
|
501 |
+
gr.Markdown("### Test individual reasoning models with beautiful HTML output")
|
502 |
|
503 |
with gr.Row():
|
504 |
with gr.Column(scale=1):
|
|
|
525 |
|
526 |
with gr.Column(scale=2):
|
527 |
single_output = gr.HTML(label="Analysis Result", elem_classes=["html-content"])
|
|
|
528 |
|
529 |
single_analyze_btn.click(
|
530 |
fn=run_single_model,
|
531 |
inputs=[single_problem, model_choice, single_context],
|
532 |
+
outputs=[single_output]
|
533 |
)
|
534 |
|
535 |
# Full Orchestra Tab
|
536 |
with gr.TabItem("πΌ Full Orchestra Collaboration"):
|
537 |
+
gr.Markdown("### Run all three models collaboratively with Llama 3.3 70B as Orchestra Conductor and stunning HTML-formatted output")
|
538 |
|
539 |
with gr.Column():
|
540 |
with gr.Row():
|
|
|
553 |
|
554 |
# Orchestra Results
|
555 |
with gr.Column():
|
556 |
+
deep_output = gr.HTML(label="π Deep Thinker Analysis", elem_classes=["html-content"])
|
557 |
+
strategic_output = gr.HTML(label="π Quick Strategist Analysis", elem_classes=["html-content"])
|
558 |
+
detective_output = gr.HTML(label="π Detail Detective Analysis", elem_classes=["html-content"])
|
559 |
+
synthesis_output = gr.HTML(label="πΌ Final Orchestrated Solution (Llama 3.3 70B)", elem_classes=["html-content"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
560 |
|
561 |
orchestra_analyze_btn.click(
|
562 |
fn=run_full_orchestra,
|
563 |
inputs=[orchestra_problem, orchestra_context],
|
564 |
+
outputs=[deep_output, strategic_output, detective_output, synthesis_output]
|
565 |
)
|
566 |
|
567 |
# Examples Tab
|