Thanh Vinh Vo
commited on
Commit
·
81b21f9
1
Parent(s):
70da83b
update
Browse files- app.py +38 -3
- requirements.txt +1 -0
app.py
CHANGED
|
@@ -1,10 +1,12 @@
|
|
| 1 |
import inspect
|
| 2 |
import json
|
| 3 |
import os
|
|
|
|
| 4 |
|
| 5 |
import gradio as gr
|
| 6 |
import pandas as pd
|
| 7 |
import requests
|
|
|
|
| 8 |
from smolagents import (
|
| 9 |
CodeAgent,
|
| 10 |
DuckDuckGoSearchTool,
|
|
@@ -23,6 +25,36 @@ from smolagents import (
|
|
| 23 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 24 |
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
# --- Basic Agent Definition ---
|
| 27 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 28 |
class BasicAgent:
|
|
@@ -44,14 +76,16 @@ class BasicAgent:
|
|
| 44 |
"markdownify",
|
| 45 |
"wikipedia",
|
| 46 |
"pandas",
|
|
|
|
|
|
|
| 47 |
],
|
| 48 |
)
|
| 49 |
|
| 50 |
-
def __call__(self, question: str) -> str:
|
| 51 |
print(f"Agent received question: {question}")
|
| 52 |
prompt = f"""
|
| 53 |
Answer the following question:
|
| 54 |
-
"{question}"
|
| 55 |
|
| 56 |
Please follow hints below:
|
| 57 |
1. `wikipedia` Python package is provided to interact with Wikipedia.
|
|
@@ -127,11 +161,12 @@ def run_and_submit_all(questions_index: str, profile: gr.OAuthProfile | None):
|
|
| 127 |
print(f"Question data: {json.dumps(item)}")
|
| 128 |
task_id = item.get("task_id")
|
| 129 |
question_text = item.get("question")
|
|
|
|
| 130 |
if not task_id or question_text is None:
|
| 131 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 132 |
continue
|
| 133 |
try:
|
| 134 |
-
submitted_answer = agent(question_text)
|
| 135 |
answers_payload.append(
|
| 136 |
{"task_id": task_id, "submitted_answer": submitted_answer}
|
| 137 |
)
|
|
|
|
| 1 |
import inspect
|
| 2 |
import json
|
| 3 |
import os
|
| 4 |
+
from io import BytesIO
|
| 5 |
|
| 6 |
import gradio as gr
|
| 7 |
import pandas as pd
|
| 8 |
import requests
|
| 9 |
+
from PIL import Image
|
| 10 |
from smolagents import (
|
| 11 |
CodeAgent,
|
| 12 |
DuckDuckGoSearchTool,
|
|
|
|
| 25 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 26 |
|
| 27 |
|
| 28 |
+
@tool
|
| 29 |
+
def get_image_content(file_name: str) -> Image.Image | None:
|
| 30 |
+
"""
|
| 31 |
+
A tool that load the content of an image.
|
| 32 |
+
Args:
|
| 33 |
+
file_name: File name to load.
|
| 34 |
+
"""
|
| 35 |
+
from io import BytesIO
|
| 36 |
+
|
| 37 |
+
import requests
|
| 38 |
+
from PIL import Image
|
| 39 |
+
|
| 40 |
+
try:
|
| 41 |
+
# Construct the API endpoint URL for file retrieval
|
| 42 |
+
file_url = f"{DEFAULT_API_URL}/files/{file_name}"
|
| 43 |
+
|
| 44 |
+
# Make GET request to retrieve file contents
|
| 45 |
+
response = requests.get(file_url, timeout=30)
|
| 46 |
+
response.raise_for_status()
|
| 47 |
+
|
| 48 |
+
image = Image.open(BytesIO(response.content)).convert("RGB")
|
| 49 |
+
# Return the file contents as text
|
| 50 |
+
return image
|
| 51 |
+
|
| 52 |
+
except requests.exceptions.RequestException as e:
|
| 53 |
+
print(f"Error retrieving file '{file_name}': {e}")
|
| 54 |
+
except Exception as e:
|
| 55 |
+
print(f"Unexpected error retrieving file '{file_name}': {e}")
|
| 56 |
+
|
| 57 |
+
|
| 58 |
# --- Basic Agent Definition ---
|
| 59 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 60 |
class BasicAgent:
|
|
|
|
| 76 |
"markdownify",
|
| 77 |
"wikipedia",
|
| 78 |
"pandas",
|
| 79 |
+
"io",
|
| 80 |
+
"PIL",
|
| 81 |
],
|
| 82 |
)
|
| 83 |
|
| 84 |
+
def __call__(self, question: str, file_name: str) -> str:
|
| 85 |
print(f"Agent received question: {question}")
|
| 86 |
prompt = f"""
|
| 87 |
Answer the following question:
|
| 88 |
+
"{question} {", with the following file: " if file_name else ""} {file_name}"
|
| 89 |
|
| 90 |
Please follow hints below:
|
| 91 |
1. `wikipedia` Python package is provided to interact with Wikipedia.
|
|
|
|
| 161 |
print(f"Question data: {json.dumps(item)}")
|
| 162 |
task_id = item.get("task_id")
|
| 163 |
question_text = item.get("question")
|
| 164 |
+
file_name = item.get("file_name")
|
| 165 |
if not task_id or question_text is None:
|
| 166 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 167 |
continue
|
| 168 |
try:
|
| 169 |
+
submitted_answer = agent(question_text, file_name)
|
| 170 |
answers_payload.append(
|
| 171 |
{"task_id": task_id, "submitted_answer": submitted_answer}
|
| 172 |
)
|
requirements.txt
CHANGED
|
@@ -7,3 +7,4 @@ markdownify
|
|
| 7 |
wikipedia
|
| 8 |
pandas
|
| 9 |
openai
|
|
|
|
|
|
| 7 |
wikipedia
|
| 8 |
pandas
|
| 9 |
openai
|
| 10 |
+
PIL
|