flyfir248 commited on
Commit
fd8f5e3
·
1 Parent(s): 8befb33

Commit : Updated header and agent html and routes.py |

Browse files
Files changed (3) hide show
  1. App/routes.py +71 -0
  2. Templates/agent.html +82 -0
  3. Templates/header.html +11 -0
App/routes.py CHANGED
@@ -594,6 +594,77 @@ def rag_search():
594
  history=session.get('chat_history', []))
595
  # ====== EXISTING DISCOVERY ROUTE (PRESERVED) ======
596
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
597
  @main_bp.route('/discovery')
598
  def expert_discovery():
599
  """
 
594
  history=session.get('chat_history', []))
595
  # ====== EXISTING DISCOVERY ROUTE (PRESERVED) ======
596
 
597
+
598
+ # Helper to fetch OpenAlex data
599
+ def get_experts_by_topic(topic_query):
600
+ """
601
+ Finds the 'Mixture of Experts' by searching authors whose works
602
+ match the topic and ranking them by impact.
603
+ """
604
+ # We query authors directly.
605
+ # The 'filter' parameter allows us to search for authors who have
606
+ # published in specific concepts/topics related to your query.
607
+ api_url = "https://api.openalex.org/authors"
608
+
609
+ params = {
610
+ # filter=display_name.search matches authors who publish in this topic
611
+ "filter": f"display_name.search:{topic_query}",
612
+ "sort": "cited_by_count:desc",
613
+ "per-page": 10,
614
+ "mailto": "your-email@example.com" # Use your real email to access the polite pool
615
+ }
616
+
617
+ try:
618
+ print(f"DEBUG: Discovery Agent initiating search for: {topic_query}")
619
+ response = requests.get(api_url, params=params, timeout=15)
620
+ response.raise_for_status()
621
+ data = response.json()
622
+
623
+ results = data.get('results', [])
624
+ print(f"DEBUG: Successfully retrieved {len(results)} experts from OpenAlex")
625
+
626
+ experts = []
627
+ for r in results:
628
+ # Safely navigate the institutions list
629
+ institutions = r.get('last_known_institutions', [])
630
+ inst_name = institutions[0].get('display_name', 'Global Researcher') if institutions else 'Independent'
631
+
632
+ # Get stats safely
633
+ stats = r.get('summary_stats', {})
634
+
635
+ experts.append({
636
+ "name": r.get('display_name'),
637
+ "institution": inst_name,
638
+ "citations": r.get('cited_by_count', 0),
639
+ "works_count": r.get('works_count', 0),
640
+ "h_index": stats.get('h_index', 'N/A'),
641
+ "i10_index": stats.get('i10_index', 'N/A'),
642
+ "id": r.get('id').split('/')[-1], # Extracts 'A5012345' from the URL
643
+ "score": round((r.get('cited_by_count', 0) / 1000) + (stats.get('h_index', 0)), 1)
644
+ # Custom expertise score
645
+ })
646
+
647
+ # Sort by citations just in case OpenAlex sorting didn't catch a specific edge case
648
+ return sorted(experts, key=lambda x: x['citations'], reverse=True)
649
+
650
+ except Exception as e:
651
+ print(f"CRITICAL ERROR in OpenAlex API call: {e}")
652
+ return []
653
+
654
+
655
+ @main_bp.route('/agent', methods=['GET', 'POST'])
656
+ def discovery_agent():
657
+ experts = []
658
+ query = ""
659
+
660
+ if request.method == 'POST':
661
+ # Retrieve the topic from the frontend form
662
+ query = request.form.get('topic_query', '').strip()
663
+ if query:
664
+ experts = get_experts_by_topic(query)
665
+
666
+ return render_template('agent.html', experts=experts, query=query)
667
+
668
  @main_bp.route('/discovery')
669
  def expert_discovery():
670
  """
