Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -132,34 +132,40 @@ def extract_raw_text(output):
|
|
| 132 |
|
| 133 |
return "\n".join(texts).strip()
|
| 134 |
|
| 135 |
-
raise TypeError(f"Unsupported output type: {type(output).__name__}")
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
try:
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
"x-goog-api-key": os.environ["GEMINI_API_KEY"]
|
| 142 |
-
},
|
| 143 |
-
timeout=20
|
| 144 |
-
)
|
| 145 |
|
| 146 |
-
print("Gemini HTTP Status:", response.status_code)
|
| 147 |
-
print("Gemini Response:", response.text[:500])
|
| 148 |
|
| 149 |
-
|
|
|
|
| 150 |
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
| 155 |
class BasicAgent:
|
| 156 |
def __init__(self):
|
| 157 |
|
| 158 |
-
#
|
| 159 |
self.model = RetryOpenAIModel(
|
| 160 |
-
model_id="
|
| 161 |
-
api_base="https://
|
| 162 |
-
api_key=os.environ["
|
| 163 |
client_kwargs={
|
| 164 |
"timeout": 60.0,
|
| 165 |
"max_retries": 0
|
|
@@ -370,15 +376,12 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 370 |
return f"An unexpected error occurred loading questions: {e}", None
|
| 371 |
|
| 372 |
# 3. Run your Agent
|
| 373 |
-
|
|
|
|
| 374 |
|
| 375 |
-
if cache_file.exists():
|
| 376 |
-
with open(cache_file, "r", encoding="utf-8") as f:
|
| 377 |
-
saved_answers = json.load(f)
|
| 378 |
-
else:
|
| 379 |
-
saved_answers = {}
|
| 380 |
results_log = []
|
| 381 |
answers_payload = []
|
|
|
|
| 382 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 383 |
for item in questions_data:
|
| 384 |
task_id = item.get("task_id")
|
|
@@ -400,21 +403,34 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 400 |
+ file_content
|
| 401 |
)
|
| 402 |
|
| 403 |
-
if task_id in
|
|
|
|
|
|
|
|
|
|
|
|
|
| 404 |
submitted_answer = saved_answers[task_id]
|
| 405 |
-
print(f"Using
|
|
|
|
| 406 |
else:
|
| 407 |
submitted_answer = agent(agent_question)
|
| 408 |
|
| 409 |
-
|
| 410 |
-
saved_answers[task_id] = submitted_answer
|
| 411 |
-
|
| 412 |
-
with open(cache_file, "w", encoding="utf-8") as f:
|
| 413 |
-
json.dump(saved_answers, f, ensure_ascii=False, indent=2)
|
| 414 |
if submitted_answer is None:
|
| 415 |
raise ValueError("Submitted answer is None")
|
|
|
|
| 416 |
if not isinstance(submitted_answer, (str, int, float)):
|
| 417 |
raise TypeError("Submitted answer has invalid type")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 418 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 419 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 420 |
except Exception as e:
|
|
@@ -493,18 +509,6 @@ with gr.Blocks() as demo:
|
|
| 493 |
gr.LoginButton()
|
| 494 |
|
| 495 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 496 |
-
check_gemini_btn = gr.Button("Check Gemini API")
|
| 497 |
-
|
| 498 |
-
gemini_output = gr.Textbox(
|
| 499 |
-
label="Gemini API Result",
|
| 500 |
-
lines=6
|
| 501 |
-
)
|
| 502 |
-
|
| 503 |
-
check_gemini_btn.click(
|
| 504 |
-
fn=check_gemini_api,
|
| 505 |
-
inputs=[],
|
| 506 |
-
outputs=gemini_output
|
| 507 |
-
)
|
| 508 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 509 |
# Removed max_rows=10 from DataFrame constructor
|
| 510 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
|
|
|
| 132 |
|
| 133 |
return "\n".join(texts).strip()
|
| 134 |
|
| 135 |
+
raise TypeError(f"Unsupported output type: {type(output).__name__}")
|
| 136 |
+
|
| 137 |
+
CACHE_FILE = Path("answers_cache.json")
|
| 138 |
+
VERIFIED_FILE = Path("verified_answers.json")
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
def load_cache(path):
|
| 142 |
+
if not path.exists():
|
| 143 |
+
return {}
|
| 144 |
+
|
| 145 |
try:
|
| 146 |
+
return json.loads(path.read_text(encoding="utf-8"))
|
| 147 |
+
except (OSError, json.JSONDecodeError):
|
| 148 |
+
return {}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
|
|
|
|
|
|
| 150 |
|
| 151 |
+
def save_cache(path, data):
|
| 152 |
+
temp_path = path.with_name(path.name + ".tmp")
|
| 153 |
|
| 154 |
+
temp_path.write_text(
|
| 155 |
+
json.dumps(data, ensure_ascii=False, indent=2),
|
| 156 |
+
encoding="utf-8"
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
temp_path.replace(path)
|
| 160 |
+
|
| 161 |
class BasicAgent:
|
| 162 |
def __init__(self):
|
| 163 |
|
| 164 |
+
# model
|
| 165 |
self.model = RetryOpenAIModel(
|
| 166 |
+
model_id="gemini-2.5-flash",
|
| 167 |
+
api_base="https://generativelanguage.googleapis.com/v1beta/openai/",
|
| 168 |
+
api_key=os.environ["GEMINI_API_KEY"],
|
| 169 |
client_kwargs={
|
| 170 |
"timeout": 60.0,
|
| 171 |
"max_retries": 0
|
|
|
|
| 376 |
return f"An unexpected error occurred loading questions: {e}", None
|
| 377 |
|
| 378 |
# 3. Run your Agent
|
| 379 |
+
saved_answers = load_cache(CACHE_FILE)
|
| 380 |
+
verified_answers = load_cache(VERIFIED_FILE)
|
| 381 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 382 |
results_log = []
|
| 383 |
answers_payload = []
|
| 384 |
+
|
| 385 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 386 |
for item in questions_data:
|
| 387 |
task_id = item.get("task_id")
|
|
|
|
| 403 |
+ file_content
|
| 404 |
)
|
| 405 |
|
| 406 |
+
if task_id in verified_answers:
|
| 407 |
+
submitted_answer = verified_answers[task_id]
|
| 408 |
+
print(f"[VERIFIED] Using answer for {task_id}")
|
| 409 |
+
|
| 410 |
+
elif task_id in saved_answers:
|
| 411 |
submitted_answer = saved_answers[task_id]
|
| 412 |
+
print(f"[CACHE] Using answer for {task_id}")
|
| 413 |
+
|
| 414 |
else:
|
| 415 |
submitted_answer = agent(agent_question)
|
| 416 |
|
| 417 |
+
# Kiểm tra đáp án
|
|
|
|
|
|
|
|
|
|
|
|
|
| 418 |
if submitted_answer is None:
|
| 419 |
raise ValueError("Submitted answer is None")
|
| 420 |
+
|
| 421 |
if not isinstance(submitted_answer, (str, int, float)):
|
| 422 |
raise TypeError("Submitted answer has invalid type")
|
| 423 |
+
|
| 424 |
+
submitted_answer = str(submitted_answer).strip()
|
| 425 |
+
|
| 426 |
+
if not submitted_answer:
|
| 427 |
+
raise ValueError("Submitted answer is empty")
|
| 428 |
+
|
| 429 |
+
# Lưu đáp án mới
|
| 430 |
+
if task_id not in verified_answers and task_id not in saved_answers:
|
| 431 |
+
saved_answers[task_id] = submitted_answer
|
| 432 |
+
save_cache(cache_file, saved_answers)
|
| 433 |
+
print(f"[SAVED] {task_id}: {submitted_answer}")
|
| 434 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 435 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 436 |
except Exception as e:
|
|
|
|
| 509 |
gr.LoginButton()
|
| 510 |
|
| 511 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 512 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 513 |
# Removed max_rows=10 from DataFrame constructor
|
| 514 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|