Update app.py
Browse files4 unmodified lines56789101112131415161718192021222324252627287 unmodified lines363738394041421 unmodified line4445464748495051525354554 unmodified linesimport pandas as pdfrom smolagents import CodeAgent, DuckDuckGoSearchTool, OpenAIServerModel# (Keep Constants as is)# --- Constants ---DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"# --- Basic Agent Definition ---# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------class BasicAgent:def __init__(self, GOOGLE_API_KEY):self.GOOGLE_API_KEY = GOOGLE_API_KEYprint("BasicAgent initialized.")# Initialize the model#model = HfApiModel()model = OpenAIServerModel(model_id="gemini-2.5-flash",# Google Gemini OpenAI-compatible API base URLapi_base="https://generativelanguage.googleapis.com/v1beta/openai/",api_key=GOOGLE_API_KEY)# Initialize the search toolsearch_tool = DuckDuckGoSearchTool()# Initialize Agent7 unmodified linesprint(f"Agent returning fixed answer: {fixed_answer}")return fixed_answerdef run_and_submit_all(profile: gr.OAuthProfile | None, openai_key: str):"""Fetches all questions, runs the BasicAgent on them, submits all answers,and displays the results.1 unmodified line# --- Determine HF Space Runtime URL and Repo URL ---space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the codeif 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"4 unmodified lines5678910111213141516171819202122232425262728293031327 unmodified lines404142434445461 unmodified line4849505152535455565758596061626364656667684 unmodified linesimport pandas as pdfrom smolagents import CodeAgent, DuckDuckGoSearchTool, OpenAIServerModel# (Keep Constants as is)from dotenv import load_dotenvload_dotenv()# --- Constants ---DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"if os.getenv('HF_TOKEN'):os.environ['HF_TOKEN'] = os.getenv('HF_TOKEN')else:print("HF_TOKEN environment variable not set!")# --- Basic Agent Definition ---S# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------class BasicAgent:def __init__(self, api_key):self.api_key = api_keyprint("BasicAgent initialized.")# Initialize the model#model = HfApiModel()model = OpenAIServerModel(model_id="gemma2:latest",api_base="http://localhost:11434/v1/",api_key=api_key if api_key else "dummy-key-for-ollama")# Initialize the search toolsearch_tool = DuckDuckGoSearchTool()# Initialize Agent7 unmodified linesprint(f"Agent returning fixed answer: {fixed_answer}")return fixed_answerdef run_and_submit_all(openai_key: str):"""Fetches all questions, runs the BasicAgent on them, submits all answers,and displays the results.1 unmodified line# --- Determine HF Space Runtime URL and Repo URL ---space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code# Get user info from Gradio's login statetry:import gradio as gruser_info = gr.user_info()if user_info and hasattr(user_info, 'username'):username = user_info.usernameprint(f"User logged in: {username}")else:print("User not logged in.")return "Please Login to Hugging Face with the button.", Noneexcept Exception as e:print(f"Error getting user info: {e}")# Fallback: try to get username from environment or use a defaultusername = os.getenv("USER", "anonymous")print(f"Using fallback username: {username}")api_url = DEFAULT_API_URLquestions_url = f"{api_url}/questions"
app.py+29-16