Spaces:
Paused
Paused
Update app.py
Browse files1234545653 unmodified lines60616265666768697071726364654 unmodified lines70717283848586878889909192939495969798991001011021037374753 unmodified lines798081113114115116117118119120121122123124125828384856 unmodified lines9293941389596979899100145101102103104import gradio as grfrom gradio_client import Clientimport osimport zipfilefrom huggingface_hub import HfApiimport loggingimport time # Import time module for adding delays
53 unmodified lines file_path = file.name if hasattr(file, "name") else file chunks = segment_text(file_path) # Initialize Hugging Face API hf_api = HfApi(token=os.environ.get("HUGGINGFACE_TOKEN")) if not hf_api.token: raise ValueError("Hugging Face token not found in environment variables.") # Repository name on Hugging Face Hub repo_name = "TeacherPuffy/book2" # Process each chunk with a 15-second delay between API calls results = [] for idx, chunk in enumerate(chunks):4 unmodified lines results.append(result) logger.info(f"Chunk {idx + 1} processed successfully.") # Save the result to a file os.makedirs("outputs", exist_ok=True) output_file = f"outputs/output_{idx}.txt" with open(output_file, "w", encoding="utf-8") as f: f.write(result) logger.info(f"Saved result to {output_file}") # Upload the chunk as an individual text file to Hugging Face try: logger.info(f"Uploading chunk {idx + 1} to Hugging Face...") hf_api.upload_file( path_or_fileobj=output_file, path_in_repo=f"output_{idx}.txt", # File name in the repository repo_id=repo_name, repo_type="dataset", ) logger.info(f"Chunk {idx + 1} uploaded to Hugging Face successfully.") except Exception as e: logger.error(f"Failed to upload chunk {idx + 1} to Hugging Face: {e}") raise gr.Error(f"Failed to upload chunk {idx + 1} to Hugging Face: {str(e)}") # Wait 15 seconds before the next API call if idx < len(chunks) - 1: # No need to wait after the last chunk logger.info("Waiting 15 seconds before the next API call...")3 unmodified lines logger.error(f"Failed to process chunk {idx + 1}: {e}") raise gr.Error(f"Failed to process chunk {idx + 1}: {str(e)}") # Create a ZIP file of all outputs try: logger.info("Creating ZIP file...") with zipfile.ZipFile("outputs.zip", "w") as zipf: for root, dirs, files in os.walk("outputs"): for file in files: zipf.write(os.path.join(root, file), file) logger.info("ZIP file created successfully.") except Exception as e: logger.error(f"Failed to create ZIP file: {e}") raise gr.Error(f"Failed to create ZIP file: {str(e)}") return "outputs.zip", "All chunks processed and uploaded to Hugging Face. ZIP file created." return "All chunks processed successfully." except Exception as e: logger.error(f"An error occurred during processing: {e}")6 unmodified lines file_input = gr.File(label="Upload Text File") prompt_input = gr.Textbox(label="Enter Prompt") with gr.Row(): output_zip = gr.File(label="Download ZIP File") output_message = gr.Textbox(label="Status Message") submit_button = gr.Button("Submit") submit_button.click( process_text, inputs=[file_input, prompt_input], outputs=[output_zip, output_message] outputs=[output_message] )
# Launch the Gradio app with a public linkapp.py+2-46