Spaces:
Sleeping
Sleeping
Update genesis/providers.py
Browse files- genesis/providers.py +138 -53
genesis/providers.py
CHANGED
@@ -1,79 +1,164 @@
|
|
1 |
# genesis/providers.py
|
2 |
import os
|
3 |
import requests
|
4 |
-
|
5 |
-
from
|
6 |
-
from genesis.api_clients.bioportal_api import search_bioportal
|
7 |
-
from genesis.api_clients.umls_api import search_umls
|
8 |
-
from genesis.api_clients.ncbi_api import search_ncbi
|
9 |
|
10 |
-
|
11 |
-
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
12 |
DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY")
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
def run_pubmed_literature(query: str, max_results: int = 10):
|
18 |
-
"""Fetch literature from PubMed."""
|
19 |
-
return search_pubmed(query, max_results=max_results)
|
20 |
|
21 |
-
#
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
24 |
try:
|
25 |
url = "https://api.deepseek.com/v1/chat/completions"
|
26 |
headers = {"Authorization": f"Bearer {DEEPSEEK_API_KEY}"}
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
31 |
except Exception as e:
|
32 |
print(f"[DeepSeek] Failed: {e}")
|
33 |
return None
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
38 |
try:
|
39 |
url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key={GEMINI_API_KEY}"
|
40 |
headers = {"Content-Type": "application/json"}
|
41 |
-
payload = {"contents": [{"parts": [{"text":
|
42 |
-
|
43 |
-
|
44 |
-
return
|
45 |
except Exception as e:
|
46 |
print(f"[Gemini] Failed: {e}")
|
47 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
-
#
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
|
|
53 |
try:
|
54 |
-
url =
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
60 |
|
61 |
-
|
|
|
|
|
|
|
|
|
|
|
62 |
try:
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
try:
|
72 |
-
|
73 |
-
headers = {
|
74 |
-
|
75 |
-
|
76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
except Exception as e:
|
78 |
-
print(f"[
|
79 |
return None
|
|
|
1 |
# genesis/providers.py
|
2 |
import os
|
3 |
import requests
|
4 |
+
import base64
|
5 |
+
from typing import Optional
|
|
|
|
|
|
|
6 |
|
7 |
+
# Load secrets from environment
|
|
|
8 |
DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY")
|
9 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
10 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
11 |
+
HF_API_KEY = os.getenv("HF_API_KEY")
|
12 |
+
ELEVEN_LABS_API_KEY = os.getenv("ELEVEN_LABS_API_KEY")
|
|
|
|
|
|
|
13 |
|
14 |
+
# ------------------------------
|
15 |
+
# 1 β DEEPSEEK SUMMARIZER
|
16 |
+
# ------------------------------
|
17 |
+
def run_deepseek_summary(prompt: str) -> Optional[str]:
|
18 |
+
if not DEEPSEEK_API_KEY:
|
19 |
+
return None
|
20 |
try:
|
21 |
url = "https://api.deepseek.com/v1/chat/completions"
|
22 |
headers = {"Authorization": f"Bearer {DEEPSEEK_API_KEY}"}
|
23 |
+
payload = {
|
24 |
+
"model": "deepseek-chat",
|
25 |
+
"messages": [{"role": "user", "content": prompt}],
|
26 |
+
"temperature": 0.3
|
27 |
+
}
|
28 |
+
resp = requests.post(url, headers=headers, json=payload, timeout=60)
|
29 |
+
resp.raise_for_status()
|
30 |
+
return resp.json()["choices"][0]["message"]["content"]
|
31 |
except Exception as e:
|
32 |
print(f"[DeepSeek] Failed: {e}")
|
33 |
return None
|
34 |
|
35 |
+
|
36 |
+
# ------------------------------
|
37 |
+
# 2 β GEMINI SUMMARIZER
|
38 |
+
# ------------------------------
|
39 |
+
def run_gemini_summary(prompt: str) -> Optional[str]:
|
40 |
+
if not GEMINI_API_KEY:
|
41 |
+
return None
|
42 |
try:
|
43 |
url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key={GEMINI_API_KEY}"
|
44 |
headers = {"Content-Type": "application/json"}
|
45 |
+
payload = {"contents": [{"parts": [{"text": prompt}]}]}
|
46 |
+
resp = requests.post(url, headers=headers, json=payload, timeout=60)
|
47 |
+
resp.raise_for_status()
|
48 |
+
return resp.json()["candidates"][0]["content"]["parts"][0]["text"]
|
49 |
except Exception as e:
|
50 |
print(f"[Gemini] Failed: {e}")
|
51 |
+
return None
|
52 |
+
|
53 |
+
|
54 |
+
# ------------------------------
|
55 |
+
# 3 β OPENAI SUMMARIZER
|
56 |
+
# ------------------------------
|
57 |
+
def run_openai_summary(prompt: str) -> Optional[str]:
|
58 |
+
if not OPENAI_API_KEY:
|
59 |
+
return None
|
60 |
+
try:
|
61 |
+
url = "https://api.openai.com/v1/chat/completions"
|
62 |
+
headers = {"Authorization": f"Bearer {OPENAI_API_KEY}"}
|
63 |
+
payload = {
|
64 |
+
"model": "gpt-4o-mini",
|
65 |
+
"messages": [{"role": "user", "content": prompt}],
|
66 |
+
"temperature": 0.3
|
67 |
+
}
|
68 |
+
resp = requests.post(url, headers=headers, json=payload, timeout=60)
|
69 |
+
resp.raise_for_status()
|
70 |
+
return resp.json()["choices"][0]["message"]["content"]
|
71 |
+
except Exception as e:
|
72 |
+
print(f"[OpenAI] Failed: {e}")
|
73 |
+
return None
|
74 |
+
|
75 |
+
|
76 |
+
# ------------------------------
|
77 |
+
# 4 β GEMINI IMAGE GENERATOR
|
78 |
+
# ------------------------------
|
79 |
+
def run_gemini_image(prompt: str) -> Optional[str]:
|
80 |
+
if not GEMINI_API_KEY:
|
81 |
+
return None
|
82 |
+
try:
|
83 |
+
# Gemini image generation (beta)
|
84 |
+
url = f"https://generativelanguage.googleapis.com/v1beta/models/imagegeneration:generateImage?key={GEMINI_API_KEY}"
|
85 |
+
payload = {"prompt": prompt, "size": "1024x1024"}
|
86 |
+
headers = {"Content-Type": "application/json"}
|
87 |
+
resp = requests.post(url, headers=headers, json=payload, timeout=60)
|
88 |
+
resp.raise_for_status()
|
89 |
+
data = resp.json()
|
90 |
+
if "data" in data and len(data["data"]) > 0:
|
91 |
+
return data["data"][0]["url"]
|
92 |
+
return None
|
93 |
+
except Exception as e:
|
94 |
+
print(f"[Gemini Image] Failed: {e}")
|
95 |
+
return None
|
96 |
+
|
97 |
|
98 |
+
# ------------------------------
|
99 |
+
# 5 β OPENAI IMAGE GENERATOR
|
100 |
+
# ------------------------------
|
101 |
+
def run_openai_image(prompt: str) -> Optional[str]:
|
102 |
+
if not OPENAI_API_KEY:
|
103 |
+
return None
|
104 |
try:
|
105 |
+
url = "https://api.openai.com/v1/images/generations"
|
106 |
+
headers = {"Authorization": f"Bearer {OPENAI_API_KEY}"}
|
107 |
+
payload = {"model": "gpt-image-1", "prompt": prompt, "size": "1024x1024"}
|
108 |
+
resp = requests.post(url, headers=headers, json=payload, timeout=60)
|
109 |
+
resp.raise_for_status()
|
110 |
+
return resp.json()["data"][0]["url"]
|
111 |
+
except Exception as e:
|
112 |
+
print(f"[OpenAI Image] Failed: {e}")
|
113 |
+
return None
|
114 |
+
|
115 |
|
116 |
+
# ------------------------------
|
117 |
+
# 6 β HF STABLE DIFFUSION IMAGE
|
118 |
+
# ------------------------------
|
119 |
+
def run_hf_image(prompt: str) -> Optional[str]:
|
120 |
+
if not HF_API_KEY:
|
121 |
+
return None
|
122 |
try:
|
123 |
+
url = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2"
|
124 |
+
headers = {"Authorization": f"Bearer {HF_API_KEY}"}
|
125 |
+
payload = {"inputs": prompt}
|
126 |
+
resp = requests.post(url, headers=headers, json=payload, timeout=120)
|
127 |
+
resp.raise_for_status()
|
128 |
+
if resp.headers.get("content-type") == "image/png":
|
129 |
+
filename = f"generated_{hash(prompt)}.png"
|
130 |
+
with open(filename, "wb") as f:
|
131 |
+
f.write(resp.content)
|
132 |
+
return filename
|
133 |
+
return None
|
134 |
+
except Exception as e:
|
135 |
+
print(f"[HF Image] Failed: {e}")
|
136 |
+
return None
|
137 |
|
138 |
+
|
139 |
+
# ------------------------------
|
140 |
+
# 7 β ELEVENLABS TEXT-TO-SPEECH
|
141 |
+
# ------------------------------
|
142 |
+
def narrate_text_elevenlabs(text: str) -> Optional[str]:
|
143 |
+
if not ELEVEN_LABS_API_KEY:
|
144 |
+
return None
|
145 |
try:
|
146 |
+
url = "https://api.elevenlabs.io/v1/text-to-speech"
|
147 |
+
headers = {
|
148 |
+
"xi-api-key": ELEVEN_LABS_API_KEY,
|
149 |
+
"Content-Type": "application/json"
|
150 |
+
}
|
151 |
+
payload = {
|
152 |
+
"text": text,
|
153 |
+
"voice": "Bella", # You can make this configurable
|
154 |
+
"model_id": "eleven_monolingual_v1"
|
155 |
+
}
|
156 |
+
resp = requests.post(url, headers=headers, json=payload, timeout=60)
|
157 |
+
resp.raise_for_status()
|
158 |
+
filename = f"narration_{hash(text)}.mp3"
|
159 |
+
with open(filename, "wb") as f:
|
160 |
+
f.write(resp.content)
|
161 |
+
return filename
|
162 |
except Exception as e:
|
163 |
+
print(f"[ElevenLabs] Failed: {e}")
|
164 |
return None
|