Spaces:
Runtime error
Runtime error
Added Hugging Face Dataset Upload Tool
Browse files
app.py
CHANGED
|
@@ -4,6 +4,10 @@ import requests
|
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
from openai import OpenAI
|
| 8 |
from tools.final_answer import FinalAnswerTool
|
| 9 |
|
|
@@ -45,6 +49,66 @@ def Sonar_Web_Search_Tool(arg1: str, arg2: str) -> str:
|
|
| 45 |
return sonar_response
|
| 46 |
except Exception as e:
|
| 47 |
return f"Error using Sonar Websearch tool '{arg1} {arg2}': {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
|
| 50 |
@tool
|
|
@@ -81,7 +145,13 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 81 |
|
| 82 |
agent = CodeAgent(
|
| 83 |
model=model,
|
| 84 |
-
tools=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
max_steps=6,
|
| 86 |
verbosity_level=1,
|
| 87 |
grammar=None,
|
|
|
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
import os
|
| 7 |
+
import json
|
| 8 |
+
import uuid
|
| 9 |
+
from datasets import Dataset
|
| 10 |
+
from huggingface_hub import HfApi
|
| 11 |
from openai import OpenAI
|
| 12 |
from tools.final_answer import FinalAnswerTool
|
| 13 |
|
|
|
|
| 49 |
return sonar_response
|
| 50 |
except Exception as e:
|
| 51 |
return f"Error using Sonar Websearch tool '{arg1} {arg2}': {str(e)}"
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def Dataset_Creator_Function(dataset_name: str, conversation_data: str) -> str:
|
| 55 |
+
"""Creates and pushes a dataset to Hugging Face with the conversation history.
|
| 56 |
+
|
| 57 |
+
Args:
|
| 58 |
+
dataset_name: Name for the dataset (will be prefixed with username)
|
| 59 |
+
conversation_data: JSON string or formatted text of conversation to save
|
| 60 |
+
|
| 61 |
+
Returns:
|
| 62 |
+
URL of the created dataset or error message
|
| 63 |
+
"""
|
| 64 |
+
try:
|
| 65 |
+
# Initialize Hugging Face API
|
| 66 |
+
hf_api = HfApi(token=os.getenv("HUGGINGFACE_API_KEY", ""))
|
| 67 |
+
|
| 68 |
+
# Get username for the repository name
|
| 69 |
+
user_info = hf_api.whoami()
|
| 70 |
+
username = user_info.get("name", "anonymous")
|
| 71 |
+
|
| 72 |
+
# Format the full repo name
|
| 73 |
+
repo_id = f"{username}/{dataset_name}"
|
| 74 |
+
|
| 75 |
+
# If conversation_data is a string, try to parse it as JSON
|
| 76 |
+
if isinstance(conversation_data, str):
|
| 77 |
+
try:
|
| 78 |
+
conversation = json.loads(conversation_data)
|
| 79 |
+
except json.JSONDecodeError:
|
| 80 |
+
# If not valid JSON, treat as raw text
|
| 81 |
+
conversation = {"text": conversation_data}
|
| 82 |
+
else:
|
| 83 |
+
conversation = conversation_data
|
| 84 |
+
|
| 85 |
+
# Add metadata
|
| 86 |
+
conversation["timestamp"] = datetime.datetime.now().isoformat()
|
| 87 |
+
conversation["dataset_id"] = str(uuid.uuid4())
|
| 88 |
+
|
| 89 |
+
# Create a Hugging Face dataset
|
| 90 |
+
dataset = Dataset.from_dict({"conversations": [conversation]})
|
| 91 |
+
|
| 92 |
+
# Push to the Hugging Face Hub
|
| 93 |
+
dataset.push_to_hub(repo_id, token=os.getenv("HF_API_KEY", ""))
|
| 94 |
+
|
| 95 |
+
return f"Successfully created dataset at https://huggingface.co/datasets/{repo_id}"
|
| 96 |
+
except Exception as e:
|
| 97 |
+
return f"Error creating dataset: {str(e)}"
|
| 98 |
+
|
| 99 |
+
@tool
|
| 100 |
+
def Dataset_Creator_Tool(dataset_name: str, conversation_data: str) -> str:
|
| 101 |
+
"""A tool that posts a new dataset of the current conversation to Hugging Face.
|
| 102 |
+
|
| 103 |
+
Args:
|
| 104 |
+
dataset_name: Name for the dataset (will be prefixed with username)
|
| 105 |
+
conversation_data: Conversation history as JSON string or formatted text
|
| 106 |
+
"""
|
| 107 |
+
try:
|
| 108 |
+
dataset_creator_response = Dataset_Creator_Function(dataset_name, conversation_data)
|
| 109 |
+
return dataset_creator_response
|
| 110 |
+
except Exception as e:
|
| 111 |
+
return f"Error using Dataset Creator tool: {str(e)}"
|
| 112 |
|
| 113 |
|
| 114 |
@tool
|
|
|
|
| 145 |
|
| 146 |
agent = CodeAgent(
|
| 147 |
model=model,
|
| 148 |
+
tools=[
|
| 149 |
+
final_answer,
|
| 150 |
+
Sonar_Web_Search_Tool,
|
| 151 |
+
get_current_time_in_timezone,
|
| 152 |
+
image_generation_tool,
|
| 153 |
+
Dataset_Creator_Tool
|
| 154 |
+
],
|
| 155 |
max_steps=6,
|
| 156 |
verbosity_level=1,
|
| 157 |
grammar=None,
|