Update app.py
Browse files
app.py
CHANGED
|
@@ -1,116 +1,36 @@
|
|
| 1 |
-
import base64
|
| 2 |
-
import inspect
|
| 3 |
-
import mimetypes
|
| 4 |
import os
|
| 5 |
-
import tempfile
|
| 6 |
-
|
| 7 |
import gradio as gr
|
| 8 |
-
import pandas as pd
|
| 9 |
import requests
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
from
|
| 13 |
-
|
| 14 |
# (Keep Constants as is)
|
| 15 |
# --- Constants ---
|
| 16 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 17 |
|
|
|
|
| 18 |
# --- Basic Agent Definition ---
|
| 19 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
For .xlsx files, extract the text and append it to the question, since LLMs do not natively support .xlsx.
|
| 35 |
-
Follows: https://python.langchain.com/docs/how_to/multimodal_inputs/
|
| 36 |
-
"""
|
| 37 |
-
content = []
|
| 38 |
-
# Special handling for .xlsx files
|
| 39 |
-
if file_bytes and file_name and file_name.lower().endswith('.xlsx'):
|
| 40 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix='.xlsx') as tmp:
|
| 41 |
-
tmp.write(file_bytes)
|
| 42 |
-
tmp_path = tmp.name
|
| 43 |
-
loader = UnstructuredExcelLoader(tmp_path, mode="elements")
|
| 44 |
-
docs = loader.load()
|
| 45 |
-
excel_text = "\n".join(doc.page_content for doc in docs)
|
| 46 |
-
question = f"{question}\n\n[Excel file content follows:]\n{excel_text}"
|
| 47 |
-
content.append({"type": "text", "text": question})
|
| 48 |
-
if file_bytes and file_name and not file_name.lower().endswith('.xlsx'):
|
| 49 |
-
ext = file_name.lower().split('.')[-1]
|
| 50 |
-
b64_data = base64.b64encode(file_bytes).decode("utf-8")
|
| 51 |
-
mime_type, _ = mimetypes.guess_type(file_name)
|
| 52 |
-
# Handle common audio/image types explicitly
|
| 53 |
-
if ext in ["png"]:
|
| 54 |
-
mime_type = "image/png"
|
| 55 |
-
block_type = "image"
|
| 56 |
-
elif ext in ["jpg", "jpeg"]:
|
| 57 |
-
mime_type = "image/jpeg"
|
| 58 |
-
block_type = "image"
|
| 59 |
-
elif ext == "mp3":
|
| 60 |
-
mime_type = "audio/mpeg"
|
| 61 |
-
block_type = "audio"
|
| 62 |
-
elif ext == "wav":
|
| 63 |
-
mime_type = "audio/wav"
|
| 64 |
-
block_type = "audio"
|
| 65 |
-
elif ext == "m4a":
|
| 66 |
-
mime_type = "audio/mp4"
|
| 67 |
-
block_type = "audio"
|
| 68 |
-
else:
|
| 69 |
-
block_type = "file"
|
| 70 |
-
if not mime_type:
|
| 71 |
-
mime_type = "application/octet-stream"
|
| 72 |
-
block = {
|
| 73 |
-
"type": block_type,
|
| 74 |
-
"source_type": "base64",
|
| 75 |
-
"data": b64_data,
|
| 76 |
-
"mime_type": mime_type,
|
| 77 |
-
"filename": file_name,
|
| 78 |
-
}
|
| 79 |
-
content.append(block)
|
| 80 |
-
return [{"role": "user", "content": content}]
|
| 81 |
-
|
| 82 |
-
def filter_supported_content_blocks(messages):
|
| 83 |
-
allowed_types = {"text", "image_url", "input_audio", "refusal", "audio", "file", "image"}
|
| 84 |
-
filtered = []
|
| 85 |
-
for msg in messages:
|
| 86 |
-
if "content" in msg and isinstance(msg["content"], list):
|
| 87 |
-
filtered_content = [block for block in msg["content"] if block.get("type") in allowed_types]
|
| 88 |
-
msg = dict(msg)
|
| 89 |
-
msg["content"] = filtered_content
|
| 90 |
-
filtered.append(msg)
|
| 91 |
-
return filtered
|
| 92 |
-
|
| 93 |
-
class Agent:
|
| 94 |
-
def __init__(self, main_agent):
|
| 95 |
-
self.main_agent = main_agent
|
| 96 |
-
print("Agent initialized.")
|
| 97 |
-
def __call__(self, question: str, file_name: str = "", task_id: str = "") -> str:
|
| 98 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
message = filter_supported_content_blocks(message)
|
| 103 |
-
result = self.main_agent.invoke({"messages": message})
|
| 104 |
-
answer = result["messages"][-1]
|
| 105 |
-
content = answer.content
|
| 106 |
-
if isinstance(content, list) and content and isinstance(content[0], dict) and "text" in content[0]:
|
| 107 |
-
return content[0]["text"]
|
| 108 |
-
elif isinstance(content, str):
|
| 109 |
-
return content
|
| 110 |
-
else:
|
| 111 |
-
return str(content)
|
| 112 |
|
| 113 |
-
def run_and_submit_all(profile: gr.OAuthProfile | None
|
| 114 |
"""
|
| 115 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 116 |
and displays the results.
|
|
@@ -119,7 +39,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None, openai_key: str, google_
|
|
| 119 |
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
| 120 |
|
| 121 |
if profile:
|
| 122 |
-
username
|
| 123 |
print(f"User logged in: {username}")
|
| 124 |
else:
|
| 125 |
print("User not logged in.")
|
|
@@ -131,11 +51,11 @@ def run_and_submit_all(profile: gr.OAuthProfile | None, openai_key: str, google_
|
|
| 131 |
|
| 132 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 133 |
try:
|
| 134 |
-
agent =
|
| 135 |
except Exception as e:
|
| 136 |
print(f"Error instantiating agent: {e}")
|
| 137 |
return f"Error initializing agent: {e}", None
|
| 138 |
-
# In the case of an app running as a hugging Face space, this link points toward your codebase (
|
| 139 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 140 |
print(agent_code)
|
| 141 |
|
|
@@ -239,7 +159,7 @@ with gr.Blocks() as demo:
|
|
| 239 |
**Instructions:**
|
| 240 |
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
| 241 |
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
| 242 |
-
3. Enter your OpenAI
|
| 243 |
4. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
| 244 |
---
|
| 245 |
**Disclaimers:**
|
|
@@ -251,7 +171,6 @@ with gr.Blocks() as demo:
|
|
| 251 |
gr.LoginButton()
|
| 252 |
|
| 253 |
openai_key_box = gr.Textbox(label="OpenAI API Key", type="password", placeholder="sk-...", lines=1)
|
| 254 |
-
google_key_box = gr.Textbox(label="Google API Key", type="password", placeholder="AIza...", lines=1)
|
| 255 |
|
| 256 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 257 |
|
|
@@ -260,7 +179,7 @@ with gr.Blocks() as demo:
|
|
| 260 |
|
| 261 |
run_button.click(
|
| 262 |
fn=run_and_submit_all,
|
| 263 |
-
inputs=[openai_key_box
|
| 264 |
outputs=[status_output, results_table]
|
| 265 |
)
|
| 266 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
import requests
|
| 4 |
+
import inspect
|
| 5 |
+
import pandas as pd
|
| 6 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, OpenAIServerModel
|
|
|
|
| 7 |
# (Keep Constants as is)
|
| 8 |
# --- Constants ---
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
|
| 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.")
|
| 17 |
+
# Initialize the model
|
| 18 |
+
#model = HfApiModel()
|
| 19 |
+
model = OpenAIServerModel(model_id="gpt-4.1")
|
| 20 |
+
# Initialize the search tool
|
| 21 |
+
search_tool = DuckDuckGoSearchTool()
|
| 22 |
+
# Initialize Agent
|
| 23 |
+
self.agent = CodeAgent(
|
| 24 |
+
model = model,
|
| 25 |
+
tools=[search_tool]
|
| 26 |
+
)
|
| 27 |
+
def __call__(self, question: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 29 |
+
fixed_answer =self.agent.run(question)
|
| 30 |
+
print(f"Agent returning fixed answer: {fixed_answer}")
|
| 31 |
+
return fixed_answer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 34 |
"""
|
| 35 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 36 |
and displays the results.
|
|
|
|
| 39 |
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
| 40 |
|
| 41 |
if profile:
|
| 42 |
+
username= f"{profile.username}"
|
| 43 |
print(f"User logged in: {username}")
|
| 44 |
else:
|
| 45 |
print("User not logged in.")
|
|
|
|
| 51 |
|
| 52 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 53 |
try:
|
| 54 |
+
agent = BasicAgent()
|
| 55 |
except Exception as e:
|
| 56 |
print(f"Error instantiating agent: {e}")
|
| 57 |
return f"Error initializing agent: {e}", None
|
| 58 |
+
# 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)
|
| 59 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 60 |
print(agent_code)
|
| 61 |
|
|
|
|
| 159 |
**Instructions:**
|
| 160 |
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
| 161 |
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
| 162 |
+
3. Enter your OpenAI key below (if required by your agent).
|
| 163 |
4. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
| 164 |
---
|
| 165 |
**Disclaimers:**
|
|
|
|
| 171 |
gr.LoginButton()
|
| 172 |
|
| 173 |
openai_key_box = gr.Textbox(label="OpenAI API Key", type="password", placeholder="sk-...", lines=1)
|
|
|
|
| 174 |
|
| 175 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 176 |
|
|
|
|
| 179 |
|
| 180 |
run_button.click(
|
| 181 |
fn=run_and_submit_all,
|
| 182 |
+
inputs=[openai_key_box],
|
| 183 |
outputs=[status_output, results_table]
|
| 184 |
)
|
| 185 |
|