Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,46 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import pandas as pd
|
|
|
|
| 4 |
|
| 5 |
# --- Constants ---
|
| 6 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# --- Dummy Agent ---
|
| 9 |
class DummyAgent:
|
| 10 |
def __init__(self):
|
| 11 |
-
print("DummyAgent initialized
|
| 12 |
|
| 13 |
def __call__(self, question: str) -> str:
|
| 14 |
return "Error: Could not generate answer."
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
# --- Main Run & Submit Function ---
|
| 17 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 18 |
-
# Use your actual Space ID here as fallback if env not set
|
| 19 |
space_id = os.getenv("SPACE_ID", "XeroDN/final_assignment")
|
| 20 |
|
| 21 |
if profile:
|
|
@@ -27,41 +52,11 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 27 |
questions_url = f"{DEFAULT_API_URL}/questions"
|
| 28 |
submit_url = f"{DEFAULT_API_URL}/submit"
|
| 29 |
|
|
|
|
| 30 |
try:
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
except ImportError:
|
| 35 |
-
ssl_supported = False
|
| 36 |
-
|
| 37 |
-
if ssl_supported:
|
| 38 |
-
try:
|
| 39 |
-
from openai import OpenAI
|
| 40 |
-
|
| 41 |
-
class OpenAIAgent:
|
| 42 |
-
def __init__(self):
|
| 43 |
-
self.client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 44 |
-
|
| 45 |
-
def __call__(self, question: str) -> str:
|
| 46 |
-
try:
|
| 47 |
-
response = self.client.chat.completions.create(
|
| 48 |
-
model="gpt-3.5-turbo", # or "gpt-4" if you have access
|
| 49 |
-
messages=[
|
| 50 |
-
{"role": "system", "content": "You are a helpful and precise reasoning agent. Think step-by-step, and be concise."},
|
| 51 |
-
{"role": "user", "content": question}
|
| 52 |
-
]
|
| 53 |
-
)
|
| 54 |
-
return response.choices[0].message.content.strip()
|
| 55 |
-
except Exception as e:
|
| 56 |
-
print(f"[OpenAI API Error] {e}")
|
| 57 |
-
return "Error: Could not generate answer."
|
| 58 |
-
|
| 59 |
-
agent = OpenAIAgent()
|
| 60 |
-
|
| 61 |
-
except Exception as e:
|
| 62 |
-
print("Falling back to DummyAgent:", e)
|
| 63 |
-
agent = DummyAgent()
|
| 64 |
-
else:
|
| 65 |
agent = DummyAgent()
|
| 66 |
|
| 67 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
|
@@ -118,7 +113,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 118 |
|
| 119 |
# --- Gradio UI ---
|
| 120 |
with gr.Blocks() as demo:
|
| 121 |
-
gr.Markdown("## 🤖 GAIA Benchmark Agent Submission")
|
| 122 |
gr.Markdown("Click below to log in and run your agent on the GAIA benchmark.")
|
| 123 |
|
| 124 |
gr.LoginButton()
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import pandas as pd
|
| 4 |
+
import requests
|
| 5 |
|
| 6 |
# --- Constants ---
|
| 7 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 8 |
+
HF_MODEL = "bigscience/bloom-560m"
|
| 9 |
+
HF_API_URL = f"https://api-inference.huggingface.co/models/{HF_MODEL}"
|
| 10 |
+
HF_API_TOKEN = os.getenv("HF_API_TOKEN")
|
| 11 |
|
| 12 |
# --- Dummy Agent ---
|
| 13 |
class DummyAgent:
|
| 14 |
def __init__(self):
|
| 15 |
+
print("DummyAgent initialized.")
|
| 16 |
|
| 17 |
def __call__(self, question: str) -> str:
|
| 18 |
return "Error: Could not generate answer."
|
| 19 |
|
| 20 |
+
# --- Hugging Face Inference API Agent ---
|
| 21 |
+
class HFInferenceAgent:
|
| 22 |
+
def __init__(self):
|
| 23 |
+
if not HF_API_TOKEN:
|
| 24 |
+
raise ValueError("HF_API_TOKEN environment variable is not set.")
|
| 25 |
+
self.headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
| 26 |
+
|
| 27 |
+
def __call__(self, question: str) -> str:
|
| 28 |
+
payload = {
|
| 29 |
+
"inputs": question,
|
| 30 |
+
"parameters": {"max_new_tokens": 100, "return_full_text": False},
|
| 31 |
+
"options": {"use_cache": False}
|
| 32 |
+
}
|
| 33 |
+
try:
|
| 34 |
+
response = requests.post(HF_API_URL, headers=self.headers, json=payload, timeout=30)
|
| 35 |
+
response.raise_for_status()
|
| 36 |
+
result = response.json()
|
| 37 |
+
return result[0]['generated_text'].strip()
|
| 38 |
+
except Exception as e:
|
| 39 |
+
print(f"[HF Inference API Error] {e}")
|
| 40 |
+
return "Error: Could not generate answer."
|
| 41 |
+
|
| 42 |
# --- Main Run & Submit Function ---
|
| 43 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
|
|
| 44 |
space_id = os.getenv("SPACE_ID", "XeroDN/final_assignment")
|
| 45 |
|
| 46 |
if profile:
|
|
|
|
| 52 |
questions_url = f"{DEFAULT_API_URL}/questions"
|
| 53 |
submit_url = f"{DEFAULT_API_URL}/submit"
|
| 54 |
|
| 55 |
+
# Initialize agent
|
| 56 |
try:
|
| 57 |
+
agent = HFInferenceAgent()
|
| 58 |
+
except Exception as e:
|
| 59 |
+
print("Falling back to DummyAgent:", e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
agent = DummyAgent()
|
| 61 |
|
| 62 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
|
|
|
| 113 |
|
| 114 |
# --- Gradio UI ---
|
| 115 |
with gr.Blocks() as demo:
|
| 116 |
+
gr.Markdown("## 🤖 GAIA Benchmark Agent Submission (Hugging Face Inference API)")
|
| 117 |
gr.Markdown("Click below to log in and run your agent on the GAIA benchmark.")
|
| 118 |
|
| 119 |
gr.LoginButton()
|