Spaces:
Sleeping
Sleeping
updated app.py
Browse files
app.py
CHANGED
|
@@ -5,12 +5,10 @@ import inspect
|
|
| 5 |
import pandas as pd
|
| 6 |
from agent import TurboNerd
|
| 7 |
|
| 8 |
-
# (Keep Constants as is)
|
| 9 |
# --- Constants ---
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 11 |
|
| 12 |
# --- Basic Agent Definition ---
|
| 13 |
-
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 14 |
class BasicAgent:
|
| 15 |
def __init__(self):
|
| 16 |
print("BasicAgent initialized.")
|
|
@@ -22,7 +20,36 @@ class BasicAgent:
|
|
| 22 |
print(f"Agent returning answer: {answer[:50]}...")
|
| 23 |
return answer
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
"""
|
| 27 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 28 |
and displays the results.
|
|
@@ -47,6 +74,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 47 |
except Exception as e:
|
| 48 |
print(f"Error instantiating agent: {e}")
|
| 49 |
return f"Error initializing agent: {e}", None
|
|
|
|
| 50 |
# In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)
|
| 51 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 52 |
print(agent_code)
|
|
@@ -142,37 +170,81 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 142 |
results_df = pd.DataFrame(results_log)
|
| 143 |
return status_message, results_df
|
| 144 |
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
gr.
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
if __name__ == "__main__":
|
| 178 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
|
@@ -195,5 +267,5 @@ if __name__ == "__main__":
|
|
| 195 |
|
| 196 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 197 |
|
| 198 |
-
print("Launching Gradio Interface for
|
| 199 |
demo.launch(debug=True, share=False)
|
|
|
|
| 5 |
import pandas as pd
|
| 6 |
from agent import TurboNerd
|
| 7 |
|
|
|
|
| 8 |
# --- Constants ---
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
|
| 11 |
# --- Basic Agent Definition ---
|
|
|
|
| 12 |
class BasicAgent:
|
| 13 |
def __init__(self):
|
| 14 |
print("BasicAgent initialized.")
|
|
|
|
| 20 |
print(f"Agent returning answer: {answer[:50]}...")
|
| 21 |
return answer
|
| 22 |
|
| 23 |
+
# --- Chat Interface Functions ---
|
| 24 |
+
def chat_with_agent(question: str, history: list) -> tuple:
|
| 25 |
+
"""
|
| 26 |
+
Handle chat interaction with TurboNerd agent.
|
| 27 |
+
"""
|
| 28 |
+
if not question.strip():
|
| 29 |
+
return history, ""
|
| 30 |
+
|
| 31 |
+
try:
|
| 32 |
+
# Initialize agent
|
| 33 |
+
agent = TurboNerd()
|
| 34 |
+
|
| 35 |
+
# Get response from agent
|
| 36 |
+
response = agent(question)
|
| 37 |
+
|
| 38 |
+
# Add question and response to history
|
| 39 |
+
history.append([question, response])
|
| 40 |
+
|
| 41 |
+
return history, ""
|
| 42 |
+
except Exception as e:
|
| 43 |
+
error_message = f"Error: {str(e)}"
|
| 44 |
+
history.append([question, error_message])
|
| 45 |
+
return history, ""
|
| 46 |
+
|
| 47 |
+
def clear_chat():
|
| 48 |
+
"""Clear the chat history."""
|
| 49 |
+
return [], ""
|
| 50 |
+
|
| 51 |
+
# --- Evaluation Functions ---
|
| 52 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 53 |
"""
|
| 54 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 55 |
and displays the results.
|
|
|
|
| 74 |
except Exception as e:
|
| 75 |
print(f"Error instantiating agent: {e}")
|
| 76 |
return f"Error initializing agent: {e}", None
|
| 77 |
+
|
| 78 |
# In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)
|
| 79 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 80 |
print(agent_code)
|
|
|
|
| 170 |
results_df = pd.DataFrame(results_log)
|
| 171 |
return status_message, results_df
|
| 172 |
|
| 173 |
+
# --- Build Gradio Interface using Blocks with Tabs ---
|
| 174 |
+
with gr.Blocks(title="TurboNerd Agent") as demo:
|
| 175 |
+
gr.Markdown("# TurboNerd Agent")
|
| 176 |
+
|
| 177 |
+
with gr.Tabs():
|
| 178 |
+
# Tab 1: Chat Interface
|
| 179 |
+
with gr.TabItem("Chat", id="chat"):
|
| 180 |
+
gr.Markdown("""
|
| 181 |
+
## Chat with TurboNerd
|
| 182 |
+
Ask any question and get an answer from the TurboNerd agent. The agent can:
|
| 183 |
+
- Execute Python code
|
| 184 |
+
- Search Wikipedia and the web
|
| 185 |
+
- Process Excel files
|
| 186 |
+
- Transcribe YouTube videos and audio files
|
| 187 |
+
- And much more!
|
| 188 |
+
""")
|
| 189 |
+
|
| 190 |
+
with gr.Row():
|
| 191 |
+
with gr.Column(scale=4):
|
| 192 |
+
chatbot = gr.Chatbot(label="Conversation", height=500)
|
| 193 |
+
question_input = gr.Textbox(
|
| 194 |
+
label="Ask a question",
|
| 195 |
+
placeholder="Type your question here...",
|
| 196 |
+
lines=2,
|
| 197 |
+
max_lines=5
|
| 198 |
+
)
|
| 199 |
+
with gr.Row():
|
| 200 |
+
submit_btn = gr.Button("Send", variant="primary")
|
| 201 |
+
clear_btn = gr.Button("Clear Chat", variant="secondary")
|
| 202 |
+
|
| 203 |
+
# Chat interface event handlers
|
| 204 |
+
submit_btn.click(
|
| 205 |
+
fn=chat_with_agent,
|
| 206 |
+
inputs=[question_input, chatbot],
|
| 207 |
+
outputs=[chatbot, question_input]
|
| 208 |
+
)
|
| 209 |
+
|
| 210 |
+
question_input.submit(
|
| 211 |
+
fn=chat_with_agent,
|
| 212 |
+
inputs=[question_input, chatbot],
|
| 213 |
+
outputs=[chatbot, question_input]
|
| 214 |
+
)
|
| 215 |
+
|
| 216 |
+
clear_btn.click(
|
| 217 |
+
fn=clear_chat,
|
| 218 |
+
outputs=[chatbot, question_input]
|
| 219 |
+
)
|
| 220 |
+
|
| 221 |
+
# Tab 2: Evaluation Interface
|
| 222 |
+
with gr.TabItem("Evaluation", id="evaluation"):
|
| 223 |
+
gr.Markdown("""
|
| 224 |
+
## Agent Evaluation Runner
|
| 225 |
+
|
| 226 |
+
**Instructions:**
|
| 227 |
+
|
| 228 |
+
1. Log in to your Hugging Face account using the button below.
|
| 229 |
+
2. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
| 230 |
+
|
| 231 |
+
---
|
| 232 |
+
**Disclaimers:**
|
| 233 |
+
Once clicking on the "submit" button, it can take quite some time (this is the time for the agent to go through all the questions).
|
| 234 |
+
This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution.
|
| 235 |
+
""")
|
| 236 |
+
|
| 237 |
+
gr.LoginButton()
|
| 238 |
+
|
| 239 |
+
run_button = gr.Button("Run Evaluation & Submit All Answers", variant="primary")
|
| 240 |
+
|
| 241 |
+
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 242 |
+
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 243 |
+
|
| 244 |
+
run_button.click(
|
| 245 |
+
fn=run_and_submit_all,
|
| 246 |
+
outputs=[status_output, results_table]
|
| 247 |
+
)
|
| 248 |
|
| 249 |
if __name__ == "__main__":
|
| 250 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
|
|
|
| 267 |
|
| 268 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 269 |
|
| 270 |
+
print("Launching Gradio Interface for TurboNerd Agent...")
|
| 271 |
demo.launch(debug=True, share=False)
|