import os import gradio as gr import requests import pandas as pd # --- Constants --- DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space" # --- Новый агент: KeywordAgent --- class KeywordAgent: def __init__(self): self.keywords = { "python": "Python — это мощный язык программирования, широко используемый в науке и разработке.", "ai": "AI (искусственный интеллект) позволяет компьютерам решать задачи, которые обычно требуют разума.", "data": "Данные — это основа всех аналитических систем и машинного обучения.", "huggingface": "Hugging Face — это платформа для работы с ИИ, особенно NLP-моделями.", "gradio": "Gradio — это библиотека для создания веб-интерфейсов ИИ-моделей." } self.default_answer = "Извините, я пока не знаю, как ответить на этот вопрос. Попробуйте иначе." def __call__(self, question: str) -> str: print(f"🔎 Вопрос получен: {question[:50]}...") q_lower = question.lower() for word, response in self.keywords.items(): if word in q_lower: print(f"✅ Найдено ключевое слово: {word}") return response print("⚠️ Ключевые слова не найдены.") return self.default_answer # --- Run Agent only --- def run_agent_only(profile: gr.OAuthProfile | None): if not profile: return "Please login first.", None, None try: agent = KeywordAgent() except Exception as e: return f"Error initializing agent: {e}", None, None questions_url = f"{DEFAULT_API_URL}/questions" try: response = requests.get(questions_url, timeout=10) response.raise_for_status() questions_data = response.json() except Exception as e: return f"Failed to fetch questions: {e}", None, None results_log = [] answers_payload = [] for item in questions_data: task_id = item.get("task_id") question_text = item.get("question") if not task_id or not question_text: continue try: answer = agent(question_text) results_log.append({ "Task ID": task_id, "Question": question_text, "Submitted Answer": answer }) answers_payload.append({ "task_id": task_id, "submitted_answer": answer }) except Exception as e: results_log.append({ "Task ID": task_id, "Question": question_text, "Submitted Answer": f"ERROR: {e}" }) return "✅ Answers generated. You can now submit.", pd.DataFrame(results_log), answers_payload # --- Submit only --- def submit_only(profile: gr.OAuthProfile | None, answers_payload: list | None): if not profile: return "Please login first.", None if not answers_payload: return "No answers to submit. Run the agent first.", None username = profile.username space_id = os.getenv("SPACE_ID") agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" submit_url = f"{DEFAULT_API_URL}/submit" try: submission_data = { "username": username.strip(), "agent_code": agent_code, "answers": answers_payload } response = requests.post(submit_url, json=submission_data, timeout=30) response.raise_for_status() result_data = response.json() final_status = ( f"🎉 Submission Successful!\n" f"👤 User: {result_data.get('username')}\n" f"✅ Score: {result_data.get('score')}%\n" f"🎯 Correct: {result_data.get('correct_count')}/{result_data.get('total_attempted')}" ) return final_status, None except Exception as e: return f"❌ Submission failed: {e}", None # --- Gradio UI --- with gr.Blocks() as demo: gr.Markdown("# 🤖 Keyword Agent Evaluation Runner") gr.Markdown(""" **Steps to Complete Your Hugging Face Certification:** 1. Clone this Space and build your own logic in the `KeywordAgent`. 2. Log in using the Hugging Face button. 3. Click `Run Agent` to generate answers. 4. Once ready, click `Submit` to send your answers and view your score. """) gr.LoginButton() profile_input = gr.OAuthProfileComponent() run_button = gr.Button("🧠 Run Agent (Generate Answers)") submit_button = gr.Button("📤 Submit All Answers") status_output = gr.Textbox(label="Status", lines=5, interactive=False) results_table = gr.DataFrame(label="Agent Answers", wrap=True) answers_state = gr.State(value=None) run_button.click( fn=run_agent_only, inputs=[profile_input], outputs=[status_output, results_table, answers_state] ) submit_button.click( fn=submit_only, inputs=[profile_input, answers_state], outputs=[status_output, results_table] ) # --- Startup --- if __name__ == "__main__": print("\n" + "-"*30 + " App Starting " + "-"*30) space_host_startup = os.getenv("SPACE_HOST") space_id_startup = os.getenv("SPACE_ID") if space_host_startup: print(f"✅ SPACE_HOST: https://{space_host_startup}.hf.space") else: print("ℹ️ SPACE_HOST not found.") if space_id_startup: print(f"✅ SPACE_ID: {space_id_startup}") print(f" Repo: https://huggingface.co/spaces/{space_id_startup}") else: print("ℹ️ SPACE_ID not found.") print("-"*(60 + len(" App Starting ")) + "\n") demo.launch(debug=True, share=False)