Spaces:
Sleeping
Sleeping
| # 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) |