Spaces:
Running
Running
Luong Huu Thanh
commited on
Commit
·
67d801e
1
Parent(s):
bc622ec
some updates
Browse files- README.md +173 -86
- app.py +350 -197
- evaluation_app.py +211 -0
- qna_ui.py +0 -364
README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.25.2
|
| 8 |
app_file: app.py
|
|
@@ -12,58 +12,166 @@ hf_oauth: true
|
|
| 12 |
hf_oauth_expiration_minutes: 480
|
| 13 |
---
|
| 14 |
|
| 15 |
-
# **GAIA Agent**
|
| 16 |
|
| 17 |
-
## **Introduction**
|
| 18 |
|
| 19 |
-
**GAIA Agent** is
|
| 20 |
|
| 21 |
-
## **
|
| 22 |
|
| 23 |
-
|
| 24 |
-
-
|
| 25 |
-
-
|
| 26 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
- **Execute Multi-programming Language:** Execute code in multiple languages (Python, Bash, SQL, C, Java) and return results.
|
| 30 |
|
| 31 |
-
|
| 32 |
-
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
- **
|
| 44 |
-
- **
|
| 45 |
-
- **
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
- **Generate Simple Image:** Generate a simple image (gradient, noise, pattern, chart)
|
| 52 |
-
- **Combine Images:** Combine multiple images (collage, stack, blend)
|
| 53 |
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
### **
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
|
| 60 |
-
To set up the vector similarity search function in your Supabase database, execute the following SQL commands:
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
```sql
|
| 63 |
-
--
|
| 64 |
CREATE EXTENSION IF NOT EXISTS vector;
|
| 65 |
|
| 66 |
-
--
|
| 67 |
CREATE OR REPLACE FUNCTION public.match_documents_2(
|
| 68 |
query_embedding vector(768)
|
| 69 |
)
|
|
@@ -87,63 +195,42 @@ AS $$
|
|
| 87 |
LIMIT 10;
|
| 88 |
$$;
|
| 89 |
|
| 90 |
-
--
|
| 91 |
-
GRANT EXECUTE ON FUNCTION public.
|
| 92 |
```
|
| 93 |
|
| 94 |
-
|
| 95 |
-
1. Enables the pgvector extension for vector operations
|
| 96 |
-
2. Creates a stored function that performs vector similarity search using cosine distance (`<=>`)
|
| 97 |
-
3. Grants execution permissions to anonymous and authenticated users
|
| 98 |
-
4. Returns the top 10 most similar documents with their similarity scores
|
| 99 |
-
|
| 100 |
-
## **Installation**
|
| 101 |
-
Clone the repository, change the current working directory to this repository's root folder:
|
| 102 |
|
|
|
|
|
|
|
|
|
|
| 103 |
```
|
| 104 |
-
|
| 105 |
-
```
|
| 106 |
-
```
|
| 107 |
-
cd gaia-agent
|
| 108 |
-
```
|
| 109 |
-
|
| 110 |
-
Install ```requirements.txt``` (replace `3.11` with your installed Python version):
|
| 111 |
|
|
|
|
|
|
|
|
|
|
| 112 |
```
|
| 113 |
-
|
| 114 |
-
```
|
| 115 |
-
|
| 116 |
-
## **Environment Variables**
|
| 117 |
-
Store some API keys an variables in the `.env` file and load it in your code using `load_dotenv`
|
| 118 |
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
SUPABASE_SERVICE_ROLE_KEY=...
|
| 122 |
-
SUPABASE_SERVICE_KEY=...
|
| 123 |
-
HUGGINGFACEHUB_API_TOKEN=...
|
| 124 |
-
GROQ_API_KEY=...
|
| 125 |
-
TAVILY_API_KEY=...
|
| 126 |
-
LANGSMITH_API_KEY=...
|
| 127 |
|
| 128 |
-
|
| 129 |
-
LANGSMITH_PROJECT=ai_agent_course
|
| 130 |
-
LANGSMITH_ENDPOINT=https://api.smith.langchain.com
|
| 131 |
-
```
|
| 132 |
|
| 133 |
-
## **Demo**
|
| 134 |
-
To run the application using the command line, use the following command (replace `3.11` with your installed Python version):
|
| 135 |
-
```
|
| 136 |
-
py -3.11 app.py
|
| 137 |
-
```
|
| 138 |
-
Or run in the [Hugging Face Space](https://huggingface.co/spaces/fisherman611/gaia-agent)
|
| 139 |
-
## **Resources**
|
| 140 |
- [GAIA Benchmark](https://huggingface.co/spaces/gaia-benchmark/leaderboard)
|
| 141 |
- [Hugging Face Agents Course](https://huggingface.co/agents-course)
|
| 142 |
-
- [
|
|
|
|
|
|
|
|
|
|
| 143 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
-
## **
|
| 146 |
-
Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request.
|
| 147 |
|
| 148 |
-
## **License**
|
| 149 |
This project is licensed under the [MIT License](https://mit-license.org/).
|
|
|
|
| 1 |
---
|
| 2 |
+
title: GAIA Agent - Q&A Chatbot
|
| 3 |
+
emoji: 🤖
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: purple
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.25.2
|
| 8 |
app_file: app.py
|
|
|
|
| 12 |
hf_oauth_expiration_minutes: 480
|
| 13 |
---
|
| 14 |
|
| 15 |
+
# 🤖 **GAIA Agent - Advanced Q&A Chatbot**
|
| 16 |
|
| 17 |
+
## 🌟 **Introduction**
|
| 18 |
|
| 19 |
+
**GAIA Agent** is a sophisticated AI-powered chatbot system designed to handle complex questions and tasks through an intuitive Q&A interface. Built on top of the GAIA benchmark framework, this agent combines advanced reasoning, code execution, web search, document processing, and multimodal understanding capabilities. The system features both a user-friendly chatbot interface and a comprehensive evaluation runner for benchmark testing.
|
| 20 |
|
| 21 |
+
## 🚀 **Key Features**
|
| 22 |
|
| 23 |
+
- **🔍 Multi-Modal Search**: Web search, Wikipedia, and arXiv paper search
|
| 24 |
+
- **💻 Code Execution**: Support for Python, Bash, SQL, C, and Java
|
| 25 |
+
- **🖼️ Image Processing**: Analysis, transformation, OCR, and generation
|
| 26 |
+
- **📄 Document Processing**: PDF, CSV, Excel, and text file analysis
|
| 27 |
+
- **📁 File Upload Support**: Handle multiple file types with drag-and-drop
|
| 28 |
+
- **🧮 Mathematical Operations**: Complete set of mathematical tools
|
| 29 |
+
- **💬 Conversational Interface**: Natural chat-based interaction
|
| 30 |
+
- **📊 Evaluation System**: Automated benchmark testing and submission
|
| 31 |
|
| 32 |
+
## 🏗️ **Project Structure**
|
|
|
|
| 33 |
|
| 34 |
+
```
|
| 35 |
+
gaia-agent/
|
| 36 |
+
├── app.py # Main Q&A chatbot interface
|
| 37 |
+
├── evaluation_app.py # GAIA benchmark evaluation runner
|
| 38 |
+
├── agent.py # Core agent implementation with tools
|
| 39 |
+
├── code_interpreter.py # Multi-language code execution
|
| 40 |
+
├── image_processing.py # Image processing utilities
|
| 41 |
+
├── system_prompt.txt # System prompt for the agent
|
| 42 |
+
├── requirements.txt # Python dependencies
|
| 43 |
+
├── metadata.jsonl # GAIA benchmark metadata
|
| 44 |
+
├── explore_metadata.ipynb # Data exploration notebook
|
| 45 |
+
└── README.md # This file
|
| 46 |
+
```
|
| 47 |
|
| 48 |
+
## 🛠️ **Tool Categories**
|
| 49 |
+
|
| 50 |
+
### **🌐 Browser & Search Tools**
|
| 51 |
+
- **Wikipedia Search**: Search Wikipedia with up to 2 results
|
| 52 |
+
- **Web Search**: Tavily-powered web search with up to 3 results
|
| 53 |
+
- **arXiv Search**: Academic paper search with up to 3 results
|
| 54 |
+
|
| 55 |
+
### **💻 Code Interpreter Tools**
|
| 56 |
+
- **Multi-Language Execution**: Python, Bash, SQL, C, Java support
|
| 57 |
+
- **Plot Generation**: Matplotlib visualization support
|
| 58 |
+
- **DataFrame Analysis**: Pandas data processing
|
| 59 |
+
- **Error Handling**: Comprehensive error reporting
|
| 60 |
+
|
| 61 |
+
### **🧮 Mathematical Tools**
|
| 62 |
+
- **Basic Operations**: Add, subtract, multiply, divide
|
| 63 |
+
- **Advanced Functions**: Modulus, power, square root
|
| 64 |
+
- **Complex Numbers**: Support for complex number operations
|
| 65 |
+
|
| 66 |
+
### **📄 Document Processing Tools**
|
| 67 |
+
- **File Operations**: Save, read, and download files
|
| 68 |
+
- **CSV Analysis**: Pandas-based data analysis
|
| 69 |
+
- **Excel Processing**: Excel file analysis and processing
|
| 70 |
+
- **OCR**: Extract text from images using Tesseract
|
| 71 |
+
|
| 72 |
+
### **🖼️ Image Processing & Generation Tools**
|
| 73 |
+
- **Image Analysis**: Size, color, and property analysis
|
| 74 |
+
- **Transformations**: Resize, rotate, crop, flip, adjust brightness/contrast
|
| 75 |
+
- **Drawing Tools**: Add shapes, text, and annotations
|
| 76 |
+
- **Image Generation**: Create gradients, noise patterns, and simple graphics
|
| 77 |
+
- **Image Combination**: Stack and combine multiple images
|
| 78 |
+
|
| 79 |
+
## 🎯 **How to Use**
|
| 80 |
+
|
| 81 |
+
### **Q&A Chatbot Interface (app.py)**
|
| 82 |
+
|
| 83 |
+
1. **Start the Chatbot:**
|
| 84 |
+
```bash
|
| 85 |
+
python app.py
|
| 86 |
+
```
|
| 87 |
+
|
| 88 |
+
2. **Access the Interface:**
|
| 89 |
+
- Open `http://localhost:7860` in your browser
|
| 90 |
+
- Upload files (images, documents, CSV, etc.) if needed
|
| 91 |
+
- Ask questions in natural language
|
| 92 |
+
- Get comprehensive answers with tool usage
|
| 93 |
+
|
| 94 |
+
3. **Supported Interactions:**
|
| 95 |
+
- **Text Questions**: "What is the capital of France?"
|
| 96 |
+
- **Math Problems**: "Calculate the square root of 144"
|
| 97 |
+
- **Code Requests**: "Write a Python function to sort a list"
|
| 98 |
+
- **Image Analysis**: Upload an image and ask "What do you see?"
|
| 99 |
+
- **Data Analysis**: Upload a CSV and ask "What are the trends?"
|
| 100 |
+
- **Web Search**: "What are the latest AI developments?"
|
| 101 |
+
|
| 102 |
+
### **Evaluation Runner (evaluation_app.py)**
|
| 103 |
+
|
| 104 |
+
1. **Run the Evaluation:**
|
| 105 |
+
```bash
|
| 106 |
+
python evaluation_app.py
|
| 107 |
+
```
|
| 108 |
+
|
| 109 |
+
2. **Benchmark Testing:**
|
| 110 |
+
- Log in with your Hugging Face account
|
| 111 |
+
- Click "Run Evaluation & Submit All Answers"
|
| 112 |
+
- Monitor progress as the agent processes GAIA benchmark questions
|
| 113 |
+
- View results and scores automatically
|
| 114 |
+
|
| 115 |
+
## 🔧 **Technical Architecture**
|
| 116 |
+
|
| 117 |
+
### **LangGraph State Machine**
|
| 118 |
+
```
|
| 119 |
+
START → Retriever → Assistant → Tools → Assistant
|
| 120 |
+
↑ ↓
|
| 121 |
+
└──────────────┘
|
| 122 |
+
```
|
| 123 |
|
| 124 |
+
1. **Retriever Node**: Searches vector database for similar questions
|
| 125 |
+
2. **Assistant Node**: LLM processes question with available tools
|
| 126 |
+
3. **Tools Node**: Executes selected tools (web search, code, etc.)
|
| 127 |
+
4. **Conditional Routing**: Dynamically routes between assistant and tools
|
|
|
|
|
|
|
| 128 |
|
| 129 |
+
### **Vector Database Integration**
|
| 130 |
+
- **Supabase Vector Store**: Stores GAIA benchmark Q&A pairs
|
| 131 |
+
- **Semantic Search**: Finds similar questions for context
|
| 132 |
+
- **HuggingFace Embeddings**: sentence-transformers/all-mpnet-base-v2
|
| 133 |
|
| 134 |
+
### **Multi-Modal File Support**
|
| 135 |
+
- **Images**: JPG, PNG, GIF, BMP, WebP (Base64 processing)
|
| 136 |
+
- **Documents**: PDF, DOC, DOCX, TXT, MD (Content extraction)
|
| 137 |
+
- **Data**: CSV, Excel (Pandas analysis)
|
| 138 |
+
- **Code**: Python, JavaScript, HTML, CSS, JSON, XML
|
| 139 |
|
| 140 |
+
## ⚙️ **Installation & Setup**
|
|
|
|
| 141 |
|
| 142 |
+
### **1. Clone Repository**
|
| 143 |
+
```bash
|
| 144 |
+
git clone https://github.com/fisherman611/gaia-agent.git
|
| 145 |
+
cd gaia-agent
|
| 146 |
+
```
|
| 147 |
+
|
| 148 |
+
### **2. Install Dependencies**
|
| 149 |
+
```bash
|
| 150 |
+
pip install -r requirements.txt
|
| 151 |
+
```
|
| 152 |
+
|
| 153 |
+
### **3. Environment Variables**
|
| 154 |
+
Create a `.env` file with your API keys:
|
| 155 |
+
```env
|
| 156 |
+
SUPABASE_URL=your_supabase_url
|
| 157 |
+
SUPABASE_SERVICE_ROLE_KEY=your_supabase_key
|
| 158 |
+
GROQ_API_KEY=your_groq_api_key
|
| 159 |
+
TAVILY_API_KEY=your_tavily_api_key
|
| 160 |
+
HUGGINGFACEHUB_API_TOKEN=your_hf_token
|
| 161 |
+
LANGSMITH_API_KEY=your_langsmith_key
|
| 162 |
+
|
| 163 |
+
LANGSMITH_TRACING=true
|
| 164 |
+
LANGSMITH_PROJECT=ai_agent_course
|
| 165 |
+
LANGSMITH_ENDPOINT=https://api.smith.langchain.com
|
| 166 |
+
```
|
| 167 |
+
|
| 168 |
+
### **4. Database Setup (Supabase)**
|
| 169 |
+
Execute this SQL in your Supabase database:
|
| 170 |
```sql
|
| 171 |
+
-- Enable pgvector extension
|
| 172 |
CREATE EXTENSION IF NOT EXISTS vector;
|
| 173 |
|
| 174 |
+
-- Create match function for documents2 table
|
| 175 |
CREATE OR REPLACE FUNCTION public.match_documents_2(
|
| 176 |
query_embedding vector(768)
|
| 177 |
)
|
|
|
|
| 195 |
LIMIT 10;
|
| 196 |
$$;
|
| 197 |
|
| 198 |
+
-- Grant permissions
|
| 199 |
+
GRANT EXECUTE ON FUNCTION public.match_documents_2(vector) TO anon, authenticated;
|
| 200 |
```
|
| 201 |
|
| 202 |
+
## 🚀 **Running the Application**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
|
| 204 |
+
### **Chatbot Interface**
|
| 205 |
+
```bash
|
| 206 |
+
python app.py
|
| 207 |
```
|
| 208 |
+
Access at: `http://localhost:7860`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
|
| 210 |
+
### **Evaluation Runner**
|
| 211 |
+
```bash
|
| 212 |
+
python evaluation_app.py
|
| 213 |
```
|
| 214 |
+
Access at: `http://localhost:7860`
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
|
| 216 |
+
### **Live Demo**
|
| 217 |
+
Try it online: [Hugging Face Space](https://huggingface.co/spaces/fisherman611/gaia-agent)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
|
| 219 |
+
## 🔗 **Resources**
|
|
|
|
|
|
|
|
|
|
| 220 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
- [GAIA Benchmark](https://huggingface.co/spaces/gaia-benchmark/leaderboard)
|
| 222 |
- [Hugging Face Agents Course](https://huggingface.co/agents-course)
|
| 223 |
+
- [LangGraph Documentation](https://langchain-ai.github.io/langgraph/)
|
| 224 |
+
- [Supabase Vector Store](https://supabase.com/docs/guides/ai/vector-columns)
|
| 225 |
+
|
| 226 |
+
## 🤝 **Contributing**
|
| 227 |
|
| 228 |
+
Contributions are welcome! Areas for improvement:
|
| 229 |
+
- **New Tools**: Add specialized tools for specific domains
|
| 230 |
+
- **UI Enhancements**: Improve the chatbot interface
|
| 231 |
+
- **Performance**: Optimize response times and accuracy
|
| 232 |
+
- **Documentation**: Expand examples and use cases
|
| 233 |
|
| 234 |
+
## 📄 **License**
|
|
|
|
| 235 |
|
|
|
|
| 236 |
This project is licensed under the [MIT License](https://mit-license.org/).
|
app.py
CHANGED
|
@@ -1,211 +1,364 @@
|
|
| 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 |
-
|
| 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("
|
|
|
|
| 25 |
self.graph = build_graph()
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 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 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
except Exception as e:
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
)
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
|
| 189 |
if __name__ == "__main__":
|
| 190 |
-
print("\n" + "-"*
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 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)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
# Custom CSS for better styling
|
| 161 |
+
custom_css = """
|
| 162 |
+
.gradio-container {
|
| 163 |
+
max-width: 1200px !important;
|
| 164 |
+
margin: auto !important;
|
| 165 |
+
}
|
| 166 |
+
.chat-message {
|
| 167 |
+
padding: 10px !important;
|
| 168 |
+
margin: 5px 0 !important;
|
| 169 |
+
border-radius: 10px !important;
|
| 170 |
+
}
|
| 171 |
+
.user-message {
|
| 172 |
+
background-color: #e3f2fd !important;
|
| 173 |
+
margin-left: 20% !important;
|
| 174 |
+
}
|
| 175 |
+
.bot-message {
|
| 176 |
+
background-color: #f5f5f5 !important;
|
| 177 |
+
margin-right: 20% !important;
|
| 178 |
+
}
|
| 179 |
+
"""
|
| 180 |
+
|
| 181 |
+
with gr.Blocks(css=custom_css, title="GAIA Agent - Q&A Chatbot") as demo:
|
| 182 |
+
gr.Markdown(
|
| 183 |
+
"""
|
| 184 |
+
# 🤖 GAIA Agent - Q&A Chatbot
|
| 185 |
+
|
| 186 |
+
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.
|
| 187 |
+
|
| 188 |
+
**What I can do:**
|
| 189 |
+
- 🔍 Search the web, Wikipedia, and academic papers
|
| 190 |
+
- 🧮 Perform mathematical calculations
|
| 191 |
+
- 💻 Execute code in multiple languages (Python, Bash, SQL, C, Java)
|
| 192 |
+
- 📊 Analyze CSV and Excel files
|
| 193 |
+
- 🖼️ Process and analyze images (JPG, PNG, GIF, etc.)
|
| 194 |
+
- 📄 Extract text from images (OCR)
|
| 195 |
+
- 📁 Handle file uploads and processing (PDF, DOC, TXT, etc.)
|
| 196 |
+
- 📈 Create visualizations and charts
|
| 197 |
+
- 🔧 Multi-file analysis and comparison
|
| 198 |
+
- And much more!
|
| 199 |
+
|
| 200 |
+
---
|
| 201 |
+
"""
|
| 202 |
)
|
| 203 |
+
|
| 204 |
+
# Chat interface
|
| 205 |
+
with gr.Row():
|
| 206 |
+
with gr.Column(scale=1):
|
| 207 |
+
chatbot_interface = gr.Chatbot(
|
| 208 |
+
label="Conversation",
|
| 209 |
+
height=500,
|
| 210 |
+
show_label=True,
|
| 211 |
+
container=True,
|
| 212 |
+
bubble_full_width=False
|
| 213 |
+
)
|
| 214 |
+
|
| 215 |
+
# File upload section
|
| 216 |
+
with gr.Row():
|
| 217 |
+
with gr.Column():
|
| 218 |
+
file_upload = gr.File(
|
| 219 |
+
label="📁 Upload Files (Images, Documents, CSV, Excel, etc.)",
|
| 220 |
+
file_count="multiple",
|
| 221 |
+
file_types=[
|
| 222 |
+
".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp", # Images
|
| 223 |
+
".txt", ".md", ".py", ".js", ".html", ".css", ".json", ".xml", # Text files
|
| 224 |
+
".csv", ".xlsx", ".xls", # Data files
|
| 225 |
+
".pdf", ".doc", ".docx" # Documents
|
| 226 |
+
],
|
| 227 |
+
height=100
|
| 228 |
+
)
|
| 229 |
+
|
| 230 |
+
with gr.Row():
|
| 231 |
+
with gr.Column(scale=8):
|
| 232 |
+
question_input = gr.Textbox(
|
| 233 |
+
label="Ask a question",
|
| 234 |
+
placeholder="Type your question here or upload files above... (e.g., 'What is the capital of France?', 'Analyze this image', 'Summarize this document')",
|
| 235 |
+
lines=2,
|
| 236 |
+
max_lines=5
|
| 237 |
+
)
|
| 238 |
+
with gr.Column(scale=1, min_width=100):
|
| 239 |
+
submit_btn = gr.Button("Send", variant="primary", size="lg")
|
| 240 |
+
|
| 241 |
+
with gr.Row():
|
| 242 |
+
clear_btn = gr.Button("Clear History", variant="secondary")
|
| 243 |
+
clear_files_btn = gr.Button("Clear Files", variant="secondary")
|
| 244 |
+
|
| 245 |
+
# Example questions
|
| 246 |
+
with gr.Row():
|
| 247 |
+
gr.Markdown("### 💡 Example Questions:")
|
| 248 |
+
|
| 249 |
+
with gr.Row():
|
| 250 |
+
with gr.Column():
|
| 251 |
+
gr.Examples(
|
| 252 |
+
examples=[
|
| 253 |
+
"What is the current population of Tokyo?",
|
| 254 |
+
"Calculate the square root of 144",
|
| 255 |
+
"Write a Python function to sort a list",
|
| 256 |
+
"What are the latest developments in AI?",
|
| 257 |
+
"Explain quantum computing in simple terms",
|
| 258 |
+
],
|
| 259 |
+
inputs=question_input,
|
| 260 |
+
label="General Questions"
|
| 261 |
+
)
|
| 262 |
+
with gr.Column():
|
| 263 |
+
gr.Examples(
|
| 264 |
+
examples=[
|
| 265 |
+
"Search for recent papers on machine learning",
|
| 266 |
+
"What is the weather like today?",
|
| 267 |
+
"Create a simple bar chart using Python",
|
| 268 |
+
"Convert 100 USD to EUR",
|
| 269 |
+
"What are the benefits of renewable energy?",
|
| 270 |
+
],
|
| 271 |
+
inputs=question_input,
|
| 272 |
+
label="Research & Analysis"
|
| 273 |
+
)
|
| 274 |
+
with gr.Column():
|
| 275 |
+
gr.Examples(
|
| 276 |
+
examples=[
|
| 277 |
+
"Analyze this image and describe what you see",
|
| 278 |
+
"Extract text from this image using OCR",
|
| 279 |
+
"Summarize the content of this document",
|
| 280 |
+
"Analyze the data in this CSV file",
|
| 281 |
+
"What insights can you find in this Excel file?",
|
| 282 |
+
],
|
| 283 |
+
inputs=question_input,
|
| 284 |
+
label="File Analysis"
|
| 285 |
+
)
|
| 286 |
+
|
| 287 |
+
# Event handlers
|
| 288 |
+
def submit_question(question, history, files):
|
| 289 |
+
print(f"🎯 UI: Submit button clicked")
|
| 290 |
+
print(f"📝 UI: Question length: {len(question) if question else 0}")
|
| 291 |
+
print(f"📁 UI: Files count: {len(files) if files else 0}")
|
| 292 |
+
result_question, result_history = chatbot.process_question(question, history, files)
|
| 293 |
+
print(f"🔄 UI: Returning results and clearing files")
|
| 294 |
+
return result_question, result_history, None # Clear files after processing
|
| 295 |
+
|
| 296 |
+
def clear_conversation():
|
| 297 |
+
print("🧹 UI: Clear conversation button clicked")
|
| 298 |
+
return chatbot.clear_history()
|
| 299 |
+
|
| 300 |
+
def clear_files():
|
| 301 |
+
print("🗑️ UI: Clear files button clicked")
|
| 302 |
+
return None
|
| 303 |
+
|
| 304 |
+
# Connect the events
|
| 305 |
+
submit_btn.click(
|
| 306 |
+
fn=submit_question,
|
| 307 |
+
inputs=[question_input, chatbot_interface, file_upload],
|
| 308 |
+
outputs=[question_input, chatbot_interface, file_upload],
|
| 309 |
+
show_progress=True
|
| 310 |
+
)
|
| 311 |
+
|
| 312 |
+
question_input.submit(
|
| 313 |
+
fn=submit_question,
|
| 314 |
+
inputs=[question_input, chatbot_interface, file_upload],
|
| 315 |
+
outputs=[question_input, chatbot_interface, file_upload],
|
| 316 |
+
show_progress=True
|
| 317 |
+
)
|
| 318 |
+
|
| 319 |
+
clear_btn.click(
|
| 320 |
+
fn=clear_conversation,
|
| 321 |
+
outputs=[chatbot_interface],
|
| 322 |
+
show_progress=False
|
| 323 |
+
)
|
| 324 |
+
|
| 325 |
+
clear_files_btn.click(
|
| 326 |
+
fn=clear_files,
|
| 327 |
+
outputs=[file_upload],
|
| 328 |
+
show_progress=False
|
| 329 |
+
)
|
| 330 |
+
|
| 331 |
+
# Footer
|
| 332 |
+
gr.Markdown(
|
| 333 |
+
"""
|
| 334 |
+
---
|
| 335 |
+
|
| 336 |
+
**Note:** This agent uses various tools and APIs to provide comprehensive answers.
|
| 337 |
+
Processing complex questions and file analysis may take some time. Please be patient!
|
| 338 |
+
|
| 339 |
+
**Supported file types:**
|
| 340 |
+
- **Images:** JPG, PNG, GIF, BMP, WebP
|
| 341 |
+
- **Documents:** PDF, DOC, DOCX, TXT, MD
|
| 342 |
+
- **Data files:** CSV, Excel (XLS, XLSX)
|
| 343 |
+
- **Code files:** Python, JavaScript, HTML, CSS, JSON, XML
|
| 344 |
+
|
| 345 |
+
**Powered by:** LangGraph, Groq, and various specialized tools
|
| 346 |
+
"""
|
| 347 |
+
)
|
| 348 |
+
|
| 349 |
+
return demo
|
| 350 |
|
| 351 |
if __name__ == "__main__":
|
| 352 |
+
print("\n" + "-"*50)
|
| 353 |
+
print("🚀 Starting GAIA Agent Q&A Chatbot...")
|
| 354 |
+
print("-"*50 + "\n")
|
| 355 |
+
|
| 356 |
+
# Create and launch the interface
|
| 357 |
+
demo = create_qna_interface()
|
| 358 |
+
demo.launch(
|
| 359 |
+
debug=True,
|
| 360 |
+
share=False,
|
| 361 |
+
server_name="0.0.0.0",
|
| 362 |
+
server_port=7860,
|
| 363 |
+
show_error=True
|
| 364 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
evaluation_app.py
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|
qna_ui.py
DELETED
|
@@ -1,364 +0,0 @@
|
|
| 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 |
-
# Custom CSS for better styling
|
| 161 |
-
custom_css = """
|
| 162 |
-
.gradio-container {
|
| 163 |
-
max-width: 1200px !important;
|
| 164 |
-
margin: auto !important;
|
| 165 |
-
}
|
| 166 |
-
.chat-message {
|
| 167 |
-
padding: 10px !important;
|
| 168 |
-
margin: 5px 0 !important;
|
| 169 |
-
border-radius: 10px !important;
|
| 170 |
-
}
|
| 171 |
-
.user-message {
|
| 172 |
-
background-color: #e3f2fd !important;
|
| 173 |
-
margin-left: 20% !important;
|
| 174 |
-
}
|
| 175 |
-
.bot-message {
|
| 176 |
-
background-color: #f5f5f5 !important;
|
| 177 |
-
margin-right: 20% !important;
|
| 178 |
-
}
|
| 179 |
-
"""
|
| 180 |
-
|
| 181 |
-
with gr.Blocks(css=custom_css, title="GAIA Agent - Q&A Chatbot") as demo:
|
| 182 |
-
gr.Markdown(
|
| 183 |
-
"""
|
| 184 |
-
# 🤖 GAIA Agent - Q&A Chatbot
|
| 185 |
-
|
| 186 |
-
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.
|
| 187 |
-
|
| 188 |
-
**What I can do:**
|
| 189 |
-
- 🔍 Search the web, Wikipedia, and academic papers
|
| 190 |
-
- 🧮 Perform mathematical calculations
|
| 191 |
-
- 💻 Execute code in multiple languages (Python, Bash, SQL, C, Java)
|
| 192 |
-
- 📊 Analyze CSV and Excel files
|
| 193 |
-
- 🖼️ Process and analyze images (JPG, PNG, GIF, etc.)
|
| 194 |
-
- 📄 Extract text from images (OCR)
|
| 195 |
-
- 📁 Handle file uploads and processing (PDF, DOC, TXT, etc.)
|
| 196 |
-
- 📈 Create visualizations and charts
|
| 197 |
-
- 🔧 Multi-file analysis and comparison
|
| 198 |
-
- And much more!
|
| 199 |
-
|
| 200 |
-
---
|
| 201 |
-
"""
|
| 202 |
-
)
|
| 203 |
-
|
| 204 |
-
# Chat interface
|
| 205 |
-
with gr.Row():
|
| 206 |
-
with gr.Column(scale=1):
|
| 207 |
-
chatbot_interface = gr.Chatbot(
|
| 208 |
-
label="Conversation",
|
| 209 |
-
height=500,
|
| 210 |
-
show_label=True,
|
| 211 |
-
container=True,
|
| 212 |
-
bubble_full_width=False
|
| 213 |
-
)
|
| 214 |
-
|
| 215 |
-
# File upload section
|
| 216 |
-
with gr.Row():
|
| 217 |
-
with gr.Column():
|
| 218 |
-
file_upload = gr.File(
|
| 219 |
-
label="📁 Upload Files (Images, Documents, CSV, Excel, etc.)",
|
| 220 |
-
file_count="multiple",
|
| 221 |
-
file_types=[
|
| 222 |
-
".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp", # Images
|
| 223 |
-
".txt", ".md", ".py", ".js", ".html", ".css", ".json", ".xml", # Text files
|
| 224 |
-
".csv", ".xlsx", ".xls", # Data files
|
| 225 |
-
".pdf", ".doc", ".docx" # Documents
|
| 226 |
-
],
|
| 227 |
-
height=100
|
| 228 |
-
)
|
| 229 |
-
|
| 230 |
-
with gr.Row():
|
| 231 |
-
with gr.Column(scale=8):
|
| 232 |
-
question_input = gr.Textbox(
|
| 233 |
-
label="Ask a question",
|
| 234 |
-
placeholder="Type your question here or upload files above... (e.g., 'What is the capital of France?', 'Analyze this image', 'Summarize this document')",
|
| 235 |
-
lines=2,
|
| 236 |
-
max_lines=5
|
| 237 |
-
)
|
| 238 |
-
with gr.Column(scale=1, min_width=100):
|
| 239 |
-
submit_btn = gr.Button("Send", variant="primary", size="lg")
|
| 240 |
-
|
| 241 |
-
with gr.Row():
|
| 242 |
-
clear_btn = gr.Button("Clear History", variant="secondary")
|
| 243 |
-
clear_files_btn = gr.Button("Clear Files", variant="secondary")
|
| 244 |
-
|
| 245 |
-
# Example questions
|
| 246 |
-
with gr.Row():
|
| 247 |
-
gr.Markdown("### 💡 Example Questions:")
|
| 248 |
-
|
| 249 |
-
with gr.Row():
|
| 250 |
-
with gr.Column():
|
| 251 |
-
gr.Examples(
|
| 252 |
-
examples=[
|
| 253 |
-
"What is the current population of Tokyo?",
|
| 254 |
-
"Calculate the square root of 144",
|
| 255 |
-
"Write a Python function to sort a list",
|
| 256 |
-
"What are the latest developments in AI?",
|
| 257 |
-
"Explain quantum computing in simple terms",
|
| 258 |
-
],
|
| 259 |
-
inputs=question_input,
|
| 260 |
-
label="General Questions"
|
| 261 |
-
)
|
| 262 |
-
with gr.Column():
|
| 263 |
-
gr.Examples(
|
| 264 |
-
examples=[
|
| 265 |
-
"Search for recent papers on machine learning",
|
| 266 |
-
"What is the weather like today?",
|
| 267 |
-
"Create a simple bar chart using Python",
|
| 268 |
-
"Convert 100 USD to EUR",
|
| 269 |
-
"What are the benefits of renewable energy?",
|
| 270 |
-
],
|
| 271 |
-
inputs=question_input,
|
| 272 |
-
label="Research & Analysis"
|
| 273 |
-
)
|
| 274 |
-
with gr.Column():
|
| 275 |
-
gr.Examples(
|
| 276 |
-
examples=[
|
| 277 |
-
"Analyze this image and describe what you see",
|
| 278 |
-
"Extract text from this image using OCR",
|
| 279 |
-
"Summarize the content of this document",
|
| 280 |
-
"Analyze the data in this CSV file",
|
| 281 |
-
"What insights can you find in this Excel file?",
|
| 282 |
-
],
|
| 283 |
-
inputs=question_input,
|
| 284 |
-
label="File Analysis"
|
| 285 |
-
)
|
| 286 |
-
|
| 287 |
-
# Event handlers
|
| 288 |
-
def submit_question(question, history, files):
|
| 289 |
-
print(f"🎯 UI: Submit button clicked")
|
| 290 |
-
print(f"📝 UI: Question length: {len(question) if question else 0}")
|
| 291 |
-
print(f"📁 UI: Files count: {len(files) if files else 0}")
|
| 292 |
-
result_question, result_history = chatbot.process_question(question, history, files)
|
| 293 |
-
print(f"🔄 UI: Returning results and clearing files")
|
| 294 |
-
return result_question, result_history, None # Clear files after processing
|
| 295 |
-
|
| 296 |
-
def clear_conversation():
|
| 297 |
-
print("🧹 UI: Clear conversation button clicked")
|
| 298 |
-
return chatbot.clear_history()
|
| 299 |
-
|
| 300 |
-
def clear_files():
|
| 301 |
-
print("🗑️ UI: Clear files button clicked")
|
| 302 |
-
return None
|
| 303 |
-
|
| 304 |
-
# Connect the events
|
| 305 |
-
submit_btn.click(
|
| 306 |
-
fn=submit_question,
|
| 307 |
-
inputs=[question_input, chatbot_interface, file_upload],
|
| 308 |
-
outputs=[question_input, chatbot_interface, file_upload],
|
| 309 |
-
show_progress=True
|
| 310 |
-
)
|
| 311 |
-
|
| 312 |
-
question_input.submit(
|
| 313 |
-
fn=submit_question,
|
| 314 |
-
inputs=[question_input, chatbot_interface, file_upload],
|
| 315 |
-
outputs=[question_input, chatbot_interface, file_upload],
|
| 316 |
-
show_progress=True
|
| 317 |
-
)
|
| 318 |
-
|
| 319 |
-
clear_btn.click(
|
| 320 |
-
fn=clear_conversation,
|
| 321 |
-
outputs=[chatbot_interface],
|
| 322 |
-
show_progress=False
|
| 323 |
-
)
|
| 324 |
-
|
| 325 |
-
clear_files_btn.click(
|
| 326 |
-
fn=clear_files,
|
| 327 |
-
outputs=[file_upload],
|
| 328 |
-
show_progress=False
|
| 329 |
-
)
|
| 330 |
-
|
| 331 |
-
# Footer
|
| 332 |
-
gr.Markdown(
|
| 333 |
-
"""
|
| 334 |
-
---
|
| 335 |
-
|
| 336 |
-
**Note:** This agent uses various tools and APIs to provide comprehensive answers.
|
| 337 |
-
Processing complex questions and file analysis may take some time. Please be patient!
|
| 338 |
-
|
| 339 |
-
**Supported file types:**
|
| 340 |
-
- **Images:** JPG, PNG, GIF, BMP, WebP
|
| 341 |
-
- **Documents:** PDF, DOC, DOCX, TXT, MD
|
| 342 |
-
- **Data files:** CSV, Excel (XLS, XLSX)
|
| 343 |
-
- **Code files:** Python, JavaScript, HTML, CSS, JSON, XML
|
| 344 |
-
|
| 345 |
-
**Powered by:** LangGraph, Groq, and various specialized tools
|
| 346 |
-
"""
|
| 347 |
-
)
|
| 348 |
-
|
| 349 |
-
return demo
|
| 350 |
-
|
| 351 |
-
if __name__ == "__main__":
|
| 352 |
-
print("\n" + "-"*50)
|
| 353 |
-
print("🚀 Starting GAIA Agent Q&A Chatbot...")
|
| 354 |
-
print("-"*50 + "\n")
|
| 355 |
-
|
| 356 |
-
# Create and launch the interface
|
| 357 |
-
demo = create_qna_interface()
|
| 358 |
-
demo.launch(
|
| 359 |
-
debug=True,
|
| 360 |
-
share=False,
|
| 361 |
-
server_name="0.0.0.0",
|
| 362 |
-
server_port=7860,
|
| 363 |
-
show_error=True
|
| 364 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|