Spaces:
Running
Running
| import yaml | |
| import os | |
| # Ultimate Gradio compatibility fix - dynamically filter kwargs based on component signatures | |
| import gradio as gr | |
| from functools import wraps | |
| import inspect | |
| from huggingface_hub import login | |
| from dotenv import load_dotenv | |
| import os | |
| load_dotenv() | |
| def patch_gradio_component(component_class): | |
| """Patch a Gradio component to accept only supported kwargs""" | |
| original_init = component_class.__init__ | |
| def patched_init(self, *args, **kwargs): | |
| try: | |
| sig = inspect.signature(original_init) | |
| # Filter kwargs to only include parameters the original __init__ accepts | |
| valid_params = set(sig.parameters.keys()) | |
| filtered_kwargs = {k: v for k, v in kwargs.items() if k in valid_params or | |
| any(p.kind == inspect.Parameter.VAR_KEYWORD for p in sig.parameters.values())} | |
| return original_init(self, *args, **filtered_kwargs) | |
| except Exception as e: | |
| # Fallback to original call | |
| try: | |
| return original_init(self, *args, **kwargs) | |
| except TypeError: | |
| # Last resort - call with no kwargs | |
| return original_init(self, *args) | |
| component_class.__init__ = patched_init | |
| return component_class | |
| # Patch all Gradio components | |
| for name in dir(gr): | |
| try: | |
| obj = getattr(gr, name) | |
| if inspect.isclass(obj) and hasattr(obj, '__init__'): | |
| patch_gradio_component(obj) | |
| except: | |
| pass | |
| from smolagents import GradioUI, CodeAgent, InferenceClientModel | |
| # Get current directory path | |
| CURRENT_DIR = os.path.dirname(os.path.abspath(__file__)) | |
| from tools.web_search import DuckDuckGoSearchTool as WebSearch | |
| from tools.suggest_menu import SimpleTool as SuggestMenu | |
| from tools.final_answer import FinalAnswerTool as FinalAnswer | |
| # Authenticate with Hugging Face using HF_TOKEN from environment | |
| if hf_token := os.getenv('HF_TOKEN'): | |
| login(hf_token) | |
| model = InferenceClientModel( | |
| model_id='Qwen/Qwen3-Next-80B-A3B-Thinking', | |
| api_key=os.getenv('HF_TOKEN'), | |
| ) | |
| web_search = WebSearch() | |
| suggest_menu = SuggestMenu() | |
| final_answer = FinalAnswer() | |
| with open(os.path.join(CURRENT_DIR, "prompts.yaml"), 'r') as stream: | |
| prompt_templates = yaml.safe_load(stream) | |
| agent = CodeAgent( | |
| model=model, | |
| tools=[web_search, suggest_menu], | |
| managed_agents=[], | |
| max_steps=20, | |
| verbosity_level=1, | |
| planning_interval=None, | |
| name=None, | |
| description=None, | |
| executor_type='local', | |
| executor_kwargs={}, | |
| max_print_outputs_length=None, | |
| prompt_templates=prompt_templates | |
| ) | |
| if __name__ == "__main__": | |
| GradioUI(agent).launch() | |