RalphThings commited on
Commit
0775c3f
·
verified ·
1 Parent(s): 81917a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -0
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import os
2
  import gradio as gr
3
  import requests
@@ -7,6 +8,7 @@ import pandas as pd
7
  # (Keep Constants as is)
8
  # --- Constants ---
9
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
 
10
 
11
  # --- Basic Agent Definition ---
12
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
@@ -19,6 +21,44 @@ class BasicAgent:
19
  print(f"Agent returning fixed answer: {fixed_answer}")
20
  return fixed_answer
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  def run_and_submit_all( profile: gr.OAuthProfile | None):
23
  """
24
  Fetches all questions, runs the BasicAgent on them, submits all answers,
 
1
+ from transformers import pipeline
2
  import os
3
  import gradio as gr
4
  import requests
 
8
  # (Keep Constants as is)
9
  # --- Constants ---
10
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
11
+ HF_TOKEN = os.getenv("HF_TOKEN", None)
12
 
13
  # --- Basic Agent Definition ---
14
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
 
21
  print(f"Agent returning fixed answer: {fixed_answer}")
22
  return fixed_answer
23
 
24
+ class BasicAgent:
25
+ def __init__(self):
26
+ # initialize HF inference pipeline once
27
+ if HF_TOKEN is None:
28
+ raise ValueError("HF_TOKEN not set in environment")
29
+ self.generator = pipeline(
30
+ "text-generation",
31
+ model="gpt2", # or your chosen model
32
+ tokenizer="gpt2",
33
+ device=-1, # CPU; adjust if GPU
34
+ use_auth_token=HF_TOKEN,
35
+ )
36
+ # The GAIA system prompt (no "FINAL ANSWER:" at the end)
37
+ self.system_prompt = (
38
+ "You are a general AI assistant. "
39
+ "I will ask you a question. Answer in as few words as possible—"
40
+ "either a number, a few words, or a comma-separated list—"
41
+ "with no extra commentary, prefixes, or units.\n\n"
42
+ )
43
+ print("BasicAgent initialized with LLM.")
44
+
45
+ def __call__(self, question: str) -> str:
46
+ # Combine prompt + question
47
+ prompt = f"{self.system_prompt}Q: {question}\nA:"
48
+ # Run the model
49
+ out = self.generator(
50
+ prompt,
51
+ max_length=len(prompt.split()) + 20, # short answer
52
+ num_return_sequences=1,
53
+ pad_token_id=self.generator.tokenizer.eos_token_id
54
+ )
55
+ # Extract the portion after "A:"
56
+ text = out[0]["generated_text"]
57
+ # split on the last "\nA:" or just on "A:" if needed
58
+ answer = text.split("\nA:")[-1].strip()
59
+ # final cleanup: no newlines, no trailing punctuation
60
+ return answer.replace("\n", " ").strip()
61
+
62
  def run_and_submit_all( profile: gr.OAuthProfile | None):
63
  """
64
  Fetches all questions, runs the BasicAgent on them, submits all answers,