Spaces:
Sleeping
Sleeping
Upload 11 files
Browse files- README.md +15 -12
- agent.py +801 -0
- app.py +945 -0
- code_interpreter.py +281 -0
- evaluation_app.py +211 -0
- explore_metadata.ipynb +332 -0
- image_processing.py +26 -0
- metadata.jsonl +0 -0
- requirements.txt +21 -0
- supabase_docs.csv +0 -0
- system_prompt.txt +5 -0
README.md
CHANGED
|
@@ -1,12 +1,15 @@
|
|
| 1 |
-
---
|
| 2 |
-
title: Final
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
-
sdk: gradio
|
| 7 |
-
sdk_version: 5.
|
| 8 |
-
app_file: app.py
|
| 9 |
-
pinned: false
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Template Final Assignment
|
| 3 |
+
emoji: π΅π»ββοΈ
|
| 4 |
+
colorFrom: indigo
|
| 5 |
+
colorTo: indigo
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 5.25.2
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
hf_oauth: true
|
| 11 |
+
# optional, default duration is 8 hours/480 minutes. Max duration is 30 days/43200 minutes.
|
| 12 |
+
hf_oauth_expiration_minutes: 480
|
| 13 |
+
---
|
| 14 |
+
|
| 15 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
agent.py
ADDED
|
@@ -0,0 +1,801 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
from typing import List, Dict, Any, Optional
|
| 4 |
+
import tempfile
|
| 5 |
+
import re
|
| 6 |
+
import json
|
| 7 |
+
import requests
|
| 8 |
+
from urllib.parse import urlparse
|
| 9 |
+
import pytesseract
|
| 10 |
+
from PIL import Image, ImageDraw, ImageFont, ImageEnhance, ImageFilter
|
| 11 |
+
import cmath
|
| 12 |
+
import pandas as pd
|
| 13 |
+
import uuid
|
| 14 |
+
import numpy as np
|
| 15 |
+
from code_interpreter import CodeInterpreter
|
| 16 |
+
|
| 17 |
+
interpreter_instance = CodeInterpreter()
|
| 18 |
+
|
| 19 |
+
from image_processing import *
|
| 20 |
+
|
| 21 |
+
"""Langraph"""
|
| 22 |
+
from langgraph.graph import START, StateGraph, MessagesState
|
| 23 |
+
from langchain_community.tools.tavily_search import TavilySearchResults
|
| 24 |
+
from langchain_community.document_loaders import WikipediaLoader
|
| 25 |
+
from langchain_community.document_loaders import ArxivLoader
|
| 26 |
+
from langgraph.prebuilt import ToolNode, tools_condition
|
| 27 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 28 |
+
from langchain_groq import ChatGroq
|
| 29 |
+
from langchain_huggingface import (
|
| 30 |
+
ChatHuggingFace,
|
| 31 |
+
HuggingFaceEndpoint,
|
| 32 |
+
HuggingFaceEmbeddings,
|
| 33 |
+
)
|
| 34 |
+
from langchain_community.vectorstores import SupabaseVectorStore
|
| 35 |
+
from langchain_core.messages import SystemMessage, HumanMessage
|
| 36 |
+
from langchain_core.tools import tool
|
| 37 |
+
from langchain.tools.retriever import create_retriever_tool
|
| 38 |
+
from supabase.client import Client, create_client
|
| 39 |
+
|
| 40 |
+
load_dotenv()
|
| 41 |
+
|
| 42 |
+
### =============== BROWSER TOOLS =============== ###
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
@tool
|
| 46 |
+
def wiki_search(query: str) -> str:
|
| 47 |
+
"""Search Wikipedia for a query and return maximum 2 results.
|
| 48 |
+
|
| 49 |
+
Args:
|
| 50 |
+
query: The search query."""
|
| 51 |
+
search_docs = WikipediaLoader(query=query, load_max_docs=2).load()
|
| 52 |
+
formatted_search_docs = "\n\n---\n\n".join(
|
| 53 |
+
[
|
| 54 |
+
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
|
| 55 |
+
for doc in search_docs
|
| 56 |
+
]
|
| 57 |
+
)
|
| 58 |
+
return {"wiki_results": formatted_search_docs}
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
@tool
|
| 62 |
+
def web_search(query: str) -> str:
|
| 63 |
+
"""Search Tavily for a query and return maximum 3 results.
|
| 64 |
+
|
| 65 |
+
Args:
|
| 66 |
+
query: The search query."""
|
| 67 |
+
search_docs = TavilySearchResults(max_results=3).invoke(query)
|
| 68 |
+
formatted_search_docs = "\n\n---\n\n".join(
|
| 69 |
+
[
|
| 70 |
+
f'<Document source="{doc.get("url", "")}" title="{doc.get("title", "")}"/>\n{doc.get("content", "")}\n</Document>'
|
| 71 |
+
for doc in search_docs
|
| 72 |
+
]
|
| 73 |
+
)
|
| 74 |
+
return {"web_results": formatted_search_docs}
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
@tool
|
| 78 |
+
def arxiv_search(query: str) -> str:
|
| 79 |
+
"""Search Arxiv for a query and return maximum 3 result.
|
| 80 |
+
|
| 81 |
+
Args:
|
| 82 |
+
query: The search query."""
|
| 83 |
+
search_docs = ArxivLoader(query=query, load_max_docs=3).load()
|
| 84 |
+
formatted_search_docs = "\n\n---\n\n".join(
|
| 85 |
+
[
|
| 86 |
+
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content[:1000]}\n</Document>'
|
| 87 |
+
for doc in search_docs
|
| 88 |
+
]
|
| 89 |
+
)
|
| 90 |
+
return {"arxiv_results": formatted_search_docs}
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
### =============== CODE INTERPRETER TOOLS =============== ###
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
@tool
|
| 97 |
+
def execute_code_multilang(code: str, language: str = "python") -> str:
|
| 98 |
+
"""Execute code in multiple languages (Python, Bash, SQL, C, Java) and return results.
|
| 99 |
+
|
| 100 |
+
Args:
|
| 101 |
+
code (str): The source code to execute.
|
| 102 |
+
language (str): The language of the code. Supported: "python", "bash", "sql", "c", "java".
|
| 103 |
+
|
| 104 |
+
Returns:
|
| 105 |
+
A string summarizing the execution results (stdout, stderr, errors, plots, dataframes if any).
|
| 106 |
+
"""
|
| 107 |
+
supported_languages = ["python", "bash", "sql", "c", "java"]
|
| 108 |
+
language = language.lower()
|
| 109 |
+
|
| 110 |
+
if language not in supported_languages:
|
| 111 |
+
return f"β Unsupported language: {language}. Supported languages are: {', '.join(supported_languages)}"
|
| 112 |
+
|
| 113 |
+
result = interpreter_instance.execute_code(code, language=language)
|
| 114 |
+
|
| 115 |
+
response = []
|
| 116 |
+
|
| 117 |
+
if result["status"] == "success":
|
| 118 |
+
response.append(f"β
Code executed successfully in **{language.upper()}**")
|
| 119 |
+
|
| 120 |
+
if result.get("stdout"):
|
| 121 |
+
response.append(
|
| 122 |
+
"\n**Standard Output:**\n```\n" + result["stdout"].strip() + "\n```"
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
if result.get("stderr"):
|
| 126 |
+
response.append(
|
| 127 |
+
"\n**Standard Error (if any):**\n```\n"
|
| 128 |
+
+ result["stderr"].strip()
|
| 129 |
+
+ "\n```"
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
if result.get("result") is not None:
|
| 133 |
+
response.append(
|
| 134 |
+
"\n**Execution Result:**\n```\n"
|
| 135 |
+
+ str(result["result"]).strip()
|
| 136 |
+
+ "\n```"
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
if result.get("dataframes"):
|
| 140 |
+
for df_info in result["dataframes"]:
|
| 141 |
+
response.append(
|
| 142 |
+
f"\n**DataFrame `{df_info['name']}` (Shape: {df_info['shape']})**"
|
| 143 |
+
)
|
| 144 |
+
df_preview = pd.DataFrame(df_info["head"])
|
| 145 |
+
response.append("First 5 rows:\n```\n" + str(df_preview) + "\n```")
|
| 146 |
+
|
| 147 |
+
if result.get("plots"):
|
| 148 |
+
response.append(
|
| 149 |
+
f"\n**Generated {len(result['plots'])} plot(s)** (Image data returned separately)"
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
else:
|
| 153 |
+
response.append(f"β Code execution failed in **{language.upper()}**")
|
| 154 |
+
if result.get("stderr"):
|
| 155 |
+
response.append(
|
| 156 |
+
"\n**Error Log:**\n```\n" + result["stderr"].strip() + "\n```"
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
return "\n".join(response)
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
### =============== MATHEMATICAL TOOLS =============== ###
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
@tool
|
| 166 |
+
def multiply(a: float, b: float) -> float:
|
| 167 |
+
"""
|
| 168 |
+
Multiplies two numbers.
|
| 169 |
+
|
| 170 |
+
Args:
|
| 171 |
+
a (float): the first number
|
| 172 |
+
b (float): the second number
|
| 173 |
+
"""
|
| 174 |
+
return a * b
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
@tool
|
| 178 |
+
def add(a: float, b: float) -> float:
|
| 179 |
+
"""
|
| 180 |
+
Adds two numbers.
|
| 181 |
+
|
| 182 |
+
Args:
|
| 183 |
+
a (float): the first number
|
| 184 |
+
b (float): the second number
|
| 185 |
+
"""
|
| 186 |
+
return a + b
|
| 187 |
+
|
| 188 |
+
|
| 189 |
+
@tool
|
| 190 |
+
def subtract(a: float, b: float) -> int:
|
| 191 |
+
"""
|
| 192 |
+
Subtracts two numbers.
|
| 193 |
+
|
| 194 |
+
Args:
|
| 195 |
+
a (float): the first number
|
| 196 |
+
b (float): the second number
|
| 197 |
+
"""
|
| 198 |
+
return a - b
|
| 199 |
+
|
| 200 |
+
|
| 201 |
+
@tool
|
| 202 |
+
def divide(a: float, b: float) -> float:
|
| 203 |
+
"""
|
| 204 |
+
Divides two numbers.
|
| 205 |
+
|
| 206 |
+
Args:
|
| 207 |
+
a (float): the first float number
|
| 208 |
+
b (float): the second float number
|
| 209 |
+
"""
|
| 210 |
+
if b == 0:
|
| 211 |
+
raise ValueError("Cannot divided by zero.")
|
| 212 |
+
return a / b
|
| 213 |
+
|
| 214 |
+
|
| 215 |
+
@tool
|
| 216 |
+
def modulus(a: int, b: int) -> int:
|
| 217 |
+
"""
|
| 218 |
+
Get the modulus of two numbers.
|
| 219 |
+
|
| 220 |
+
Args:
|
| 221 |
+
a (int): the first number
|
| 222 |
+
b (int): the second number
|
| 223 |
+
"""
|
| 224 |
+
return a % b
|
| 225 |
+
|
| 226 |
+
|
| 227 |
+
@tool
|
| 228 |
+
def power(a: float, b: float) -> float:
|
| 229 |
+
"""
|
| 230 |
+
Get the power of two numbers.
|
| 231 |
+
|
| 232 |
+
Args:
|
| 233 |
+
a (float): the first number
|
| 234 |
+
b (float): the second number
|
| 235 |
+
"""
|
| 236 |
+
return a**b
|
| 237 |
+
|
| 238 |
+
|
| 239 |
+
@tool
|
| 240 |
+
def square_root(a: float) -> float | complex:
|
| 241 |
+
"""
|
| 242 |
+
Get the square root of a number.
|
| 243 |
+
|
| 244 |
+
Args:
|
| 245 |
+
a (float): the number to get the square root of
|
| 246 |
+
"""
|
| 247 |
+
if a >= 0:
|
| 248 |
+
return a**0.5
|
| 249 |
+
return cmath.sqrt(a)
|
| 250 |
+
|
| 251 |
+
|
| 252 |
+
### =============== DOCUMENT PROCESSING TOOLS =============== ###
|
| 253 |
+
|
| 254 |
+
|
| 255 |
+
@tool
|
| 256 |
+
def save_and_read_file(content: str, filename: Optional[str] = None) -> str:
|
| 257 |
+
"""
|
| 258 |
+
Save content to a file and return the path.
|
| 259 |
+
|
| 260 |
+
Args:
|
| 261 |
+
content (str): the content to save to the file
|
| 262 |
+
filename (str, optional): the name of the file. If not provided, a random name file will be created.
|
| 263 |
+
"""
|
| 264 |
+
temp_dir = tempfile.gettempdir()
|
| 265 |
+
if filename is None:
|
| 266 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, dir=temp_dir)
|
| 267 |
+
filepath = temp_file.name
|
| 268 |
+
else:
|
| 269 |
+
filepath = os.path.join(temp_dir, filename)
|
| 270 |
+
|
| 271 |
+
with open(filepath, "w") as f:
|
| 272 |
+
f.write(content)
|
| 273 |
+
|
| 274 |
+
return f"File saved to {filepath}. You can read this file to process its contents."
|
| 275 |
+
|
| 276 |
+
|
| 277 |
+
@tool
|
| 278 |
+
def download_file_from_url(url: str, filename: Optional[str] = None) -> str:
|
| 279 |
+
"""
|
| 280 |
+
Download a file from a URL and save it to a temporary location.
|
| 281 |
+
|
| 282 |
+
Args:
|
| 283 |
+
url (str): the URL of the file to download.
|
| 284 |
+
filename (str, optional): the name of the file. If not provided, a random name file will be created.
|
| 285 |
+
"""
|
| 286 |
+
try:
|
| 287 |
+
# Parse URL to get filename if not provided
|
| 288 |
+
if not filename:
|
| 289 |
+
path = urlparse(url).path
|
| 290 |
+
filename = os.path.basename(path)
|
| 291 |
+
if not filename:
|
| 292 |
+
filename = f"downloaded_{uuid.uuid4().hex[:8]}"
|
| 293 |
+
|
| 294 |
+
# Create temporary file
|
| 295 |
+
temp_dir = tempfile.gettempdir()
|
| 296 |
+
filepath = os.path.join(temp_dir, filename)
|
| 297 |
+
|
| 298 |
+
# Download the file
|
| 299 |
+
response = requests.get(url, stream=True)
|
| 300 |
+
response.raise_for_status()
|
| 301 |
+
|
| 302 |
+
# Save the file
|
| 303 |
+
with open(filepath, "wb") as f:
|
| 304 |
+
for chunk in response.iter_content(chunk_size=8192):
|
| 305 |
+
f.write(chunk)
|
| 306 |
+
|
| 307 |
+
return f"File downloaded to {filepath}. You can read this file to process its contents."
|
| 308 |
+
except Exception as e:
|
| 309 |
+
return f"Error downloading file: {str(e)}"
|
| 310 |
+
|
| 311 |
+
|
| 312 |
+
@tool
|
| 313 |
+
def extract_text_from_image(image_path: str) -> str:
|
| 314 |
+
"""
|
| 315 |
+
Extract text from an image using OCR library pytesseract (if available).
|
| 316 |
+
|
| 317 |
+
Args:
|
| 318 |
+
image_path (str): the path to the image file.
|
| 319 |
+
"""
|
| 320 |
+
try:
|
| 321 |
+
# Open the image
|
| 322 |
+
image = Image.open(image_path)
|
| 323 |
+
|
| 324 |
+
# Extract text from the image
|
| 325 |
+
text = pytesseract.image_to_string(image)
|
| 326 |
+
|
| 327 |
+
return f"Extracted text from image:\n\n{text}"
|
| 328 |
+
except Exception as e:
|
| 329 |
+
return f"Error extracting text from image: {str(e)}"
|
| 330 |
+
|
| 331 |
+
|
| 332 |
+
@tool
|
| 333 |
+
def analyze_csv_file(file_path: str, query: str) -> str:
|
| 334 |
+
"""
|
| 335 |
+
Analyze a CSV file using pandas and answer a question about it.
|
| 336 |
+
|
| 337 |
+
Args:
|
| 338 |
+
file_path (str): the path to the CSV file.
|
| 339 |
+
query (str): Question about the data
|
| 340 |
+
"""
|
| 341 |
+
try:
|
| 342 |
+
# Read the CSV file
|
| 343 |
+
df = pd.read_csv(file_path)
|
| 344 |
+
|
| 345 |
+
# Run various analyses based on the query
|
| 346 |
+
result = f"CSV file loaded with {len(df)} rows and {len(df.columns)} columns.\n"
|
| 347 |
+
result += f"Columns: {', '.join(df.columns)}\n\n"
|
| 348 |
+
|
| 349 |
+
# Add summary statistics
|
| 350 |
+
result += "Summary statistics:\n"
|
| 351 |
+
result += str(df.describe())
|
| 352 |
+
|
| 353 |
+
return result
|
| 354 |
+
|
| 355 |
+
except Exception as e:
|
| 356 |
+
return f"Error analyzing CSV file: {str(e)}"
|
| 357 |
+
|
| 358 |
+
|
| 359 |
+
@tool
|
| 360 |
+
def analyze_excel_file(file_path: str, query: str) -> str:
|
| 361 |
+
"""
|
| 362 |
+
Analyze an Excel file using pandas and answer a question about it.
|
| 363 |
+
|
| 364 |
+
Args:
|
| 365 |
+
file_path (str): the path to the Excel file.
|
| 366 |
+
query (str): Question about the data
|
| 367 |
+
"""
|
| 368 |
+
try:
|
| 369 |
+
# Read the Excel file
|
| 370 |
+
df = pd.read_excel(file_path)
|
| 371 |
+
|
| 372 |
+
# Run various analyses based on the query
|
| 373 |
+
result = (
|
| 374 |
+
f"Excel file loaded with {len(df)} rows and {len(df.columns)} columns.\n"
|
| 375 |
+
)
|
| 376 |
+
result += f"Columns: {', '.join(df.columns)}\n\n"
|
| 377 |
+
|
| 378 |
+
# Add summary statistics
|
| 379 |
+
result += "Summary statistics:\n"
|
| 380 |
+
result += str(df.describe())
|
| 381 |
+
|
| 382 |
+
return result
|
| 383 |
+
|
| 384 |
+
except Exception as e:
|
| 385 |
+
return f"Error analyzing Excel file: {str(e)}"
|
| 386 |
+
|
| 387 |
+
|
| 388 |
+
### ============== IMAGE PROCESSING AND GENERATION TOOLS =============== ###
|
| 389 |
+
|
| 390 |
+
|
| 391 |
+
@tool
|
| 392 |
+
def analyze_image(image_base64: str) -> Dict[str, Any]:
|
| 393 |
+
"""
|
| 394 |
+
Analyze basic properties of an image (size, mode, color analysis, thumbnail preview).
|
| 395 |
+
|
| 396 |
+
Args:
|
| 397 |
+
image_base64 (str): Base64 encoded image string
|
| 398 |
+
|
| 399 |
+
Returns:
|
| 400 |
+
Dictionary with analysis result
|
| 401 |
+
"""
|
| 402 |
+
try:
|
| 403 |
+
img = decode_image(image_base64)
|
| 404 |
+
width, height = img.size
|
| 405 |
+
mode = img.mode
|
| 406 |
+
|
| 407 |
+
if mode in ("RGB", "RGBA"):
|
| 408 |
+
arr = np.array(img)
|
| 409 |
+
avg_colors = arr.mean(axis=(0, 1))
|
| 410 |
+
dominant = ["Red", "Green", "Blue"][np.argmax(avg_colors[:3])]
|
| 411 |
+
brightness = avg_colors.mean()
|
| 412 |
+
color_analysis = {
|
| 413 |
+
"average_rgb": avg_colors.tolist(),
|
| 414 |
+
"brightness": brightness,
|
| 415 |
+
"dominant_color": dominant,
|
| 416 |
+
}
|
| 417 |
+
else:
|
| 418 |
+
color_analysis = {"note": f"No color analysis for mode {mode}"}
|
| 419 |
+
|
| 420 |
+
thumbnail = img.copy()
|
| 421 |
+
thumbnail.thumbnail((100, 100))
|
| 422 |
+
thumb_path = save_image(thumbnail, "thumbnails")
|
| 423 |
+
thumbnail_base64 = encode_image(thumb_path)
|
| 424 |
+
|
| 425 |
+
return {
|
| 426 |
+
"dimensions": (width, height),
|
| 427 |
+
"mode": mode,
|
| 428 |
+
"color_analysis": color_analysis,
|
| 429 |
+
"thumbnail": thumbnail_base64,
|
| 430 |
+
}
|
| 431 |
+
except Exception as e:
|
| 432 |
+
return {"error": str(e)}
|
| 433 |
+
|
| 434 |
+
|
| 435 |
+
@tool
|
| 436 |
+
def transform_image(
|
| 437 |
+
image_base64: str, operation: str, params: Optional[Dict[str, Any]] = None
|
| 438 |
+
) -> Dict[str, Any]:
|
| 439 |
+
"""
|
| 440 |
+
Apply transformations: resize, rotate, crop, flip, brightness, contrast, blur, sharpen, grayscale.
|
| 441 |
+
|
| 442 |
+
Args:
|
| 443 |
+
image_base64 (str): Base64 encoded input image
|
| 444 |
+
operation (str): Transformation operation
|
| 445 |
+
params (Dict[str, Any], optional): Parameters for the operation
|
| 446 |
+
|
| 447 |
+
Returns:
|
| 448 |
+
Dictionary with transformed image (base64)
|
| 449 |
+
"""
|
| 450 |
+
try:
|
| 451 |
+
img = decode_image(image_base64)
|
| 452 |
+
params = params or {}
|
| 453 |
+
|
| 454 |
+
if operation == "resize":
|
| 455 |
+
img = img.resize(
|
| 456 |
+
(
|
| 457 |
+
params.get("width", img.width // 2),
|
| 458 |
+
params.get("height", img.height // 2),
|
| 459 |
+
)
|
| 460 |
+
)
|
| 461 |
+
elif operation == "rotate":
|
| 462 |
+
img = img.rotate(params.get("angle", 90), expand=True)
|
| 463 |
+
elif operation == "crop":
|
| 464 |
+
img = img.crop(
|
| 465 |
+
(
|
| 466 |
+
params.get("left", 0),
|
| 467 |
+
params.get("top", 0),
|
| 468 |
+
params.get("right", img.width),
|
| 469 |
+
params.get("bottom", img.height),
|
| 470 |
+
)
|
| 471 |
+
)
|
| 472 |
+
elif operation == "flip":
|
| 473 |
+
if params.get("direction", "horizontal") == "horizontal":
|
| 474 |
+
img = img.transpose(Image.FLIP_LEFT_RIGHT)
|
| 475 |
+
else:
|
| 476 |
+
img = img.transpose(Image.FLIP_TOP_BOTTOM)
|
| 477 |
+
elif operation == "adjust_brightness":
|
| 478 |
+
img = ImageEnhance.Brightness(img).enhance(params.get("factor", 1.5))
|
| 479 |
+
elif operation == "adjust_contrast":
|
| 480 |
+
img = ImageEnhance.Contrast(img).enhance(params.get("factor", 1.5))
|
| 481 |
+
elif operation == "blur":
|
| 482 |
+
img = img.filter(ImageFilter.GaussianBlur(params.get("radius", 2)))
|
| 483 |
+
elif operation == "sharpen":
|
| 484 |
+
img = img.filter(ImageFilter.SHARPEN)
|
| 485 |
+
elif operation == "grayscale":
|
| 486 |
+
img = img.convert("L")
|
| 487 |
+
else:
|
| 488 |
+
return {"error": f"Unknown operation: {operation}"}
|
| 489 |
+
|
| 490 |
+
result_path = save_image(img)
|
| 491 |
+
result_base64 = encode_image(result_path)
|
| 492 |
+
return {"transformed_image": result_base64}
|
| 493 |
+
|
| 494 |
+
except Exception as e:
|
| 495 |
+
return {"error": str(e)}
|
| 496 |
+
|
| 497 |
+
|
| 498 |
+
@tool
|
| 499 |
+
def draw_on_image(
|
| 500 |
+
image_base64: str, drawing_type: str, params: Dict[str, Any]
|
| 501 |
+
) -> Dict[str, Any]:
|
| 502 |
+
"""
|
| 503 |
+
Draw shapes (rectangle, circle, line) or text onto an image.
|
| 504 |
+
|
| 505 |
+
Args:
|
| 506 |
+
image_base64 (str): Base64 encoded input image
|
| 507 |
+
drawing_type (str): Drawing type
|
| 508 |
+
params (Dict[str, Any]): Drawing parameters
|
| 509 |
+
|
| 510 |
+
Returns:
|
| 511 |
+
Dictionary with result image (base64)
|
| 512 |
+
"""
|
| 513 |
+
try:
|
| 514 |
+
img = decode_image(image_base64)
|
| 515 |
+
draw = ImageDraw.Draw(img)
|
| 516 |
+
color = params.get("color", "red")
|
| 517 |
+
|
| 518 |
+
if drawing_type == "rectangle":
|
| 519 |
+
draw.rectangle(
|
| 520 |
+
[params["left"], params["top"], params["right"], params["bottom"]],
|
| 521 |
+
outline=color,
|
| 522 |
+
width=params.get("width", 2),
|
| 523 |
+
)
|
| 524 |
+
elif drawing_type == "circle":
|
| 525 |
+
x, y, r = params["x"], params["y"], params["radius"]
|
| 526 |
+
draw.ellipse(
|
| 527 |
+
(x - r, y - r, x + r, y + r),
|
| 528 |
+
outline=color,
|
| 529 |
+
width=params.get("width", 2),
|
| 530 |
+
)
|
| 531 |
+
elif drawing_type == "line":
|
| 532 |
+
draw.line(
|
| 533 |
+
(
|
| 534 |
+
params["start_x"],
|
| 535 |
+
params["start_y"],
|
| 536 |
+
params["end_x"],
|
| 537 |
+
params["end_y"],
|
| 538 |
+
),
|
| 539 |
+
fill=color,
|
| 540 |
+
width=params.get("width", 2),
|
| 541 |
+
)
|
| 542 |
+
elif drawing_type == "text":
|
| 543 |
+
font_size = params.get("font_size", 20)
|
| 544 |
+
try:
|
| 545 |
+
font = ImageFont.truetype("arial.ttf", font_size)
|
| 546 |
+
except IOError:
|
| 547 |
+
font = ImageFont.load_default()
|
| 548 |
+
draw.text(
|
| 549 |
+
(params["x"], params["y"]),
|
| 550 |
+
params.get("text", "Text"),
|
| 551 |
+
fill=color,
|
| 552 |
+
font=font,
|
| 553 |
+
)
|
| 554 |
+
else:
|
| 555 |
+
return {"error": f"Unknown drawing type: {drawing_type}"}
|
| 556 |
+
|
| 557 |
+
result_path = save_image(img)
|
| 558 |
+
result_base64 = encode_image(result_path)
|
| 559 |
+
return {"result_image": result_base64}
|
| 560 |
+
|
| 561 |
+
except Exception as e:
|
| 562 |
+
return {"error": str(e)}
|
| 563 |
+
|
| 564 |
+
|
| 565 |
+
@tool
|
| 566 |
+
def generate_simple_image(
|
| 567 |
+
image_type: str,
|
| 568 |
+
width: int = 500,
|
| 569 |
+
height: int = 500,
|
| 570 |
+
params: Optional[Dict[str, Any]] = None,
|
| 571 |
+
) -> Dict[str, Any]:
|
| 572 |
+
"""
|
| 573 |
+
Generate a simple image (gradient, noise, pattern, chart).
|
| 574 |
+
|
| 575 |
+
Args:
|
| 576 |
+
image_type (str): Type of image
|
| 577 |
+
width (int), height (int)
|
| 578 |
+
params (Dict[str, Any], optional): Specific parameters
|
| 579 |
+
|
| 580 |
+
Returns:
|
| 581 |
+
Dictionary with generated image (base64)
|
| 582 |
+
"""
|
| 583 |
+
try:
|
| 584 |
+
params = params or {}
|
| 585 |
+
|
| 586 |
+
if image_type == "gradient":
|
| 587 |
+
direction = params.get("direction", "horizontal")
|
| 588 |
+
start_color = params.get("start_color", (255, 0, 0))
|
| 589 |
+
end_color = params.get("end_color", (0, 0, 255))
|
| 590 |
+
|
| 591 |
+
img = Image.new("RGB", (width, height))
|
| 592 |
+
draw = ImageDraw.Draw(img)
|
| 593 |
+
|
| 594 |
+
if direction == "horizontal":
|
| 595 |
+
for x in range(width):
|
| 596 |
+
r = int(
|
| 597 |
+
start_color[0] + (end_color[0] - start_color[0]) * x / width
|
| 598 |
+
)
|
| 599 |
+
g = int(
|
| 600 |
+
start_color[1] + (end_color[1] - start_color[1]) * x / width
|
| 601 |
+
)
|
| 602 |
+
b = int(
|
| 603 |
+
start_color[2] + (end_color[2] - start_color[2]) * x / width
|
| 604 |
+
)
|
| 605 |
+
draw.line([(x, 0), (x, height)], fill=(r, g, b))
|
| 606 |
+
else:
|
| 607 |
+
for y in range(height):
|
| 608 |
+
r = int(
|
| 609 |
+
start_color[0] + (end_color[0] - start_color[0]) * y / height
|
| 610 |
+
)
|
| 611 |
+
g = int(
|
| 612 |
+
start_color[1] + (end_color[1] - start_color[1]) * y / height
|
| 613 |
+
)
|
| 614 |
+
b = int(
|
| 615 |
+
start_color[2] + (end_color[2] - start_color[2]) * y / height
|
| 616 |
+
)
|
| 617 |
+
draw.line([(0, y), (width, y)], fill=(r, g, b))
|
| 618 |
+
|
| 619 |
+
elif image_type == "noise":
|
| 620 |
+
noise_array = np.random.randint(0, 256, (height, width, 3), dtype=np.uint8)
|
| 621 |
+
img = Image.fromarray(noise_array, "RGB")
|
| 622 |
+
|
| 623 |
+
else:
|
| 624 |
+
return {"error": f"Unsupported image_type {image_type}"}
|
| 625 |
+
|
| 626 |
+
result_path = save_image(img)
|
| 627 |
+
result_base64 = encode_image(result_path)
|
| 628 |
+
return {"generated_image": result_base64}
|
| 629 |
+
|
| 630 |
+
except Exception as e:
|
| 631 |
+
return {"error": str(e)}
|
| 632 |
+
|
| 633 |
+
|
| 634 |
+
@tool
|
| 635 |
+
def combine_images(
|
| 636 |
+
images_base64: List[str], operation: str, params: Optional[Dict[str, Any]] = None
|
| 637 |
+
) -> Dict[str, Any]:
|
| 638 |
+
"""
|
| 639 |
+
Combine multiple images (collage, stack, blend).
|
| 640 |
+
|
| 641 |
+
Args:
|
| 642 |
+
images_base64 (List[str]): List of base64 images
|
| 643 |
+
operation (str): Combination type
|
| 644 |
+
params (Dict[str, Any], optional)
|
| 645 |
+
|
| 646 |
+
Returns:
|
| 647 |
+
Dictionary with combined image (base64)
|
| 648 |
+
"""
|
| 649 |
+
try:
|
| 650 |
+
images = [decode_image(b64) for b64 in images_base64]
|
| 651 |
+
params = params or {}
|
| 652 |
+
|
| 653 |
+
if operation == "stack":
|
| 654 |
+
direction = params.get("direction", "horizontal")
|
| 655 |
+
if direction == "horizontal":
|
| 656 |
+
total_width = sum(img.width for img in images)
|
| 657 |
+
max_height = max(img.height for img in images)
|
| 658 |
+
new_img = Image.new("RGB", (total_width, max_height))
|
| 659 |
+
x = 0
|
| 660 |
+
for img in images:
|
| 661 |
+
new_img.paste(img, (x, 0))
|
| 662 |
+
x += img.width
|
| 663 |
+
else:
|
| 664 |
+
max_width = max(img.width for img in images)
|
| 665 |
+
total_height = sum(img.height for img in images)
|
| 666 |
+
new_img = Image.new("RGB", (max_width, total_height))
|
| 667 |
+
y = 0
|
| 668 |
+
for img in images:
|
| 669 |
+
new_img.paste(img, (0, y))
|
| 670 |
+
y += img.height
|
| 671 |
+
else:
|
| 672 |
+
return {"error": f"Unsupported combination operation {operation}"}
|
| 673 |
+
|
| 674 |
+
result_path = save_image(new_img)
|
| 675 |
+
result_base64 = encode_image(result_path)
|
| 676 |
+
return {"combined_image": result_base64}
|
| 677 |
+
|
| 678 |
+
except Exception as e:
|
| 679 |
+
return {"error": str(e)}
|
| 680 |
+
|
| 681 |
+
|
| 682 |
+
# load the system prompt from the file
|
| 683 |
+
with open("system_prompt.txt", "r", encoding="utf-8") as f:
|
| 684 |
+
system_prompt = f.read()
|
| 685 |
+
print(system_prompt)
|
| 686 |
+
|
| 687 |
+
# System message
|
| 688 |
+
sys_msg = SystemMessage(content=system_prompt)
|
| 689 |
+
|
| 690 |
+
# build a retriever
|
| 691 |
+
embeddings = HuggingFaceEmbeddings(
|
| 692 |
+
model_name="sentence-transformers/all-mpnet-base-v2"
|
| 693 |
+
) # dim=768
|
| 694 |
+
supabase: Client = create_client(
|
| 695 |
+
os.environ.get("SUPABASE_URL"), os.environ.get("SUPABASE_SERVICE_ROLE_KEY")
|
| 696 |
+
)
|
| 697 |
+
vector_store = SupabaseVectorStore(
|
| 698 |
+
client=supabase,
|
| 699 |
+
embedding=embeddings,
|
| 700 |
+
table_name="documents2",
|
| 701 |
+
query_name="match_documents_2",
|
| 702 |
+
)
|
| 703 |
+
create_retriever_tool = create_retriever_tool(
|
| 704 |
+
retriever=vector_store.as_retriever(),
|
| 705 |
+
name="Question Search",
|
| 706 |
+
description="A tool to retrieve similar questions from a vector store.",
|
| 707 |
+
)
|
| 708 |
+
|
| 709 |
+
|
| 710 |
+
tools = [
|
| 711 |
+
web_search,
|
| 712 |
+
wiki_search,
|
| 713 |
+
arxiv_search,
|
| 714 |
+
multiply,
|
| 715 |
+
add,
|
| 716 |
+
subtract,
|
| 717 |
+
divide,
|
| 718 |
+
modulus,
|
| 719 |
+
power,
|
| 720 |
+
square_root,
|
| 721 |
+
save_and_read_file,
|
| 722 |
+
download_file_from_url,
|
| 723 |
+
extract_text_from_image,
|
| 724 |
+
analyze_csv_file,
|
| 725 |
+
analyze_excel_file,
|
| 726 |
+
execute_code_multilang,
|
| 727 |
+
analyze_image,
|
| 728 |
+
transform_image,
|
| 729 |
+
draw_on_image,
|
| 730 |
+
generate_simple_image,
|
| 731 |
+
combine_images,
|
| 732 |
+
]
|
| 733 |
+
|
| 734 |
+
|
| 735 |
+
# Build graph function
|
| 736 |
+
def build_graph(provider: str = "groq"):
|
| 737 |
+
"""Build the graph"""
|
| 738 |
+
# Load environment variables from .env file
|
| 739 |
+
if provider == "groq":
|
| 740 |
+
# Groq https://console.groq.com/docs/models
|
| 741 |
+
llm = ChatGroq(model="qwen/qwen3-32b", temperature=0)
|
| 742 |
+
elif provider == "huggingface":
|
| 743 |
+
# TODO: Add huggingface endpoint
|
| 744 |
+
llm = ChatHuggingFace(
|
| 745 |
+
llm=HuggingFaceEndpoint(
|
| 746 |
+
repo_id="TinyLlama/TinyLlama-1.1B-Chat-v1.0",
|
| 747 |
+
task="text-generation", # for chatβstyle use βtext-generationβ
|
| 748 |
+
max_new_tokens=1024,
|
| 749 |
+
do_sample=False,
|
| 750 |
+
repetition_penalty=1.03,
|
| 751 |
+
temperature=0,
|
| 752 |
+
),
|
| 753 |
+
verbose=True,
|
| 754 |
+
)
|
| 755 |
+
else:
|
| 756 |
+
raise ValueError("Invalid provider. Choose 'groq' or 'huggingface'.")
|
| 757 |
+
# Bind tools to LLM
|
| 758 |
+
llm_with_tools = llm.bind_tools(tools)
|
| 759 |
+
|
| 760 |
+
# Node
|
| 761 |
+
def assistant(state: MessagesState):
|
| 762 |
+
"""Assistant node"""
|
| 763 |
+
return {"messages": [llm_with_tools.invoke(state["messages"])]}
|
| 764 |
+
|
| 765 |
+
def retriever(state: MessagesState):
|
| 766 |
+
"""Retriever node"""
|
| 767 |
+
similar_question = vector_store.similarity_search(state["messages"][0].content)
|
| 768 |
+
|
| 769 |
+
if similar_question: # Check if the list is not empty
|
| 770 |
+
example_msg = HumanMessage(
|
| 771 |
+
content=f"Here I provide a similar question and answer for reference: \n\n{similar_question[0].page_content}",
|
| 772 |
+
)
|
| 773 |
+
return {"messages": [sys_msg] + state["messages"] + [example_msg]}
|
| 774 |
+
else:
|
| 775 |
+
# Handle the case when no similar questions are found
|
| 776 |
+
return {"messages": [sys_msg] + state["messages"]}
|
| 777 |
+
|
| 778 |
+
builder = StateGraph(MessagesState)
|
| 779 |
+
builder.add_node("retriever", retriever)
|
| 780 |
+
builder.add_node("assistant", assistant)
|
| 781 |
+
builder.add_node("tools", ToolNode(tools))
|
| 782 |
+
builder.add_edge(START, "retriever")
|
| 783 |
+
builder.add_edge("retriever", "assistant")
|
| 784 |
+
builder.add_conditional_edges(
|
| 785 |
+
"assistant",
|
| 786 |
+
tools_condition,
|
| 787 |
+
)
|
| 788 |
+
builder.add_edge("tools", "assistant")
|
| 789 |
+
|
| 790 |
+
# Compile graph
|
| 791 |
+
return builder.compile()
|
| 792 |
+
|
| 793 |
+
|
| 794 |
+
# test
|
| 795 |
+
if __name__ == "__main__":
|
| 796 |
+
question = "When was a picture of St. Thomas Aquinas first added to the Wikipedia page on the Principle of double effect?"
|
| 797 |
+
graph = build_graph(provider="groq")
|
| 798 |
+
messages = [HumanMessage(content=question)]
|
| 799 |
+
messages = graph.invoke({"messages": messages})
|
| 800 |
+
for m in messages["messages"]:
|
| 801 |
+
m.pretty_print()
|
app.py
ADDED
|
@@ -0,0 +1,945 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import time
|
| 3 |
+
import os
|
| 4 |
+
import base64
|
| 5 |
+
from typing import List, Tuple, Optional
|
| 6 |
+
from langchain_core.messages import HumanMessage
|
| 7 |
+
from agent import build_graph
|
| 8 |
+
|
| 9 |
+
class QnAChatbot:
|
| 10 |
+
"""A Q&A chatbot interface for the agent."""
|
| 11 |
+
|
| 12 |
+
def __init__(self):
|
| 13 |
+
print("π€ QnAChatbot initializing...")
|
| 14 |
+
print("π§ Building agent graph...")
|
| 15 |
+
self.graph = build_graph()
|
| 16 |
+
self.conversation_history = []
|
| 17 |
+
print("β
QnAChatbot initialized successfully")
|
| 18 |
+
|
| 19 |
+
def process_question(self, question: str, history: List[Tuple[str, str]], uploaded_files: Optional[List] = None) -> Tuple[str, List[Tuple[str, str]]]:
|
| 20 |
+
"""Process a question and return the response with updated history."""
|
| 21 |
+
if not question.strip() and not uploaded_files:
|
| 22 |
+
print("β οΈ No question or files provided")
|
| 23 |
+
return "", history
|
| 24 |
+
|
| 25 |
+
try:
|
| 26 |
+
print(f"\n{'='*60}")
|
| 27 |
+
print(f"π€ Processing new question...")
|
| 28 |
+
print(f"π Question: {question[:100]}{'...' if len(question) > 100 else ''}")
|
| 29 |
+
print(f"π Files uploaded: {len(uploaded_files) if uploaded_files else 0}")
|
| 30 |
+
|
| 31 |
+
# Handle uploaded files
|
| 32 |
+
file_context = ""
|
| 33 |
+
if uploaded_files:
|
| 34 |
+
print(f"π Processing {len(uploaded_files)} uploaded file(s)...")
|
| 35 |
+
file_context = self._process_uploaded_files(uploaded_files)
|
| 36 |
+
if file_context:
|
| 37 |
+
original_question = question
|
| 38 |
+
question = f"{question}\n\n{file_context}" if question.strip() else file_context
|
| 39 |
+
print(f"π File context added to question (length: {len(file_context)} chars)")
|
| 40 |
+
|
| 41 |
+
# Wrap the question in a HumanMessage
|
| 42 |
+
messages = [HumanMessage(content=question)]
|
| 43 |
+
print(f"π Invoking agent graph...")
|
| 44 |
+
|
| 45 |
+
# Get response from the agent
|
| 46 |
+
result = self.graph.invoke({"messages": messages})
|
| 47 |
+
print(f"π¨ Received {len(result['messages'])} message(s) from agent")
|
| 48 |
+
|
| 49 |
+
# Print all messages for debugging
|
| 50 |
+
for i, msg in enumerate(result['messages']):
|
| 51 |
+
print(f"π§ Message {i+1}: {type(msg).__name__}")
|
| 52 |
+
if hasattr(msg, 'content'):
|
| 53 |
+
content_preview = msg.content[:200] + "..." if len(msg.content) > 200 else msg.content
|
| 54 |
+
print(f" Content preview: {content_preview}")
|
| 55 |
+
|
| 56 |
+
answer = result['messages'][-1].content
|
| 57 |
+
|
| 58 |
+
# Clean up the answer if it starts with "Assistant: "
|
| 59 |
+
if answer.startswith("Assistant: "):
|
| 60 |
+
answer = answer[11:]
|
| 61 |
+
print("π§Ή Cleaned 'Assistant: ' prefix from response")
|
| 62 |
+
|
| 63 |
+
# Update conversation history
|
| 64 |
+
history.append((question, answer))
|
| 65 |
+
print(f"β
Question processed successfully")
|
| 66 |
+
print(f"π Response length: {len(answer)} characters")
|
| 67 |
+
print(f"π¬ Total conversation history: {len(history)} exchanges")
|
| 68 |
+
print(f"{'='*60}\n")
|
| 69 |
+
|
| 70 |
+
return "", history
|
| 71 |
+
|
| 72 |
+
except Exception as e:
|
| 73 |
+
error_msg = f"Error processing question: {str(e)}"
|
| 74 |
+
print(f"β {error_msg}")
|
| 75 |
+
print(f"π Exception details: {type(e).__name__}: {str(e)}")
|
| 76 |
+
import traceback
|
| 77 |
+
print(f"π Traceback:\n{traceback.format_exc()}")
|
| 78 |
+
history.append((question, error_msg))
|
| 79 |
+
print(f"{'='*60}\n")
|
| 80 |
+
return "", history
|
| 81 |
+
|
| 82 |
+
def _process_uploaded_files(self, uploaded_files: List) -> str:
|
| 83 |
+
"""Process uploaded files and return context for the question."""
|
| 84 |
+
file_contexts = []
|
| 85 |
+
|
| 86 |
+
for file_path in uploaded_files:
|
| 87 |
+
if not file_path or not os.path.exists(file_path):
|
| 88 |
+
print(f"β οΈ Skipping invalid file path: {file_path}")
|
| 89 |
+
continue
|
| 90 |
+
|
| 91 |
+
try:
|
| 92 |
+
file_name = os.path.basename(file_path)
|
| 93 |
+
file_ext = os.path.splitext(file_name)[1].lower()
|
| 94 |
+
file_size = os.path.getsize(file_path)
|
| 95 |
+
|
| 96 |
+
print(f"π Processing file: {file_name} ({file_size} bytes, {file_ext})")
|
| 97 |
+
|
| 98 |
+
# Handle different file types
|
| 99 |
+
if file_ext in ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp']:
|
| 100 |
+
# Image file - convert to base64
|
| 101 |
+
with open(file_path, 'rb') as f:
|
| 102 |
+
image_data = base64.b64encode(f.read()).decode('utf-8')
|
| 103 |
+
file_contexts.append(f"[UPLOADED IMAGE: {file_name}] - Base64 data: {image_data}")
|
| 104 |
+
print(f"πΌοΈ Image converted to base64 ({len(image_data)} chars)")
|
| 105 |
+
|
| 106 |
+
elif file_ext in ['.txt', '.md', '.py', '.js', '.html', '.css', '.json', '.xml']:
|
| 107 |
+
# Text file - read content
|
| 108 |
+
with open(file_path, 'r', encoding='utf-8') as f:
|
| 109 |
+
content = f.read()
|
| 110 |
+
file_contexts.append(f"[UPLOADED TEXT FILE: {file_name}]\nContent:\n{content}")
|
| 111 |
+
print(f"π Text file content read ({len(content)} chars)")
|
| 112 |
+
|
| 113 |
+
elif file_ext in ['.csv']:
|
| 114 |
+
# CSV file - provide file path for analysis
|
| 115 |
+
file_contexts.append(f"[UPLOADED CSV FILE: {file_name}] - File path: {file_path}")
|
| 116 |
+
print(f"π CSV file prepared for analysis")
|
| 117 |
+
|
| 118 |
+
elif file_ext in ['.xlsx', '.xls']:
|
| 119 |
+
# Excel file - provide file path for analysis
|
| 120 |
+
file_contexts.append(f"[UPLOADED EXCEL FILE: {file_name}] - File path: {file_path}")
|
| 121 |
+
print(f"π Excel file prepared for analysis")
|
| 122 |
+
|
| 123 |
+
elif file_ext in ['.pdf']:
|
| 124 |
+
# PDF file - mention it's available
|
| 125 |
+
file_contexts.append(f"[UPLOADED PDF FILE: {file_name}] - File path: {file_path}")
|
| 126 |
+
print(f"π PDF file prepared for processing")
|
| 127 |
+
|
| 128 |
+
else:
|
| 129 |
+
# Other file types - just mention the file
|
| 130 |
+
file_contexts.append(f"[UPLOADED FILE: {file_name}] - File path: {file_path}")
|
| 131 |
+
print(f"π Generic file prepared for processing")
|
| 132 |
+
|
| 133 |
+
except Exception as e:
|
| 134 |
+
error_msg = f"Error processing file {file_path}: {e}"
|
| 135 |
+
print(f"β {error_msg}")
|
| 136 |
+
print(f"π File processing error details: {type(e).__name__}: {str(e)}")
|
| 137 |
+
file_contexts.append(f"[ERROR PROCESSING FILE: {os.path.basename(file_path)}] - {str(e)}")
|
| 138 |
+
|
| 139 |
+
total_context = "\n\n".join(file_contexts) if file_contexts else ""
|
| 140 |
+
if total_context:
|
| 141 |
+
print(f"π Total file context generated: {len(total_context)} characters")
|
| 142 |
+
|
| 143 |
+
return total_context
|
| 144 |
+
|
| 145 |
+
def clear_history(self):
|
| 146 |
+
"""Clear the conversation history."""
|
| 147 |
+
print("π§Ή Clearing conversation history...")
|
| 148 |
+
self.conversation_history = []
|
| 149 |
+
print("β
Conversation history cleared")
|
| 150 |
+
return []
|
| 151 |
+
|
| 152 |
+
def create_qna_interface():
|
| 153 |
+
"""Create the Q&A chatbot interface."""
|
| 154 |
+
|
| 155 |
+
print("π Creating Q&A interface...")
|
| 156 |
+
# Initialize the chatbot
|
| 157 |
+
chatbot = QnAChatbot()
|
| 158 |
+
print("π¨ Setting up UI components...")
|
| 159 |
+
|
| 160 |
+
# Enhanced Custom CSS for modern, professional styling
|
| 161 |
+
custom_css = """
|
| 162 |
+
/* Import Google Fonts */
|
| 163 |
+
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&family=JetBrains+Mono:wght@400;500&display=swap');
|
| 164 |
+
|
| 165 |
+
/* CSS Variables for Theme Support */
|
| 166 |
+
:root {
|
| 167 |
+
--primary-gradient: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 168 |
+
--secondary-gradient: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
|
| 169 |
+
--success-gradient: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
|
| 170 |
+
--warning-gradient: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
|
| 171 |
+
--glass-bg: rgba(255, 255, 255, 0.95);
|
| 172 |
+
--glass-border: rgba(255, 255, 255, 0.2);
|
| 173 |
+
--text-primary: #2d3748;
|
| 174 |
+
--text-secondary: #4a5568;
|
| 175 |
+
--text-light: #718096;
|
| 176 |
+
--bg-light: #f7fafc;
|
| 177 |
+
--bg-card: #ffffff;
|
| 178 |
+
--shadow-sm: 0 2px 10px rgba(0, 0, 0, 0.05);
|
| 179 |
+
--shadow-md: 0 10px 30px rgba(0, 0, 0, 0.1);
|
| 180 |
+
--shadow-lg: 0 20px 40px rgba(0, 0, 0, 0.15);
|
| 181 |
+
--border-radius: 12px;
|
| 182 |
+
--border-radius-lg: 20px;
|
| 183 |
+
--transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
/* Global Styles */
|
| 187 |
+
* {
|
| 188 |
+
font-family: 'Inter', sans-serif !important;
|
| 189 |
+
box-sizing: border-box !important;
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
+
/* Main Container with Enhanced Background */
|
| 193 |
+
.gradio-container {
|
| 194 |
+
max-width: 1400px !important;
|
| 195 |
+
margin: 0 auto !important;
|
| 196 |
+
background:
|
| 197 |
+
radial-gradient(circle at 20% 50%, rgba(120, 119, 198, 0.3), transparent 50%),
|
| 198 |
+
radial-gradient(circle at 80% 20%, rgba(255, 119, 198, 0.3), transparent 50%),
|
| 199 |
+
radial-gradient(circle at 40% 80%, rgba(120, 219, 255, 0.3), transparent 50%),
|
| 200 |
+
var(--primary-gradient) !important;
|
| 201 |
+
min-height: 100vh !important;
|
| 202 |
+
padding: 20px !important;
|
| 203 |
+
position: relative !important;
|
| 204 |
+
overflow-x: hidden !important;
|
| 205 |
+
}
|
| 206 |
+
|
| 207 |
+
/* Animated Background Particles */
|
| 208 |
+
.gradio-container::before {
|
| 209 |
+
content: '' !important;
|
| 210 |
+
position: fixed !important;
|
| 211 |
+
top: 0 !important;
|
| 212 |
+
left: 0 !important;
|
| 213 |
+
width: 100% !important;
|
| 214 |
+
height: 100% !important;
|
| 215 |
+
background: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23ffffff' fill-opacity='0.05'%3E%3Ccircle cx='7' cy='7' r='1'/%3E%3Ccircle cx='53' cy='7' r='1'/%3E%3Ccircle cx='7' cy='53' r='1'/%3E%3Ccircle cx='53' cy='53' r='1'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E") !important;
|
| 216 |
+
animation: float 20s ease-in-out infinite !important;
|
| 217 |
+
pointer-events: none !important;
|
| 218 |
+
z-index: 0 !important;
|
| 219 |
+
}
|
| 220 |
+
|
| 221 |
+
/* Main Content Area with Glass Effect */
|
| 222 |
+
.main-content {
|
| 223 |
+
background: var(--glass-bg) !important;
|
| 224 |
+
backdrop-filter: blur(20px) !important;
|
| 225 |
+
-webkit-backdrop-filter: blur(20px) !important;
|
| 226 |
+
border-radius: var(--border-radius-lg) !important;
|
| 227 |
+
box-shadow:
|
| 228 |
+
var(--shadow-lg),
|
| 229 |
+
inset 0 1px 0 var(--glass-border) !important;
|
| 230 |
+
padding: 40px !important;
|
| 231 |
+
margin: 20px 0 !important;
|
| 232 |
+
position: relative !important;
|
| 233 |
+
z-index: 1 !important;
|
| 234 |
+
border: 1px solid var(--glass-border) !important;
|
| 235 |
+
transition: var(--transition) !important;
|
| 236 |
+
}
|
| 237 |
+
|
| 238 |
+
.main-content:hover {
|
| 239 |
+
transform: translateY(-2px) !important;
|
| 240 |
+
box-shadow:
|
| 241 |
+
0 25px 50px rgba(0, 0, 0, 0.15),
|
| 242 |
+
inset 0 1px 0 var(--glass-border) !important;
|
| 243 |
+
}
|
| 244 |
+
|
| 245 |
+
/* Enhanced Header with Animations */
|
| 246 |
+
.markdown h1 {
|
| 247 |
+
background: var(--primary-gradient) !important;
|
| 248 |
+
-webkit-background-clip: text !important;
|
| 249 |
+
-webkit-text-fill-color: transparent !important;
|
| 250 |
+
background-clip: text !important;
|
| 251 |
+
font-size: 3rem !important;
|
| 252 |
+
font-weight: 800 !important;
|
| 253 |
+
text-align: center !important;
|
| 254 |
+
margin-bottom: 1.5rem !important;
|
| 255 |
+
position: relative !important;
|
| 256 |
+
animation: titleGlow 3s ease-in-out infinite alternate !important;
|
| 257 |
+
}
|
| 258 |
+
|
| 259 |
+
.markdown h1::after {
|
| 260 |
+
content: '' !important;
|
| 261 |
+
position: absolute !important;
|
| 262 |
+
bottom: -10px !important;
|
| 263 |
+
left: 50% !important;
|
| 264 |
+
transform: translateX(-50%) !important;
|
| 265 |
+
width: 100px !important;
|
| 266 |
+
height: 4px !important;
|
| 267 |
+
background: var(--primary-gradient) !important;
|
| 268 |
+
border-radius: 2px !important;
|
| 269 |
+
animation: pulse 2s ease-in-out infinite !important;
|
| 270 |
+
}
|
| 271 |
+
|
| 272 |
+
/* Enhanced Chat Interface */
|
| 273 |
+
.chatbot {
|
| 274 |
+
border: none !important;
|
| 275 |
+
border-radius: var(--border-radius) !important;
|
| 276 |
+
box-shadow: var(--shadow-md) !important;
|
| 277 |
+
background: var(--bg-card) !important;
|
| 278 |
+
overflow: hidden !important;
|
| 279 |
+
position: relative !important;
|
| 280 |
+
}
|
| 281 |
+
|
| 282 |
+
/* Chat Messages with Better Styling */
|
| 283 |
+
.chatbot .message-wrap {
|
| 284 |
+
padding: 20px !important;
|
| 285 |
+
margin: 15px !important;
|
| 286 |
+
border-radius: 18px !important;
|
| 287 |
+
max-width: 85% !important;
|
| 288 |
+
animation: messageSlideIn 0.5s cubic-bezier(0.4, 0, 0.2, 1) !important;
|
| 289 |
+
position: relative !important;
|
| 290 |
+
word-wrap: break-word !important;
|
| 291 |
+
}
|
| 292 |
+
|
| 293 |
+
.chatbot .message.user {
|
| 294 |
+
background: var(--primary-gradient) !important;
|
| 295 |
+
color: white !important;
|
| 296 |
+
margin-left: auto !important;
|
| 297 |
+
margin-right: 15px !important;
|
| 298 |
+
box-shadow: 0 8px 25px rgba(102, 126, 234, 0.4) !important;
|
| 299 |
+
border-bottom-right-radius: 5px !important;
|
| 300 |
+
}
|
| 301 |
+
|
| 302 |
+
.chatbot .message.user::before {
|
| 303 |
+
content: 'π§βπ»' !important;
|
| 304 |
+
position: absolute !important;
|
| 305 |
+
top: -25px !important;
|
| 306 |
+
right: 10px !important;
|
| 307 |
+
font-size: 16px !important;
|
| 308 |
+
background: var(--bg-card) !important;
|
| 309 |
+
padding: 5px 8px !important;
|
| 310 |
+
border-radius: 20px !important;
|
| 311 |
+
box-shadow: var(--shadow-sm) !important;
|
| 312 |
+
}
|
| 313 |
+
|
| 314 |
+
.chatbot .message.bot {
|
| 315 |
+
background: linear-gradient(135deg, #f8f9ff 0%, #e8eeff 100%) !important;
|
| 316 |
+
color: var(--text-primary) !important;
|
| 317 |
+
margin-right: auto !important;
|
| 318 |
+
margin-left: 15px !important;
|
| 319 |
+
border: 1px solid #e2e8f0 !important;
|
| 320 |
+
box-shadow: var(--shadow-sm) !important;
|
| 321 |
+
border-bottom-left-radius: 5px !important;
|
| 322 |
+
}
|
| 323 |
+
|
| 324 |
+
.chatbot .message.bot::before {
|
| 325 |
+
content: 'π€' !important;
|
| 326 |
+
position: absolute !important;
|
| 327 |
+
top: -25px !important;
|
| 328 |
+
left: 10px !important;
|
| 329 |
+
font-size: 16px !important;
|
| 330 |
+
background: var(--bg-card) !important;
|
| 331 |
+
padding: 5px 8px !important;
|
| 332 |
+
border-radius: 20px !important;
|
| 333 |
+
box-shadow: var(--shadow-sm) !important;
|
| 334 |
+
}
|
| 335 |
+
|
| 336 |
+
/* Typing Indicator */
|
| 337 |
+
.typing-indicator {
|
| 338 |
+
display: flex !important;
|
| 339 |
+
align-items: center !important;
|
| 340 |
+
padding: 15px 20px !important;
|
| 341 |
+
margin: 15px !important;
|
| 342 |
+
background: linear-gradient(135deg, #f8f9ff 0%, #e8eeff 100%) !important;
|
| 343 |
+
border-radius: 18px !important;
|
| 344 |
+
max-width: 85% !important;
|
| 345 |
+
margin-right: auto !important;
|
| 346 |
+
margin-left: 15px !important;
|
| 347 |
+
border: 1px solid #e2e8f0 !important;
|
| 348 |
+
animation: messageSlideIn 0.5s cubic-bezier(0.4, 0, 0.2, 1) !important;
|
| 349 |
+
}
|
| 350 |
+
|
| 351 |
+
.typing-dots {
|
| 352 |
+
display: flex !important;
|
| 353 |
+
align-items: center !important;
|
| 354 |
+
gap: 4px !important;
|
| 355 |
+
}
|
| 356 |
+
|
| 357 |
+
.typing-dots span {
|
| 358 |
+
width: 8px !important;
|
| 359 |
+
height: 8px !important;
|
| 360 |
+
border-radius: 50% !important;
|
| 361 |
+
background: var(--text-light) !important;
|
| 362 |
+
animation: typingDots 1.4s ease-in-out infinite both !important;
|
| 363 |
+
}
|
| 364 |
+
|
| 365 |
+
.typing-dots span:nth-child(1) { animation-delay: -0.32s !important; }
|
| 366 |
+
.typing-dots span:nth-child(2) { animation-delay: -0.16s !important; }
|
| 367 |
+
.typing-dots span:nth-child(3) { animation-delay: 0s !important; }
|
| 368 |
+
|
| 369 |
+
/* Enhanced Input Areas */
|
| 370 |
+
.textbox input, .textbox textarea {
|
| 371 |
+
border: 2px solid #e2e8f0 !important;
|
| 372 |
+
border-radius: var(--border-radius) !important;
|
| 373 |
+
padding: 18px 24px !important;
|
| 374 |
+
font-size: 16px !important;
|
| 375 |
+
line-height: 1.5 !important;
|
| 376 |
+
transition: var(--transition) !important;
|
| 377 |
+
background: var(--bg-card) !important;
|
| 378 |
+
box-shadow: var(--shadow-sm) !important;
|
| 379 |
+
font-family: 'Inter', sans-serif !important;
|
| 380 |
+
}
|
| 381 |
+
|
| 382 |
+
.textbox input:focus, .textbox textarea:focus {
|
| 383 |
+
border-color: #667eea !important;
|
| 384 |
+
box-shadow:
|
| 385 |
+
0 0 0 4px rgba(102, 126, 234, 0.1),
|
| 386 |
+
var(--shadow-md) !important;
|
| 387 |
+
outline: none !important;
|
| 388 |
+
transform: translateY(-1px) !important;
|
| 389 |
+
background: #ffffff !important;
|
| 390 |
+
}
|
| 391 |
+
|
| 392 |
+
/* Enhanced Buttons with Micro-interactions */
|
| 393 |
+
.btn {
|
| 394 |
+
border-radius: var(--border-radius) !important;
|
| 395 |
+
font-weight: 600 !important;
|
| 396 |
+
text-transform: none !important;
|
| 397 |
+
letter-spacing: 0.3px !important;
|
| 398 |
+
transition: var(--transition) !important;
|
| 399 |
+
border: none !important;
|
| 400 |
+
box-shadow: var(--shadow-sm) !important;
|
| 401 |
+
padding: 14px 28px !important;
|
| 402 |
+
font-size: 16px !important;
|
| 403 |
+
position: relative !important;
|
| 404 |
+
overflow: hidden !important;
|
| 405 |
+
cursor: pointer !important;
|
| 406 |
+
}
|
| 407 |
+
|
| 408 |
+
.btn::before {
|
| 409 |
+
content: '' !important;
|
| 410 |
+
position: absolute !important;
|
| 411 |
+
top: 0 !important;
|
| 412 |
+
left: -100% !important;
|
| 413 |
+
width: 100% !important;
|
| 414 |
+
height: 100% !important;
|
| 415 |
+
background: linear-gradient(90deg, transparent, rgba(255,255,255,0.3), transparent) !important;
|
| 416 |
+
transition: left 0.5s !important;
|
| 417 |
+
}
|
| 418 |
+
|
| 419 |
+
.btn:hover::before {
|
| 420 |
+
left: 100% !important;
|
| 421 |
+
}
|
| 422 |
+
|
| 423 |
+
.btn-primary {
|
| 424 |
+
background: var(--primary-gradient) !important;
|
| 425 |
+
color: white !important;
|
| 426 |
+
box-shadow: 0 8px 25px rgba(102, 126, 234, 0.3) !important;
|
| 427 |
+
}
|
| 428 |
+
|
| 429 |
+
.btn-primary:hover {
|
| 430 |
+
transform: translateY(-3px) scale(1.02) !important;
|
| 431 |
+
box-shadow: 0 12px 35px rgba(102, 126, 234, 0.4) !important;
|
| 432 |
+
}
|
| 433 |
+
|
| 434 |
+
.btn-primary:active {
|
| 435 |
+
transform: translateY(-1px) scale(0.98) !important;
|
| 436 |
+
}
|
| 437 |
+
|
| 438 |
+
.btn-secondary {
|
| 439 |
+
background: linear-gradient(135deg, #f7fafc 0%, #edf2f7 100%) !important;
|
| 440 |
+
color: var(--text-secondary) !important;
|
| 441 |
+
border: 1px solid #e2e8f0 !important;
|
| 442 |
+
}
|
| 443 |
+
|
| 444 |
+
.btn-secondary:hover {
|
| 445 |
+
transform: translateY(-2px) !important;
|
| 446 |
+
background: linear-gradient(135deg, #edf2f7 0%, #e2e8f0 100%) !important;
|
| 447 |
+
box-shadow: var(--shadow-md) !important;
|
| 448 |
+
}
|
| 449 |
+
|
| 450 |
+
/* Enhanced File Upload with Drag & Drop Animation */
|
| 451 |
+
.file-upload {
|
| 452 |
+
border: 3px dashed #cbd5e0 !important;
|
| 453 |
+
border-radius: var(--border-radius) !important;
|
| 454 |
+
background: linear-gradient(135deg, #f7fafc 0%, #edf2f7 100%) !important;
|
| 455 |
+
padding: 40px !important;
|
| 456 |
+
text-align: center !important;
|
| 457 |
+
transition: var(--transition) !important;
|
| 458 |
+
position: relative !important;
|
| 459 |
+
overflow: hidden !important;
|
| 460 |
+
cursor: pointer !important;
|
| 461 |
+
}
|
| 462 |
+
|
| 463 |
+
.file-upload:hover {
|
| 464 |
+
border-color: #667eea !important;
|
| 465 |
+
background: linear-gradient(135deg, #edf2f7 0%, #e2e8f0 100%) !important;
|
| 466 |
+
transform: translateY(-2px) scale(1.01) !important;
|
| 467 |
+
box-shadow: var(--shadow-md) !important;
|
| 468 |
+
}
|
| 469 |
+
|
| 470 |
+
.file-upload::before {
|
| 471 |
+
content: 'π' !important;
|
| 472 |
+
font-size: 4rem !important;
|
| 473 |
+
display: block !important;
|
| 474 |
+
margin-bottom: 15px !important;
|
| 475 |
+
animation: fileFloat 3s ease-in-out infinite !important;
|
| 476 |
+
}
|
| 477 |
+
|
| 478 |
+
.file-upload::after {
|
| 479 |
+
content: 'Drag files here or click to browse' !important;
|
| 480 |
+
position: absolute !important;
|
| 481 |
+
bottom: 15px !important;
|
| 482 |
+
left: 50% !important;
|
| 483 |
+
transform: translateX(-50%) !important;
|
| 484 |
+
font-size: 14px !important;
|
| 485 |
+
color: var(--text-light) !important;
|
| 486 |
+
font-weight: 500 !important;
|
| 487 |
+
}
|
| 488 |
+
|
| 489 |
+
/* Enhanced Examples Section */
|
| 490 |
+
.examples {
|
| 491 |
+
background: linear-gradient(135deg, #f8f9ff 0%, #e8eeff 100%) !important;
|
| 492 |
+
border-radius: var(--border-radius) !important;
|
| 493 |
+
padding: 30px !important;
|
| 494 |
+
margin: 25px 0 !important;
|
| 495 |
+
border: 1px solid #e2e8f0 !important;
|
| 496 |
+
box-shadow: var(--shadow-sm) !important;
|
| 497 |
+
position: relative !important;
|
| 498 |
+
}
|
| 499 |
+
|
| 500 |
+
.examples::before {
|
| 501 |
+
content: 'π‘' !important;
|
| 502 |
+
position: absolute !important;
|
| 503 |
+
top: -15px !important;
|
| 504 |
+
left: 30px !important;
|
| 505 |
+
background: var(--bg-card) !important;
|
| 506 |
+
padding: 10px !important;
|
| 507 |
+
border-radius: 50% !important;
|
| 508 |
+
font-size: 20px !important;
|
| 509 |
+
box-shadow: var(--shadow-sm) !important;
|
| 510 |
+
}
|
| 511 |
+
|
| 512 |
+
.examples h3 {
|
| 513 |
+
color: #667eea !important;
|
| 514 |
+
font-weight: 700 !important;
|
| 515 |
+
margin-bottom: 20px !important;
|
| 516 |
+
font-size: 1.3rem !important;
|
| 517 |
+
margin-left: 20px !important;
|
| 518 |
+
}
|
| 519 |
+
|
| 520 |
+
/* Feature Cards with Hover Effects */
|
| 521 |
+
.feature-card {
|
| 522 |
+
background: var(--bg-card) !important;
|
| 523 |
+
border-radius: var(--border-radius) !important;
|
| 524 |
+
padding: 25px !important;
|
| 525 |
+
margin: 15px 0 !important;
|
| 526 |
+
box-shadow: var(--shadow-sm) !important;
|
| 527 |
+
border-left: 4px solid transparent !important;
|
| 528 |
+
transition: var(--transition) !important;
|
| 529 |
+
cursor: pointer !important;
|
| 530 |
+
}
|
| 531 |
+
|
| 532 |
+
.feature-card:hover {
|
| 533 |
+
transform: translateY(-5px) translateX(5px) !important;
|
| 534 |
+
box-shadow: var(--shadow-md) !important;
|
| 535 |
+
}
|
| 536 |
+
|
| 537 |
+
.feature-card.research {
|
| 538 |
+
border-left-color: #667eea !important;
|
| 539 |
+
}
|
| 540 |
+
|
| 541 |
+
.feature-card.code {
|
| 542 |
+
border-left-color: #48bb78 !important;
|
| 543 |
+
}
|
| 544 |
+
|
| 545 |
+
.feature-card.data {
|
| 546 |
+
border-left-color: #ed8936 !important;
|
| 547 |
+
}
|
| 548 |
+
|
| 549 |
+
.feature-card.image {
|
| 550 |
+
border-left-color: #dd6b20 !important;
|
| 551 |
+
}
|
| 552 |
+
|
| 553 |
+
/* Status Indicator with Pulse Animation */
|
| 554 |
+
.status-indicator {
|
| 555 |
+
display: inline-block !important;
|
| 556 |
+
width: 12px !important;
|
| 557 |
+
height: 12px !important;
|
| 558 |
+
border-radius: 50% !important;
|
| 559 |
+
background: radial-gradient(circle, #48bb78, #38a169) !important;
|
| 560 |
+
margin-right: 12px !important;
|
| 561 |
+
animation: statusPulse 2s ease-in-out infinite !important;
|
| 562 |
+
box-shadow: 0 0 0 0 rgba(72, 187, 120, 0.7) !important;
|
| 563 |
+
}
|
| 564 |
+
|
| 565 |
+
/* Enhanced Footer */
|
| 566 |
+
.footer {
|
| 567 |
+
background: var(--glass-bg) !important;
|
| 568 |
+
backdrop-filter: blur(10px) !important;
|
| 569 |
+
border-radius: var(--border-radius) !important;
|
| 570 |
+
padding: 30px !important;
|
| 571 |
+
margin-top: 40px !important;
|
| 572 |
+
text-align: center !important;
|
| 573 |
+
border: 1px solid var(--glass-border) !important;
|
| 574 |
+
box-shadow: var(--shadow-sm) !important;
|
| 575 |
+
}
|
| 576 |
+
|
| 577 |
+
/* Advanced Animations */
|
| 578 |
+
@keyframes titleGlow {
|
| 579 |
+
0%, 100% { text-shadow: 0 0 20px rgba(102, 126, 234, 0.5); }
|
| 580 |
+
50% { text-shadow: 0 0 30px rgba(102, 126, 234, 0.8), 0 0 40px rgba(118, 75, 162, 0.6); }
|
| 581 |
+
}
|
| 582 |
+
|
| 583 |
+
@keyframes messageSlideIn {
|
| 584 |
+
from {
|
| 585 |
+
opacity: 0;
|
| 586 |
+
transform: translateY(30px) scale(0.95);
|
| 587 |
+
}
|
| 588 |
+
to {
|
| 589 |
+
opacity: 1;
|
| 590 |
+
transform: translateY(0) scale(1);
|
| 591 |
+
}
|
| 592 |
+
}
|
| 593 |
+
|
| 594 |
+
@keyframes typingDots {
|
| 595 |
+
0%, 80%, 100% { transform: scale(0.8); opacity: 0.5; }
|
| 596 |
+
40% { transform: scale(1.2); opacity: 1; }
|
| 597 |
+
}
|
| 598 |
+
|
| 599 |
+
@keyframes fileFloat {
|
| 600 |
+
0%, 100% { transform: translateY(0px); }
|
| 601 |
+
50% { transform: translateY(-10px); }
|
| 602 |
+
}
|
| 603 |
+
|
| 604 |
+
@keyframes statusPulse {
|
| 605 |
+
0% {
|
| 606 |
+
transform: scale(0.95);
|
| 607 |
+
box-shadow: 0 0 0 0 rgba(72, 187, 120, 0.7);
|
| 608 |
+
}
|
| 609 |
+
70% {
|
| 610 |
+
transform: scale(1);
|
| 611 |
+
box-shadow: 0 0 0 10px rgba(72, 187, 120, 0);
|
| 612 |
+
}
|
| 613 |
+
100% {
|
| 614 |
+
transform: scale(0.95);
|
| 615 |
+
box-shadow: 0 0 0 0 rgba(72, 187, 120, 0);
|
| 616 |
+
}
|
| 617 |
+
}
|
| 618 |
+
|
| 619 |
+
@keyframes float {
|
| 620 |
+
0%, 100% { transform: translateY(0px) rotate(0deg); }
|
| 621 |
+
33% { transform: translateY(-10px) rotate(1deg); }
|
| 622 |
+
66% { transform: translateY(-5px) rotate(-1deg); }
|
| 623 |
+
}
|
| 624 |
+
|
| 625 |
+
@keyframes pulse {
|
| 626 |
+
0%, 100% { transform: scale(1) scaleX(1); }
|
| 627 |
+
50% { transform: scale(1.05) scaleX(1.1); }
|
| 628 |
+
}
|
| 629 |
+
|
| 630 |
+
/* Responsive Design Enhancements */
|
| 631 |
+
@media (max-width: 1024px) {
|
| 632 |
+
.gradio-container { padding: 15px !important; }
|
| 633 |
+
.main-content { padding: 25px !important; margin: 15px 0 !important; }
|
| 634 |
+
.markdown h1 { font-size: 2.5rem !important; }
|
| 635 |
+
}
|
| 636 |
+
|
| 637 |
+
@media (max-width: 768px) {
|
| 638 |
+
.gradio-container { padding: 10px !important; }
|
| 639 |
+
.main-content { padding: 20px !important; margin: 10px 0 !important; }
|
| 640 |
+
.chatbot .message-wrap { max-width: 90% !important; margin: 10px !important; padding: 15px !important; }
|
| 641 |
+
.markdown h1 { font-size: 2rem !important; }
|
| 642 |
+
.btn { padding: 12px 20px !important; font-size: 14px !important; }
|
| 643 |
+
.file-upload { padding: 30px 20px !important; }
|
| 644 |
+
}
|
| 645 |
+
|
| 646 |
+
@media (max-width: 480px) {
|
| 647 |
+
.markdown h1 { font-size: 1.8rem !important; }
|
| 648 |
+
.chatbot .message-wrap { margin: 8px !important; padding: 12px !important; }
|
| 649 |
+
.main-content { padding: 15px !important; }
|
| 650 |
+
}
|
| 651 |
+
|
| 652 |
+
/* Custom Scrollbar Enhancement */
|
| 653 |
+
::-webkit-scrollbar { width: 12px; }
|
| 654 |
+
::-webkit-scrollbar-track {
|
| 655 |
+
background: rgba(241, 241, 241, 0.5);
|
| 656 |
+
border-radius: 6px;
|
| 657 |
+
}
|
| 658 |
+
::-webkit-scrollbar-thumb {
|
| 659 |
+
background: var(--primary-gradient);
|
| 660 |
+
border-radius: 6px;
|
| 661 |
+
border: 2px solid rgba(255, 255, 255, 0.2);
|
| 662 |
+
}
|
| 663 |
+
::-webkit-scrollbar-thumb:hover {
|
| 664 |
+
background: linear-gradient(135deg, #5a6fd8 0%, #6a4190 100%);
|
| 665 |
+
}
|
| 666 |
+
|
| 667 |
+
/* Loading States */
|
| 668 |
+
.loading { animation: pulse 2s infinite !important; }
|
| 669 |
+
.processing {
|
| 670 |
+
position: relative !important;
|
| 671 |
+
overflow: hidden !important;
|
| 672 |
+
}
|
| 673 |
+
.processing::after {
|
| 674 |
+
content: '' !important;
|
| 675 |
+
position: absolute !important;
|
| 676 |
+
top: 0 !important;
|
| 677 |
+
left: -100% !important;
|
| 678 |
+
width: 100% !important;
|
| 679 |
+
height: 100% !important;
|
| 680 |
+
background: linear-gradient(90deg, transparent, rgba(255,255,255,0.4), transparent) !important;
|
| 681 |
+
animation: shimmer 2s infinite !important;
|
| 682 |
+
}
|
| 683 |
+
|
| 684 |
+
@keyframes shimmer {
|
| 685 |
+
0% { left: -100%; }
|
| 686 |
+
100% { left: 100%; }
|
| 687 |
+
}
|
| 688 |
+
|
| 689 |
+
/* Dark mode support (future enhancement) */
|
| 690 |
+
@media (prefers-color-scheme: dark) {
|
| 691 |
+
:root {
|
| 692 |
+
--glass-bg: rgba(26, 32, 44, 0.95);
|
| 693 |
+
--text-primary: #e2e8f0;
|
| 694 |
+
--text-secondary: #cbd5e0;
|
| 695 |
+
--text-light: #a0aec0;
|
| 696 |
+
--bg-card: #2d3748;
|
| 697 |
+
--bg-light: #1a202c;
|
| 698 |
+
}
|
| 699 |
+
}
|
| 700 |
+
"""
|
| 701 |
+
|
| 702 |
+
with gr.Blocks(css=custom_css, title="GAIA Agent - Q&A Chatbot", theme=gr.themes.Soft()) as demo:
|
| 703 |
+
# Header with enhanced styling
|
| 704 |
+
with gr.Row(elem_classes="main-content"):
|
| 705 |
+
gr.Markdown(
|
| 706 |
+
"""
|
| 707 |
+
<h1 align="center">π€ GAIA Agent - Advanced Q&A Chatbot</h1>
|
| 708 |
+
|
| 709 |
+
Welcome to the **GAIA Agent Q&A interface**! Ask me anything and I'll help you find the answer using my various tools and capabilities.
|
| 710 |
+
|
| 711 |
+
### π **What I can do:**
|
| 712 |
+
|
| 713 |
+
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 20px; margin: 20px 0;">
|
| 714 |
+
<div class="feature-card research" style="background: linear-gradient(135deg, #f8f9ff 0%, #e8eeff 100%); padding: 25px; border-radius: 12px; border-left: 4px solid #667eea; cursor: pointer; transition: all 0.3s ease;">
|
| 715 |
+
<div style="display: flex; align-items: center; margin-bottom: 10px;">
|
| 716 |
+
<span style="font-size: 2rem; margin-right: 15px;">π</span>
|
| 717 |
+
<strong style="font-size: 1.1rem; color: #667eea;">Research & Search</strong>
|
| 718 |
+
</div>
|
| 719 |
+
<p style="margin: 0; color: #718096; font-size: 14px; line-height: 1.5;">Web search, Wikipedia, academic papers, arXiv research</p>
|
| 720 |
+
</div>
|
| 721 |
+
<div class="feature-card code" style="background: linear-gradient(135deg, #f0fff4 0%, #c6f6d5 100%); padding: 25px; border-radius: 12px; border-left: 4px solid #48bb78; cursor: pointer; transition: all 0.3s ease;">
|
| 722 |
+
<div style="display: flex; align-items: center; margin-bottom: 10px;">
|
| 723 |
+
<span style="font-size: 2rem; margin-right: 15px;">π»</span>
|
| 724 |
+
<strong style="font-size: 1.1rem; color: #48bb78;">Code Execution</strong>
|
| 725 |
+
</div>
|
| 726 |
+
<p style="margin: 0; color: #718096; font-size: 14px; line-height: 1.5;">Python, Bash, SQL, C, Java with real-time results</p>
|
| 727 |
+
</div>
|
| 728 |
+
<div class="feature-card data" style="background: linear-gradient(135deg, #fffaf0 0%, #fbd38d 100%); padding: 25px; border-radius: 12px; border-left: 4px solid #ed8936; cursor: pointer; transition: all 0.3s ease;">
|
| 729 |
+
<div style="display: flex; align-items: center; margin-bottom: 10px;">
|
| 730 |
+
<span style="font-size: 2rem; margin-right: 15px;">π</span>
|
| 731 |
+
<strong style="font-size: 1.1rem; color: #ed8936;">Data Analysis</strong>
|
| 732 |
+
</div>
|
| 733 |
+
<p style="margin: 0; color: #718096; font-size: 14px; line-height: 1.5;">CSV, Excel, visualizations, statistical analysis</p>
|
| 734 |
+
</div>
|
| 735 |
+
<div class="feature-card image" style="background: linear-gradient(135deg, #fef5e7 0%, #f6ad55 100%); padding: 25px; border-radius: 12px; border-left: 4px solid #dd6b20; cursor: pointer; transition: all 0.3s ease;">
|
| 736 |
+
<div style="display: flex; align-items: center; margin-bottom: 10px;">
|
| 737 |
+
<span style="font-size: 2rem; margin-right: 15px;">πΌοΈ</span>
|
| 738 |
+
<strong style="font-size: 1.1rem; color: #dd6b20;">Image Processing</strong>
|
| 739 |
+
</div>
|
| 740 |
+
<p style="margin: 0; color: #718096; font-size: 14px; line-height: 1.5;">Analysis, OCR, transformations, generation</p>
|
| 741 |
+
</div>
|
| 742 |
+
</div>
|
| 743 |
+
|
| 744 |
+
---
|
| 745 |
+
"""
|
| 746 |
+
)
|
| 747 |
+
|
| 748 |
+
# Chat interface with enhanced styling
|
| 749 |
+
with gr.Row(elem_classes="main-content"):
|
| 750 |
+
with gr.Column(scale=1):
|
| 751 |
+
chatbot_interface = gr.Chatbot(
|
| 752 |
+
label="π¬ Conversation",
|
| 753 |
+
height=600,
|
| 754 |
+
show_label=True,
|
| 755 |
+
container=True,
|
| 756 |
+
bubble_full_width=False,
|
| 757 |
+
elem_classes="chatbot"
|
| 758 |
+
)
|
| 759 |
+
|
| 760 |
+
# File upload section with enhanced styling
|
| 761 |
+
with gr.Row(elem_classes="main-content"):
|
| 762 |
+
with gr.Column():
|
| 763 |
+
file_upload = gr.File(
|
| 764 |
+
label="π Upload Files - Drag & drop or click to upload images, documents, CSV, Excel files, etc.",
|
| 765 |
+
file_count="multiple",
|
| 766 |
+
file_types=[
|
| 767 |
+
".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp", # Images
|
| 768 |
+
".txt", ".md", ".py", ".js", ".html", ".css", ".json", ".xml", # Text files
|
| 769 |
+
".csv", ".xlsx", ".xls", # Data files
|
| 770 |
+
".pdf", ".doc", ".docx" # Documents
|
| 771 |
+
],
|
| 772 |
+
height=120,
|
| 773 |
+
elem_classes="file-upload"
|
| 774 |
+
)
|
| 775 |
+
|
| 776 |
+
# Input and buttons with enhanced styling
|
| 777 |
+
with gr.Row(elem_classes="main-content"):
|
| 778 |
+
with gr.Column(scale=8):
|
| 779 |
+
question_input = gr.Textbox(
|
| 780 |
+
label="π Ask a question",
|
| 781 |
+
placeholder="Type your question here or upload files above... (e.g., 'What is the capital of France?', 'Analyze this image', 'Summarize this document')",
|
| 782 |
+
lines=3,
|
| 783 |
+
max_lines=5,
|
| 784 |
+
elem_classes="textbox"
|
| 785 |
+
)
|
| 786 |
+
with gr.Column(scale=2, min_width=120):
|
| 787 |
+
submit_btn = gr.Button("π Send", variant="primary", size="lg", elem_classes="btn btn-primary")
|
| 788 |
+
|
| 789 |
+
with gr.Row(elem_classes="main-content"):
|
| 790 |
+
with gr.Column(scale=1):
|
| 791 |
+
clear_btn = gr.Button("π§Ή Clear History", variant="secondary", elem_classes="btn btn-secondary")
|
| 792 |
+
with gr.Column(scale=1):
|
| 793 |
+
clear_files_btn = gr.Button("ποΈ Clear Files", variant="secondary", elem_classes="btn btn-secondary")
|
| 794 |
+
with gr.Column(scale=1):
|
| 795 |
+
export_btn = gr.Button("πΎ Export Chat", variant="secondary", elem_classes="btn btn-secondary")
|
| 796 |
+
|
| 797 |
+
# Hidden download component for chat export
|
| 798 |
+
download_file = gr.File(visible=False)
|
| 799 |
+
|
| 800 |
+
with gr.Row(elem_classes="main-content"):
|
| 801 |
+
with gr.Column():
|
| 802 |
+
gr.Examples(
|
| 803 |
+
examples=[
|
| 804 |
+
"What is the current population of Tokyo?",
|
| 805 |
+
"Calculate the square root of 144",
|
| 806 |
+
"Write a Python function to sort a list",
|
| 807 |
+
"What are the latest developments in AI?",
|
| 808 |
+
"Explain quantum computing in simple terms",
|
| 809 |
+
],
|
| 810 |
+
inputs=question_input,
|
| 811 |
+
label="π General Questions"
|
| 812 |
+
)
|
| 813 |
+
with gr.Column():
|
| 814 |
+
gr.Examples(
|
| 815 |
+
examples=[
|
| 816 |
+
"Search for recent papers on machine learning",
|
| 817 |
+
"What is the weather like today?",
|
| 818 |
+
"Create a simple bar chart using Python",
|
| 819 |
+
"Convert 100 USD to EUR",
|
| 820 |
+
"What are the benefits of renewable energy?",
|
| 821 |
+
],
|
| 822 |
+
inputs=question_input,
|
| 823 |
+
label="π¬ Research & Analysis"
|
| 824 |
+
)
|
| 825 |
+
with gr.Column():
|
| 826 |
+
gr.Examples(
|
| 827 |
+
examples=[
|
| 828 |
+
"Analyze this image and describe what you see",
|
| 829 |
+
"Extract text from this image using OCR",
|
| 830 |
+
"Summarize the content of this document",
|
| 831 |
+
"Analyze the data in this CSV file",
|
| 832 |
+
"What insights can you find in this Excel file?",
|
| 833 |
+
],
|
| 834 |
+
inputs=question_input,
|
| 835 |
+
label="π File Analysis"
|
| 836 |
+
)
|
| 837 |
+
|
| 838 |
+
# Event handlers
|
| 839 |
+
def submit_question(question, history, files):
|
| 840 |
+
print(f"π― UI: Submit button clicked")
|
| 841 |
+
print(f"π UI: Question length: {len(question) if question else 0}")
|
| 842 |
+
print(f"π UI: Files count: {len(files) if files else 0}")
|
| 843 |
+
result_question, result_history = chatbot.process_question(question, history, files)
|
| 844 |
+
print(f"π UI: Returning results and clearing files")
|
| 845 |
+
return result_question, result_history, None # Clear files after processing
|
| 846 |
+
|
| 847 |
+
def clear_conversation():
|
| 848 |
+
print("π§Ή UI: Clear conversation button clicked")
|
| 849 |
+
return chatbot.clear_history()
|
| 850 |
+
|
| 851 |
+
def clear_files():
|
| 852 |
+
print("ποΈ UI: Clear files button clicked")
|
| 853 |
+
return None
|
| 854 |
+
|
| 855 |
+
def export_conversation(history):
|
| 856 |
+
"""Export conversation history to a text file"""
|
| 857 |
+
print("πΎ UI: Export conversation button clicked")
|
| 858 |
+
if not history:
|
| 859 |
+
print("β οΈ No conversation to export")
|
| 860 |
+
return None
|
| 861 |
+
|
| 862 |
+
try:
|
| 863 |
+
import tempfile
|
| 864 |
+
import datetime
|
| 865 |
+
|
| 866 |
+
# Create export content
|
| 867 |
+
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 868 |
+
export_content = f"# GAIA Agent Conversation Export\n"
|
| 869 |
+
export_content += f"Export Date: {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n"
|
| 870 |
+
export_content += f"Total Messages: {len(history)}\n\n"
|
| 871 |
+
export_content += "=" * 50 + "\n\n"
|
| 872 |
+
|
| 873 |
+
for i, (user_msg, bot_msg) in enumerate(history, 1):
|
| 874 |
+
export_content += f"## Message {i}\n\n"
|
| 875 |
+
export_content += f"**User:** {user_msg}\n\n"
|
| 876 |
+
export_content += f"**Assistant:** {bot_msg}\n\n"
|
| 877 |
+
export_content += "-" * 30 + "\n\n"
|
| 878 |
+
|
| 879 |
+
# Save to temporary file
|
| 880 |
+
temp_file = tempfile.NamedTemporaryFile(
|
| 881 |
+
mode='w',
|
| 882 |
+
suffix=f'_gaia_chat_export_{timestamp}.md',
|
| 883 |
+
delete=False,
|
| 884 |
+
encoding='utf-8'
|
| 885 |
+
)
|
| 886 |
+
temp_file.write(export_content)
|
| 887 |
+
temp_file.close()
|
| 888 |
+
|
| 889 |
+
print(f"π Conversation exported to: {temp_file.name}")
|
| 890 |
+
return temp_file.name
|
| 891 |
+
|
| 892 |
+
except Exception as e:
|
| 893 |
+
print(f"β Error exporting conversation: {e}")
|
| 894 |
+
return None
|
| 895 |
+
|
| 896 |
+
# Connect the events
|
| 897 |
+
submit_btn.click(
|
| 898 |
+
fn=submit_question,
|
| 899 |
+
inputs=[question_input, chatbot_interface, file_upload],
|
| 900 |
+
outputs=[question_input, chatbot_interface, file_upload],
|
| 901 |
+
show_progress=True
|
| 902 |
+
)
|
| 903 |
+
|
| 904 |
+
question_input.submit(
|
| 905 |
+
fn=submit_question,
|
| 906 |
+
inputs=[question_input, chatbot_interface, file_upload],
|
| 907 |
+
outputs=[question_input, chatbot_interface, file_upload],
|
| 908 |
+
show_progress=True
|
| 909 |
+
)
|
| 910 |
+
|
| 911 |
+
clear_btn.click(
|
| 912 |
+
fn=clear_conversation,
|
| 913 |
+
outputs=[chatbot_interface],
|
| 914 |
+
show_progress=False
|
| 915 |
+
)
|
| 916 |
+
|
| 917 |
+
clear_files_btn.click(
|
| 918 |
+
fn=clear_files,
|
| 919 |
+
outputs=[file_upload],
|
| 920 |
+
show_progress=False
|
| 921 |
+
)
|
| 922 |
+
|
| 923 |
+
export_btn.click(
|
| 924 |
+
fn=export_conversation,
|
| 925 |
+
inputs=[chatbot_interface],
|
| 926 |
+
outputs=[download_file],
|
| 927 |
+
show_progress=True
|
| 928 |
+
)
|
| 929 |
+
|
| 930 |
+
return demo
|
| 931 |
+
|
| 932 |
+
if __name__ == "__main__":
|
| 933 |
+
print("\n" + "-"*50)
|
| 934 |
+
print("π Starting GAIA Agent Q&A Chatbot...")
|
| 935 |
+
print("-"*50 + "\n")
|
| 936 |
+
|
| 937 |
+
# Create and launch the interface
|
| 938 |
+
demo = create_qna_interface()
|
| 939 |
+
demo.launch(
|
| 940 |
+
debug=True,
|
| 941 |
+
share=False,
|
| 942 |
+
server_name="0.0.0.0",
|
| 943 |
+
server_port=7860,
|
| 944 |
+
show_error=True
|
| 945 |
+
)
|
code_interpreter.py
ADDED
|
@@ -0,0 +1,281 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import io
|
| 3 |
+
import sys
|
| 4 |
+
import uuid
|
| 5 |
+
import base64
|
| 6 |
+
import traceback
|
| 7 |
+
import contextlib
|
| 8 |
+
import tempfile
|
| 9 |
+
import subprocess
|
| 10 |
+
import sqlite3
|
| 11 |
+
from typing import Dict, List, Any, Optional, Union
|
| 12 |
+
import numpy as np
|
| 13 |
+
import pandas as pd
|
| 14 |
+
import matplotlib.pyplot as plt
|
| 15 |
+
from PIL import Image
|
| 16 |
+
|
| 17 |
+
class CodeInterpreter:
|
| 18 |
+
def __init__(self, allowed_modules=None, max_execution_time=30, working_directory=None):
|
| 19 |
+
"""Initialize the code interpreter with safety measures."""
|
| 20 |
+
self.allowed_modules = allowed_modules or [
|
| 21 |
+
"numpy", "pandas", "matplotlib", "scipy", "sklearn",
|
| 22 |
+
"math", "random", "statistics", "datetime", "collections",
|
| 23 |
+
"itertools", "functools", "operator", "re", "json",
|
| 24 |
+
"sympy", "networkx", "nltk", "PIL", "pytesseract",
|
| 25 |
+
"cmath", "uuid", "tempfile", "requests", "urllib"
|
| 26 |
+
]
|
| 27 |
+
self.max_execution_time = max_execution_time
|
| 28 |
+
self.working_directory = working_directory or os.path.join(os.getcwd())
|
| 29 |
+
if not os.path.exists(self.working_directory):
|
| 30 |
+
os.makedirs(self.working_directory)
|
| 31 |
+
|
| 32 |
+
self.globals = {
|
| 33 |
+
"__builtins__": __builtins__,
|
| 34 |
+
"np": np,
|
| 35 |
+
"pd": pd,
|
| 36 |
+
"plt": plt,
|
| 37 |
+
"Image": Image,
|
| 38 |
+
}
|
| 39 |
+
self.temp_sqlite_db = os.path.join(tempfile.gettempdir(), "code_exec.db")
|
| 40 |
+
|
| 41 |
+
def execute_code(self, code: str, language: str = "python") -> Dict[str, Any]:
|
| 42 |
+
"""Execute the provided code in the selected programming language."""
|
| 43 |
+
language = language.lower()
|
| 44 |
+
execution_id = str(uuid.uuid4())
|
| 45 |
+
|
| 46 |
+
result = {
|
| 47 |
+
"execution_id": execution_id,
|
| 48 |
+
"status": "error",
|
| 49 |
+
"stdout": "",
|
| 50 |
+
"stderr": "",
|
| 51 |
+
"result": None,
|
| 52 |
+
"plots": [],
|
| 53 |
+
"dataframes": []
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
try:
|
| 57 |
+
if language == "python":
|
| 58 |
+
return self._execute_python(code, execution_id)
|
| 59 |
+
elif language == "bash":
|
| 60 |
+
return self._execute_bash(code, execution_id)
|
| 61 |
+
elif language == "sql":
|
| 62 |
+
return self._execute_sql(code, execution_id)
|
| 63 |
+
elif language == "c":
|
| 64 |
+
return self._execute_c(code, execution_id)
|
| 65 |
+
elif language == "java":
|
| 66 |
+
return self._execute_java(code, execution_id)
|
| 67 |
+
else:
|
| 68 |
+
result["stderr"] = f"Unsupported language: {language}"
|
| 69 |
+
except Exception as e:
|
| 70 |
+
result["stderr"] = str(e)
|
| 71 |
+
|
| 72 |
+
return result
|
| 73 |
+
|
| 74 |
+
def _execute_python(self, code: str, execution_id: str) -> dict:
|
| 75 |
+
output_buffer = io.StringIO()
|
| 76 |
+
error_buffer = io.StringIO()
|
| 77 |
+
result = {
|
| 78 |
+
"execution_id": execution_id,
|
| 79 |
+
"status": "error",
|
| 80 |
+
"stdout": "",
|
| 81 |
+
"stderr": "",
|
| 82 |
+
"result": None,
|
| 83 |
+
"plots": [],
|
| 84 |
+
"dataframes": []
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
try:
|
| 88 |
+
exec_dir = os.path.join(self.working_directory, execution_id)
|
| 89 |
+
os.makedirs(exec_dir, exist_ok=True)
|
| 90 |
+
plt.switch_backend('Agg')
|
| 91 |
+
|
| 92 |
+
with contextlib.redirect_stdout(output_buffer), contextlib.redirect_stderr(error_buffer):
|
| 93 |
+
exec_result = exec(code, self.globals)
|
| 94 |
+
|
| 95 |
+
if plt.get_fignums():
|
| 96 |
+
for i, fig_num in enumerate(plt.get_fignums()):
|
| 97 |
+
fig = plt.figure(fig_num)
|
| 98 |
+
img_path = os.path.join(exec_dir, f"plot_{i}.png")
|
| 99 |
+
fig.savefig(img_path)
|
| 100 |
+
with open(img_path, "rb") as img_file:
|
| 101 |
+
img_data = base64.b64encode(img_file.read()).decode('utf-8')
|
| 102 |
+
result["plots"].append({
|
| 103 |
+
"figure_number": fig_num,
|
| 104 |
+
"data": img_data
|
| 105 |
+
})
|
| 106 |
+
|
| 107 |
+
for var_name, var_value in self.globals.items():
|
| 108 |
+
if isinstance(var_value, pd.DataFrame) and len(var_value) > 0:
|
| 109 |
+
result["dataframes"].append({
|
| 110 |
+
"name": var_name,
|
| 111 |
+
"head": var_value.head().to_dict(),
|
| 112 |
+
"shape": var_value.shape,
|
| 113 |
+
"dtypes": str(var_value.dtypes)
|
| 114 |
+
})
|
| 115 |
+
|
| 116 |
+
result["status"] = "success"
|
| 117 |
+
result["stdout"] = output_buffer.getvalue()
|
| 118 |
+
result["result"] = exec_result
|
| 119 |
+
|
| 120 |
+
except Exception as e:
|
| 121 |
+
result["status"] = "error"
|
| 122 |
+
result["stderr"] = f"{error_buffer.getvalue()}\n{traceback.format_exc()}"
|
| 123 |
+
|
| 124 |
+
return result
|
| 125 |
+
|
| 126 |
+
def _execute_bash(self, code: str, execution_id: str) -> dict:
|
| 127 |
+
try:
|
| 128 |
+
completed = subprocess.run(
|
| 129 |
+
code, shell=True, capture_output=True, text=True, timeout=self.max_execution_time
|
| 130 |
+
)
|
| 131 |
+
return {
|
| 132 |
+
"execution_id": execution_id,
|
| 133 |
+
"status": "success" if completed.returncode == 0 else "error",
|
| 134 |
+
"stdout": completed.stdout,
|
| 135 |
+
"stderr": completed.stderr,
|
| 136 |
+
"result": None,
|
| 137 |
+
"plots": [],
|
| 138 |
+
"dataframes": []
|
| 139 |
+
}
|
| 140 |
+
except subprocess.TimeoutExpired:
|
| 141 |
+
return {
|
| 142 |
+
"execution_id": execution_id,
|
| 143 |
+
"status": "error",
|
| 144 |
+
"stdout": "",
|
| 145 |
+
"stderr": "Execution timed out.",
|
| 146 |
+
"result": None,
|
| 147 |
+
"plots": [],
|
| 148 |
+
"dataframes": []
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
def _execute_sql(self, code: str, execution_id: str) -> dict:
|
| 152 |
+
result = {
|
| 153 |
+
"execution_id": execution_id,
|
| 154 |
+
"status": "error",
|
| 155 |
+
"stdout": "",
|
| 156 |
+
"stderr": "",
|
| 157 |
+
"result": None,
|
| 158 |
+
"plots": [],
|
| 159 |
+
"dataframes": []
|
| 160 |
+
}
|
| 161 |
+
try:
|
| 162 |
+
conn = sqlite3.connect(self.temp_sqlite_db)
|
| 163 |
+
cur = conn.cursor()
|
| 164 |
+
cur.execute(code)
|
| 165 |
+
if code.strip().lower().startswith("select"):
|
| 166 |
+
columns = [description[0] for description in cur.description]
|
| 167 |
+
rows = cur.fetchall()
|
| 168 |
+
df = pd.DataFrame(rows, columns=columns)
|
| 169 |
+
result["dataframes"].append({
|
| 170 |
+
"name": "query_result",
|
| 171 |
+
"head": df.head().to_dict(),
|
| 172 |
+
"shape": df.shape,
|
| 173 |
+
"dtypes": str(df.dtypes)
|
| 174 |
+
})
|
| 175 |
+
else:
|
| 176 |
+
conn.commit()
|
| 177 |
+
|
| 178 |
+
result["status"] = "success"
|
| 179 |
+
result["stdout"] = "Query executed successfully."
|
| 180 |
+
|
| 181 |
+
except Exception as e:
|
| 182 |
+
result["stderr"] = str(e)
|
| 183 |
+
finally:
|
| 184 |
+
conn.close()
|
| 185 |
+
|
| 186 |
+
return result
|
| 187 |
+
|
| 188 |
+
def _execute_c(self, code: str, execution_id: str) -> dict:
|
| 189 |
+
temp_dir = tempfile.mkdtemp()
|
| 190 |
+
source_path = os.path.join(temp_dir, "program.c")
|
| 191 |
+
binary_path = os.path.join(temp_dir, "program")
|
| 192 |
+
|
| 193 |
+
try:
|
| 194 |
+
with open(source_path, "w") as f:
|
| 195 |
+
f.write(code)
|
| 196 |
+
|
| 197 |
+
compile_proc = subprocess.run(
|
| 198 |
+
["gcc", source_path, "-o", binary_path],
|
| 199 |
+
capture_output=True, text=True, timeout=self.max_execution_time
|
| 200 |
+
)
|
| 201 |
+
if compile_proc.returncode != 0:
|
| 202 |
+
return {
|
| 203 |
+
"execution_id": execution_id,
|
| 204 |
+
"status": "error",
|
| 205 |
+
"stdout": compile_proc.stdout,
|
| 206 |
+
"stderr": compile_proc.stderr,
|
| 207 |
+
"result": None,
|
| 208 |
+
"plots": [],
|
| 209 |
+
"dataframes": []
|
| 210 |
+
}
|
| 211 |
+
|
| 212 |
+
run_proc = subprocess.run(
|
| 213 |
+
[binary_path],
|
| 214 |
+
capture_output=True, text=True, timeout=self.max_execution_time
|
| 215 |
+
)
|
| 216 |
+
return {
|
| 217 |
+
"execution_id": execution_id,
|
| 218 |
+
"status": "success" if run_proc.returncode == 0 else "error",
|
| 219 |
+
"stdout": run_proc.stdout,
|
| 220 |
+
"stderr": run_proc.stderr,
|
| 221 |
+
"result": None,
|
| 222 |
+
"plots": [],
|
| 223 |
+
"dataframes": []
|
| 224 |
+
}
|
| 225 |
+
except Exception as e:
|
| 226 |
+
return {
|
| 227 |
+
"execution_id": execution_id,
|
| 228 |
+
"status": "error",
|
| 229 |
+
"stdout": "",
|
| 230 |
+
"stderr": str(e),
|
| 231 |
+
"result": None,
|
| 232 |
+
"plots": [],
|
| 233 |
+
"dataframes": []
|
| 234 |
+
}
|
| 235 |
+
|
| 236 |
+
def _execute_java(self, code: str, execution_id: str) -> dict:
|
| 237 |
+
temp_dir = tempfile.mkdtemp()
|
| 238 |
+
source_path = os.path.join(temp_dir, "Main.java")
|
| 239 |
+
|
| 240 |
+
try:
|
| 241 |
+
with open(source_path, "w") as f:
|
| 242 |
+
f.write(code)
|
| 243 |
+
|
| 244 |
+
compile_proc = subprocess.run(
|
| 245 |
+
["javac", source_path],
|
| 246 |
+
capture_output=True, text=True, timeout=self.max_execution_time
|
| 247 |
+
)
|
| 248 |
+
if compile_proc.returncode != 0:
|
| 249 |
+
return {
|
| 250 |
+
"execution_id": execution_id,
|
| 251 |
+
"status": "error",
|
| 252 |
+
"stdout": compile_proc.stdout,
|
| 253 |
+
"stderr": compile_proc.stderr,
|
| 254 |
+
"result": None,
|
| 255 |
+
"plots": [],
|
| 256 |
+
"dataframes": []
|
| 257 |
+
}
|
| 258 |
+
|
| 259 |
+
run_proc = subprocess.run(
|
| 260 |
+
["java", "-cp", temp_dir, "Main"],
|
| 261 |
+
capture_output=True, text=True, timeout=self.max_execution_time
|
| 262 |
+
)
|
| 263 |
+
return {
|
| 264 |
+
"execution_id": execution_id,
|
| 265 |
+
"status": "success" if run_proc.returncode == 0 else "error",
|
| 266 |
+
"stdout": run_proc.stdout,
|
| 267 |
+
"stderr": run_proc.stderr,
|
| 268 |
+
"result": None,
|
| 269 |
+
"plots": [],
|
| 270 |
+
"dataframes": []
|
| 271 |
+
}
|
| 272 |
+
except Exception as e:
|
| 273 |
+
return {
|
| 274 |
+
"execution_id": execution_id,
|
| 275 |
+
"status": "error",
|
| 276 |
+
"stdout": "",
|
| 277 |
+
"stderr": str(e),
|
| 278 |
+
"result": None,
|
| 279 |
+
"plots": [],
|
| 280 |
+
"dataframes": []
|
| 281 |
+
}
|
evaluation_app.py
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
| 1 |
+
""" Basic Agent Evaluation Runner"""
|
| 2 |
+
import os
|
| 3 |
+
import inspect
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import requests
|
| 6 |
+
import pandas as pd
|
| 7 |
+
import time
|
| 8 |
+
from langchain_core.messages import HumanMessage
|
| 9 |
+
from agent import build_graph
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
# (Keep Constants as is)
|
| 14 |
+
# --- Constants ---
|
| 15 |
+
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 16 |
+
|
| 17 |
+
# --- Basic Agent Definition ---
|
| 18 |
+
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
class BasicAgent:
|
| 22 |
+
"""A langgraph agent."""
|
| 23 |
+
def __init__(self):
|
| 24 |
+
print("BasicAgent initialized.")
|
| 25 |
+
self.graph = build_graph()
|
| 26 |
+
|
| 27 |
+
def __call__(self, question: str) -> str:
|
| 28 |
+
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 29 |
+
# Wrap the question in a HumanMessage from langchain_core
|
| 30 |
+
messages = [HumanMessage(content=question)]
|
| 31 |
+
messages = self.graph.invoke({"messages": messages})
|
| 32 |
+
answer = messages['messages'][-1].content
|
| 33 |
+
return answer[14:]
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 37 |
+
"""
|
| 38 |
+
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 39 |
+
and displays the results.
|
| 40 |
+
"""
|
| 41 |
+
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 42 |
+
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
| 43 |
+
|
| 44 |
+
if profile:
|
| 45 |
+
username= f"{profile.username}"
|
| 46 |
+
print(f"User logged in: {username}")
|
| 47 |
+
else:
|
| 48 |
+
print("User not logged in.")
|
| 49 |
+
return "Please Login to Hugging Face with the button.", None
|
| 50 |
+
|
| 51 |
+
api_url = DEFAULT_API_URL
|
| 52 |
+
questions_url = f"{api_url}/questions"
|
| 53 |
+
submit_url = f"{api_url}/submit"
|
| 54 |
+
|
| 55 |
+
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 56 |
+
try:
|
| 57 |
+
agent = BasicAgent()
|
| 58 |
+
except Exception as e:
|
| 59 |
+
print(f"Error instantiating agent: {e}")
|
| 60 |
+
return f"Error initializing agent: {e}", None
|
| 61 |
+
# 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)
|
| 62 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 63 |
+
print(agent_code)
|
| 64 |
+
|
| 65 |
+
# 2. Fetch Questions
|
| 66 |
+
print(f"Fetching questions from: {questions_url}")
|
| 67 |
+
try:
|
| 68 |
+
response = requests.get(questions_url, timeout=15)
|
| 69 |
+
response.raise_for_status()
|
| 70 |
+
questions_data = response.json()
|
| 71 |
+
if not questions_data:
|
| 72 |
+
print("Fetched questions list is empty.")
|
| 73 |
+
return "Fetched questions list is empty or invalid format.", None
|
| 74 |
+
print(f"Fetched {len(questions_data)} questions.")
|
| 75 |
+
except requests.exceptions.RequestException as e:
|
| 76 |
+
print(f"Error fetching questions: {e}")
|
| 77 |
+
return f"Error fetching questions: {e}", None
|
| 78 |
+
except requests.exceptions.JSONDecodeError as e:
|
| 79 |
+
print(f"Error decoding JSON response from questions endpoint: {e}")
|
| 80 |
+
print(f"Response text: {response.text[:500]}")
|
| 81 |
+
return f"Error decoding server response for questions: {e}", None
|
| 82 |
+
except Exception as e:
|
| 83 |
+
print(f"An unexpected error occurred fetching questions: {e}")
|
| 84 |
+
return f"An unexpected error occurred fetching questions: {e}", None
|
| 85 |
+
|
| 86 |
+
# 3. Run your Agent
|
| 87 |
+
results_log = []
|
| 88 |
+
answers_payload = []
|
| 89 |
+
print(f"Running agent on {len(questions_data)} questions...")
|
| 90 |
+
for item in questions_data:
|
| 91 |
+
task_id = item.get("task_id")
|
| 92 |
+
question_text = item.get("question")
|
| 93 |
+
if not task_id or question_text is None:
|
| 94 |
+
print(f"Skipping item with missing task_id or question: {item}")
|
| 95 |
+
continue
|
| 96 |
+
|
| 97 |
+
time.sleep(30)
|
| 98 |
+
|
| 99 |
+
try:
|
| 100 |
+
submitted_answer = agent(question_text)
|
| 101 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 102 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 103 |
+
except Exception as e:
|
| 104 |
+
print(f"Error running agent on task {task_id}: {e}")
|
| 105 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
| 106 |
+
|
| 107 |
+
if not answers_payload:
|
| 108 |
+
print("Agent did not produce any answers to submit.")
|
| 109 |
+
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 110 |
+
|
| 111 |
+
# 4. Prepare Submission
|
| 112 |
+
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 113 |
+
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 114 |
+
print(status_update)
|
| 115 |
+
|
| 116 |
+
# 5. Submit
|
| 117 |
+
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
| 118 |
+
try:
|
| 119 |
+
response = requests.post(submit_url, json=submission_data, timeout=60)
|
| 120 |
+
response.raise_for_status()
|
| 121 |
+
result_data = response.json()
|
| 122 |
+
final_status = (
|
| 123 |
+
f"Submission Successful!\n"
|
| 124 |
+
f"User: {result_data.get('username')}\n"
|
| 125 |
+
f"Overall Score: {result_data.get('score', 'N/A')}% "
|
| 126 |
+
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
| 127 |
+
f"Message: {result_data.get('message', 'No message received.')}"
|
| 128 |
+
)
|
| 129 |
+
print("Submission successful.")
|
| 130 |
+
results_df = pd.DataFrame(results_log)
|
| 131 |
+
return final_status, results_df
|
| 132 |
+
except requests.exceptions.HTTPError as e:
|
| 133 |
+
error_detail = f"Server responded with status {e.response.status_code}."
|
| 134 |
+
try:
|
| 135 |
+
error_json = e.response.json()
|
| 136 |
+
error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
|
| 137 |
+
except requests.exceptions.JSONDecodeError:
|
| 138 |
+
error_detail += f" Response: {e.response.text[:500]}"
|
| 139 |
+
status_message = f"Submission Failed: {error_detail}"
|
| 140 |
+
print(status_message)
|
| 141 |
+
results_df = pd.DataFrame(results_log)
|
| 142 |
+
return status_message, results_df
|
| 143 |
+
except requests.exceptions.Timeout:
|
| 144 |
+
status_message = "Submission Failed: The request timed out."
|
| 145 |
+
print(status_message)
|
| 146 |
+
results_df = pd.DataFrame(results_log)
|
| 147 |
+
return status_message, results_df
|
| 148 |
+
except requests.exceptions.RequestException as e:
|
| 149 |
+
status_message = f"Submission Failed: Network error - {e}"
|
| 150 |
+
print(status_message)
|
| 151 |
+
results_df = pd.DataFrame(results_log)
|
| 152 |
+
return status_message, results_df
|
| 153 |
+
except Exception as e:
|
| 154 |
+
status_message = f"An unexpected error occurred during submission: {e}"
|
| 155 |
+
print(status_message)
|
| 156 |
+
results_df = pd.DataFrame(results_log)
|
| 157 |
+
return status_message, results_df
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
# --- Build Gradio Interface using Blocks ---
|
| 161 |
+
with gr.Blocks() as demo:
|
| 162 |
+
gr.Markdown("# Basic Agent Evaluation Runner")
|
| 163 |
+
gr.Markdown(
|
| 164 |
+
"""
|
| 165 |
+
**Instructions:**
|
| 166 |
+
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
| 167 |
+
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
| 168 |
+
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
| 169 |
+
---
|
| 170 |
+
**Disclaimers:**
|
| 171 |
+
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).
|
| 172 |
+
This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a seperate action or even to answer the questions in async.
|
| 173 |
+
"""
|
| 174 |
+
)
|
| 175 |
+
|
| 176 |
+
gr.LoginButton()
|
| 177 |
+
|
| 178 |
+
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 179 |
+
|
| 180 |
+
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 181 |
+
# Removed max_rows=10 from DataFrame constructor
|
| 182 |
+
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 183 |
+
|
| 184 |
+
run_button.click(
|
| 185 |
+
fn=run_and_submit_all,
|
| 186 |
+
outputs=[status_output, results_table]
|
| 187 |
+
)
|
| 188 |
+
|
| 189 |
+
if __name__ == "__main__":
|
| 190 |
+
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 191 |
+
# Check for SPACE_HOST and SPACE_ID at startup for information
|
| 192 |
+
space_host_startup = os.getenv("SPACE_HOST")
|
| 193 |
+
space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
|
| 194 |
+
|
| 195 |
+
if space_host_startup:
|
| 196 |
+
print(f"β
SPACE_HOST found: {space_host_startup}")
|
| 197 |
+
print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
| 198 |
+
else:
|
| 199 |
+
print("βΉοΈ SPACE_HOST environment variable not found (running locally?).")
|
| 200 |
+
|
| 201 |
+
if space_id_startup: # Print repo URLs if SPACE_ID is found
|
| 202 |
+
print(f"β
SPACE_ID found: {space_id_startup}")
|
| 203 |
+
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
| 204 |
+
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
| 205 |
+
else:
|
| 206 |
+
print("βΉοΈ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
| 207 |
+
|
| 208 |
+
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 209 |
+
|
| 210 |
+
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 211 |
+
demo.launch(debug=True, share=False)
|
explore_metadata.ipynb
ADDED
|
@@ -0,0 +1,332 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 9,
|
| 6 |
+
"id": "a600d7fc",
|
| 7 |
+
"metadata": {},
|
| 8 |
+
"outputs": [],
|
| 9 |
+
"source": [
|
| 10 |
+
"import json \n",
|
| 11 |
+
"with open('metadata.jsonl', 'r') as f: \n",
|
| 12 |
+
" json_list = list(f)\n",
|
| 13 |
+
"\n",
|
| 14 |
+
"json_QA = []\n",
|
| 15 |
+
"for json_str in json_list: \n",
|
| 16 |
+
" json_data = json.loads(json_str)\n",
|
| 17 |
+
" json_QA.append(json_data)"
|
| 18 |
+
]
|
| 19 |
+
},
|
| 20 |
+
{
|
| 21 |
+
"cell_type": "code",
|
| 22 |
+
"execution_count": 10,
|
| 23 |
+
"id": "fa5d8eb8",
|
| 24 |
+
"metadata": {},
|
| 25 |
+
"outputs": [
|
| 26 |
+
{
|
| 27 |
+
"name": "stdout",
|
| 28 |
+
"output_type": "stream",
|
| 29 |
+
"text": [
|
| 30 |
+
"==================================================\n",
|
| 31 |
+
"Task ID: d1af70ea-a9a4-421a-b9cc-94b5e02f1788\n",
|
| 32 |
+
"Question: As of the 2020 census, what was the population difference between the largest county seat and smallest county seat, by land area of the county seat, in Washington state? For population figures, please use the official data from data.census.gov. Please report the integer difference.\n",
|
| 33 |
+
"Level: 2\n",
|
| 34 |
+
"Final Answer: 736455\n",
|
| 35 |
+
"Annotator Metadata: \n",
|
| 36 |
+
" βββ Steps: \n",
|
| 37 |
+
" β βββ Step 1: Using a web browser, access a search engine and conduct a search, \"Washington cities by area\"\n",
|
| 38 |
+
" β βββ Step 2: Navigate to the second search result, https://en.wikipedia.org/wiki/List_of_municipalities_in_Washington\n",
|
| 39 |
+
" β βββ Step 3: Evaluate the page contents, finding the largest and smallest county seats by land area, Seattle and Cathlamet\n",
|
| 40 |
+
" β βββ Step 4: Using a web browser, navigate to https://data.census.gov/\n",
|
| 41 |
+
" β βββ Step 5: Using the website's search area, conduct a search, Seattle, Washington\n",
|
| 42 |
+
" β βββ Step 6: Record the reported 2020 Decennial Census population of Seattle, Washington, 737,015\n",
|
| 43 |
+
" β βββ Step 7: Using the website's search area, conduct a search, Cathlamet, Washington\n",
|
| 44 |
+
" β βββ Step 8: Record the reported 2020 Decennial Census population of Cathlamet, Washington, 560\n",
|
| 45 |
+
" β βββ Step 9: Using a calculator, find the difference in populations,\n",
|
| 46 |
+
" β βββ \n",
|
| 47 |
+
" β βββ 737,015 - 560\n",
|
| 48 |
+
" β βββ 736,455\n",
|
| 49 |
+
" β βββ Step 10: Report the correct answer to my user in the requested format, \"736,455\"\n",
|
| 50 |
+
" βββ Number of steps: 10\n",
|
| 51 |
+
" βββ How long did this take?: 5 minutes\n",
|
| 52 |
+
" βββ Tools:\n",
|
| 53 |
+
" β βββ 1. A web browser\n",
|
| 54 |
+
" β βββ 2. A search engine\n",
|
| 55 |
+
" β βββ 3. A calculator\n",
|
| 56 |
+
" βββ Number of tools: 3\n",
|
| 57 |
+
"==================================================\n"
|
| 58 |
+
]
|
| 59 |
+
}
|
| 60 |
+
],
|
| 61 |
+
"source": [
|
| 62 |
+
"import random\n",
|
| 63 |
+
"random_samples = random.sample(json_QA, 1)\n",
|
| 64 |
+
"for sample in random_samples:\n",
|
| 65 |
+
" print(\"=\" * 50)\n",
|
| 66 |
+
" print(f\"Task ID: {sample['task_id']}\")\n",
|
| 67 |
+
" print(f\"Question: {sample['Question']}\")\n",
|
| 68 |
+
" print(f\"Level: {sample['Level']}\")\n",
|
| 69 |
+
" print(f\"Final Answer: {sample['Final answer']}\")\n",
|
| 70 |
+
" print(f\"Annotator Metadata: \")\n",
|
| 71 |
+
" print(f\" βββ Steps: \")\n",
|
| 72 |
+
" for step in sample['Annotator Metadata']['Steps'].split('\\n'):\n",
|
| 73 |
+
" print(f\" β βββ {step}\")\n",
|
| 74 |
+
" print(f\" βββ Number of steps: {sample['Annotator Metadata']['Number of steps']}\")\n",
|
| 75 |
+
" print(f\" βββ How long did this take?: {sample['Annotator Metadata']['How long did this take?']}\")\n",
|
| 76 |
+
" print(f\" βββ Tools:\")\n",
|
| 77 |
+
" for tool in sample['Annotator Metadata']['Tools'].split('\\n'):\n",
|
| 78 |
+
" print(f\" β βββ {tool}\")\n",
|
| 79 |
+
" print(f\" βββ Number of tools: {sample['Annotator Metadata']['Number of tools']}\")\n",
|
| 80 |
+
"print(\"=\" * 50)"
|
| 81 |
+
]
|
| 82 |
+
},
|
| 83 |
+
{
|
| 84 |
+
"cell_type": "code",
|
| 85 |
+
"execution_count": 11,
|
| 86 |
+
"id": "05076516",
|
| 87 |
+
"metadata": {},
|
| 88 |
+
"outputs": [],
|
| 89 |
+
"source": [
|
| 90 |
+
"import os\n",
|
| 91 |
+
"from dotenv import load_dotenv\n",
|
| 92 |
+
"from langchain_huggingface import HuggingFaceEmbeddings\n",
|
| 93 |
+
"from langchain_community.vectorstores import SupabaseVectorStore\n",
|
| 94 |
+
"from supabase.client import Client, create_client\n",
|
| 95 |
+
"\n",
|
| 96 |
+
"\n",
|
| 97 |
+
"load_dotenv()\n",
|
| 98 |
+
"embeddings = HuggingFaceEmbeddings(model_name=\"sentence-transformers/all-mpnet-base-v2\") # dim=768\n",
|
| 99 |
+
"\n",
|
| 100 |
+
"supabase_url = os.environ.get(\"SUPABASE_URL\")\n",
|
| 101 |
+
"supabase_key = os.environ.get(\"SUPABASE_SERVICE_ROLE_KEY\")\n",
|
| 102 |
+
"supabase: Client = create_client(supabase_url, supabase_key)"
|
| 103 |
+
]
|
| 104 |
+
},
|
| 105 |
+
{
|
| 106 |
+
"cell_type": "code",
|
| 107 |
+
"execution_count": 20,
|
| 108 |
+
"id": "aa1402e3",
|
| 109 |
+
"metadata": {},
|
| 110 |
+
"outputs": [],
|
| 111 |
+
"source": [
|
| 112 |
+
"from langchain.schema import Document\n",
|
| 113 |
+
"docs = []\n",
|
| 114 |
+
"cnt = 0 \n",
|
| 115 |
+
"for sample in json_QA:\n",
|
| 116 |
+
" content = f\"Question : {sample['Question']}\\n\\nFinal answer : {sample['Final answer']}\"\n",
|
| 117 |
+
" doc = {\n",
|
| 118 |
+
" \"id\" : cnt,\n",
|
| 119 |
+
" \"content\" : content,\n",
|
| 120 |
+
" \"metadata\" : {\n",
|
| 121 |
+
" \"source\" : sample['task_id']\n",
|
| 122 |
+
" },\n",
|
| 123 |
+
" \"embedding\" : embeddings.embed_query(content),\n",
|
| 124 |
+
" }\n",
|
| 125 |
+
" docs.append(doc)\n",
|
| 126 |
+
" cnt += 1\n",
|
| 127 |
+
"\n",
|
| 128 |
+
"# upload the documents to the vector database\n",
|
| 129 |
+
"try:\n",
|
| 130 |
+
" response = (\n",
|
| 131 |
+
" supabase.table(\"documents2\")\n",
|
| 132 |
+
" .insert(docs)\n",
|
| 133 |
+
" .execute()\n",
|
| 134 |
+
" )\n",
|
| 135 |
+
"except Exception as exception:\n",
|
| 136 |
+
" print(\"Error inserting data into Supabase:\", exception)\n",
|
| 137 |
+
"\n",
|
| 138 |
+
"# # Save the documents (a list of dict) into a csv file, and manually upload it to Supabase\n",
|
| 139 |
+
"# import pandas as pd\n",
|
| 140 |
+
"# df = pd.DataFrame(docs)\n",
|
| 141 |
+
"# df.to_csv('supabase_docs.csv',index=False)"
|
| 142 |
+
]
|
| 143 |
+
},
|
| 144 |
+
{
|
| 145 |
+
"cell_type": "code",
|
| 146 |
+
"execution_count": 41,
|
| 147 |
+
"id": "9aa7eb5e",
|
| 148 |
+
"metadata": {},
|
| 149 |
+
"outputs": [],
|
| 150 |
+
"source": [
|
| 151 |
+
"# add items to vector database\n",
|
| 152 |
+
"vector_store = SupabaseVectorStore(\n",
|
| 153 |
+
" client=supabase,\n",
|
| 154 |
+
" embedding= embeddings,\n",
|
| 155 |
+
" table_name=\"documents2\",\n",
|
| 156 |
+
" query_name=\"match_documents_2\",\n",
|
| 157 |
+
")\n",
|
| 158 |
+
"retriever = vector_store.as_retriever()"
|
| 159 |
+
]
|
| 160 |
+
},
|
| 161 |
+
{
|
| 162 |
+
"cell_type": "code",
|
| 163 |
+
"execution_count": 42,
|
| 164 |
+
"id": "9eecafd1",
|
| 165 |
+
"metadata": {},
|
| 166 |
+
"outputs": [],
|
| 167 |
+
"source": [
|
| 168 |
+
"query = \"On June 6, 2023, an article by Carolyn Collins Petersen was published in Universe Today. This article mentions a team that produced a paper about their observations, linked at the bottom of the article. Find this paper. Under what NASA award number was the work performed by R. G. Arendt supported by?\"\n",
|
| 169 |
+
"# matched_docs = vector_store.similarity_search(query, k=2)\n",
|
| 170 |
+
"docs = retriever.invoke(query)"
|
| 171 |
+
]
|
| 172 |
+
},
|
| 173 |
+
{
|
| 174 |
+
"cell_type": "code",
|
| 175 |
+
"execution_count": 43,
|
| 176 |
+
"id": "ff917840",
|
| 177 |
+
"metadata": {},
|
| 178 |
+
"outputs": [
|
| 179 |
+
{
|
| 180 |
+
"data": {
|
| 181 |
+
"text/plain": [
|
| 182 |
+
"Document(metadata={'source': '840bfca7-4f7b-481a-8794-c560c340185d'}, page_content='Question : On June 6, 2023, an article by Carolyn Collins Petersen was published in Universe Today. This article mentions a team that produced a paper about their observations, linked at the bottom of the article. Find this paper. Under what NASA award number was the work performed by R. G. Arendt supported by?\\n\\nFinal answer : 80GSFC21M0002')"
|
| 183 |
+
]
|
| 184 |
+
},
|
| 185 |
+
"execution_count": 43,
|
| 186 |
+
"metadata": {},
|
| 187 |
+
"output_type": "execute_result"
|
| 188 |
+
}
|
| 189 |
+
],
|
| 190 |
+
"source": [
|
| 191 |
+
"docs[0]"
|
| 192 |
+
]
|
| 193 |
+
},
|
| 194 |
+
{
|
| 195 |
+
"cell_type": "code",
|
| 196 |
+
"execution_count": 44,
|
| 197 |
+
"id": "01c8f337",
|
| 198 |
+
"metadata": {},
|
| 199 |
+
"outputs": [
|
| 200 |
+
{
|
| 201 |
+
"name": "stdout",
|
| 202 |
+
"output_type": "stream",
|
| 203 |
+
"text": [
|
| 204 |
+
"List of tools used in all samples:\n",
|
| 205 |
+
"Total number of tools used: 83\n",
|
| 206 |
+
" βββ web browser: 107\n",
|
| 207 |
+
" βββ image recognition tools (to identify and parse a figure with three axes): 1\n",
|
| 208 |
+
" βββ search engine: 101\n",
|
| 209 |
+
" βββ calculator: 34\n",
|
| 210 |
+
" βββ unlambda compiler (optional): 1\n",
|
| 211 |
+
" βββ a web browser.: 2\n",
|
| 212 |
+
" βββ a search engine.: 2\n",
|
| 213 |
+
" βββ a calculator.: 1\n",
|
| 214 |
+
" βββ microsoft excel: 5\n",
|
| 215 |
+
" βββ google search: 1\n",
|
| 216 |
+
" βββ ne: 9\n",
|
| 217 |
+
" βββ pdf access: 7\n",
|
| 218 |
+
" βββ file handling: 2\n",
|
| 219 |
+
" βββ python: 3\n",
|
| 220 |
+
" βββ image recognition tools: 12\n",
|
| 221 |
+
" βββ jsonld file access: 1\n",
|
| 222 |
+
" βββ video parsing: 1\n",
|
| 223 |
+
" βββ python compiler: 1\n",
|
| 224 |
+
" βββ video recognition tools: 3\n",
|
| 225 |
+
" βββ pdf viewer: 7\n",
|
| 226 |
+
" βββ microsoft excel / google sheets: 3\n",
|
| 227 |
+
" βββ word document access: 1\n",
|
| 228 |
+
" βββ tool to extract text from images: 1\n",
|
| 229 |
+
" βββ a word reversal tool / script: 1\n",
|
| 230 |
+
" βββ counter: 1\n",
|
| 231 |
+
" βββ excel: 3\n",
|
| 232 |
+
" βββ image recognition: 5\n",
|
| 233 |
+
" βββ color recognition: 3\n",
|
| 234 |
+
" βββ excel file access: 3\n",
|
| 235 |
+
" βββ xml file access: 1\n",
|
| 236 |
+
" βββ access to the internet archive, web.archive.org: 1\n",
|
| 237 |
+
" βββ text processing/diff tool: 1\n",
|
| 238 |
+
" βββ gif parsing tools: 1\n",
|
| 239 |
+
" βββ a web browser: 7\n",
|
| 240 |
+
" βββ a search engine: 7\n",
|
| 241 |
+
" βββ a speech-to-text tool: 2\n",
|
| 242 |
+
" βββ code/data analysis tools: 1\n",
|
| 243 |
+
" βββ audio capability: 2\n",
|
| 244 |
+
" βββ pdf reader: 1\n",
|
| 245 |
+
" βββ markdown: 1\n",
|
| 246 |
+
" βββ a calculator: 5\n",
|
| 247 |
+
" βββ access to wikipedia: 3\n",
|
| 248 |
+
" βββ image recognition/ocr: 3\n",
|
| 249 |
+
" βββ google translate access: 1\n",
|
| 250 |
+
" βββ ocr: 4\n",
|
| 251 |
+
" βββ bass note data: 1\n",
|
| 252 |
+
" βββ text editor: 1\n",
|
| 253 |
+
" βββ xlsx file access: 1\n",
|
| 254 |
+
" βββ powerpoint viewer: 1\n",
|
| 255 |
+
" βββ csv file access: 1\n",
|
| 256 |
+
" βββ calculator (or use excel): 1\n",
|
| 257 |
+
" βββ computer algebra system: 1\n",
|
| 258 |
+
" βββ video processing software: 1\n",
|
| 259 |
+
" βββ audio processing software: 1\n",
|
| 260 |
+
" βββ computer vision: 1\n",
|
| 261 |
+
" βββ google maps: 1\n",
|
| 262 |
+
" βββ access to excel files: 1\n",
|
| 263 |
+
" βββ calculator (or ability to count): 1\n",
|
| 264 |
+
" βββ a file interface: 3\n",
|
| 265 |
+
" βββ a python ide: 1\n",
|
| 266 |
+
" βββ spreadsheet editor: 1\n",
|
| 267 |
+
" βββ tools required: 1\n",
|
| 268 |
+
" βββ b browser: 1\n",
|
| 269 |
+
" βββ image recognition and processing tools: 1\n",
|
| 270 |
+
" βββ computer vision or ocr: 1\n",
|
| 271 |
+
" βββ c++ compiler: 1\n",
|
| 272 |
+
" βββ access to google maps: 1\n",
|
| 273 |
+
" βββ youtube player: 1\n",
|
| 274 |
+
" βββ natural language processor: 1\n",
|
| 275 |
+
" βββ graph interaction tools: 1\n",
|
| 276 |
+
" βββ bablyonian cuniform -> arabic legend: 1\n",
|
| 277 |
+
" βββ access to youtube: 1\n",
|
| 278 |
+
" βββ image search tools: 1\n",
|
| 279 |
+
" βββ calculator or counting function: 1\n",
|
| 280 |
+
" βββ a speech-to-text audio processing tool: 1\n",
|
| 281 |
+
" βββ access to academic journal websites: 1\n",
|
| 282 |
+
" βββ pdf reader/extracter: 1\n",
|
| 283 |
+
" βββ rubik's cube model: 1\n",
|
| 284 |
+
" βββ wikipedia: 1\n",
|
| 285 |
+
" βββ video capability: 1\n",
|
| 286 |
+
" βββ image processing tools: 1\n",
|
| 287 |
+
" βββ age recognition software: 1\n",
|
| 288 |
+
" βββ youtube: 1\n"
|
| 289 |
+
]
|
| 290 |
+
}
|
| 291 |
+
],
|
| 292 |
+
"source": [
|
| 293 |
+
"# list of the tools used in all the samples\n",
|
| 294 |
+
"from collections import Counter, OrderedDict\n",
|
| 295 |
+
"\n",
|
| 296 |
+
"tools = []\n",
|
| 297 |
+
"for sample in json_QA:\n",
|
| 298 |
+
" for tool in sample['Annotator Metadata']['Tools'].split('\\n'):\n",
|
| 299 |
+
" tool = tool[2:].strip().lower()\n",
|
| 300 |
+
" if tool.startswith(\"(\"):\n",
|
| 301 |
+
" tool = tool[11:].strip()\n",
|
| 302 |
+
" tools.append(tool)\n",
|
| 303 |
+
"tools_counter = OrderedDict(Counter(tools))\n",
|
| 304 |
+
"print(\"List of tools used in all samples:\")\n",
|
| 305 |
+
"print(\"Total number of tools used:\", len(tools_counter))\n",
|
| 306 |
+
"for tool, count in tools_counter.items():\n",
|
| 307 |
+
" print(f\" βββ {tool}: {count}\")"
|
| 308 |
+
]
|
| 309 |
+
}
|
| 310 |
+
],
|
| 311 |
+
"metadata": {
|
| 312 |
+
"kernelspec": {
|
| 313 |
+
"display_name": "env",
|
| 314 |
+
"language": "python",
|
| 315 |
+
"name": "python3"
|
| 316 |
+
},
|
| 317 |
+
"language_info": {
|
| 318 |
+
"codemirror_mode": {
|
| 319 |
+
"name": "ipython",
|
| 320 |
+
"version": 3
|
| 321 |
+
},
|
| 322 |
+
"file_extension": ".py",
|
| 323 |
+
"mimetype": "text/x-python",
|
| 324 |
+
"name": "python",
|
| 325 |
+
"nbconvert_exporter": "python",
|
| 326 |
+
"pygments_lexer": "ipython3",
|
| 327 |
+
"version": "3.11.9"
|
| 328 |
+
}
|
| 329 |
+
},
|
| 330 |
+
"nbformat": 4,
|
| 331 |
+
"nbformat_minor": 5
|
| 332 |
+
}
|
image_processing.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import io
|
| 3 |
+
import base64
|
| 4 |
+
import uuid
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
# Helper functions for image processing
|
| 8 |
+
def encode_image(image_path: str) -> str:
|
| 9 |
+
"""Convert an image file to base64 string."""
|
| 10 |
+
with open(image_path, "rb") as image_file:
|
| 11 |
+
return base64.b64encode(image_file.read()).decode("utf-8")
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def decode_image(base64_string: str) -> Image.Image:
|
| 15 |
+
"""Convert a base64 string to a PIL Image."""
|
| 16 |
+
image_data = base64.b64decode(base64_string)
|
| 17 |
+
return Image.open(io.BytesIO(image_data))
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def save_image(image: Image.Image, directory: str = "image_outputs") -> str:
|
| 21 |
+
"""Save a PIL Image to disk and return the path."""
|
| 22 |
+
os.makedirs(directory, exist_ok=True)
|
| 23 |
+
image_id = str(uuid.uuid4())
|
| 24 |
+
image_path = os.path.join(directory, f"{image_id}.png")
|
| 25 |
+
image.save(image_path)
|
| 26 |
+
return image_path
|
metadata.jsonl
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
requirements.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
requests
|
| 3 |
+
langchain
|
| 4 |
+
langchain-community
|
| 5 |
+
langchain-core
|
| 6 |
+
langchain-google-genai
|
| 7 |
+
langchain-huggingface
|
| 8 |
+
langchain-groq
|
| 9 |
+
langchain-tavily
|
| 10 |
+
langchain-chroma
|
| 11 |
+
langgraph
|
| 12 |
+
huggingface_hub
|
| 13 |
+
supabase
|
| 14 |
+
arxiv
|
| 15 |
+
pymupdf
|
| 16 |
+
wikipedia
|
| 17 |
+
pgvector
|
| 18 |
+
python-dotenv
|
| 19 |
+
pytesseract
|
| 20 |
+
matplotlib
|
| 21 |
+
sentence_transformers
|
supabase_docs.csv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
system_prompt.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
You are a helpful assistant tasked with answering questions using a set of tools.
|
| 2 |
+
Now, I will ask you a question. Report your thoughts, and finish your answer with the following template:
|
| 3 |
+
FINAL ANSWER: [YOUR FINAL ANSWER].
|
| 4 |
+
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, Apply the rules above for each element (number or string), ensure there is exactly one space after each comma.
|
| 5 |
+
Your answer should only start with "FINAL ANSWER: ", then follows with the answer.
|