Spaces:
Sleeping
Sleeping
Update genesis/providers.py
Browse files- genesis/providers.py +9 -13
genesis/providers.py
CHANGED
@@ -22,15 +22,14 @@ 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 |
-
return data["choices"][0]["message"]["content"]
|
27 |
except Exception as e:
|
28 |
print(f"[DeepSeek] Failed: {e}")
|
29 |
-
return prompt
|
30 |
|
31 |
# -------- Gemini Polish --------
|
32 |
def run_gemini_polish(text: str) -> str:
|
33 |
-
"""Polish the summary using Gemini for clarity
|
34 |
if not GEMINI_API_KEY:
|
35 |
return text
|
36 |
try:
|
@@ -38,15 +37,14 @@ def run_gemini_polish(text: str) -> str:
|
|
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 |
-
|
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 |
-
# -------- OpenAI
|
48 |
-
def
|
49 |
-
"""Generate a research diagram
|
50 |
# Try OpenAI first
|
51 |
if OPENAI_API_KEY:
|
52 |
try:
|
@@ -55,12 +53,11 @@ def run_gemini_image(query: str) -> str:
|
|
55 |
payload = {"model": "gpt-image-1", "prompt": f"Scientific diagram about: {query}", "size": "1024x1024"}
|
56 |
r = requests.post(url, headers=headers, json=payload, timeout=60)
|
57 |
r.raise_for_status()
|
58 |
-
|
59 |
-
return data["data"][0]["url"]
|
60 |
except Exception as e:
|
61 |
print(f"[OpenAI Image] Failed: {e}")
|
62 |
|
63 |
-
# Hugging Face
|
64 |
if HF_TOKEN:
|
65 |
try:
|
66 |
print("[Image] Falling back to Hugging Face Stable Diffusion...")
|
@@ -69,7 +66,6 @@ def run_gemini_image(query: str) -> str:
|
|
69 |
payload = {"inputs": f"highly detailed scientific diagram about: {query}"}
|
70 |
r = requests.post(url, headers=headers, json=payload, timeout=60)
|
71 |
r.raise_for_status()
|
72 |
-
# Save to local file
|
73 |
img_path = f"/tmp/{query.replace(' ', '_')}.png"
|
74 |
with open(img_path, "wb") as f:
|
75 |
f.write(r.content)
|
|
|
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 |
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:
|
|
|
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...")
|
|
|
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)
|