File size: 6,090 Bytes
e07bdae
 
 
 
 
 
 
 
1a807af
 
e07bdae
1a807af
 
 
 
 
 
 
 
e07bdae
 
1a807af
 
 
 
 
 
 
 
 
 
e07bdae
 
 
 
 
1a807af
e07bdae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a807af
e07bdae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a807af
e07bdae
1a807af
e07bdae
 
1a807af
e07bdae
 
 
 
 
65f5939
46b63d2
e07bdae
 
 
 
 
 
 
 
 
 
 
46b63d2
e07bdae
 
 
 
 
46b63d2
e07bdae
 
 
1a807af
e07bdae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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)