Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
class BasicAgent:
|
| 2 |
|
| 3 |
def __init__(self):
|
| 4 |
|
| 5 |
-
print("Initializing
|
| 6 |
|
| 7 |
self.model = OpenAIServerModel(
|
| 8 |
model_id="llama-3.1-8b-instant",
|
|
@@ -10,7 +22,7 @@ class BasicAgent:
|
|
| 10 |
api_key=os.getenv("GROQ_API_KEY")
|
| 11 |
)
|
| 12 |
|
| 13 |
-
print("
|
| 14 |
|
| 15 |
def clean_answer(self, text):
|
| 16 |
|
|
@@ -23,19 +35,21 @@ class BasicAgent:
|
|
| 23 |
text = text.replace("FINAL ANSWER:", "")
|
| 24 |
text = text.replace("Answer:", "")
|
| 25 |
|
| 26 |
-
text = text.strip()
|
|
|
|
|
|
|
| 27 |
|
| 28 |
return text[:200]
|
| 29 |
|
| 30 |
def __call__(self, question: str) -> str:
|
| 31 |
|
| 32 |
prompt = f"""
|
| 33 |
-
Answer the question.
|
| 34 |
|
| 35 |
IMPORTANT:
|
| 36 |
- Return ONLY the final answer.
|
| 37 |
- No reasoning.
|
| 38 |
-
- No
|
| 39 |
- Keep answers concise.
|
| 40 |
|
| 41 |
Question:
|
|
@@ -46,17 +60,143 @@ Question:
|
|
| 46 |
|
| 47 |
response = self.model(
|
| 48 |
prompt,
|
| 49 |
-
max_tokens=
|
| 50 |
)
|
| 51 |
|
| 52 |
-
|
| 53 |
|
| 54 |
-
print(
|
| 55 |
|
| 56 |
-
return
|
| 57 |
|
| 58 |
except Exception as e:
|
| 59 |
|
| 60 |
-
print(e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import requests
|
| 4 |
+
import pandas as pd
|
| 5 |
+
|
| 6 |
+
from smolagents.models import OpenAIServerModel
|
| 7 |
+
|
| 8 |
+
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 9 |
+
|
| 10 |
+
# ---------------------------------------------------
|
| 11 |
+
# AGENT
|
| 12 |
+
# ---------------------------------------------------
|
| 13 |
class BasicAgent:
|
| 14 |
|
| 15 |
def __init__(self):
|
| 16 |
|
| 17 |
+
print("Initializing agent...")
|
| 18 |
|
| 19 |
self.model = OpenAIServerModel(
|
| 20 |
model_id="llama-3.1-8b-instant",
|
|
|
|
| 22 |
api_key=os.getenv("GROQ_API_KEY")
|
| 23 |
)
|
| 24 |
|
| 25 |
+
print("Agent initialized.")
|
| 26 |
|
| 27 |
def clean_answer(self, text):
|
| 28 |
|
|
|
|
| 35 |
text = text.replace("FINAL ANSWER:", "")
|
| 36 |
text = text.replace("Answer:", "")
|
| 37 |
|
| 38 |
+
text = text.strip()
|
| 39 |
+
|
| 40 |
+
text = text.split("\n")[0]
|
| 41 |
|
| 42 |
return text[:200]
|
| 43 |
|
| 44 |
def __call__(self, question: str) -> str:
|
| 45 |
|
| 46 |
prompt = f"""
|
| 47 |
+
Answer the question accurately.
|
| 48 |
|
| 49 |
IMPORTANT:
|
| 50 |
- Return ONLY the final answer.
|
| 51 |
- No reasoning.
|
| 52 |
+
- No explanations.
|
| 53 |
- Keep answers concise.
|
| 54 |
|
| 55 |
Question:
|
|
|
|
| 60 |
|
| 61 |
response = self.model(
|
| 62 |
prompt,
|
| 63 |
+
max_tokens=64
|
| 64 |
)
|
| 65 |
|
| 66 |
+
answer = self.clean_answer(response)
|
| 67 |
|
| 68 |
+
print(answer)
|
| 69 |
|
| 70 |
+
return answer
|
| 71 |
|
| 72 |
except Exception as e:
|
| 73 |
|
| 74 |
+
print(f"ERROR: {e}")
|
| 75 |
+
|
| 76 |
+
return ""
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
# ---------------------------------------------------
|
| 80 |
+
# MAIN FUNCTION
|
| 81 |
+
# ---------------------------------------------------
|
| 82 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 83 |
+
|
| 84 |
+
if profile:
|
| 85 |
+
username = profile.username
|
| 86 |
+
else:
|
| 87 |
+
return "Please login first.", None
|
| 88 |
+
|
| 89 |
+
questions_url = f"{DEFAULT_API_URL}/questions"
|
| 90 |
+
submit_url = f"{DEFAULT_API_URL}/submit"
|
| 91 |
+
|
| 92 |
+
# init agent
|
| 93 |
+
try:
|
| 94 |
+
agent = BasicAgent()
|
| 95 |
+
except Exception as e:
|
| 96 |
+
return f"Agent init error: {e}", None
|
| 97 |
+
|
| 98 |
+
# fetch questions
|
| 99 |
+
try:
|
| 100 |
+
|
| 101 |
+
response = requests.get(questions_url)
|
| 102 |
+
|
| 103 |
+
questions_data = response.json()
|
| 104 |
+
|
| 105 |
+
except Exception as e:
|
| 106 |
+
|
| 107 |
+
return f"Question fetch error: {e}", None
|
| 108 |
+
|
| 109 |
+
answers_payload = []
|
| 110 |
+
results_log = []
|
| 111 |
+
|
| 112 |
+
# run questions
|
| 113 |
+
for item in questions_data:
|
| 114 |
+
|
| 115 |
+
task_id = item.get("task_id")
|
| 116 |
+
question = item.get("question")
|
| 117 |
+
|
| 118 |
+
try:
|
| 119 |
+
|
| 120 |
+
answer = agent(question)
|
| 121 |
+
|
| 122 |
+
answers_payload.append({
|
| 123 |
+
"task_id": task_id,
|
| 124 |
+
"submitted_answer": answer
|
| 125 |
+
})
|
| 126 |
+
|
| 127 |
+
results_log.append({
|
| 128 |
+
"Task ID": task_id,
|
| 129 |
+
"Question": question,
|
| 130 |
+
"Answer": answer
|
| 131 |
+
})
|
| 132 |
+
|
| 133 |
+
except Exception as e:
|
| 134 |
+
|
| 135 |
+
results_log.append({
|
| 136 |
+
"Task ID": task_id,
|
| 137 |
+
"Question": question,
|
| 138 |
+
"Answer": f"ERROR: {e}"
|
| 139 |
+
})
|
| 140 |
+
|
| 141 |
+
# submit
|
| 142 |
+
try:
|
| 143 |
+
|
| 144 |
+
submission = {
|
| 145 |
+
"username": username,
|
| 146 |
+
"agent_code": "https://huggingface.co",
|
| 147 |
+
"answers": answers_payload
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
response = requests.post(
|
| 151 |
+
submit_url,
|
| 152 |
+
json=submission,
|
| 153 |
+
timeout=120
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
result = response.json()
|
| 157 |
+
|
| 158 |
+
status = (
|
| 159 |
+
f"Score: {result.get('score')}%\n"
|
| 160 |
+
f"Correct: {result.get('correct_count')}/"
|
| 161 |
+
f"{result.get('total_attempted')}"
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
return status, pd.DataFrame(results_log)
|
| 165 |
+
|
| 166 |
+
except Exception as e:
|
| 167 |
+
|
| 168 |
+
return f"Submit error: {e}", pd.DataFrame(results_log)
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
# ---------------------------------------------------
|
| 172 |
+
# UI
|
| 173 |
+
# ---------------------------------------------------
|
| 174 |
+
with gr.Blocks() as demo:
|
| 175 |
+
|
| 176 |
+
gr.Markdown("# HF Agents Course Assignment")
|
| 177 |
+
|
| 178 |
+
gr.LoginButton()
|
| 179 |
+
|
| 180 |
+
btn = gr.Button("Run Evaluation")
|
| 181 |
+
|
| 182 |
+
output = gr.Textbox(
|
| 183 |
+
label="Submission Result",
|
| 184 |
+
lines=6
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
table = gr.DataFrame()
|
| 188 |
+
|
| 189 |
+
btn.click(
|
| 190 |
+
fn=run_and_submit_all,
|
| 191 |
+
outputs=[output, table]
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
# ---------------------------------------------------
|
| 196 |
+
# LAUNCH
|
| 197 |
+
# ---------------------------------------------------
|
| 198 |
+
if __name__ == "__main__":
|
| 199 |
+
|
| 200 |
+
print("Launching app...")
|
| 201 |
|
| 202 |
+
demo.launch()
|