def _detect_and_process_direct_attachments(self, file_name: str) -> Tuple[List[str], List[str], List[str]]: """ Detect and process a single attachment directly attached to a question (not as a URL). Returns (image_files, audio_files, code_files) """ image_files = [] audio_files = [] code_files = [] if not file_name: return image_files, audio_files, code_files try: # Construct the file path (assuming file is in current directory) file_path = os.path.join(os.getcwd(), file_name) # Check if file exists if not os.path.exists(file_path): if self.debug: print(f"File not found: {file_path}") return image_files, audio_files, code_files # Get file extension file_ext = Path(file_name).suffix.lower() # Determine category is_image = ( file_ext in ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp', '.tiff'] ) is_audio = ( file_ext in ['.mp3', '.wav', '.m4a', '.ogg', '.flac', '.aac'] ) is_code = ( file_ext in ['.py', '.txt', '.js', '.html', '.css', '.json', '.xml', '.md', '.c', '.cpp', '.java'] ) # Categorize the file if is_image: image_files.append(file_path) elif is_audio: audio_files.append(file_path) elif is_code: code_files.append(file_path) else: # Default to code/text for unknown types code_files.append(file_path) if self.debug: print(f"Processed file: {file_name} -> {'image' if is_image else 'audio' if is_audio else 'code'}") except Exception as e: if self.debug: print(f"Error processing attachment {file_name}: {e}") if self.debug: print(f"Processed attachment: {len(image_files)} images, {len(audio_files)} audio, {len(code_files)} code files") return image_files, audio_files, code_files def process_question_with_attachments(self, question_data: dict) -> str: """ Process a question that may have attachments and URLs. """ question_text = question_data.get('question', '') if self.debug: print(f"Question data keys: {list(question_data.keys())}") print(f"\n1. Processing question with potential attachments and URLs: {question_text[:300]}...") try: # Detect and process URLs if self.debug: print(f"2. Detecting and processing URLs...") url_context = self._extract_and_process_urls(question_text) if self.debug and url_context: print(f"URL context found: {len(url_context)} characters") except Exception as e: if self.debug: print(f"Error extracting URLs: {e}") url_context = "" try: # Detect and download attachments if self.debug: print(f"3. Searching for images, audio or code attachments...") attachment_name = question_data.get('file_name', '') if self.debug: print(f"Attachment name from question_data: '{attachment_name}'") image_files, audio_files, code_files = self._detect_and_process_direct_attachments(attachment_name) # Process attachments to get context attachment_context = self._process_attachments(image_files, audio_files, code_files) if self.debug and attachment_context: print(f"Attachment context: {attachment_context[:200]}...") # Decide whether to search if self._should_search(question_text, attachment_context, url_context): if self.debug: print("5. Using search-based approach") answer = self._answer_with_search(question_text, attachment_context, url_context) else: if self.debug: print("5. Using LLM-only approach") answer = self._answer_with_llm(question_text, attachment_context, url_context) if self.debug: print(f"LLM answer: {answer}") # Note: We don't cleanup files here since they're not temporary files we created # They are actual files in the working directory except Exception as e: if self.debug: print(f"Error in attachment processing: {e}") answer = f"Sorry, I encountered an error: {e}" if self.debug: print(f"6. Agent returning answer: {answer[:100]}...") return answer def fetch_questions() -> Tuple[str, Optional[pd.DataFrame]]: """ Fetch questions from the API and cache them. """ global cached_questions api_url = DEFAULT_API_URL questions_url = f"{api_url}/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: return "Fetched questions list is empty.", None cached_questions = questions_data # Create DataFrame for display display_data = [] for item in questions_data: # Check for attachments has_attachments = False attachment_info = "" # Check various fields for attachments attachment_fields = ['attachments', 'files', 'media', 'resources'] for field in attachment_fields: if field in item and item[field]: has_attachments = True if isinstance(item[field], list): attachment_info += f"{len(item[field])} {field}, " else: attachment_info += f"{field}, " # Check if question contains URLs question_text = item.get("question", "") if 'http' in question_text: has_attachments = True attachment_info += "URLs in text, " if attachment_info: attachment_info = attachment_info.rstrip(", ") display_data.append({ "Task ID": item.get("task_id", "Unknown"), "Question": question_text[:100] + "..." if len(question_text) > 100 else question_text, "Has Attachments": "Yes" if has_attachments else "No", "Attachment Info": attachment_info }) df = pd.DataFrame(display_data) attachment_count = sum(1 for item in display_data if item["Has Attachments"] == "Yes") status_msg = f"Successfully fetched {len(questions_data)} questions. {attachment_count} questions have attachments. Ready to generate answers." return status_msg, df except requests.exceptions.RequestException as e: return f"Error fetching questions: {e}", None except Exception as e: return f"An unexpected error occurred: {e}", None def generate_answers_async(model_name: str = "meta-llama/Llama-3.1-8B-Instruct", progress_callback=None): """ Generate answers for all cached questions asynchronously using the intelligent agent. """ global cached_answers, processing_status if not cached_questions: return "No questions available. Please fetch questions first." processing_status["is_processing"] = True processing_status["progress"] = 0 processing_status["total"] = len(cached_questions) try: agent = IntelligentAgent(debug=True, model_name=model_name) cached_answers = {} for i, question_data in enumerate(cached_questions): if not processing_status["is_processing"]: # Check if cancelled break task_id = question_data.get("task_id") question_text = question_data.get("question") if not task_id or question_text is None: continue try: # Use the new method that handles attachments answer = agent.process_question_with_attachments(question_data) cached_answers[task_id] = { "question": question_text, "answer": answer } except Exception as e: cached_answers[task_id] = { "question": question_text, "answer": f"AGENT ERROR: {e}" } processing_status["progress"] = i + 1 if progress_callback: progress_callback(i + 1, len(cached_questions)) except Exception as e: print(f"Error in generate_answers_async: {e}") finally: processing_status["is_processing"] = False def start_answer_generation(model_choice: str): """ Start the answer generation process in a separate thread. """ if processing_status["is_processing"]: return "Answer generation is already in progress." if not cached_questions: return "No questions available. Please fetch questions first." # Map model choice to actual model name model_map = { "Llama 3.1 8B": "meta-llama/Llama-3.1-8B-Instruct", "Llama 3.3 70B": "meta-llama/Llama-3.3-70B-Instruct", "Llama 3.3 Shallow 70B": "tokyotech-llm/Llama-3.3-Swallow-70B-Instruct-v0.4", "Mistral 7B": "mistralai/Mistral-7B-Instruct-v0.3", "Qwen 2.5": "Qwen/Qwen‑2.5‑Omni‑7B", #"Qwen 2.5 instruct": "Qwen/Qwen2.5-14B-Instruct-1M", "Qwen 3": "Qwen/Qwen3-32B" } selected_model = model_map.get(model_choice, "meta-llama/Llama-3.1-8B-Instruct") # Start generation in background thread thread = threading.Thread(target=generate_answers_async, args=(selected_model,)) thread.daemon = True thread.start() return f"Answer generation started using {model_choice}. Check progress." def get_generation_progress(): """ Get the current progress of answer generation. """ if not processing_status["is_processing"] and processing_status["progress"] == 0: return "Not started" if processing_status["is_processing"]: progress = processing_status["progress"] total = processing_status["total"] status_msg = f"Generating answers... {progress}/{total} completed" return status_msg else: # Generation completed if cached_answers: # Create DataFrame with results display_data = [] for task_id, data in cached_answers.items(): display_data.append({ "Task ID": task_id, "Question": data["question"][:100] + "..." if len(data["question"]) > 100 else data["question"], "Generated Answer": data["answer"][:200] + "..." if len(data["answer"]) > 200 else data["answer"] }) df = pd.DataFrame(display_data) status_msg = f"Answer generation completed! {len(cached_answers)} answers ready for submission." return status_msg, df else: return "Answer generation completed but no answers were generated." def submit_cached_answers(profile: gr.OAuthProfile | None): """ Submit the cached answers to the evaluation API. """ global cached_answers if not profile: return "Please log in to Hugging Face first.", None if not cached_answers: return "No cached answers available. Please generate answers first.", None username = profile.username space_id = os.getenv("SPACE_ID") agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "Unknown" # Prepare submission payload answers_payload = [] for task_id, data in cached_answers.items(): answers_payload.append({ "task_id": task_id, "submitted_answer": data["answer"] }) submission_data = { "username": username.strip(), "agent_code": agent_code, "answers": answers_payload } # Submit to API api_url = DEFAULT_API_URL submit_url = f"{api_url}/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.')}" ) # Create results DataFrame results_log = [] for task_id, data in cached_answers.items(): results_log.append({ "Task ID": task_id, "Question": data["question"], "Submitted Answer": data["answer"] }) 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: error_detail += f" Response: {e.response.text[:500]}" return f"Submission Failed: {error_detail}", None except requests.exceptions.Timeout: return "Submission Failed: The request timed out.", None except Exception as e: return f"Submission Failed: {e}", None def clear_cache(): """ Clear all cached data. """ global cached_answers, cached_questions, processing_status cached_answers = {} cached_questions = [] processing_status = {"is_processing": False, "progress": 0, "total": 0} return "Cache cleared successfully.", None # --- Enhanced Gradio Interface --- with gr.Blocks(title="Intelligent Agent with Media Processing") as demo: gr.Markdown("# Intelligent Agent with Conditional Search and Media Processing") gr.Markdown("This agent can process images and audio files, uses an LLM to decide when search is needed, optimizing for both accuracy and efficiency.") with gr.Row(): gr.LoginButton() clear_btn = gr.Button("Clear Cache", variant="secondary") with gr.Tab("Step 1: Fetch Questions"): gr.Markdown("### Fetch Questions from API") fetch_btn = gr.Button("Fetch Questions", variant="primary") fetch_status = gr.Textbox(label="Fetch Status", lines=2, interactive=False) questions_table = gr.DataFrame(label="Available Questions", wrap=True) fetch_btn.click( fn=fetch_questions, outputs=[fetch_status, questions_table] ) with gr.Tab("Step 2: Generate Answers"): gr.Markdown("### Generate Answers with Intelligent Search Decision") with gr.Row(): model_choice = gr.Dropdown( choices=["Llama 3.1 8B", "Llama 3.3 70B", "Llama 3.3 Shallow 70B", "Mistral 7B", "Qwen 2.5", "Qwen 3"], value="Llama 3.1 8B", label="Select Model" ) generate_btn = gr.Button("Start Answer Generation", variant="primary") refresh_btn = gr.Button("Refresh Progress", variant="secondary") generation_status = gr.Textbox(label="Generation Status", lines=2, interactive=False) answers_table = gr.DataFrame(label="Generated Answers", wrap=True) generate_btn.click( fn=start_answer_generation, inputs=[model_choice], outputs=generation_status ) refresh_btn.click( fn=get_generation_progress, outputs=[generation_status, answers_table] ) with gr.Tab("Step 3: Submit Results"): gr.Markdown("### Submit Generated Answers") submit_btn = gr.Button("Submit Answers", variant="primary") submit_status = gr.Textbox(label="Submission Status", lines=4, interactive=False) results_table = gr.DataFrame(label="Submission Results", wrap=True) submit_btn.click( fn=submit_cached_answers, outputs=[submit_status, results_table] ) # Clear cache functionality clear_btn.click( fn=clear_cache, outputs=[fetch_status, questions_table] ) if __name__ == "__main__": demo.launch()