Spaces:
Sleeping
Sleeping
Update genesis/providers.py
Browse files- genesis/providers.py +81 -46
genesis/providers.py
CHANGED
@@ -1,55 +1,90 @@
|
|
1 |
-
# genesis/
|
2 |
import os
|
|
|
3 |
from typing import List, Dict
|
4 |
-
from neo4j import GraphDatabase
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
17 |
try:
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
except Exception as e:
|
55 |
-
print(f"[
|
|
|
|
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
|