Spaces:
Sleeping
Sleeping
File size: 6,233 Bytes
3b8d936 22eac42 3b8d936 99a5882 3b8d936 330d726 f194a4a 3b8d936 22eac42 f194a4a 0710131 f194a4a 6ae164c f194a4a a84eae4 793f383 74006c8 2ffe740 d7c470b e3e3e01 793f383 cfb291d d7c470b 74006c8 fa906c6 2ffe740 74006c8 22eac42 cfb291d a84eae4 3b8d936 a84eae4 3b8d936 22eac42 99a5882 3b8d936 a84eae4 3b8d936 22eac42 3b8d936 a84eae4 22eac42 3b8d936 b1354c2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | # app.py - Final Answer Extractor with input/output logging
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch, os, logging
from langchain_core.prompts import PromptTemplate
from datetime import datetime
from langchain_community.llms import LlamaCpp
from llama_cpp import Llama
from huggingface_hub import hf_hub_download
# ---- logging setup ----
LOG_FILENAME = "final_answer_agent.log" # اجعله None إن لم ترد ملف لوق
logger = logging.getLogger("final_answer_agent")
logger.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s | %(levelname)s | %(message)s")
# Console handler (stdout)
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
ch.setFormatter(formatter)
logger.addHandler(ch)
# Optional file handler
if LOG_FILENAME:
fh = logging.FileHandler(LOG_FILENAME, encoding="utf-8")
fh.setLevel(logging.INFO)
fh.setFormatter(formatter)
logger.addHandler(fh)
# ---- model setup ----
MODEL_ID = "Qwen/Qwen2.5-1.5B-Instruct"
#tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
#model = AutoModelForCausalLM.from_pretrained(
# MODEL_ID,
# device_map="auto",
# torch_dtype=torch.float16
#)
#model.eval()
model_path = hf_hub_download(
repo_id="bartowski/Qwen2.5-7B-Instruct-GGUF",
filename="Qwen2.5-7B-Instruct-Q6_K.gguf",
)
llm = LlamaCpp(
model_path=model_path,
n_ctx=10000,
n_threads=4,
n_gpu_layers=0,
temperature=0.4,
top_p=0.9,
max_tokens=100,
n_batch=64,
verbose=False,
use_mmap=True
)
#1.if there is "Final Answer:" Look for the "Final Answer:" or "final answer:".
#2. Extract only what comes after it .
#3. If there is no explicit "Final Answer:", infer what you think is the final answer.
PROMPT_TEMPLATE = """You are an expert final-answer extractor working for the GAIA benchmark.you are facing a GAIA questions exam.
the exam's results evaluator is an AI model that use the exact matching method to evaluate your answers, so you must format your Final Answer in the most consice and useful format.
Your task is to read the question and the raw output of an AI agent and extract the final concise answer for the question .
extract what comes between (Final Answer) and (<<end>>)
. DO NOT include any other text, reasoning, or punctuation beyond the final concise answer.
DON'T take out your output of the logging or error information.
make your answer in gaia format( in the most concise format) as follows:
## EXAMPLES:
[
{{
"question": "What is the name of the fourth planet in the solar system?",
"agent_response": "The fourth planet from the Sun comes right after Earth. It is known for its reddish color due to the iron oxide on its surface.",
"reasoning": "The planet described as the 'red planet' is Mars.",
Final Answer: Mars
}},
{{
"question": "Order the following planets from smallest to largest by diameter: Earth, Mercury, Neptune, Venus.",
"agent_response": "The smallest is Mercury, followed by Venus, then Earth, and the largest among these four is Neptune.",
"reasoning": "The correct order is Mercury < Venus < Earth < Neptune.",
Final Answer: ["Mercury", "Venus", "Earth", "Neptune"]
}},
{{
"question": "In which year did NASA launch the Voyager 1 spacecraft?",
"agent_response": "It was launched in the late 1970s, a few weeks after Voyager 2, specifically in September 1977.",
"reasoning": "Launch year is 1977.",
Final Answer: "1977"
}},
{{
"question": "If a book costs $12 and a pen costs $3, what is the total cost of buying 5 books and 4 pens?",
"agent_response": "The books cost $60 in total, and the pens cost $12, so the total is their sum.",
"reasoning": "60 + 12 = 72.",
Final Answer: "72"
}},
{{
"question": "Which of the following animals is a mammal? [Crocodile, Dolphin, Falcon]",
"agent_response": "The crocodile is a reptile, the falcon is a bird, while the dolphin breathes air and nurses its young.",
"reasoning": "The only mammal in the list is the dolphin.",
Final Answer: "Dolphin"
}}
]
---
ignore the error messages like : agent stoped due to limit iteration.
Now extract the GAIA final concise answer from this text:
{text}
"""
#Format the answer according to GAIA Benchmark rules:
# - Numbers → return as-is (e.g., 42)
# - Dates → format as YYYY-MM-DD
# - Lists → comma-separated, e.g., "Paris, London"
# - Short words or names → return exactly that
def extract_final_answer(raw: str) -> str:
# سجل الدخل فور الاستلام
logger.info(f"INPUT received (len={len(raw) if raw else 0}): {repr(raw[:300])}{'...' if raw and len(raw)>300 else ''}")
if not raw or not raw.strip():
out_text = "No input provided."
logger.info(f"OUTPUT: {out_text}")
return out_text
prompt = PROMPT_TEMPLATE.format(text=raw)
# inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
# with torch.no_grad():
# out = model.generate(**inputs, max_new_tokens=64, temperature=0.2)
# decoded = tokenizer.decode(out[0], skip_special_tokens=True)
decoded= llm.invoke(prompt)
# محاولة إزالة الجزء المكرر من الـ prompt إن ظهر — نأخذ آخر سطر غير الفارغ
lines = [l.strip() for l in decoded.splitlines() if l.strip()]
if not lines:
out_text = decoded.strip()
else:
out_text = lines[-1]
# سجل الخرج
logger.info(f"OUTPUT (len={len(out_text)}): {repr(out_text)}")
return out_text
# Gradio interface
iface = gr.Interface(
fn=extract_final_answer,
inputs=gr.Textbox(label="Input Text", lines=6, placeholder="Paste the reasoning text from main agent..."),
outputs=gr.Textbox(label="Extracted Final Answer", lines=1),
title="Final Answer Extractor",
description="Extracts the GAIA Final Answer from reasoning text."
)
if __name__ == "__main__":
# لاحظ: iface.queue(api_open=True) كما في كودك الأصلي — يحافظ على توافق client.predict
logger.info("Starting Final Answer Extractor service...")
iface.queue(api_open=True).launch(server_name="0.0.0.0", server_port=7860) |