Spaces:
Sleeping
Sleeping
feat: implement dynamic environment detection to handle local and Hugging Face deployment logic
Browse files14 unmodified lines15161718192021222324119 unmodified lines1441451461471481491501511521531541551561571581595 unmodified lines16516616716816917017117217317417531 unmodified lines207208209210211212213214215216217218103 unmodified lines32232332432532632732832933014 unmodified linesfrom langgraph.prebuilt import tools_condition, ToolNodefrom langchain_core.messages import AnyMessage, HumanMessage, SystemMessage, ToolMessagefrom langchain_groq import ChatGroq# --- LOCAL RUN SETTINGS (Using langchain_ollama for local Ollama support) ---from langchain_ollama import ChatOllama# --- HUGGINGFACE SETTINGS (Uncomment for HF if needed, but langchain_ollama is preferred) ---# from langchain_community.chat_models import ChatOllama# (Keep Constants as is)# --- Constants ---119 unmodified linesFetches all questions, runs the BasicAgent on them, submits all answers,and displays the results."""# --- LOCAL RUN SETTINGS ---# Set a default username for local testingusername = "local_user"# --- HUGGINGFACE SETTINGS (Comment out above and uncomment below for HF) ---# if profile:# username = f"{profile.username}"# print(f"{username} logged in")# else:# return "Please login to Hugging Face", Noneapi_url = DEFAULT_API_URLquestions_url = f"{api_url}/questions"5 unmodified linesexcept Exception as e:print(f"Error instantiating agent: {e}")return f"Error initializing agent: {e}", None# --- LOCAL RUN SETTINGS ---space_id = os.getenv("SPACE_ID", "local-test-space")agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id != "local-test-space" else "local-execution"# --- END LOCAL RUN SETTINGS ---print(f"Agent code link: {agent_code}")# 2. Fetch Questions31 unmodified linesprint(f"Skipping item with missing task_id or question: {item}")continue# --- LOCAL RUN SETTINGS: Skip questions with files ---if file_name:print(f"Skipping task {task_id} because it requires a file: {file_name}")results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": "SKIPPED (Local run - No files)"})continue# --- END LOCAL RUN SETTINGS ---try:submitted_answer = agent(question_text, file_name)103 unmodified lines""")# --- LOCAL RUN SETTINGS (Comment out for HF) ---# gr.LoginButton()# --- END LOCAL RUN SETTINGS ---run_button = gr.Button("Run Full Evaluation & Submit All Answers", variant="primary")14 unmodified lines15161718192021222324252627282930119 unmodified lines1501511521531541551561571581591601611621631641655 unmodified lines17117217317417517617717817918018118218331 unmodified lines215216217218219220221222223224225103 unmodified lines32933033133233333433533614 unmodified linesfrom langgraph.prebuilt import tools_condition, ToolNodefrom langchain_core.messages import AnyMessage, HumanMessage, SystemMessage, ToolMessagefrom langchain_groq import ChatGroq# Detect if running locally or on Hugging Face SpacesIS_LOCAL = os.getenv("SPACE_ID") is Noneif IS_LOCAL:from langchain_ollama import ChatOllamaelse:try:from langchain_ollama import ChatOllamaexcept ImportError:from langchain_community.chat_models import ChatOllama# (Keep Constants as is)# --- Constants ---119 unmodified linesFetches all questions, runs the BasicAgent on them, submits all answers,and displays the results."""if IS_LOCAL:username = "local_user"print(f"Running in LOCAL MODE. Using default username: {username}")else: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.", Noneapi_url = DEFAULT_API_URLquestions_url = f"{api_url}/questions"5 unmodified linesexcept Exception as e:print(f"Error instantiating agent: {e}")return f"Error initializing agent: {e}", Noneif IS_LOCAL:space_id = "local-test-space"agent_code = "local-execution"else:space_id = os.getenv("SPACE_ID")agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"print(f"Agent code link: {agent_code}")# 2. Fetch Questions31 unmodified linesprint(f"Skipping item with missing task_id or question: {item}")continue# Skip questions with files if running locallyif IS_LOCAL and file_name:print(f"Skipping task {task_id} because it requires a file: {file_name}")results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": "SKIPPED (Local run - No files)"})continuetry:submitted_answer = agent(question_text, file_name)103 unmodified lines""")if not IS_LOCAL:gr.LoginButton()run_button = gr.Button("Run Full Evaluation & Submit All Answers", variant="primary")
app.py+31-25