Update app.py
Browse files
app.py
CHANGED
|
@@ -5,24 +5,28 @@ import inspect
|
|
| 5 |
import pandas as pd
|
| 6 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, OpenAIServerModel
|
| 7 |
# (Keep Constants as is)
|
|
|
|
|
|
|
| 8 |
# --- Constants ---
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
# --- Basic Agent Definition ---
|
| 13 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 14 |
class BasicAgent:
|
| 15 |
-
def __init__(self,
|
| 16 |
-
self.
|
| 17 |
print("BasicAgent initialized.")
|
| 18 |
# Initialize the model
|
| 19 |
#model = HfApiModel()
|
| 20 |
model = OpenAIServerModel(
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
)
|
| 26 |
# Initialize the search tool
|
| 27 |
search_tool = DuckDuckGoSearchTool()
|
| 28 |
# Initialize Agent
|
|
@@ -36,7 +40,7 @@ class BasicAgent:
|
|
| 36 |
print(f"Agent returning fixed answer: {fixed_answer}")
|
| 37 |
return fixed_answer
|
| 38 |
|
| 39 |
-
def run_and_submit_all(
|
| 40 |
"""
|
| 41 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 42 |
and displays the results.
|
|
@@ -44,12 +48,21 @@ def run_and_submit_all(profile: gr.OAuthProfile | None, openai_key: str):
|
|
| 44 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 45 |
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
api_url = DEFAULT_API_URL
|
| 55 |
questions_url = f"{api_url}/questions"
|
|
|
|
| 5 |
import pandas as pd
|
| 6 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, OpenAIServerModel
|
| 7 |
# (Keep Constants as is)
|
| 8 |
+
from dotenv import load_dotenv
|
| 9 |
+
load_dotenv()
|
| 10 |
# --- Constants ---
|
| 11 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 12 |
+
if os.getenv('HF_TOKEN'):
|
| 13 |
+
os.environ['HF_TOKEN'] = os.getenv('HF_TOKEN')
|
| 14 |
+
else:
|
| 15 |
+
print("HF_TOKEN environment variable not set!")
|
| 16 |
|
| 17 |
+
# --- Basic Agent Definition ---S
|
|
|
|
| 18 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 19 |
class BasicAgent:
|
| 20 |
+
def __init__(self, api_key):
|
| 21 |
+
self.api_key = api_key
|
| 22 |
print("BasicAgent initialized.")
|
| 23 |
# Initialize the model
|
| 24 |
#model = HfApiModel()
|
| 25 |
model = OpenAIServerModel(
|
| 26 |
+
model_id="gemma2:latest",
|
| 27 |
+
api_base="http://localhost:11434/v1/",
|
| 28 |
+
api_key=api_key if api_key else "dummy-key-for-ollama"
|
| 29 |
+
)
|
|
|
|
| 30 |
# Initialize the search tool
|
| 31 |
search_tool = DuckDuckGoSearchTool()
|
| 32 |
# Initialize Agent
|
|
|
|
| 40 |
print(f"Agent returning fixed answer: {fixed_answer}")
|
| 41 |
return fixed_answer
|
| 42 |
|
| 43 |
+
def run_and_submit_all(openai_key: str):
|
| 44 |
"""
|
| 45 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 46 |
and displays the results.
|
|
|
|
| 48 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 49 |
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
| 50 |
|
| 51 |
+
# Get user info from Gradio's login state
|
| 52 |
+
try:
|
| 53 |
+
import gradio as gr
|
| 54 |
+
user_info = gr.user_info()
|
| 55 |
+
if user_info and hasattr(user_info, 'username'):
|
| 56 |
+
username = user_info.username
|
| 57 |
+
print(f"User logged in: {username}")
|
| 58 |
+
else:
|
| 59 |
+
print("User not logged in.")
|
| 60 |
+
return "Please Login to Hugging Face with the button.", None
|
| 61 |
+
except Exception as e:
|
| 62 |
+
print(f"Error getting user info: {e}")
|
| 63 |
+
# Fallback: try to get username from environment or use a default
|
| 64 |
+
username = os.getenv("USER", "anonymous")
|
| 65 |
+
print(f"Using fallback username: {username}")
|
| 66 |
|
| 67 |
api_url = DEFAULT_API_URL
|
| 68 |
questions_url = f"{api_url}/questions"
|