katsukiai commited on
Commit
ec8a358
·
verified ·
1 Parent(s): b65f223

Create app.py

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