XeroDN commited on
Commit
758fa65
·
verified ·
1 Parent(s): a9f8083

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -24
app.py CHANGED
@@ -1,13 +1,8 @@
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:
@@ -17,30 +12,23 @@ class DummyAgent:
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,15 +40,16 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
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"
63
 
 
64
  try:
65
  response = requests.get(questions_url, timeout=15)
66
  response.raise_for_status()
@@ -113,7 +102,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
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()
 
1
  import os
2
  import gradio as gr
3
  import pandas as pd
 
4
 
5
+ from transformers import pipeline
 
 
 
 
6
 
7
  # --- Dummy Agent ---
8
  class DummyAgent:
 
12
  def __call__(self, question: str) -> str:
13
  return "Error: Could not generate answer."
14
 
15
+ # --- Local LLM Agent using distilgpt2 ---
16
+ class LocalLLMAgent:
17
  def __init__(self):
18
+ print("Loading local model distilgpt2...")
19
+ self.generator = pipeline("text-generation", model="distilgpt2")
 
20
 
21
  def __call__(self, question: str) -> str:
 
 
 
 
 
22
  try:
23
+ outputs = self.generator(question, max_length=50, do_sample=True, top_p=0.9, temperature=0.8)
24
+ return outputs[0]['generated_text'].strip()
 
 
25
  except Exception as e:
26
+ print(f"[LocalLLMAgent Error] {e}")
27
  return "Error: Could not generate answer."
28
 
29
  # --- Main Run & Submit Function ---
30
  def run_and_submit_all(profile: gr.OAuthProfile | None):
31
+ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
32
  space_id = os.getenv("SPACE_ID", "XeroDN/final_assignment")
33
 
34
  if profile:
 
40
  questions_url = f"{DEFAULT_API_URL}/questions"
41
  submit_url = f"{DEFAULT_API_URL}/submit"
42
 
43
+ # Initialize agent - use local LLM agent (no API calls)
44
  try:
45
+ agent = LocalLLMAgent()
46
  except Exception as e:
47
  print("Falling back to DummyAgent:", e)
48
  agent = DummyAgent()
49
 
50
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
51
 
52
+ import requests
53
  try:
54
  response = requests.get(questions_url, timeout=15)
55
  response.raise_for_status()
 
102
 
103
  # --- Gradio UI ---
104
  with gr.Blocks() as demo:
105
+ gr.Markdown("## 🤖 GAIA Benchmark Agent Submission (Local LLM, no API key)")
106
  gr.Markdown("Click below to log in and run your agent on the GAIA benchmark.")
107
 
108
  gr.LoginButton()