Files changed (1) hide show
  1. app.py +52 -6
app.py CHANGED
@@ -3,21 +3,67 @@ import gradio as gr
3
  import requests
4
  import inspect
5
  import pandas as pd
6
-
 
 
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 ------
 
 
 
 
 
 
 
 
 
13
  class BasicAgent:
14
  def __init__(self):
15
- print("BasicAgent initialized.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  def __call__(self, question: str) -> str:
17
- print(f"Agent received question (first 50 chars): {question[:50]}...")
18
- fixed_answer = "This is a default answer."
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
  """
 
3
  import requests
4
  import inspect
5
  import pandas as pd
6
+ from transformers import pipeline
7
+ import torch
8
+ import re
9
  # (Keep Constants as is)
10
  # --- Constants ---
11
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
12
 
13
  # --- Basic Agent Definition ---
14
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
15
+ #class BasicAgent:
16
+ # def __init__(self):
17
+ # print("BasicAgent initialized.")
18
+ # def __call__(self, question: str) -> str:
19
+ # print(f"Agent received question (first 50 chars): {question[:50]}...")
20
+ # fixed_answer = "This is a default answer."
21
+ # print(f"Agent returning fixed answer: {fixed_answer}")
22
+ # return fixed_answer
23
+
24
  class BasicAgent:
25
  def __init__(self):
26
+ print("Free Local Agent initialized.")
27
+
28
+ self.system_prompt = """You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template:
29
+ FINAL ANSWER: [YOUR FINAL ANSWER].
30
+ YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. Do not repeat instructions."""
31
+
32
+ self.pipe = pipeline(
33
+ "text-generation",
34
+ model="Qwen/Qwen2-0.5B-Instruct",
35
+ torch_dtype=torch.float32,
36
+ device_map="cpu"
37
+ )
38
+
39
+ def extract_final_answer(self, text):
40
+ match = re.search(r"FINAL ANSWER:\s*(.+)", text, re.IGNORECASE)
41
+ if match:
42
+ return match.group(1).split("\n")[0].strip()
43
+ return text.strip()
44
+
45
  def __call__(self, question: str) -> str:
46
+
47
+ prompt = f"""<|system|>
48
+ {self.system_prompt}
49
+ <|user|>
50
+ {question}
51
+ <|assistant|>
52
+ """
53
+
54
+ output = self.pipe(
55
+ prompt,
56
+ max_new_tokens=128,
57
+ do_sample=False
58
+ )[0]["generated_text"]
59
+
60
+ # Remove prompt from output
61
+ answer_part = output[len(prompt):]
62
+
63
+ final_answer = self.extract_final_answer(answer_part)
64
+
65
+ return final_answer
66
+
67
 
68
  def run_and_submit_all( profile: gr.OAuthProfile | None):
69
  """