Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import requests
|
|
| 4 |
import pandas as pd
|
| 5 |
from huggingface_hub import login
|
| 6 |
import re
|
|
|
|
| 7 |
|
| 8 |
# --- Constants ---
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
@@ -12,6 +13,7 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 12 |
class BasicAgent:
|
| 13 |
def __init__(self):
|
| 14 |
print("BasicAgent initialized.")
|
|
|
|
| 15 |
self.agent_prompt = (
|
| 16 |
"""You are a general AI assistant. I will ask you a question. Report your thoughts, and
|
| 17 |
finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER].
|
|
@@ -26,8 +28,7 @@ class BasicAgent:
|
|
| 26 |
)
|
| 27 |
|
| 28 |
def format_final_answer(self, answer: str) -> str:
|
| 29 |
-
|
| 30 |
-
cleaned = " ".join(answer.split()) # Clean extra spaces and make sure it's a single line
|
| 31 |
return f"FINAL ANSWER: {cleaned}"
|
| 32 |
|
| 33 |
def check_commutativity(self):
|
|
@@ -59,7 +60,7 @@ class BasicAgent:
|
|
| 59 |
}
|
| 60 |
) / len(words)
|
| 61 |
return reversed_ratio > 0.3
|
| 62 |
-
|
| 63 |
def solve_riddle(self, question: str) -> str:
|
| 64 |
question = question[::-1]
|
| 65 |
if "opposite of the word" in question:
|
|
@@ -74,6 +75,22 @@ class BasicAgent:
|
|
| 74 |
return self.format_final_answer(opposite)
|
| 75 |
return self.format_final_answer("COULD_NOT_SOLVE")
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
def __call__(self, question: str) -> str:
|
| 78 |
print(f"Received question: {question[:50]}...")
|
| 79 |
if "commutative" in question.lower():
|
|
@@ -81,7 +98,7 @@ class BasicAgent:
|
|
| 81 |
if self.maybe_reversed(question):
|
| 82 |
print("Detected likely reversed riddle.")
|
| 83 |
return self.solve_riddle(question)
|
| 84 |
-
return self.format_final_answer(
|
| 85 |
|
| 86 |
|
| 87 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
@@ -168,4 +185,4 @@ with gr.Blocks() as demo:
|
|
| 168 |
|
| 169 |
if __name__ == "__main__":
|
| 170 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 171 |
-
demo.launch(debug=True, share=False)
|
|
|
|
| 4 |
import pandas as pd
|
| 5 |
from huggingface_hub import login
|
| 6 |
import re
|
| 7 |
+
from groq import Groq
|
| 8 |
|
| 9 |
# --- Constants ---
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
| 13 |
class BasicAgent:
|
| 14 |
def __init__(self):
|
| 15 |
print("BasicAgent initialized.")
|
| 16 |
+
self.client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
| 17 |
self.agent_prompt = (
|
| 18 |
"""You are a general AI assistant. I will ask you a question. Report your thoughts, and
|
| 19 |
finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER].
|
|
|
|
| 28 |
)
|
| 29 |
|
| 30 |
def format_final_answer(self, answer: str) -> str:
|
| 31 |
+
cleaned = " ".join(answer.split())
|
|
|
|
| 32 |
return f"FINAL ANSWER: {cleaned}"
|
| 33 |
|
| 34 |
def check_commutativity(self):
|
|
|
|
| 60 |
}
|
| 61 |
) / len(words)
|
| 62 |
return reversed_ratio > 0.3
|
| 63 |
+
|
| 64 |
def solve_riddle(self, question: str) -> str:
|
| 65 |
question = question[::-1]
|
| 66 |
if "opposite of the word" in question:
|
|
|
|
| 75 |
return self.format_final_answer(opposite)
|
| 76 |
return self.format_final_answer("COULD_NOT_SOLVE")
|
| 77 |
|
| 78 |
+
def query_groq(self, question: str) -> str:
|
| 79 |
+
full_prompt = f"{self.agent_prompt}\n\nQuestion: {question}"
|
| 80 |
+
try:
|
| 81 |
+
response = self.client.chat.completions.create(
|
| 82 |
+
model="llama3-8b-8192",
|
| 83 |
+
messages=[{"role": "user", "content": full_prompt}]
|
| 84 |
+
)
|
| 85 |
+
answer = response.choices[0].message.content
|
| 86 |
+
if "FINAL ANSWER:" in answer:
|
| 87 |
+
return answer.split("FINAL ANSWER:")[-1].strip()
|
| 88 |
+
else:
|
| 89 |
+
return self.format_final_answer(answer)
|
| 90 |
+
except Exception as e:
|
| 91 |
+
print(f"[Groq ERROR]: {e}")
|
| 92 |
+
return self.format_final_answer("GROQ_ERROR")
|
| 93 |
+
|
| 94 |
def __call__(self, question: str) -> str:
|
| 95 |
print(f"Received question: {question[:50]}...")
|
| 96 |
if "commutative" in question.lower():
|
|
|
|
| 98 |
if self.maybe_reversed(question):
|
| 99 |
print("Detected likely reversed riddle.")
|
| 100 |
return self.solve_riddle(question)
|
| 101 |
+
return self.format_final_answer(self.query_groq(question))
|
| 102 |
|
| 103 |
|
| 104 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
|
|
| 185 |
|
| 186 |
if __name__ == "__main__":
|
| 187 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 188 |
+
demo.launch(debug=True, share=False)
|