SolshineMisfit commited on
Commit
5282c5b
·
verified ·
1 Parent(s): fd1672d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -19
app.py CHANGED
@@ -62,31 +62,29 @@ def Dataset_Creator_Function(dataset_name: str, conversation_data: str) -> str:
62
  URL of the created dataset or error message
63
  """
64
  try:
65
- # Get API key from environment variables - try both possible names
66
  api_key = os.getenv("HF_API_KEY") or os.getenv("HUGGINGFACE_API_KEY", "")
67
  if not api_key:
68
  return "Error: No Hugging Face API key found in environment variables"
69
 
 
 
 
70
  # Initialize Hugging Face API
71
  hf_api = HfApi(token=api_key)
72
 
73
- # Retrieve username for the repository name
74
- try:
75
- user_info = hf_api.whoami()
76
- username = user_info.get("name", "Misfits-and-Machines") # fallback
77
- except Exception as e:
78
- return f"Error authenticating with Hugging Face: {str(e)}"
79
-
80
  # Sanitize dataset name and create repo_id
81
  safe_dataset_name = dataset_name.replace(" ", "-").lower()
82
  repo_id = f"{username}/{safe_dataset_name}"
83
 
84
- print(f"Creating or confirming repo: {repo_id}")
85
- # Create or confirm the repository exists
 
86
  try:
87
  hf_api.create_repo(repo_id=repo_id, repo_type="dataset", exist_ok=True)
 
88
  except Exception as repo_error:
89
- print(f"Note: Repository creation returned: {str(repo_error)}")
90
 
91
  # Build conversation object
92
  conversation = {
@@ -98,16 +96,15 @@ def Dataset_Creator_Function(dataset_name: str, conversation_data: str) -> str:
98
  # Create a Hugging Face dataset
99
  dataset = Dataset.from_dict({"conversations": [conversation]})
100
 
101
- # Push to the Hub without the unsupported arguments
102
  dataset.push_to_hub(
103
  repo_id=repo_id,
104
- token=api_key,
105
- commit_message="Add conversation dataset"
106
  )
107
 
108
  dataset_url = f"https://huggingface.co/datasets/{repo_id}"
109
- print(f"Dataset successfully pushed to {dataset_url}")
110
- return dataset_url
111
  except Exception as e:
112
  import traceback
113
  error_trace = traceback.format_exc()
@@ -121,13 +118,16 @@ def Dataset_Creator_Tool(dataset_name: str, conversation_data: str) -> str:
121
  Args:
122
  dataset_name: Name for the dataset (will be prefixed with username)
123
  conversation_data: String content to save to the dataset (no JSON conversion needed)
124
- """
125
  try:
 
126
  result = Dataset_Creator_Function(dataset_name, conversation_data)
127
- print(f"Dataset_Creator_Function returned: {result}")
128
  return result
129
  except Exception as e:
130
- return f"Error using Dataset Creator tool: {str(e)}"
 
 
131
 
132
 
133
  @tool
 
62
  URL of the created dataset or error message
63
  """
64
  try:
65
+ # Get API key from environment variables
66
  api_key = os.getenv("HF_API_KEY") or os.getenv("HUGGINGFACE_API_KEY", "")
67
  if not api_key:
68
  return "Error: No Hugging Face API key found in environment variables"
69
 
70
+ # Force the username to be the known value
71
+ username = "Misfits-and-Machines"
72
+
73
  # Initialize Hugging Face API
74
  hf_api = HfApi(token=api_key)
75
 
 
 
 
 
 
 
 
76
  # Sanitize dataset name and create repo_id
77
  safe_dataset_name = dataset_name.replace(" ", "-").lower()
78
  repo_id = f"{username}/{safe_dataset_name}"
79
 
80
+ print(f"Creating dataset repository: {repo_id}")
81
+
82
+ # Create the repository explicitly
83
  try:
84
  hf_api.create_repo(repo_id=repo_id, repo_type="dataset", exist_ok=True)
85
+ print(f"Repository {repo_id} created or confirmed")
86
  except Exception as repo_error:
87
+ print(f"Repository creation note: {str(repo_error)}")
88
 
89
  # Build conversation object
90
  conversation = {
 
96
  # Create a Hugging Face dataset
97
  dataset = Dataset.from_dict({"conversations": [conversation]})
98
 
99
+ # Push dataset to the Hub with minimal parameters
100
  dataset.push_to_hub(
101
  repo_id=repo_id,
102
+ token=api_key
 
103
  )
104
 
105
  dataset_url = f"https://huggingface.co/datasets/{repo_id}"
106
+ print(f"Dataset successfully pushed to: {dataset_url}")
107
+ return f"Successfully created dataset: {dataset_url}"
108
  except Exception as e:
109
  import traceback
110
  error_trace = traceback.format_exc()
 
118
  Args:
119
  dataset_name: Name for the dataset (will be prefixed with username)
120
  conversation_data: String content to save to the dataset (no JSON conversion needed)
121
+ """
122
  try:
123
+ print(f"Creating dataset '{dataset_name}' with {len(conversation_data)} characters of data")
124
  result = Dataset_Creator_Function(dataset_name, conversation_data)
125
+ print(f"Result: {result}")
126
  return result
127
  except Exception as e:
128
+ import traceback
129
+ error_trace = traceback.format_exc()
130
+ return f"Error using Dataset Creator tool: {str(e)}\n{error_trace}"
131
 
132
 
133
  @tool