Spaces:
Runtime error
Runtime error
Commit
·
1a807af
1
Parent(s):
65f5939
app.py file changed
Browse files- app.py +26 -16
- requirements.txt +3 -2
app.py
CHANGED
@@ -6,24 +6,35 @@ import pandas as pd
|
|
6 |
# --- Constants ---
|
7 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
8 |
|
9 |
-
# ---
|
10 |
-
class
|
11 |
def __init__(self):
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
def __call__(self, question: str) -> str:
|
15 |
-
print(f"
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
21 |
def run_agent_only(profile: gr.OAuthProfile | None):
|
22 |
if not profile:
|
23 |
return "Please login first.", None, None
|
24 |
|
25 |
try:
|
26 |
-
agent =
|
27 |
except Exception as e:
|
28 |
return f"Error initializing agent: {e}", None, None
|
29 |
|
@@ -63,7 +74,7 @@ def run_agent_only(profile: gr.OAuthProfile | None):
|
|
63 |
|
64 |
return "✅ Answers generated. You can now submit.", pd.DataFrame(results_log), answers_payload
|
65 |
|
66 |
-
# --- Submit
|
67 |
def submit_only(profile: gr.OAuthProfile | None, answers_payload: list | None):
|
68 |
if not profile:
|
69 |
return "Please login first.", None
|
@@ -94,12 +105,12 @@ def submit_only(profile: gr.OAuthProfile | None, answers_payload: list | None):
|
|
94 |
except Exception as e:
|
95 |
return f"❌ Submission failed: {e}", None
|
96 |
|
97 |
-
# --- Gradio
|
98 |
with gr.Blocks() as demo:
|
99 |
-
gr.Markdown("# 🤖
|
100 |
gr.Markdown("""
|
101 |
**Steps to Complete Your Hugging Face Certification:**
|
102 |
-
1. Clone this Space and build your own logic in the `
|
103 |
2. Log in using the Hugging Face button.
|
104 |
3. Click `Run Agent` to generate answers.
|
105 |
4. Once ready, click `Submit` to send your answers and view your score.
|
@@ -115,7 +126,6 @@ with gr.Blocks() as demo:
|
|
115 |
|
116 |
answers_state = gr.State(value=None)
|
117 |
|
118 |
-
# ✅ Профиль не создаём вручную — указываем как тип input
|
119 |
run_button.click(
|
120 |
fn=run_agent_only,
|
121 |
inputs=[gr.OAuthProfile],
|
@@ -128,7 +138,7 @@ with gr.Blocks() as demo:
|
|
128 |
outputs=[status_output, results_table]
|
129 |
)
|
130 |
|
131 |
-
# ---
|
132 |
if __name__ == "__main__":
|
133 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
134 |
space_host_startup = os.getenv("SPACE_HOST")
|
|
|
6 |
# --- Constants ---
|
7 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
8 |
|
9 |
+
# --- Новый агент: KeywordAgent ---
|
10 |
+
class KeywordAgent:
|
11 |
def __init__(self):
|
12 |
+
self.keywords = {
|
13 |
+
"python": "Python — это мощный язык программирования, широко используемый в науке и разработке.",
|
14 |
+
"ai": "AI (искусственный интеллект) позволяет компьютерам решать задачи, которые обычно требуют разума.",
|
15 |
+
"data": "Данные — это основа всех аналитических систем и машинного обучения.",
|
16 |
+
"huggingface": "Hugging Face — это платформа для работы с ИИ, особенно NLP-моделями.",
|
17 |
+
"gradio": "Gradio — это библиотека для создания веб-интерфейсов ИИ-моделей."
|
18 |
+
}
|
19 |
+
self.default_answer = "Извините, я пока не знаю, как ответить на этот вопрос. Попробуйте иначе."
|
20 |
|
21 |
def __call__(self, question: str) -> str:
|
22 |
+
print(f"🔎 Вопрос получен: {question[:50]}...")
|
23 |
+
q_lower = question.lower()
|
24 |
+
for word, response in self.keywords.items():
|
25 |
+
if word in q_lower:
|
26 |
+
print(f"✅ Найдено ключевое слово: {word}")
|
27 |
+
return response
|
28 |
+
print("⚠️ Ключевые слова не найдены.")
|
29 |
+
return self.default_answer
|
30 |
+
|
31 |
+
# --- Run Agent only ---
|
32 |
def run_agent_only(profile: gr.OAuthProfile | None):
|
33 |
if not profile:
|
34 |
return "Please login first.", None, None
|
35 |
|
36 |
try:
|
37 |
+
agent = KeywordAgent()
|
38 |
except Exception as e:
|
39 |
return f"Error initializing agent: {e}", None, None
|
40 |
|
|
|
74 |
|
75 |
return "✅ Answers generated. You can now submit.", pd.DataFrame(results_log), answers_payload
|
76 |
|
77 |
+
# --- Submit only ---
|
78 |
def submit_only(profile: gr.OAuthProfile | None, answers_payload: list | None):
|
79 |
if not profile:
|
80 |
return "Please login first.", None
|
|
|
105 |
except Exception as e:
|
106 |
return f"❌ Submission failed: {e}", None
|
107 |
|
108 |
+
# --- Gradio UI ---
|
109 |
with gr.Blocks() as demo:
|
110 |
+
gr.Markdown("# 🤖 Keyword Agent Evaluation Runner")
|
111 |
gr.Markdown("""
|
112 |
**Steps to Complete Your Hugging Face Certification:**
|
113 |
+
1. Clone this Space and build your own logic in the `KeywordAgent`.
|
114 |
2. Log in using the Hugging Face button.
|
115 |
3. Click `Run Agent` to generate answers.
|
116 |
4. Once ready, click `Submit` to send your answers and view your score.
|
|
|
126 |
|
127 |
answers_state = gr.State(value=None)
|
128 |
|
|
|
129 |
run_button.click(
|
130 |
fn=run_agent_only,
|
131 |
inputs=[gr.OAuthProfile],
|
|
|
138 |
outputs=[status_output, results_table]
|
139 |
)
|
140 |
|
141 |
+
# --- Startup ---
|
142 |
if __name__ == "__main__":
|
143 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
144 |
space_host_startup = os.getenv("SPACE_HOST")
|
requirements.txt
CHANGED
@@ -1,2 +1,3 @@
|
|
1 |
-
gradio
|
2 |
-
requests
|
|
|
|
1 |
+
gradio==4.26.0
|
2 |
+
requests
|
3 |
+
pandas
|