Spaces:
Runtime error
Runtime error
File size: 3,470 Bytes
ec8a358 0506cec ba51acd 0506cec ec8a358 0506cec 86c0663 0506cec 86c0663 0506cec ba51acd 0506cec 86c0663 ad7dde5 0506cec ba51acd 0506cec ba51acd 0506cec 86c0663 ad7dde5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
import json
import logging
import os
import datetime
import gradio as gr
from huggingface_hub import HfApi, HfFolder
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Define the function to convert text to JSON
def text_to_json(text):
lines = text.strip().split('\n')
data = [{"text": line} for line in lines]
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"converted/output_{timestamp}.json"
with open(filename, "w") as f:
json.dump(data, f, indent=4)
return filename
# Define the function to generate and upload the JSON file
def generate_and_upload(text):
try:
if not text:
raise ValueError("Text input is empty.")
logger.info(f"Received text input: {text}")
# Convert text to JSON and save to file
json_file = text_to_json(text)
logger.info(f"JSON file created: {json_file}")
# Authenticate with Hugging Face Hub
api = HfApi()
token = os.environ['HUGGINGFACE_API_TOKEN']
if token is None:
raise ValueError("Hugging Face API token not found. Please set HUGGINGFACE_API_TOKEN environment variable.")
# Upload the file to the dataset repository
repo_id = "katsukiai/DeepFocus-X3"
upload_info = api.upload_file(
path_or_fileobj=json_file,
path_in_repo=os.path.basename(json_file),
repo_id=repo_id,
repo_type="dataset",
token=token
)
logger.info(f"Upload info: {upload_info}")
message = f"Upload successful! Filename: {os.path.basename(json_file)}"
return message, json_file
except Exception as e:
logger.error(f"Error uploading file: {e}")
return f"Error: {e}", None
# Create the Gradio interface
with gr.Blocks() as demo:
with gr.Tab("About"):
gr.Markdown("""
# Text to JSON uploader
This app allows you to input text, convert it to JSON format, and upload it to the Hugging Face dataset repository.
## Instructions
1. Enter your text in the "Generate" tab.
2. Click the "Generate and Upload" button.
3. Download the JSON file if desired.
4. Check the message for upload status.
## Requirements
- Hugging Face API token set as environment variable `HUGGINGFACE_API_TOKEN`.
## Obtaining Hugging Face API Token
1. Log in to your Hugging Face account.
2. Go to your profile settings.
3. Generate a new token or use an existing one.
4. Set the token as an environment variable named `HUGGINGFACE_API_TOKEN`.
## Setting Environment Variable
- **Windows**: Set it in System Properties > Advanced > Environment Variables.
- **macOS/Linux**: Add `export HUGGINGFACE_API_TOKEN=your_token` to your shell profile (e.g., `.bashrc`, `.zshrc`).
""")
with gr.Tab("Generate"):
text_input = gr.Textbox(label="Enter text")
output_message = gr.Textbox(label="Status message")
json_file_downloader = gr.File(label="Download JSON", interactive=False)
generate_button = gr.Button("Generate and Upload")
generate_button.click(fn=generate_and_upload, inputs=text_input, outputs=[output_message, json_file_downloader])
# Launch the Gradio app
demo.launch()
|