File size: 11,705 Bytes
6a2aeb0 ecb4e3d 737fe0e ecb4e3d 737fe0e a3faa74 737fe0e 6a2aeb0 a3faa74 737fe0e 6a2aeb0 e4abfe8 737fe0e ecb4e3d 0d32a9e a3faa74 0d32a9e a3faa74 0d32a9e a3faa74 0d32a9e 737fe0e a3faa74 ecb4e3d a3faa74 ecb4e3d a3faa74 ecb4e3d a3faa74 ecb4e3d 737fe0e a3faa74 737fe0e a3faa74 737fe0e a3faa74 737fe0e 6a2aeb0 ecb4e3d 737fe0e 6a2aeb0 737fe0e ecb4e3d 737fe0e ecb4e3d a3faa74 737fe0e a3faa74 af37df4 737fe0e ecb4e3d a3faa74 737fe0e a3faa74 737fe0e a3faa74 0d32a9e a3faa74 0d32a9e a3faa74 0d32a9e a3faa74 0d32a9e a3faa74 0d32a9e a3faa74 0d32a9e a3faa74 0d32a9e a3faa74 0d32a9e a3faa74 0d32a9e a3faa74 737fe0e 0d32a9e a3faa74 0d32a9e a3faa74 0d32a9e a3faa74 af37df4 ecb4e3d a3faa74 6a2aeb0 a3faa74 737fe0e a3faa74 737fe0e a3faa74 865c342 a3faa74 737fe0e e4abfe8 a3faa74 e4abfe8 b8312c7 e4abfe8 737fe0e a3faa74 ecb4e3d 737fe0e 6a2aeb0 865c342 a3faa74 6a2aeb0 ec14f23 6a2aeb0 a3faa74 865c342 737fe0e a3faa74 865c342 |
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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
import re
import requests
import pandas as pd
import torch
import gradio as gr
from tqdm import tqdm
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
from typing import List, Dict, Any, Tuple, Optional
import json
import ast
import numpy as np
from PIL import Image, UnidentifiedImageError
import io
import base64
import logging
import time
import sys
# Настройка логирования
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger("GAIA-Mastermind")
# Конфигурация
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
MODEL_NAME = "google/flan-t5-large" # Оптимизировано для CPU
API_RETRIES = 3
API_TIMEOUT = 45
# === ЯДРО СИСТЕМЫ ===
class GAIAThoughtProcessor:
def __init__(self):
self.device = "cuda" if torch.cuda.is_available() else "cpu"
logger.info(f"⚡ Инициализация GAIAThoughtProcessor на {self.device.upper()}")
try:
# Оптимизированная загрузка модели для CPU
self.tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
self.model = AutoModelForSeq2SeqLM.from_pretrained(
MODEL_NAME,
device_map="auto" if torch.cuda.is_available() else None,
torch_dtype=torch.float32,
low_cpu_mem_usage=True
).eval()
# Создаем пайплайн для генерации текста
self.text_generator = pipeline(
"text2text-generation",
model=self.model,
tokenizer=self.tokenizer,
device=-1 if self.device == "cpu" else 0,
max_new_tokens=128
)
logger.info("✅ GAIAThoughtProcessor готов")
except Exception as e:
logger.exception("Ошибка инициализации модели")
raise RuntimeError(f"Ошибка инициализации: {str(e)}")
def process_question(self, question: str, task_id: str) -> str:
"""Упрощенная обработка вопроса"""
try:
prompt = f"Реши задачу шаг за шагом: {question}\n\nФинальный ответ:"
result = self.text_generator(
prompt,
max_new_tokens=128,
num_beams=2,
early_stopping=True,
temperature=0.1
)
response = result[0]['generated_text'].strip()
# Создаем JSON ответ
return json.dumps({"final_answer": response})
except Exception as e:
logger.error(f"Ошибка обработки вопроса: {str(e)}")
return json.dumps({
"task_id": task_id,
"error": str(e),
"final_answer": f"ERROR: {str(e)}"
})
# === СИСТЕМА ОЦЕНКИ ===
class GAIAEvaluationRunner:
def __init__(self, api_url: str = DEFAULT_API_URL):
self.api_url = api_url
self.questions_url = f"{api_url}/questions"
self.submit_url = f"{api_url}/submit"
self.session = requests.Session()
self.session.headers.update({
"Accept": "application/json",
"User-Agent": "GAIA-Mastermind/1.0",
"Content-Type": "application/json"
})
logger.info(f"🌐 Инициализирован GAIAEvaluationRunner для {api_url}")
def _fetch_questions(self) -> Tuple[list, str]:
"""Получение вопросов с API"""
logger.info(f"🔍 Запрос вопросов с {self.questions_url}")
try:
response = self.session.get(
self.questions_url,
timeout=API_TIMEOUT
)
logger.info(f"Статус ответа: {response.status_code}")
if response.status_code == 200:
questions = response.json()
logger.info(f"Получено {len(questions)} вопросов")
return questions, "success"
else:
error_msg = f"Ошибка API: HTTP {response.status_code}"
logger.error(error_msg)
return [], error_msg
except Exception as e:
error_msg = f"Ошибка соединения: {str(e)}"
logger.exception(error_msg)
return [], error_msg
def _submit_answers(self, username: str, agent_code: str, answers: list) -> Tuple[str, int]:
"""Отправка ответов на сервер"""
logger.info(f"📤 Отправка ответов для пользователя {username}")
try:
payload = {
"username": username.strip(),
"agent_code": agent_code.strip(),
"answers": answers
}
response = self.session.post(
self.submit_url,
json=payload,
timeout=API_TIMEOUT * 2
)
logger.info(f"Статус отправки: {response.status_code}")
if response.status_code == 200:
result = response.json()
score = result.get("score", 0)
return result.get("message", "Ответы успешно отправлены"), score
else:
error = f"HTTP Ошибка {response.status_code}"
if response.text:
error += f": {response.text[:200]}"
logger.error(error)
return error, 0
except Exception as e:
error = f"Ошибка отправки: {str(e)}"
logger.exception(error)
return error, 0
def run_evaluation(self, agent, username: str, agent_code: str, progress=gr.Progress()):
"""Основной процесс оценки"""
# Получение вопросов
progress(0.1, desc="Получение вопросов")
questions, status = self._fetch_questions()
if status != "success":
return status, 0, 0, pd.DataFrame()
total_questions = len(questions)
if total_questions == 0:
return "Получено 0 вопросов", 0, 0, pd.DataFrame()
# Обработка вопросов
results = []
answers = []
for i, q in enumerate(questions):
progress(i / total_questions, desc=f"Обработка задачи {i+1}/{total_questions}")
try:
task_id = q.get("task_id", f"task_{i}")
logger.info(f"🔧 Обработка задачи {task_id}")
json_response = agent.process_question(q["question"], task_id)
# Парсинг ответа
try:
response_obj = json.loads(json_response)
final_answer = response_obj.get("final_answer", "")
except:
final_answer = json_response
answers.append({
"task_id": task_id,
"answer": str(final_answer)[:500]
})
results.append({
"Task ID": task_id,
"Question": q["question"][:50] + "..." if len(q["question"]) > 50 else q["question"],
"Answer": str(final_answer)[:50] + "..." if len(str(final_answer)) > 50 else str(final_answer),
"Status": "Processed"
})
except Exception as e:
logger.error(f"Ошибка обработки задачи: {str(e)}")
answers.append({
"task_id": task_id,
"answer": f"ERROR: {str(e)}"
})
results.append({
"Task ID": task_id,
"Question": "Error",
"Answer": f"ERROR: {str(e)}",
"Status": "Failed"
})
# Отправка ответов
progress(0.9, desc="Отправка результатов")
submission_result, score = self._submit_answers(username, agent_code, answers)
return submission_result, score, total_questions, pd.DataFrame(results)
# === ИНТЕРФЕЙС GRADIO ===
def run_evaluation(username: str, agent_code: str, progress=gr.Progress()):
try:
progress(0, desc="Инициализация агента")
agent = GAIAThoughtProcessor()
progress(0.1, desc="Подключение к API")
runner = GAIAEvaluationRunner()
# Запуск оценки
return runner.run_evaluation(agent, username, agent_code, progress)
except Exception as e:
logger.exception("Критическая ошибка в run_evaluation")
error_df = pd.DataFrame([{
"Task ID": "ERROR",
"Question": f"Критическая ошибка: {str(e)}",
"Answer": "См. логи",
"Status": "Failed"
}])
return f"Ошибка: {str(e)}", 0, 0, error_df
# Создание интерфейса
with gr.Blocks(title="GAIA Mastermind") as demo:
gr.Markdown("# GAIA Mastermind")
gr.Markdown("Многошаговое решение задач с декомпозицией")
with gr.Row():
with gr.Column():
gr.Markdown("## 🔐 Авторизация")
username = gr.Textbox(label="HF Username", value="yoshizen")
agent_code = gr.Textbox(label="Agent Code", value="https://huggingface.co/spaces/yoshizen/FinalTest")
run_btn = gr.Button("Запустить оценку")
gr.Markdown("## ⚙️ Статус системы")
sys_info = gr.Textbox(label="Системная информация", interactive=False)
with gr.Column():
gr.Markdown("## 📊 Результаты GAIA")
with gr.Row():
result_output = gr.Textbox(label="Статус отправки", interactive=False)
correct_output = gr.Number(label="Правильные ответы", interactive=False)
total_output = gr.Number(label="Всего вопросов", interactive=False)
results_table = gr.Dataframe(
label="Детализация ответов",
headers=["Task ID", "Question", "Answer", "Status"],
interactive=False
)
# Системная информация
def get_system_info():
device = "GPU" if torch.cuda.is_available() else "CPU"
return f"Device: {device} | Model: {MODEL_NAME} | API: {DEFAULT_API_URL}"
demo.load(get_system_info, inputs=None, outputs=sys_info)
run_btn.click(
fn=run_evaluation,
inputs=[username, agent_code],
outputs=[result_output, correct_output, total_output, results_table],
concurrency_limit=1
)
if __name__ == "__main__":
demo.queue(max_size=1).launch(
server_name="0.0.0.0",
server_port=7860,
share=False,
show_error=True
) |