Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from datasets import Dataset
|
| 5 |
+
from huggingface_hub import login
|
| 6 |
+
|
| 7 |
+
def extract_json_from_http_text(text: str):
|
| 8 |
+
"""Strips HTTP headers and extracts the JSON payload."""
|
| 9 |
+
try:
|
| 10 |
+
start_idx = text.find('{')
|
| 11 |
+
if start_idx != -1:
|
| 12 |
+
return json.loads(text[start_idx:])
|
| 13 |
+
return None
|
| 14 |
+
except Exception:
|
| 15 |
+
return None
|
| 16 |
+
|
| 17 |
+
def process_and_upload(videos_file, notes_file, repo_id):
|
| 18 |
+
# 1. Check for HF Token
|
| 19 |
+
hf_token = os.environ.get("HF_TOKEN")
|
| 20 |
+
if not hf_token:
|
| 21 |
+
return "Error: HF_TOKEN secret not set in your Space settings.", None
|
| 22 |
+
|
| 23 |
+
if not videos_file or not notes_file or not repo_id:
|
| 24 |
+
return "Error: Please upload both files and provide a Target Dataset Repo.", None
|
| 25 |
+
|
| 26 |
+
# 2. Read the files
|
| 27 |
+
try:
|
| 28 |
+
with open(videos_file, 'r', encoding='utf-8') as f:
|
| 29 |
+
vid_content = f.read()
|
| 30 |
+
with open(notes_file, 'r', encoding='utf-8') as f:
|
| 31 |
+
not_content = f.read()
|
| 32 |
+
except Exception as e:
|
| 33 |
+
return f"Error reading files: {e}", None
|
| 34 |
+
|
| 35 |
+
# 3. Parse JSON
|
| 36 |
+
vid_json = extract_json_from_http_text(vid_content)
|
| 37 |
+
not_json = extract_json_from_http_text(not_content)
|
| 38 |
+
|
| 39 |
+
if not vid_json or not not_json:
|
| 40 |
+
return "Error: Could not parse JSON from the provided files.", None
|
| 41 |
+
|
| 42 |
+
combined_records = []
|
| 43 |
+
|
| 44 |
+
# 4. Extract Video Data
|
| 45 |
+
for ch in vid_json.get("data", {}).get("chapters", []):
|
| 46 |
+
combined_records.append({
|
| 47 |
+
"title": ch.get("title"),
|
| 48 |
+
"link": ch.get("link"),
|
| 49 |
+
"date": ch.get("date"),
|
| 50 |
+
"duration": ch.get("duration"),
|
| 51 |
+
"type": "VIDEO"
|
| 52 |
+
})
|
| 53 |
+
|
| 54 |
+
# 5. Extract PDF Data
|
| 55 |
+
for ch in not_json.get("data", {}).get("chapters", []):
|
| 56 |
+
combined_records.append({
|
| 57 |
+
"title": ch.get("title"),
|
| 58 |
+
"link": ch.get("link"),
|
| 59 |
+
"date": ch.get("date"),
|
| 60 |
+
"duration": ch.get("duration"),
|
| 61 |
+
"type": "PDF"
|
| 62 |
+
})
|
| 63 |
+
|
| 64 |
+
# 6. Push to Hugging Face
|
| 65 |
+
try:
|
| 66 |
+
login(token=hf_token)
|
| 67 |
+
dataset = Dataset.from_list(combined_records)
|
| 68 |
+
dataset.push_to_hub(repo_id)
|
| 69 |
+
except Exception as e:
|
| 70 |
+
return f"Failed to push to Hugging Face: {str(e)}", None
|
| 71 |
+
|
| 72 |
+
# 7. Return success and preview
|
| 73 |
+
return f"Success! Merged {len(combined_records)} records and uploaded to {repo_id}", combined_records[:5]
|
| 74 |
+
|
| 75 |
+
# Build the Web UI
|
| 76 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 77 |
+
gr.Markdown("# 🚀 JSON Data Extractor & HF Uploader")
|
| 78 |
+
gr.Markdown("Drag and drop your text files below. The app will strip the HTTP headers, merge the video and notes chapters, and upload the clean dataset directly to your Hugging Face account.")
|
| 79 |
+
|
| 80 |
+
with gr.Row():
|
| 81 |
+
vid_input = gr.File(label="Upload Video .txt File", type="filepath")
|
| 82 |
+
not_input = gr.File(label="Upload Notes .txt File", type="filepath")
|
| 83 |
+
|
| 84 |
+
repo_input = gr.Textbox(
|
| 85 |
+
label="Target Dataset Repository",
|
| 86 |
+
placeholder="e.g., your-username/english-12th-data",
|
| 87 |
+
info="Make sure you have created this dataset repository or have permission to write to it."
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
submit_btn = gr.Button("Process & Upload", variant="primary")
|
| 91 |
+
|
| 92 |
+
status_output = gr.Textbox(label="Status")
|
| 93 |
+
json_preview = gr.JSON(label="Data Preview (First 5 Records)")
|
| 94 |
+
|
| 95 |
+
submit_btn.click(
|
| 96 |
+
fn=process_and_upload,
|
| 97 |
+
inputs=[vid_input, not_input, repo_input],
|
| 98 |
+
outputs=[status_output, json_preview]
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
if __name__ == "__main__":
|
| 102 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|