Spaces:
Sleeping
Sleeping
Update genesis/providers.py
Browse files- genesis/providers.py +45 -36
genesis/providers.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
# genesis/providers.py
|
2 |
import os
|
3 |
import requests
|
4 |
from typing import List, Dict
|
@@ -22,14 +21,15 @@ def run_deepseek_summary(prompt: str) -> str:
|
|
22 |
}
|
23 |
r = requests.post(url, headers=headers, json=payload, timeout=60)
|
24 |
r.raise_for_status()
|
25 |
-
|
|
|
26 |
except Exception as e:
|
27 |
print(f"[DeepSeek] Failed: {e}")
|
28 |
return prompt
|
29 |
|
30 |
# -------- Gemini Polish --------
|
31 |
def run_gemini_polish(text: str) -> str:
|
32 |
-
"""Polish the summary using Gemini
|
33 |
if not GEMINI_API_KEY:
|
34 |
return text
|
35 |
try:
|
@@ -37,47 +37,56 @@ def run_gemini_polish(text: str) -> str:
|
|
37 |
payload = {"contents": [{"parts": [{"text": f"Polish and clarify this research report:\n\n{text}"}]}]}
|
38 |
r = requests.post(url, json=payload, timeout=30)
|
39 |
r.raise_for_status()
|
40 |
-
|
|
|
41 |
except Exception as e:
|
42 |
print(f"[Gemini] Failed: {e}")
|
43 |
return text
|
44 |
|
45 |
-
# --------
|
46 |
-
def run_openai_image(
|
47 |
-
"""Generate
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
66 |
-
payload = {"inputs": f"highly detailed scientific diagram about: {query}"}
|
67 |
-
r = requests.post(url, headers=headers, json=payload, timeout=60)
|
68 |
-
r.raise_for_status()
|
69 |
-
img_path = f"/tmp/{query.replace(' ', '_')}.png"
|
70 |
-
with open(img_path, "wb") as f:
|
71 |
-
f.write(r.content)
|
72 |
-
return img_path
|
73 |
-
except Exception as e:
|
74 |
-
print(f"[HF Image] Failed: {e}")
|
75 |
|
76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
# -------- PubMed Fallback --------
|
79 |
def pubmed_fallback_search(query: str, api_key: str, email: str) -> List[Dict]:
|
80 |
-
"""Search PubMed
|
81 |
results = []
|
82 |
if not api_key or not email:
|
83 |
return results
|
|
|
|
|
1 |
import os
|
2 |
import requests
|
3 |
from typing import List, Dict
|
|
|
21 |
}
|
22 |
r = requests.post(url, headers=headers, json=payload, timeout=60)
|
23 |
r.raise_for_status()
|
24 |
+
data = r.json()
|
25 |
+
return data["choices"][0]["message"]["content"]
|
26 |
except Exception as e:
|
27 |
print(f"[DeepSeek] Failed: {e}")
|
28 |
return prompt
|
29 |
|
30 |
# -------- Gemini Polish --------
|
31 |
def run_gemini_polish(text: str) -> str:
|
32 |
+
"""Polish the summary using Gemini."""
|
33 |
if not GEMINI_API_KEY:
|
34 |
return text
|
35 |
try:
|
|
|
37 |
payload = {"contents": [{"parts": [{"text": f"Polish and clarify this research report:\n\n{text}"}]}]}
|
38 |
r = requests.post(url, json=payload, timeout=30)
|
39 |
r.raise_for_status()
|
40 |
+
data = r.json()
|
41 |
+
return data["candidates"][0]["content"]["parts"][0]["text"]
|
42 |
except Exception as e:
|
43 |
print(f"[Gemini] Failed: {e}")
|
44 |
return text
|
45 |
|
46 |
+
# -------- OpenAI Image --------
|
47 |
+
def run_openai_image(prompt: str) -> str:
|
48 |
+
"""Generate image using OpenAI API."""
|
49 |
+
if not OPENAI_API_KEY:
|
50 |
+
return None
|
51 |
+
try:
|
52 |
+
url = "https://api.openai.com/v1/images/generations"
|
53 |
+
headers = {"Authorization": f"Bearer {OPENAI_API_KEY}"}
|
54 |
+
payload = {
|
55 |
+
"model": "gpt-image-1",
|
56 |
+
"prompt": f"Highly detailed scientific diagram: {prompt}",
|
57 |
+
"size": "1024x1024"
|
58 |
+
}
|
59 |
+
r = requests.post(url, headers=headers, json=payload, timeout=60)
|
60 |
+
r.raise_for_status()
|
61 |
+
data = r.json()
|
62 |
+
return data["data"][0]["url"]
|
63 |
+
except Exception as e:
|
64 |
+
print(f"[OpenAI Image] Failed: {e}")
|
65 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
+
# -------- Hugging Face Image (Fallback) --------
|
68 |
+
def run_hf_image(prompt: str) -> str:
|
69 |
+
"""Generate image using Hugging Face Stable Diffusion."""
|
70 |
+
if not HF_TOKEN:
|
71 |
+
return None
|
72 |
+
try:
|
73 |
+
url = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2"
|
74 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
75 |
+
payload = {"inputs": prompt}
|
76 |
+
r = requests.post(url, headers=headers, json=payload, timeout=60)
|
77 |
+
r.raise_for_status()
|
78 |
+
# Hugging Face returns raw image bytes, so we save to file
|
79 |
+
image_path = f"generated_{hash(prompt)}.png"
|
80 |
+
with open(image_path, "wb") as f:
|
81 |
+
f.write(r.content)
|
82 |
+
return image_path
|
83 |
+
except Exception as e:
|
84 |
+
print(f"[HF Image] Failed: {e}")
|
85 |
+
return None
|
86 |
|
87 |
# -------- PubMed Fallback --------
|
88 |
def pubmed_fallback_search(query: str, api_key: str, email: str) -> List[Dict]:
|
89 |
+
"""Search PubMed if no citations found."""
|
90 |
results = []
|
91 |
if not api_key or not email:
|
92 |
return results
|