Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,14 +10,50 @@ 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def __call__(self, question: str) -> str:
|
| 17 |
-
print(
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 23 |
"""
|
|
|
|
| 10 |
|
| 11 |
# --- Basic Agent Definition ---
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 14 |
+
|
| 15 |
class BasicAgent:
|
| 16 |
def __init__(self):
|
| 17 |
+
model_name = "Qwen/Qwen3-0.6B-MLX-bf16" # or "Qwen/Qwen3‑0.6B" whichever you have access to
|
| 18 |
+
print(f"Loading model {model_name} …")
|
| 19 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 20 |
+
self.model = AutoModelForCausalLM.from_pretrained(
|
| 21 |
+
model_name,
|
| 22 |
+
torch_dtype="auto",
|
| 23 |
+
device_map="auto"
|
| 24 |
+
)
|
| 25 |
+
self.generator = pipeline(
|
| 26 |
+
"text-generation",
|
| 27 |
+
model=self.model,
|
| 28 |
+
tokenizer=self.tokenizer,
|
| 29 |
+
max_new_tokens=200,
|
| 30 |
+
temperature=0.0, # greedy / deterministic
|
| 31 |
+
do_sample=False
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
def __call__(self, question: str) -> str:
|
| 35 |
+
print("Received question:", question)
|
| 36 |
+
# Use Qwen’s chat template (if available)
|
| 37 |
+
messages = [
|
| 38 |
+
{"role": "user", "content": question}
|
| 39 |
+
]
|
| 40 |
+
# The Qwen tokenizer may support chat templates
|
| 41 |
+
prompt = self.tokenizer.apply_chat_template(
|
| 42 |
+
messages,
|
| 43 |
+
tokenize=False,
|
| 44 |
+
add_generation_prompt=True,
|
| 45 |
+
enable_thinking=False # or True, depending on mode
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
outputs = self.generator(prompt)
|
| 49 |
+
text = outputs[0]["generated_text"]
|
| 50 |
+
# Remove prompt prefix, get only the answer part
|
| 51 |
+
# The answer begins after prompt
|
| 52 |
+
answer = text[len(prompt):].strip()
|
| 53 |
+
# Return first line
|
| 54 |
+
answer = answer.split("\n")[0].strip()
|
| 55 |
+
return answer
|
| 56 |
+
|
| 57 |
|
| 58 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 59 |
"""
|