mgbam commited on
Commit
afd6eb2
·
verified ·
1 Parent(s): 7b0bd9a

Update genesis/providers.py

Browse files
Files changed (1) hide show
  1. 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
- return r.json()["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 for clarity."""
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
- return r.json()["candidates"][0]["content"]["parts"][0]["text"]
 
41
  except Exception as e:
42
  print(f"[Gemini] Failed: {e}")
43
  return text
44
 
45
- # -------- Image Generation (OpenAI HF Fallback) --------
46
- def run_openai_image(query: str) -> str:
47
- """Generate a research diagram using OpenAI image API, fallback to HF Stable Diffusion."""
48
- # Try OpenAI first
49
- if OPENAI_API_KEY:
50
- try:
51
- url = "https://api.openai.com/v1/images/generations"
52
- headers = {"Authorization": f"Bearer {OPENAI_API_KEY}"}
53
- payload = {"model": "gpt-image-1", "prompt": f"Scientific diagram about: {query}", "size": "1024x1024"}
54
- r = requests.post(url, headers=headers, json=payload, timeout=60)
55
- r.raise_for_status()
56
- return r.json()["data"][0]["url"]
57
- except Exception as e:
58
- print(f"[OpenAI Image] Failed: {e}")
59
-
60
- # Hugging Face fallback
61
- if HF_TOKEN:
62
- try:
63
- print("[Image] Falling back to Hugging Face Stable Diffusion...")
64
- url = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
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
- return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
 
78
  # -------- PubMed Fallback --------
79
  def pubmed_fallback_search(query: str, api_key: str, email: str) -> List[Dict]:
80
- """Search PubMed for relevant articles if no citations found."""
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