Templates/agent.html ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {% extends "base.html" %}
2
+
3
+ {% block content %}
4
+ <div class="min-h-screen bg-gray-50">
5
+ <div class="bg-white border-b border-gray-200 py-10">
6
+ <div class="max-w-5xl mx-auto px-8">
7
+ <div class="flex items-center gap-4 mb-2">
8
+ <div class="w-10 h-10 rounded-xl bg-blue-600 text-white flex items-center justify-center shadow-lg shadow-blue-100">
9
+ <i class="fas fa-robot"></i>
10
+ </div>
11
+ <div>
12
+ <h1 class="text-4xl font-black text-slate-900">Discovery Agent</h1>
13
+ <p class="text-slate-500">Autonomous Mixture of Experts Pattern Exploration</p>
14
+ </div>
15
+ </div>
16
+
17
+ <form action="/agent" method="POST" class="mt-8 relative">
18
+ <input type="text" name="topic_query" value="{{ query }}" required
19
+ placeholder="Specify research pattern (e.g., T cell exhaustion, CRISPR-Cas9)..."
20
+ class="w-full bg-white border-2 border-slate-200 rounded-2xl px-6 py-4 text-lg shadow-sm focus:border-blue-500 transition-all outline-none pr-32">
21
+ <button type="submit" class="absolute right-2 top-2 bottom-2 bg-blue-600 text-white px-8 rounded-xl font-bold hover:bg-blue-700 transition">
22
+ Explore
23
+ </button>
24
+ </form>
25
+ </div>
26
+ </div>
27
+
28
+ <div class="max-w-5xl mx-auto px-8 py-10">
29
+ {% if experts %}
30
+ <div class="bg-white rounded-3xl shadow-xl border border-slate-100 overflow-hidden">
31
+ <div class="px-8 py-4 bg-slate-50 border-b border-slate-100 flex justify-between items-center">
32
+ <span class="text-[10px] font-black uppercase text-slate-400 tracking-widest">OpenAlex Intelligence Corpus</span>
33
+ <span class="text-[10px] font-bold text-blue-600 bg-blue-50 px-2 py-1 rounded">Ranked by Citation Impact</span>
34
+ </div>
35
+ <table class="w-full text-left">
36
+ <thead class="bg-slate-50/50 text-slate-400 text-[10px] uppercase tracking-widest">
37
+ <tr>
38
+ <th class="px-8 py-5">Expert Profile</th>
39
+ <th class="px-8 py-5">Institutional Affiliation</th>
40
+ <th class="px-8 py-5 text-center">H-Index</th>
41
+ <th class="px-8 py-5 text-center">Citations</th>
42
+ <th class="px-8 py-5 text-right">Profile</th>
43
+ </tr>
44
+ </thead>
45
+ <tbody class="divide-y divide-slate-100">
46
+ {% for expert in experts %}
47
+ <tr class="hover:bg-blue-50/30 transition-colors group">
48
+ <td class="px-8 py-6">
49
+ <div class="font-bold text-slate-900 group-hover:text-blue-600 transition-colors">{{ expert.name }}</div>
50
+ <div class="text-[10px] text-slate-400 font-mono italic">Expert Score: {{ expert.score }}</div>
51
+ </td>
52
+ <td class="px-8 py-6 text-sm text-slate-600">
53
+ {{ expert.institution }}
54
+ </td>
55
+ <td class="px-8 py-6 text-center">
56
+ <span class="px-2 py-1 bg-slate-100 rounded-md text-xs font-bold text-slate-700">{{ expert.h_index }}</span>
57
+ </td>
58
+ <td class="px-8 py-6 text-center font-black text-blue-600">
59
+ {{ "{:,}".format(expert.citations) }}
60
+ </td>
61
+ <td class="px-8 py-6 text-right">
62
+ <a href="https://openalex.org/{{ expert.id }}" target="_blank" class="text-slate-300 hover:text-blue-600 transition">
63
+ <i class="fas fa-external-link-alt"></i>
64
+ </a>
65
+ </td>
66
+ </tr>
67
+ {% endfor %}
68
+ </tbody>
69
+ </table>
70
+ </div>
71
+ {% elif query %}
72
+ <div class="text-center py-20 bg-white rounded-3xl border-2 border-dashed border-slate-200">
73
+ <div class="w-16 h-16 bg-slate-50 rounded-full flex items-center justify-center mx-auto mb-4">
74
+ <i class="fas fa-microscope text-slate-300 text-2xl"></i>
75
+ </div>
76
+ <h3 class="text-xl font-bold text-slate-900">Pattern Isolation Failed</h3>
77
+ <p class="text-slate-500">The Discovery Agent couldn't locate experts for "{{ query }}".</p>
78
+ </div>
79
+ {% endif %}
80
+ </div>
81
+ </div>
82
+ {% endblock %}
Templates/header.html CHANGED
@@ -69,6 +69,17 @@
69
  <p class="text-xs text-gray-500 italic">Pattern exploration</p>
70
  </div>
71
  </a>
 
 
 
 
 
 
 
 
 
 
 
72
  </div>
73
 
74
  <div class="py-2">
 
69
  <p class="text-xs text-gray-500 italic">Pattern exploration</p>
70
  </div>
71
  </a>
72
+
73
+ <a href="/agent" class="group flex items-center px-4 py-3 text-sm text-gray-700 hover:bg-blue-50 transition-colors border border-transparent hover:border-blue-100 rounded-xl">
74
+ <div class="w-8 h-8 rounded-lg bg-blue-100 text-blue-600 flex items-center justify-center mr-3 group-hover:scale-110 group-hover:bg-blue-600 group-hover:text-white transition-all duration-200">
75
+ <i class="fas fa-robot"></i>
76
+ </div>
77
+ <div>
78
+ <p class="font-semibold text-gray-900 leading-none">Discovery Agent</p>
79
+ <p class="text-[11px] text-gray-500 italic mt-1">Autonomous pattern exploration</p>
80
+ </div>
81
+ </a>
82
+
83
  </div>
84
 
85
  <div class="py-2">