| | import os |
| | import gradio as gr |
| | import requests |
| | from transformers import AutoModelForCausalLM, AutoTokenizer |
| |
|
| | |
| | DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space" |
| | MODEL_NAME = "meta-llama/Llama-4-Scout-17B-16E-Instruct" |
| |
|
| | |
| | class BasicAgent: |
| | def __init__(self, hf_token: str): |
| | print("Initializing Llama 4 Agent...") |
| | self.tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, use_auth_token=hf_token) |
| | self.model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, use_auth_token=hf_token) |
| |
|
| | def __call__(self, question: str) -> str: |
| | question = question.encode('utf-8', errors='ignore').decode('utf-8') |
| | inputs = self.tokenizer(question, return_tensors="pt") |
| | outputs = self.model.generate(**inputs, max_new_tokens=50) |
| | answer = self.tokenizer.decode(outputs[0], skip_special_tokens=True) |
| | |
| | if question in answer: |
| | answer = answer.replace(question, '').strip() |
| | return answer |
| |
|
| | |
| | def run_and_submit_all(profile: gr.OAuthProfile | None): |
| | hf_token = os.getenv('HF_TOKEN') |
| | if not hf_token: |
| | return "HF_TOKEN not set!", None |
| |
|
| | if not profile: |
| | return "Please login to Hugging Face.", None |
| |
|
| | username = profile.username |
| | api_url = DEFAULT_API_URL |
| | questions_url = f"{api_url}/questions" |
| | submit_url = f"{api_url}/submit" |
| |
|
| | |
| | try: |
| | agent = BasicAgent(hf_token) |
| | except Exception as e: |
| | return f"Error initializing agent: {e}", None |
| |
|
| | |
| | try: |
| | response = requests.get(questions_url, timeout=15) |
| | response.raise_for_status() |
| | questions_data = response.json() |
| | if not questions_data: |
| | return "No questions fetched.", None |
| | except Exception as e: |
| | return f"Error fetching questions: {e}", None |
| |
|
| | |
| | answers_payload = [] |
| | results_log = [] |
| | for item in questions_data: |
| | task_id = item.get('task_id') |
| | question_text = item.get('question') |
| | if not task_id or question_text is None: |
| | continue |
| | try: |
| | submitted_answer = agent(question_text) |
| | answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer}) |
| | results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer}) |
| | except Exception as e: |
| | results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"}) |
| |
|
| | if not answers_payload: |
| | return "No answers generated.", results_log |
| |
|
| | |
| | space_id = os.getenv('SPACE_ID', 'YOUR_SPACE_NAME') |
| | agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" |
| | submission_data = {"username": username, "agent_code": agent_code, "answers": answers_payload} |
| |
|
| | |
| | try: |
| | response = requests.post(submit_url, json=submission_data, timeout=60) |
| | response.raise_for_status() |
| | result_data = response.json() |
| | final_status = (f"Submission Successful!\n" |
| | f"User: {result_data.get('username')}\n" |
| | f"Overall Score: {result_data.get('score', 'N/A')}%" |
| | f" ({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n" |
| | f"Message: {result_data.get('message', '')}") |
| | return final_status, results_log |
| | except Exception as e: |
| | return f"Submission Failed: {e}", results_log |