Spaces:
Paused
Paused
Update app.py
Browse files1234567853 unmodified lines62636465666768697071727374754 unmodified lines80818283848586878889909192939495969798991001011021031041051063 unmodified lines1101111121131141151161171181191201211221231241251261271286 unmodified lines135136137138139140141142143144145146147148import gradio as grfrom gradio_client import Clientimport osimport zipfilefrom huggingface_hub import HfApiimport loggingimport time # Import time module for adding delays53 unmodified linesfile_path = file.name if hasattr(file, "name") else filechunks = segment_text(file_path)# Initialize Hugging Face APIhf_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 Hubrepo_name = "TeacherPuffy/book2"# Process each chunk with a 15-second delay between API callsresults = []for idx, chunk in enumerate(chunks):4 unmodified linesresults.append(result)logger.info(f"Chunk {idx + 1} processed successfully.")# Save the result to a fileos.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 Facetry: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 repositoryrepo_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 callif idx < len(chunks) - 1: # No need to wait after the last chunklogger.info("Waiting 15 seconds before the next API call...")3 unmodified lineslogger.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 outputstry: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."except Exception as e:logger.error(f"An error occurred during processing: {e}")6 unmodified linesfile_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])# Launch the Gradio app with a public link12345653 unmodified lines6061626364654 unmodified lines7071727374753 unmodified lines798081828384856 unmodified lines9293949596979899100101102103104import gradio as grfrom gradio_client import Clientimport osimport loggingimport time # Import time module for adding delays53 unmodified linesfile_path = file.name if hasattr(file, "name") else filechunks = segment_text(file_path)# Process each chunk with a 15-second delay between API callsresults = []for idx, chunk in enumerate(chunks):4 unmodified linesresults.append(result)logger.info(f"Chunk {idx + 1} processed successfully.")# Wait 15 seconds before the next API callif idx < len(chunks) - 1: # No need to wait after the last chunklogger.info("Waiting 15 seconds before the next API call...")3 unmodified lineslogger.error(f"Failed to process chunk {idx + 1}: {e}")raise gr.Error(f"Failed to process chunk {idx + 1}: {str(e)}")return "All chunks processed successfully."except Exception as e:logger.error(f"An error occurred during processing: {e}")6 unmodified linesfile_input = gr.File(label="Upload Text File")prompt_input = gr.Textbox(label="Enter Prompt")with gr.Row():output_message = gr.Textbox(label="Status Message")submit_button = gr.Button("Submit")submit_button.click(process_text,inputs=[file_input, prompt_input],outputs=[output_message])# Launch the Gradio app with a public link
app.py+2-46