mgbam commited on
Commit
487bfb7
·
verified ·
1 Parent(s): 17cd8b9

Update genesis/providers.py

Browse files
Files changed (1) hide show
  1. genesis/providers.py +81 -46
genesis/providers.py CHANGED
@@ -1,55 +1,90 @@
1
- # genesis/graphdb.py
2
  import os
 
3
  from typing import List, Dict
4
- from neo4j import GraphDatabase
5
 
6
- NEO4J_URI = os.getenv("NEO4J_URI")
7
- NEO4J_USER = os.getenv("NEO4J_USER")
8
- NEO4J_PASSWORD = os.getenv("NEO4J_PASSWORD")
 
 
9
 
10
- def _get_driver():
11
- if not (NEO4J_URI and NEO4J_USER and NEO4J_PASSWORD):
12
- raise ValueError("[GraphDB] Missing Neo4j credentials.")
13
- return GraphDatabase.driver(NEO4J_URI, auth=(NEO4J_USER, NEO4J_PASSWORD))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
- def write_topic_and_papers(topic: str, citations: List[Dict], expanded_terms: List[str]):
16
- """Write topic, papers, and related concepts to Neo4j."""
 
 
 
17
  try:
18
- driver = _get_driver()
19
- with driver.session() as session:
20
- # Create Topic node
21
- session.run(
22
- "MERGE (t:Topic {name: $name})",
23
- name=topic
24
- )
 
 
25
 
26
- # Create Paper nodes and relationships
27
- for cite in citations:
28
- paper_id = cite.get("id", cite.get("url", ""))
29
- session.run(
30
- """
31
- MERGE (p:Paper {id: $id, url: $url})
32
- ON CREATE SET p.type = $type
33
- MERGE (t:Topic {name: $topic})
34
- MERGE (t)-[:RELATED_TO]->(p)
35
- """,
36
- id=paper_id,
37
- url=cite.get("url"),
38
- type=cite.get("type"),
39
- topic=topic
40
- )
41
 
42
- # Create Concept nodes and relationships
43
- for term in expanded_terms:
44
- session.run(
45
- """
46
- MERGE (c:Concept {name: $term})
47
- MERGE (t:Topic {name: $topic})
48
- MERGE (t)-[:HAS_CONCEPT]->(c)
49
- """,
50
- term=term,
51
- topic=topic
52
- )
53
- driver.close()
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  except Exception as e:
55
- print(f"[GraphDB] Failed to write data: {e}")
 
 
1
+ # genesis/providers.py
2
  import os
3
+ import requests
4
  from typing import List, Dict
 
5
 
6
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
7
+ HF_TOKEN = os.getenv("HF_TOKEN")
8
+ GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
9
+ NCBI_API_KEY = os.getenv("NCBI_API_KEY")
10
+ NCBI_EMAIL = os.getenv("NCBI_EMAIL")
11
 
12
+ # -------- DeepSeek Summary --------
13
+ def run_deepseek_summary(prompt: str) -> str:
14
+ """Run a dense scientific summary using DeepSeek API."""
15
+ try:
16
+ url = "https://api.deepseek.com/v1/chat/completions" # Example endpoint
17
+ headers = {"Authorization": f"Bearer {HF_TOKEN}", "Content-Type": "application/json"}
18
+ payload = {
19
+ "model": "deepseek-science",
20
+ "messages": [{"role": "user", "content": prompt}],
21
+ "temperature": 0.2
22
+ }
23
+ r = requests.post(url, headers=headers, json=payload, timeout=60)
24
+ r.raise_for_status()
25
+ data = r.json()
26
+ return data["choices"][0]["message"]["content"]
27
+ except Exception as e:
28
+ print(f"[DeepSeek] Failed: {e}")
29
+ return prompt # fallback: return prompt
30
 
31
+ # -------- Gemini Polish --------
32
+ def run_gemini_polish(text: str) -> str:
33
+ """Polish the summary using Gemini for clarity and presentation."""
34
+ if not GEMINI_API_KEY:
35
+ return text
36
  try:
37
+ url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key={GEMINI_API_KEY}"
38
+ payload = {"contents": [{"parts": [{"text": f"Polish and clarify this research report:\n\n{text}"}]}]}
39
+ r = requests.post(url, json=payload, timeout=30)
40
+ r.raise_for_status()
41
+ data = r.json()
42
+ return data["candidates"][0]["content"]["parts"][0]["text"]
43
+ except Exception as e:
44
+ print(f"[Gemini] Failed: {e}")
45
+ return text
46
 
47
+ # -------- Gemini Image --------
48
+ def run_gemini_image(query: str) -> str:
49
+ """Generate a research diagram using Gemini Vision/Image."""
50
+ if not GEMINI_API_KEY:
51
+ return None
52
+ try:
53
+ url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-pro-vision:generateImage?key={GEMINI_API_KEY}"
54
+ payload = {"prompt": f"Generate a detailed scientific diagram about: {query}"}
55
+ r = requests.post(url, json=payload, timeout=30)
56
+ r.raise_for_status()
57
+ data = r.json()
58
+ return data.get("image_url") or ""
59
+ except Exception as e:
60
+ print(f"[Gemini Image] Failed: {e}")
61
+ return None
62
 
63
+ # -------- PubMed Fallback --------
64
+ def pubmed_fallback_search(query: str, api_key: str, email: str) -> List[Dict]:
65
+ """Search PubMed for relevant articles if no citations found."""
66
+ results = []
67
+ if not api_key or not email:
68
+ return results
69
+ try:
70
+ base_url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi"
71
+ params = {
72
+ "db": "pubmed",
73
+ "term": query,
74
+ "retmax": 3,
75
+ "api_key": api_key,
76
+ "email": email,
77
+ "retmode": "json"
78
+ }
79
+ r = requests.get(base_url, params=params, timeout=10)
80
+ r.raise_for_status()
81
+ ids = r.json().get("esearchresult", {}).get("idlist", [])
82
+ for pmid in ids:
83
+ results.append({
84
+ "type": "PMID",
85
+ "id": pmid,
86
+ "url": f"https://pubmed.ncbi.nlm.nih.gov/{pmid}/"
87
+ })
88
  except Exception as e:
89
+ print(f"[PubMed] Failed: {e}")
90
+ return results