Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,28 +19,32 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 19 |
|
| 20 |
# Gemini model wrapper (lightweight, no smolagents.model.base)
|
| 21 |
class GeminiFlashModel:
|
| 22 |
-
def __init__(self,
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
genai.configure(api_key=self.api_key)
|
| 28 |
-
self.model = genai.GenerativeModel(model_name)
|
| 29 |
|
| 30 |
def generate(self, messages, stop_sequences=None, **kwargs):
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
|
| 46 |
# Agent using Gemini
|
|
|
|
| 19 |
|
| 20 |
# Gemini model wrapper (lightweight, no smolagents.model.base)
|
| 21 |
class GeminiFlashModel:
|
| 22 |
+
def __init__(self, model_id="gemini-1.5-flash", api_key=None):
|
| 23 |
+
import google.generativeai as genai
|
| 24 |
+
genai.configure(api_key=api_key or os.getenv("GOOGLE_API_KEY"))
|
| 25 |
+
self.model = genai.GenerativeModel(model_id)
|
| 26 |
+
self.system_prompt = SYSTEM_PROMPT
|
|
|
|
|
|
|
| 27 |
|
| 28 |
def generate(self, messages, stop_sequences=None, **kwargs):
|
| 29 |
+
# ✅ Validate and prepend system prompt
|
| 30 |
+
if not isinstance(messages, list) or not all(isinstance(m, dict) for m in messages):
|
| 31 |
+
raise TypeError("Expected 'messages' to be a list of dicts")
|
| 32 |
+
|
| 33 |
+
if not any(m.get("role") == "system" for m in messages):
|
| 34 |
+
messages = [{"role": "system", "content": self.system_prompt}] + messages
|
| 35 |
+
|
| 36 |
+
# ✅ Construct prompt string (you may improve formatting here)
|
| 37 |
+
prompt = ""
|
| 38 |
+
for m in messages:
|
| 39 |
+
role = m["role"].capitalize()
|
| 40 |
+
content = m["content"]
|
| 41 |
+
prompt += f"{role}: {content}\n"
|
| 42 |
+
|
| 43 |
+
try:
|
| 44 |
+
response = self.model.generate_content(prompt)
|
| 45 |
+
return {"content": response.text.strip()} # ✅ SmolAgent expects this format
|
| 46 |
+
except Exception as e:
|
| 47 |
+
return {"content": f"GENERATION ERROR: {e}"}
|
| 48 |
|
| 49 |
|
| 50 |
# Agent using Gemini
|