Spaces:
Configuration error
Configuration error
Orel MAZOR commited on
Commit ·
e4bc671
1
Parent(s): 0d0725b
Commit 1
Browse filesFiles changed (10)
- .DS_StoreAdded
- .gradio/certificate.pemAdded+31
- .vscode/launch.jsonAdded+15
- __pycache__/agent.cpython-311.pycAdded
- __pycache__/agent2.cpython-311.pycAdded
- agent.py+1-132
- agent2.py+154-288
- app.py+2-2
- appasync.pyAdded+210
- custom_models.pyAdded+404
12345678910111213141516171819202122232425262728293031-----BEGIN CERTIFICATE-----MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAwTzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2VhcmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJuZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBYMTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAK3oJHP0FDfzm54rVygch77ct984kIxuPOZXoHj3dcKi/vVqbvYATyjb3miGbESTtrFj/RQSa78f0uoxmyF+0TM8ukj13Xnfs7j/EvEhmkvBioZxaUpmZmyPfjxwv60pIgbz5MDmgK7iS4+3mX6UA5/TR5d8mUgjU+g4rk8Kb4Mu0UlXjIB0ttov0DiNewNwIRt18jA8+o+u3dpjq+sWT8KOEUt+zwvo/7V3LvSye0rgTBIlDHCNAymg4VMk7BPZ7hm/ELNKjD+Jo2FR3qyHB5T0Y3HsLuJvW5iB4YlcNHlsdu87kGJ55tukmi8mxdAQ4Q7e2RCOFvu396j3x+UCB5iPNgiV5+I3lg02dZ77DnKxHZu8A/lJBdiB3QW0KtZB6awBdpUKD9jf1b0SHzUvKBds0pjBqAlkd25HN7rOrFleaJ1/ctaJxQZBKT5ZPt0m9STJEadao0xAH0ahmbWnOlFuhjuefXKnEgV4We0+UXgVCwOPjdAvBbI+e0ocS3MFEvzG6uBQE3xDk3SzynTnjh8BCNAw1FtxNrQHusEwMFxIt4I7mKZ9YIqioymCzLq9gwQbooMDQaHWBfEbwrbwqHyGO0aoSCqI3Haadr8faqU9GY/rOPNk3sgrDQoo//fb4hVC1CLQJ13hef4Y53CIrU7m2Ys6xt0nUW7/vGT1M0NPAgMBAAGjQjBAMA4GA1UdDwEB/wQEAwIBBjAPBgNVHRMBAf8EBTADAQH/MB0GA1UdDgQWBBR5tFnme7bl5AFzgAiIyBpY9umbbjANBgkqhkiG9w0BAQsFAAOCAgEAVR9YqbyyqFDQDLHYGmkgJykIrGF1XIpu+ILlaS/V9lZLubhzEFnTIZd+50xx+7LSYK05qAvqFyFWhfFQDlnrzuBZ6brJFe+GnY+EgPbk6ZGQ3BebYhtF8GaV0nxvwuo77x/Py9auJ/GpsMiu/X1+mvoiBOv/2X/qkSsisRcOj/KKNFtY2PwByVS5uCbMiogziUwthDyC3+6WVwW6LLv3xLfHTjuCvjHIInNzktHCgKQ5ORAzI4JMPJ+GslWYHb4phowim57iaztXOoJwTdwJx4nLCgdNbOhdjsnvzqvHu7UrTkXWStAmzOVyyghqpZXjFaH3pO3JLF+l+/+sKAIuvtd7u+Nxe5AW0wdeRlN8NwdCjNPElpzVmbUq4JUagEiuTDkHzsxHpFKVK7q4+63SM1N95R1NbdWhscdCb+ZAJzVcoyi3B43njTOQ5yOf+1CceWxG1bQVs5ZufpsMljq4Ui0/1lvh+wjChP4kqKOJ2qxq4RgqsahDYVvTH9w7jXbyLeiNdd8XM2w9U/t7y0Ff/9yi0GE44Za4rF2LN9d11TPAmRGunUHBcnWEvgJBQl9nJEiU0Zsnvgc/ubhPgXRR4Xq37Z0j4r7g1SgEEzwxA57demyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc=-----END CERTIFICATE-----123456789101112131415{ // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "Python Debugger: Current File", "type": "debugpy", "request": "launch", "program": "${file}", "console": "integratedTerminal" } ]}9 unmodified lines101112131415104 unmodified lines1201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582599 unmodified lines# Third-party importsimport requestsfrom transformers import AutoModelForCausalLM, AutoTokenizer# LlamaIndex core importsfrom llama_index.core import VectorStoreIndex, Document, Settings104 unmodified linesprint("Initializing models in non-API mode with local models...")try :from typing import Optional, List, Anyfrom pydantic import Field, PrivateAttrfrom llama_index.core.llms import CustomLLM, CompletionResponse, CompletionResponseGen, LLMMetadatafrom llama_index.core.llms.callbacks import llm_completion_callbackfrom transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessorfrom qwen_vl_utils import process_vision_infoimport torchclass QwenVL7BCustomLLM(CustomLLM):model_name: str = Field(default="Qwen/Qwen2.5-VL-7B-Instruct")context_window: int = Field(default=32768)num_output: int = Field(default=256)_model = PrivateAttr()_processor = PrivateAttr()def __init__(self, **kwargs):super().__init__(**kwargs)self._model = Qwen2_5_VLForConditionalGeneration.from_pretrained(self.model_name, torch_dtype=torch.bfloat16, device_map='balanced')self._processor = AutoProcessor.from_pretrained(self.model_name)@propertydef metadata(self) -> LLMMetadata:return LLMMetadata(context_window=self.context_window,num_output=self.num_output,model_name=self.model_name,)@llm_completion_callback()def complete(self,prompt: str,image_paths: Optional[List[str]] = None,**kwargs: Any) -> CompletionResponse:# Prepare multimodal inputmessages = [{"role": "user", "content": []}]if image_paths:for path in image_paths:messages[0]["content"].append({"type": "image", "image": path})messages[0]["content"].append({"type": "text", "text": prompt})# Tokenize and processtext = self._processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)image_inputs, video_inputs = process_vision_info(messages)inputs = self._processor(text=[text],images=image_inputs,videos=video_inputs,padding=True,return_tensors="pt",)inputs = inputs.to(self._model.device)# Generate outputgenerated_ids = self._model.generate(**inputs, max_new_tokens=self.num_output)generated_ids_trimmed = [out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)]output_text = self._processor.batch_decode(generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]return CompletionResponse(text=output_text)@llm_completion_callback()def stream_complete(self,prompt: str,image_paths: Optional[List[str]] = None,**kwargs: Any) -> CompletionResponseGen:response = self.complete(prompt, image_paths)for token in response.text:yield CompletionResponse(text=token, delta=token)proj_llm = QwenVL7BCustomLLM()from typing import Any, List, Optionalfrom llama_index.core.embeddings import BaseEmbeddingfrom sentence_transformers import SentenceTransformerfrom PIL import Imageclass MultimodalCLIPEmbedding(BaseEmbedding):"""Custom embedding class using CLIP for multimodal capabilities."""def __init__(self, model_name: str = "clip-ViT-B-32", **kwargs: Any) -> None:super().__init__(**kwargs)self._model = SentenceTransformer(model_name)@classmethoddef class_name(cls) -> str:return "multimodal_clip"def _get_query_embedding(self, query: str, image_path: Optional[str] = None) -> List[float]:if image_path:image = Image.open(image_path)embedding = self._model.encode(image)return embedding.tolist()else:embedding = self._model.encode(query)return embedding.tolist()def _get_text_embedding(self, text: str, image_path: Optional[str] = None) -> List[float]:if image_path:image = Image.open(image_path)embedding = self._model.encode(image)return embedding.tolist()else:embedding = self._model.encode(text)return embedding.tolist()def _get_text_embeddings(self, texts: List[str], image_paths: Optional[List[str]] = None) -> List[List[float]]:embeddings = []image_paths = image_paths or [None] * len(texts)for text, img_path in zip(texts, image_paths):if img_path:image = Image.open(img_path)emb = self._model.encode(image)else:emb = self._model.encode(text)embeddings.append(emb.tolist())return embeddingsasync def _aget_query_embedding(self, query: str, image_path: Optional[str] = None) -> List[float]:return self._get_query_embedding(query, image_path)async def _aget_text_embedding(self, text: str, image_path: Optional[str] = None) -> List[float]:return self._get_text_embedding(text, image_path)embed_model = MultimodalCLIPEmbedding()embed_model.max_seq_length = 10249 unmodified lines10111213141516104 unmodified lines1211221231241251261271289 unmodified lines# Third-party importsimport requestsfrom transformers import AutoModelForCausalLM, AutoTokenizerfrom custom_models import QwenVL7BCustomLLM, BaaiMultimodalEmbedding# LlamaIndex core importsfrom llama_index.core import VectorStoreIndex, Document, Settings104 unmodified linesprint("Initializing models in non-API mode with local models...")try :proj_llm = QwenVL7BCustomLLM()embed_model = MultimodalCLIPEmbedding()embed_model.max_seq_length = 1024
4 unmodified lines5678910111213141516171819201 unmodified line2223242526272829303127 unmodified lines596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318415 unmodified lines2002012022032042051 unmodified line2072082092102112122132142152162172182192202212222232242252262272282292302312321 unmodified line23423523623723823931 unmodified lines27127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731856 unmodified lines3753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084092 unmodified lines4124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574581 unmodified line46046146246346446546646746846947047147247347447547647747847948048148248348448520 unmodified lines50650750850951051151251351451551651751851952052152252352452552652752852953053153253353431 unmodified lines5665675685695705715722 unmodified lines5755765775785795805815825835845854 unmodified linesfrom langchain.docstore.document import Documentfrom langchain.text_splitter import RecursiveCharacterTextSplitterfrom langchain_community.retrievers import BM25Retrieverfrom smolagents import CodeAgent, OpenAIServerModel, tool, Toolfrom smolagents.vision_web_browser import initialize_driver, save_screenshot, helium_instructionsfrom smolagents.agents import ActionStepfrom selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.common.keys import Keysimport heliumfrom PIL import Imagefrom io import BytesIOfrom time import sleep# Langfuse observability importsfrom opentelemetry.sdk.trace import TracerProvider1 unmodified linefrom opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporterfrom opentelemetry.sdk.trace.export import SimpleSpanProcessorfrom opentelemetry import tracefrom opentelemetry.trace import format_trace_idfrom langfuse import Langfuseclass BM25RetrieverTool(Tool):"""BM25 retriever tool for document search when text documents are available27 unmodified linesfor i, doc in enumerate(docs)])@tooldef search_item_ctrl_f(text: str, nth_result: int = 1) -> str:"""Search for text on the current page via Ctrl + F and jump to the nth occurrence.Args:text: The text string to search for on the webpagenth_result: Which occurrence to jump to (default is 1 for first occurrence)Returns:str: Result of the search operation with match count and navigation status"""try:driver = helium.get_driver()elements = driver.find_elements(By.XPATH, f"//*[contains(text(), '{text}')]")if nth_result > len(elements):return f"Match n°{nth_result} not found (only {len(elements)} matches found)"result = f"Found {len(elements)} matches for '{text}'."elem = elements[nth_result - 1]driver.execute_script("arguments[0].scrollIntoView(true);", elem)result += f"Focused on element {nth_result} of {len(elements)}"return resultexcept Exception as e:return f"Error searching for text: {e}"@tooldef go_back() -> str:"""Navigate back to the previous page in browser history.Returns:str: Confirmation message or error description"""try:driver = helium.get_driver()driver.back()return "Navigated back to previous page"except Exception as e:return f"Error going back: {e}"@tooldef close_popups() -> str:"""Close any visible modal or pop-up on the page by sending ESC key.Returns:str: Confirmation message or error description"""try:driver = helium.get_driver()webdriver.ActionChains(driver).send_keys(Keys.ESCAPE).perform()return "Attempted to close popups"except Exception as e:return f"Error closing popups: {e}"@tooldef scroll_page(direction: str = "down", amount: int = 3) -> str:"""Scroll the webpage in the specified direction.Args:direction: Direction to scroll, either 'up' or 'down'amount: Number of scroll actions to performReturns:str: Confirmation message or error description"""try:driver = helium.get_driver()for _ in range(amount):if direction.lower() == "down":driver.execute_script("window.scrollBy(0, 300);")elif direction.lower() == "up":driver.execute_script("window.scrollBy(0, -300);")sleep(0.5)return f"Scrolled {direction} {amount} times"except Exception as e:return f"Error scrolling: {e}"@tooldef get_page_text() -> str:"""Extract all visible text from the current webpage.Returns:str: The visible text content of the page"""try:driver = helium.get_driver()text = driver.find_element(By.TAG_NAME, "body").textreturn f"Page text (first 2000 chars): {text[:2000]}"except Exception as e:return f"Error getting page text: {e}"def save_screenshot_callback(memory_step: ActionStep, agent: CodeAgent) -> None:"""Save screenshots for web browser automation"""try:sleep(1.0)driver = helium.get_driver()if driver is not None:# Clean up old screenshotsfor previous_memory_step in agent.memory.steps:if isinstance(previous_memory_step, ActionStep) and previous_memory_step.step_number <= memory_step.step_number - 2:previous_memory_step.observations_images = Nonepng_bytes = driver.get_screenshot_as_png()image = Image.open(BytesIO(png_bytes))memory_step.observations_images = [image.copy()]# Update observations with current URLurl_info = f"Current url: {driver.current_url}"memory_step.observations = (url_info if memory_step.observations is Noneelse memory_step.observations + "\n" + url_info)except Exception as e:print(f"Error in screenshot callback: {e}")class GAIAAgent:"""GAIA agent using smolagents with Gemini 2.0 Flash and Langfuse observability15 unmodified linesmodel_id="gemini-2.0-flash",api_base="https://generativelanguage.googleapis.com/v1beta/openai/",api_key=gemini_api_key,)# Store user and session IDs for tracking1 unmodified lineself.session_id = session_id or "gaia-session"# GAIA system prompt from the leaderboardself.system_prompt = """You are a general AI assistant. I will ask you a question. Report your thoughts and reasoning process clearly. You should use the available tools to gather information and solve problems step by step.When using web browser automation:- Use helium commands like go_to(), click(), scroll_down()- Take screenshots to see what's happening- Handle popups and forms appropriately- Be patient with page loadingFor document retrieval:- Use the BM25 retriever when there are text documents attached- Search with relevant keywords from the questionYour final answer should be as few words as possible, a number, or a comma-separated list. Don't use articles, abbreviations, or units unless specified."""# Initialize retriever tool (will be updated when documents are loaded)self.retriever_tool = BM25RetrieverTool()# Initialize web driver for browser automationself.driver = None# Create the agentself.agent = Noneself._create_agent()1 unmodified line# Initialize Langfuse clientself.langfuse = Langfuse()def _setup_langfuse_observability(self):"""Set up Langfuse observability with OpenTelemetry"""# Get Langfuse keys from environment variables31 unmodified lines"""Create the CodeAgent with tools"""base_tools = [self.retriever_tool,search_item_ctrl_f,go_back,close_popups,scroll_page,get_page_text]self.agent = CodeAgent(tools=base_tools,model=self.model,add_base_tools=True,planning_interval=3,additional_authorized_imports=["helium", "requests", "BeautifulSoup", "json"],step_callbacks=[save_screenshot_callback] if self.driver else [],max_steps=5,description=self.system_prompt,verbosity_level=2,)def initialize_browser(self):"""Initialize browser for web automation tasks"""try:chrome_options = webdriver.ChromeOptions()chrome_options.add_argument("--force-device-scale-factor=1")chrome_options.add_argument("--window-size=1000,1350")chrome_options.add_argument("--disable-pdf-viewer")chrome_options.add_argument("--window-position=0,0")chrome_options.add_argument("--no-sandbox")chrome_options.add_argument("--disable-dev-shm-usage")self.driver = helium.start_chrome(headless=False, options=chrome_options)# Recreate agent with browser toolsself._create_agent()# Import helium for the agentself.agent.python_executor("from helium import *")return Trueexcept Exception as e:print(f"Failed to initialize browser: {e}")return Falsedef load_documents_from_file(self, file_path: str):"""Load and process documents from a file for BM25 retrieval"""56 unmodified linesif task_id:trace_tags.append(f"task-{task_id}")# Start Langfuse trace with OpenTelemetrywith self.tracer.start_as_current_span("GAIA-Question-Solving") as span:try:# Set span attributes for trackingspan.set_attribute("langfuse.user.id", self.user_id)span.set_attribute("langfuse.session.id", self.session_id)span.set_attribute("langfuse.tags", trace_tags)span.set_attribute("gaia.task_id", task_id)span.set_attribute("gaia.question_length", len(question))# Get trace ID for Langfuse linkingcurrent_span = trace.get_current_span()span_context = current_span.get_span_context()trace_id = span_context.trace_idformatted_trace_id = format_trace_id(trace_id)# Create Langfuse tracelangfuse_trace = self.langfuse.trace(id=formatted_trace_id,name="GAIA Question Solving",input={"question": question, "task_id": task_id},user_id=self.user_id,session_id=self.session_id,tags=trace_tags,metadata={"model": self.model.model_id,"question_length": len(question),"has_file": bool(task_id)})# Download and load file if task_id provided2 unmodified linesfile_path = self.download_gaia_file(task_id)if file_path:file_loaded = self.load_documents_from_file(file_path)span.set_attribute("gaia.file_loaded", file_loaded)print(f"Loaded file for task {task_id}")# Check if this requires web browsingweb_indicators = ["navigate", "browser", "website", "webpage", "url", "click", "search on"]needs_browser = any(indicator in question.lower() for indicator in web_indicators)span.set_attribute("gaia.needs_browser", needs_browser)if needs_browser and not self.driver:print("Initializing browser for web automation...")browser_initialized = self.initialize_browser()span.set_attribute("gaia.browser_initialized", browser_initialized)# Prepare the promptprompt = f"""Question: {question}{f'Task ID: {task_id}' if task_id else ''}{f'File loaded: Yes' if file_loaded else 'File loaded: No'}Solve this step by step. Use the available tools to gather information and provide a precise answer."""if needs_browser:prompt += "\n" + helium_instructionsprint("=== AGENT REASONING ===")result = self.agent.run(prompt)print("=== END REASONING ===")# Update Langfuse trace with resultlangfuse_trace.update(output={"answer": str(result)},end_time=None # Will be set automatically)# Add success attributesspan.set_attribute("gaia.success", True)span.set_attribute("gaia.answer_length", len(str(result)))# Flush Langfuse dataself.langfuse.flush()return str(result)1 unmodified lineerror_msg = f"Error processing question: {str(e)}"print(error_msg)# Log error to span and Langfusespan.set_attribute("gaia.success", False)span.set_attribute("gaia.error", str(e))if 'langfuse_trace' in locals():langfuse_trace.update(output={"error": error_msg},level="ERROR")self.langfuse.flush()return error_msgfinally:# Clean up browser if initializedif self.driver:try:helium.kill_browser()except:passdef evaluate_answer(self, question: str, answer: str, expected_answer: str = None) -> Dict[str, Any]:"""20 unmodified lines# Try to parse JSON responseimport jsontry:scores = json.loads(evaluation_result)return scoresexcept:# Fallback if JSON parsing failsreturn {"accuracy": 3,"completeness": 3,"clarity": 3,"overall": 3,"reasoning": "Could not parse evaluation response","raw_evaluation": evaluation_result}except Exception as e:return {"accuracy": 1,"completeness": 1,"clarity": 1,"overall": 1,"reasoning": f"Evaluation failed: {str(e)}"}def add_user_feedback(self, trace_id: str, feedback_score: int, comment: str = None):"""Add user feedback to a specific trace31 unmodified lines# Example questionquestion_data = {"Question": "How many studio albums Mercedes Sosa has published between 2000-2009?","task_id": ""}2 unmodified linesquestion_data,tags=["music-question", "discography"])print(f"Answer: {answer}")# Evaluate the answerevaluation = agent.evaluate_answer(question_data["Question"],answer)print(f"Evaluation: {evaluation}")4 unmodified lines567891011121 unmodified line141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311427 unmodified lines14214314414514614715 unmodified lines1631641651661671681691701 unmodified line1721731741751761771781791801811821831841851861871881 unmodified line19019119219319419519619719819920020120231 unmodified lines23423523623723823924024124224324424524624724824925056 unmodified lines3073083093103113123133143153163173183193203213223233243253263273282 unmodified lines3313323333343353363373383393403413423433443453463473483493503513521 unmodified line35435535635735835936036136236336436536636720 unmodified lines38838939039139239339439539639739839940040140240340440540640731 unmodified lines4394404414424434444452 unmodified lines4484494504514 unmodified linesfrom langchain.docstore.document import Documentfrom langchain.text_splitter import RecursiveCharacterTextSplitterfrom langchain_community.retrievers import BM25Retrieverfrom smolagents import CodeAgent, OpenAIServerModel, Toolfrom smolagents import PythonInterpreterTool, SpeechToTextTool# Langfuse observability importsfrom opentelemetry.sdk.trace import TracerProvider1 unmodified linefrom opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporterfrom opentelemetry.sdk.trace.export import SimpleSpanProcessorfrom opentelemetry import tracefrom langfuse import Langfusefrom smolagents import SpeechToTextTool, PythonInterpreterToolimport requestsfrom markdownify import markdownifyfrom requests.exceptions import RequestExceptionfrom smolagents import toolimport refrom concurrent.futures import ThreadPoolExecutor, TimeoutErrorclass WebSearchTool(Tool):name = "web_search"description = """Performs a duckduckgo web search based on your query (think a Google search) then returns the top search results."""inputs = {"query": {"type": "string", "description": "The search query to perform."}}output_type = "string"def __init__(self, max_results=10, **kwargs):super().__init__()self.max_results = max_resultstry:from duckduckgo_search import DDGSexcept ImportError as e:raise ImportError("You must install package `duckduckgo_search` to run this tool: for instance run `pip install duckduckgo-search`.") from eself.ddgs = DDGS(**kwargs)def _perform_search(self, query: str):"""Internal method to perform the actual search."""return self.ddgs.text(query, max_results=self.max_results)def forward(self, query: str) -> str:results = []# First attempt with timeoutwith ThreadPoolExecutor(max_workers=1) as executor:try:future = executor.submit(self._perform_search, query)results = future.result(timeout=30) # 30 second timeoutexcept TimeoutError:print("First search attempt timed out after 30 seconds, retrying...")results = []# Retry if no results or timeout occurredif len(results) == 0:print("Retrying search...")with ThreadPoolExecutor(max_workers=1) as executor:try:future = executor.submit(self._perform_search, query)results = future.result(timeout=30) # 30 second timeout for retryexcept TimeoutError:raise Exception("Search timed out after 30 seconds on both attempts. Try a different query.")# Final check for resultsif len(results) == 0:raise Exception("No results found after two attempts! Try a less restrictive/shorter query.")postprocessed_results = [f"[{result['title']}]({result['href']})\n{result['body']}" for result in results]return "## Search Results\n\n" + "\n\n".join(postprocessed_results)@tooldef visit_webpage(url: str) -> str:"""Visits a webpage at the given URL and returns its content as a markdown string.Args:url: The URL of the webpage to visit.Returns:The content of the webpage converted to Markdown, or an error message if the request fails."""try:# Send a GET request to the URLresponse = requests.get(url)response.raise_for_status() # Raise an exception for bad status codes# Parse the content as HTML with BeautifulSoupfrom bs4 import BeautifulSoupsoup = BeautifulSoup(response.content, 'html.parser')# Extract text and convert to Markdowncontent = soup.get_text(separator="\n", strip=True)markdown_content = markdownify(content)# Clean up the markdown contentmarkdown_content = re.sub(r'\n+', '\n', markdown_content) # Remove excessive newlinesmarkdown_content = re.sub(r'\s+', ' ', markdown_content) # Remove excessive spacesmarkdown_content = markdown_content.strip() # Strip leading/trailing whitespacereturn markdown_contentexcept RequestException as e:return f"Error fetching the webpage: {str(e)}"except Exception as e:return f"An unexpected error occurred: {str(e)}"class BM25RetrieverTool(Tool):"""BM25 retriever tool for document search when text documents are available27 unmodified linesfor i, doc in enumerate(docs)])class GAIAAgent:"""GAIA agent using smolagents with Gemini 2.0 Flash and Langfuse observability15 unmodified linesmodel_id="gemini-2.0-flash",api_base="https://generativelanguage.googleapis.com/v1beta/openai/",api_key=gemini_api_key,temperature=0.0,top_p=1.0,)# Store user and session IDs for tracking1 unmodified lineself.session_id = session_id or "gaia-session"# GAIA system prompt from the leaderboardself.system_prompt = """You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.IMPORTANT :- When you need to find information in a document, use the BM25 retriever tool to search for relevant sections.- When you need to find information in a visited web page, do not use the BM25 retriever tool, but instead use the visit_webpage tool to fetch the content of the page, and then use the retrieved content to answer the question.- In the last step of your reasoning, if you think your reasoning is not able to answer the question, answer the question directy with your internal reasoning, without using the BM25 retriever tool or the visit_webpage tool."""# Initialize retriever tool (will be updated when documents are loaded)self.retriever_tool = BM25RetrieverTool()# Create the agentself.agent = Noneself._create_agent()1 unmodified line# Initialize Langfuse clientself.langfuse = Langfuse()from langfuse import get_clientself.langfuse = get_client() # ✅ Use get_client() for v3# Store user and session IDs for trackingself.user_id = user_id or "gaia-user"self.session_id = session_id or "gaia-session"def _setup_langfuse_observability(self):"""Set up Langfuse observability with OpenTelemetry"""# Get Langfuse keys from environment variables31 unmodified lines"""Create the CodeAgent with tools"""base_tools = [self.retriever_tool,visit_webpage,]self.agent = CodeAgent(tools=base_tools + [SpeechToTextTool(),WebSearchTool(),PythonInterpreterTool()],model=self.model,description=self.system_prompt,max_steps=6 )def load_documents_from_file(self, file_path: str):"""Load and process documents from a file for BM25 retrieval"""56 unmodified linesif task_id:trace_tags.append(f"task-{task_id}")# Use SDK v3 context manager approachwith self.langfuse.start_as_current_span(name="GAIA-Question-Solving",input={"question": question, "task_id": task_id},metadata={"model": self.model.model_id,"question_length": len(question),"has_file": bool(task_id)}) as span:try:# Set trace attributes using v3 syntaxspan.update_trace(user_id=self.user_id,session_id=self.session_id,tags=trace_tags)# Download and load file if task_id provided2 unmodified linesfile_path = self.download_gaia_file(task_id)if file_path:file_loaded = self.load_documents_from_file(file_path)print(f"Loaded file for task {task_id}")# Prepare the promptprompt = f"""Question: {question}{f'Task ID: {task_id}' if task_id else ''}{f'File loaded: Yes' if file_loaded else 'File loaded: No'}"""print("=== AGENT REASONING ===")result = self.agent.run(prompt)print("=== END REASONING ===")# Update span with result using v3 syntaxspan.update(output={"answer": str(result)})return str(result)1 unmodified lineerror_msg = f"Error processing question: {str(e)}"print(error_msg)# Log error using v3 syntaxspan.update(output={"error": error_msg},level="ERROR")return error_msgdef evaluate_answer(self, question: str, answer: str, expected_answer: str = None) -> Dict[str, Any]:"""20 unmodified lines# Try to parse JSON responseimport jsonscores = json.loads(evaluation_result)return scoresexcept json.JSONDecodeError:# If JSON parsing fails, return a default structureprint("Failed to parse evaluation result as JSON. Returning default scores.")return {"accuracy": 0,"completeness": 0,"clarity": 0,"overall": 0,"reasoning": "Could not parse evaluation result"}def add_user_feedback(self, trace_id: str, feedback_score: int, comment: str = None):"""Add user feedback to a specific trace31 unmodified lines# Example questionquestion_data = {"Question": "How many studio albums Mercedes Sosa has published between 2000-2009? Search on the English Wikipedia webpage.","task_id": ""}2 unmodified linesquestion_data,tags=["music-question", "discography"])print(f"Answer: {answer}")
9 unmodified lines101112131415161 unmodified line181920212223249 unmodified linesDEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"# Import your custom agent from agent.pyfrom agent import EnhancedGAIAAgent# --- Basic Agent Definition ---# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------1 unmodified linedef __init__(self):print("BasicAgent initialized.")# Initialize your enhanced GAIA agentself.gaia_agent = EnhancedGAIAAgent()async def __call__(self, question: str) -> str:try:9 unmodified lines101112131415161 unmodified line181920212223249 unmodified linesDEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"# Import your custom agent from agent.pyfrom agent2 import GAIAAgent# --- Basic Agent Definition ---# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------1 unmodified linedef __init__(self):print("BasicAgent initialized.")# Initialize your enhanced GAIA agentself.gaia_agent = GAIAAgent()async def __call__(self, question: str) -> str:try:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210import osimport gradio as grimport requestsimport inspectimport pandas as pd
# (Keep Constants as is)# --- Constants ---DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
# Import your custom agent from agent.pyfrom agent2 import GAIAAgent
# --- Basic Agent Definition ---# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------class BasicAgent: def __init__(self): print("BasicAgent initialized.") # Initialize your enhanced GAIA agent self.gaia_agent = GAIAAgent() def __call__(self, question: str) -> str: try: question_data = { "Question": question, "task_id": "basic_agent_task" } answer = self.gaia_agent.solve_gaia_question(question_data) return str(answer) except Exception as e: return e
def run_and_submit_all( profile: gr.OAuthProfile | None): """ Fetches all questions, runs the BasicAgent on them, submits all answers, and displays the results. """ # --- Determine HF Space Runtime URL and Repo URL --- space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
if profile: username= f"{profile.username}" print(f"User logged in: {username}") else: print("User not logged in.") return "Please Login to Hugging Face with the button.", None
api_url = DEFAULT_API_URL questions_url = f"{api_url}/questions" submit_url = f"{api_url}/submit"
# 1. Instantiate Agent ( modify this part to create your agent) try: agent = BasicAgent() except Exception as e: print(f"Error instantiating agent: {e}") return f"Error initializing agent: {e}", None # In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public) #agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" agent_code = "Running on Kaggle" print(agent_code)
# 2. Fetch Questions print(f"Fetching questions from: {questions_url}") try: response = requests.get(questions_url, timeout=15) response.raise_for_status() questions_data = response.json() if not questions_data: print("Fetched questions list is empty.") return "Fetched questions list is empty or invalid format.", None print(f"Fetched {len(questions_data)} questions.") except requests.exceptions.RequestException as e: print(f"Error fetching questions: {e}") return f"Error fetching questions: {e}", None except requests.exceptions.JSONDecodeError as e: print(f"Error decoding JSON response from questions endpoint: {e}") print(f"Response text: {response.text[:500]}") return f"Error decoding server response for questions: {e}", None except Exception as e: print(f"An unexpected error occurred fetching questions: {e}") return f"An unexpected error occurred fetching questions: {e}", None
# 3. Run your Agent results_log = [] answers_payload = [] print(f"Running agent on {len(questions_data)} questions...") for item in questions_data: task_id = item.get("task_id") question_text = item.get("question") print(question_text) if not task_id or question_text is None: print(f"Skipping item with missing task_id or question: {item}") continue try: submitted_answer = agent(question_text) answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer}) results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer}) except Exception as e: print(f"Error running agent on task {task_id}: {e}") results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
if not answers_payload: print("Agent did not produce any answers to submit.") return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
# 4. Prepare Submission submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload} status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..." print(status_update)
# 5. Submit print(f"Submitting {len(answers_payload)} answers to: {submit_url}") try: response = requests.post(submit_url, json=submission_data, timeout=60) response.raise_for_status() result_data = response.json() final_status = ( f"Submission Successful!\n" f"User: {result_data.get('username')}\n" f"Overall Score: {result_data.get('score', 'N/A')}% " f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n" f"Message: {result_data.get('message', 'No message received.')}" ) print("Submission successful.") results_df = pd.DataFrame(results_log) return final_status, results_df except requests.exceptions.HTTPError as e: error_detail = f"Server responded with status {e.response.status_code}." try: error_json = e.response.json() error_detail += f" Detail: {error_json.get('detail', e.response.text)}" except requests.exceptions.JSONDecodeError: error_detail += f" Response: {e.response.text[:500]}" status_message = f"Submission Failed: {error_detail}" print(status_message) results_df = pd.DataFrame(results_log) return status_message, results_df except requests.exceptions.Timeout: status_message = "Submission Failed: The request timed out." print(status_message) results_df = pd.DataFrame(results_log) return status_message, results_df except requests.exceptions.RequestException as e: status_message = f"Submission Failed: Network error - {e}" print(status_message) results_df = pd.DataFrame(results_log) return status_message, results_df except Exception as e: status_message = f"An unexpected error occurred during submission: {e}" print(status_message) results_df = pd.DataFrame(results_log) return status_message, results_df
# --- Build Gradio Interface using Blocks ---with gr.Blocks() as demo: gr.Markdown("# Basic Agent Evaluation Runner") gr.Markdown( """ **Instructions:**
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ... 2. Log in to your Hugging Face account using the button below. This uses your HF username for submission. 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
--- **Disclaimers:** Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions). This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a seperate action or even to answer the questions in async. """ )
gr.LoginButton()
run_button = gr.Button("Run Evaluation & Submit All Answers")
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False) # Removed max_rows=10 from DataFrame constructor results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
run_button.click( fn=run_and_submit_all, outputs=[status_output, results_table] )
if __name__ == "__main__": print("\n" + "-"*30 + " App Starting " + "-"*30) # Check for SPACE_HOST and SPACE_ID at startup for information #space_host_startup = os.getenv("SPACE_HOST") #space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
#if space_host_startup: #print(f"✅ SPACE_HOST found: {space_host_startup}") #print(f" Runtime URL should be: https://{space_host_startup}.hf.space") #else: #print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
#if space_id_startup: # Print repo URLs if SPACE_ID is found #print(f"✅ SPACE_ID found: {space_id_startup}") #print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}") #print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main") #else: #print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
print("-"*(60 + len(" App Starting ")) + "\n")
print("Launching Gradio Interface for Basic Agent Evaluation...") demo.launch(debug=True, share=True)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404from typing import Optional, List, Anyfrom pydantic import Field, PrivateAttrfrom llama_index.core.llms import CustomLLM, CompletionResponse, CompletionResponseGen, LLMMetadatafrom llama_index.core.llms.callbacks import llm_completion_callbackfrom transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessorfrom qwen_vl_utils import process_vision_infoimport torchfrom typing import Any, List, Optionalfrom llama_index.core.embeddings import BaseEmbeddingfrom sentence_transformers import SentenceTransformerfrom PIL import Image class QwenVL7BCustomLLM(CustomLLM): model_name: str = Field(default="Qwen/Qwen2.5-VL-7B-Instruct") context_window: int = Field(default=32768) num_output: int = Field(default=256) _model = PrivateAttr() _processor = PrivateAttr()
def __init__(self, **kwargs): super().__init__(**kwargs) self._model = Qwen2_5_VLForConditionalGeneration.from_pretrained( self.model_name, torch_dtype=torch.bfloat16, device_map='balanced' ) self._processor = AutoProcessor.from_pretrained(self.model_name)
@property def metadata(self) -> LLMMetadata: return LLMMetadata( context_window=self.context_window, num_output=self.num_output, model_name=self.model_name, )
@llm_completion_callback() def complete( self, prompt: str, image_paths: Optional[List[str]] = None, **kwargs: Any ) -> CompletionResponse: # Prepare multimodal input messages = [{"role": "user", "content": []}] if image_paths: for path in image_paths: messages[0]["content"].append({"type": "image", "image": path}) messages[0]["content"].append({"type": "text", "text": prompt})
# Tokenize and process text = self._processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) image_inputs, video_inputs = process_vision_info(messages) inputs = self._processor( text=[text], images=image_inputs, videos=video_inputs, padding=True, return_tensors="pt", ) inputs = inputs.to(self._model.device)
# Generate output generated_ids = self._model.generate(**inputs, max_new_tokens=self.num_output) generated_ids_trimmed = [out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)] output_text = self._processor.batch_decode( generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False )[0] return CompletionResponse(text=output_text)
@llm_completion_callback() def stream_complete( self, prompt: str, image_paths: Optional[List[str]] = None, **kwargs: Any ) -> CompletionResponseGen: response = self.complete(prompt, image_paths) for token in response.text: yield CompletionResponse(text=token, delta=token)
class MultimodalCLIPEmbedding(BaseEmbedding): """ Custom embedding class using CLIP for multimodal capabilities. """ def __init__(self, model_name: str = "clip-ViT-B-32", **kwargs: Any) -> None: super().__init__(**kwargs) self._model = SentenceTransformer(model_name) @classmethod def class_name(cls) -> str: return "multimodal_clip" def _get_query_embedding(self, query: str, image_path: Optional[str] = None) -> List[float]: if image_path: image = Image.open(image_path) embedding = self._model.encode(image) return embedding.tolist() else: embedding = self._model.encode(query) return embedding.tolist() def _get_text_embedding(self, text: str, image_path: Optional[str] = None) -> List[float]: if image_path: image = Image.open(image_path) embedding = self._model.encode(image) return embedding.tolist() else: embedding = self._model.encode(text) return embedding.tolist() def _get_text_embeddings(self, texts: List[str], image_paths: Optional[List[str]] = None) -> List[List[float]]: embeddings = [] image_paths = image_paths or [None] * len(texts) for text, img_path in zip(texts, image_paths): if img_path: image = Image.open(img_path) emb = self._model.encode(image) else: emb = self._model.encode(text) embeddings.append(emb.tolist()) return embeddings async def _aget_query_embedding(self, query: str, image_path: Optional[str] = None) -> List[float]: return self._get_query_embedding(query, image_path) async def _aget_text_embedding(self, text: str, image_path: Optional[str] = None) -> List[float]: return self._get_text_embedding(text, image_path) # BAAI embedding class# To run on Terminal before running the app, you need to install the FlagEmbedding package.# This can be done by cloning the repository and installing it in editable mode.#!git clone https://github.com/FlagOpen/FlagEmbedding.git#cd FlagEmbedding/research/visual_bge#pip install -e .#go back to the app directory#cd ../../..
class BaaiMultimodalEmbedding(BaseEmbedding): """ Custom embedding class using BAAI's FlagEmbedding for multimodal capabilities. Implements the visual_bge Visualized_BGE model with bge-m3 backend. """
def __init__(self, model_name_bge: str = "BAAI/bge-m3", model_weight: str = "Visualized_m3.pth", device: str = "cuda:1", **kwargs: Any) -> None: super().__init__(**kwargs)
# Set device self.device = torch.device(device if torch.cuda.is_available() else "cpu") print(f"BaaiMultimodalEmbedding initializing on device: {self.device}")
# Import the visual_bge module from visual_bge.modeling import Visualized_BGE self._model = Visualized_BGE( model_name_bge=model_name_bge, model_weight=model_weight ) self._model.to(self.device) self._model.eval() print(f"Successfully loaded BAAI Visualized_BGE with {model_name_bge}")
@classmethod def class_name(cls) -> str: return "baai_multimodal"
def _get_query_embedding(self, query: str, image_path: Optional[str] = None) -> List[float]: """Get embedding for query with optional image""" with torch.no_grad(): if hasattr(self._model, 'encode') and hasattr(self._model, 'preprocess_val'): # Using visual_bge if image_path and query: # Combined text and image query embedding = self._model.encode(image=image_path, text=query) elif image_path: # Image only embedding = self._model.encode(image=image_path) else: # Text only embedding = self._model.encode(text=query) else: # Fallback to sentence-transformers if image_path: from PIL import Image image = Image.open(image_path) embedding = self._model.encode(image) else: embedding = self._model.encode(query)
return embedding.cpu().numpy().tolist() if torch.is_tensor(embedding) else embedding.tolist()
def _get_text_embedding(self, text: str, image_path: Optional[str] = None) -> List[float]: """Get embedding for text with optional image""" return self._get_query_embedding(text, image_path)
def _get_text_embeddings(self, texts: List[str], image_paths: Optional[List[str]] = None) -> List[List[float]]: """Get embeddings for multiple texts with optional images""" embeddings = [] image_paths = image_paths or [None] * len(texts)
for text, img_path in zip(texts, image_paths): emb = self._get_text_embedding(text, img_path) embeddings.append(emb) return embeddings
async def _aget_query_embedding(self, query: str, image_path: Optional[str] = None) -> List[float]: return self._get_query_embedding(query, image_path)
async def _aget_text_embedding(self, text: str, image_path: Optional[str] = None) -> List[float]: return self._get_text_embedding(text, image_path)
class PixtralQuantizedLLM(CustomLLM): """ Pixtral 12B quantized model implementation for Kaggle compatibility. Uses float8 quantization for memory efficiency. """
model_name: str = Field(default="mistralai/Pixtral-12B-2409") context_window: int = Field(default=128000) num_output: int = Field(default=512) quantization: str = Field(default="fp8") _model = PrivateAttr() _processor = PrivateAttr()
def __init__(self, **kwargs): super().__init__(**kwargs)
# Check if we're in a Kaggle environment or have limited resources import psutil available_memory = psutil.virtual_memory().available / (1024**3) # GB
if available_memory < 20: # Less than 20GB RAM print(f"Limited memory detected ({available_memory:.1f}GB), using quantized version") self._load_quantized_model() else: print("Sufficient memory available, attempting full model load") try: self._load_full_model() except Exception as e: print(f"Full model loading failed: {e}, falling back to quantized") self._load_quantized_model()
def _load_quantized_model(self): """Load quantized Pixtral model for resource-constrained environments""" try: # Try to use a pre-quantized version from HuggingFace quantized_models = [ "RedHatAI/pixtral-12b-FP8-dynamic" ]
model_loaded = False for model_id in quantized_models: try: print(f"Attempting to load quantized model: {model_id}")
# Standard quantized model loading from transformers import AutoModelForCausalLM, AutoProcessor self._model = AutoModelForCausalLM.from_pretrained( model_id, torch_dtype=torch.float8, device_map="auto", trust_remote_code=True ) self._processor = AutoProcessor.from_pretrained(model_id)
print(f"Successfully loaded quantized Pixtral: {model_id}") model_loaded = True break
except Exception as e: print(f"Failed to load {model_id}: {e}") continue
if not model_loaded: print("All quantized models failed, using CPU-only fallback") self._load_cpu_fallback()
except Exception as e: print(f"Quantized loading failed: {e}") self._load_cpu_fallback()
def _load_full_model(self): """Load full Pixtral model""" from transformers import AutoModelForCausalLM, AutoProcessor
self._model = AutoModelForCausalLM.from_pretrained( self.model_name, torch_dtype=torch.bfloat16, device_map="auto", trust_remote_code=True ) self._processor = AutoProcessor.from_pretrained(self.model_name)
def _load_cpu_fallback(self): """Fallback to CPU-only inference""" try: from transformers import AutoModelForCausalLM, AutoProcessor
self._model = AutoModelForCausalLM.from_pretrained( "microsoft/DialoGPT-medium", # Smaller fallback model torch_dtype=torch.float32, device_map="cpu" ) self._processor = AutoProcessor.from_pretrained("microsoft/DialoGPT-medium") print("Using CPU fallback model (DialoGPT-medium)")
except Exception as e: print(f"CPU fallback failed: {e}") # Use a minimal implementation self._model = None self._processor = None
@property def metadata(self) -> LLMMetadata: return LLMMetadata( context_window=self.context_window, num_output=self.num_output, model_name=f"{self.model_name}-{self.quantization}", )
@llm_completion_callback() def complete( self, prompt: str, image_paths: Optional[List[str]] = None, **kwargs: Any ) -> CompletionResponse:
if self._model is None: return CompletionResponse(text="Model not available in current environment")
try: # Prepare multimodal input if images provided if image_paths and hasattr(self._processor, 'apply_chat_template'): # Handle multimodal input messages = [{"role": "user", "content": []}]
if image_paths: for path in image_paths[:4]: # Limit to 4 images for memory messages[0]["content"].append({"type": "image", "image": path})
messages[0]["content"].append({"type": "text", "text": prompt})
# Process the input inputs = self._processor(messages, return_tensors="pt", padding=True) inputs = {k: v.to(self._model.device) for k, v in inputs.items()}
# Generate with torch.no_grad(): outputs = self._model.generate( **inputs, max_new_tokens=min(self.num_output, 256), # Limit for memory do_sample=True, temperature=0.7, pad_token_id=self._processor.tokenizer.eos_token_id )
# Decode response response = self._processor.batch_decode(outputs, skip_special_tokens=True)[0] # Extract only the new generated part if len(messages[0]["content"]) > 0: response = response.split(prompt)[-1].strip()
else: # Text-only fallback inputs = self._processor(prompt, return_tensors="pt", padding=True) inputs = {k: v.to(self._model.device) for k, v in inputs.items()}
with torch.no_grad(): outputs = self._model.generate( **inputs, max_new_tokens=min(self.num_output, 256), do_sample=True, temperature=0.7, pad_token_id=self._processor.tokenizer.eos_token_id )
response = self._processor.batch_decode(outputs, skip_special_tokens=True)[0] response = response.replace(prompt, "").strip()
return CompletionResponse(text=response)
except Exception as e: error_msg = f"Generation error: {str(e)}" print(error_msg) return CompletionResponse(text=error_msg)
@llm_completion_callback() def stream_complete( self, prompt: str, image_paths: Optional[List[str]] = None, **kwargs: Any ) -> CompletionResponseGen: # For quantized models, streaming might not be efficient # Return the complete response as a single chunk response = self.complete(prompt, image_paths, **kwargs) yield response.DS_StoreAdded
.gradio/certificate.pemAdded+31
.vscode/launch.jsonAdded+15
__pycache__/agent.cpython-311.pycAdded
__pycache__/agent2.cpython-311.pycAdded
agent.py+1-132
agent2.py+154-288
app.py+2-2
appasync.pyAdded+210
custom_models.pyAdded+404