Spaces:
Sleeping
Sleeping
Create gemini.py
Browse files- orchestrator/gemini.py +24 -0
orchestrator/gemini.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
+
|
| 4 |
+
try:
|
| 5 |
+
import google.generativeai as genai
|
| 6 |
+
except ImportError:
|
| 7 |
+
genai = None
|
| 8 |
+
|
| 9 |
+
def get_gemini_api_key():
|
| 10 |
+
# Try Streamlit Secrets, then env var
|
| 11 |
+
if hasattr(st.secrets, "GEMINI_API_KEY"):
|
| 12 |
+
return st.secrets["GEMINI_API_KEY"]
|
| 13 |
+
return os.getenv("GEMINI_API_KEY")
|
| 14 |
+
|
| 15 |
+
def gemini_generate(prompt, model="gemini-1.5-flash"):
|
| 16 |
+
api_key = get_gemini_api_key()
|
| 17 |
+
if not api_key:
|
| 18 |
+
raise RuntimeError("Gemini API key not set!")
|
| 19 |
+
if genai is None:
|
| 20 |
+
raise ImportError("Please install google-generativeai!")
|
| 21 |
+
genai.configure(api_key=api_key)
|
| 22 |
+
model_obj = genai.GenerativeModel(model)
|
| 23 |
+
response = model_obj.generate_content(prompt)
|
| 24 |
+
return response.text if hasattr(response, \"text\") else str(response)
|