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()