Spaces:
Running
Running
TeacherPuffy
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
from gradio_client import Client
|
3 |
import os
|
4 |
-
import zipfile
|
5 |
-
from huggingface_hub import HfApi
|
6 |
import logging
|
7 |
import time # Import time module for adding delays
|
8 |
|
@@ -62,14 +60,6 @@ def process_text(file, prompt):
|
|
62 |
file_path = file.name if hasattr(file, "name") else file
|
63 |
chunks = segment_text(file_path)
|
64 |
|
65 |
-
# Initialize Hugging Face API
|
66 |
-
hf_api = HfApi(token=os.environ.get("HUGGINGFACE_TOKEN"))
|
67 |
-
if not hf_api.token:
|
68 |
-
raise ValueError("Hugging Face token not found in environment variables.")
|
69 |
-
|
70 |
-
# Repository name on Hugging Face Hub
|
71 |
-
repo_name = "TeacherPuffy/book2"
|
72 |
-
|
73 |
# Process each chunk with a 15-second delay between API calls
|
74 |
results = []
|
75 |
for idx, chunk in enumerate(chunks):
|
@@ -80,27 +70,6 @@ def process_text(file, prompt):
|
|
80 |
results.append(result)
|
81 |
logger.info(f"Chunk {idx + 1} processed successfully.")
|
82 |
|
83 |
-
# Save the result to a file
|
84 |
-
os.makedirs("outputs", exist_ok=True)
|
85 |
-
output_file = f"outputs/output_{idx}.txt"
|
86 |
-
with open(output_file, "w", encoding="utf-8") as f:
|
87 |
-
f.write(result)
|
88 |
-
logger.info(f"Saved result to {output_file}")
|
89 |
-
|
90 |
-
# Upload the chunk as an individual text file to Hugging Face
|
91 |
-
try:
|
92 |
-
logger.info(f"Uploading chunk {idx + 1} to Hugging Face...")
|
93 |
-
hf_api.upload_file(
|
94 |
-
path_or_fileobj=output_file,
|
95 |
-
path_in_repo=f"output_{idx}.txt", # File name in the repository
|
96 |
-
repo_id=repo_name,
|
97 |
-
repo_type="dataset",
|
98 |
-
)
|
99 |
-
logger.info(f"Chunk {idx + 1} uploaded to Hugging Face successfully.")
|
100 |
-
except Exception as e:
|
101 |
-
logger.error(f"Failed to upload chunk {idx + 1} to Hugging Face: {e}")
|
102 |
-
raise gr.Error(f"Failed to upload chunk {idx + 1} to Hugging Face: {str(e)}")
|
103 |
-
|
104 |
# Wait 15 seconds before the next API call
|
105 |
if idx < len(chunks) - 1: # No need to wait after the last chunk
|
106 |
logger.info("Waiting 15 seconds before the next API call...")
|
@@ -110,19 +79,7 @@ def process_text(file, prompt):
|
|
110 |
logger.error(f"Failed to process chunk {idx + 1}: {e}")
|
111 |
raise gr.Error(f"Failed to process chunk {idx + 1}: {str(e)}")
|
112 |
|
113 |
-
|
114 |
-
try:
|
115 |
-
logger.info("Creating ZIP file...")
|
116 |
-
with zipfile.ZipFile("outputs.zip", "w") as zipf:
|
117 |
-
for root, dirs, files in os.walk("outputs"):
|
118 |
-
for file in files:
|
119 |
-
zipf.write(os.path.join(root, file), file)
|
120 |
-
logger.info("ZIP file created successfully.")
|
121 |
-
except Exception as e:
|
122 |
-
logger.error(f"Failed to create ZIP file: {e}")
|
123 |
-
raise gr.Error(f"Failed to create ZIP file: {str(e)}")
|
124 |
-
|
125 |
-
return "outputs.zip", "All chunks processed and uploaded to Hugging Face. ZIP file created."
|
126 |
|
127 |
except Exception as e:
|
128 |
logger.error(f"An error occurred during processing: {e}")
|
@@ -135,14 +92,13 @@ with gr.Blocks() as demo:
|
|
135 |
file_input = gr.File(label="Upload Text File")
|
136 |
prompt_input = gr.Textbox(label="Enter Prompt")
|
137 |
with gr.Row():
|
138 |
-
output_zip = gr.File(label="Download ZIP File")
|
139 |
output_message = gr.Textbox(label="Status Message")
|
140 |
submit_button = gr.Button("Submit")
|
141 |
|
142 |
submit_button.click(
|
143 |
process_text,
|
144 |
inputs=[file_input, prompt_input],
|
145 |
-
outputs=[
|
146 |
)
|
147 |
|
148 |
# Launch the Gradio app with a public link
|
|
|
1 |
import gradio as gr
|
2 |
from gradio_client import Client
|
3 |
import os
|
|
|
|
|
4 |
import logging
|
5 |
import time # Import time module for adding delays
|
6 |
|
|
|
60 |
file_path = file.name if hasattr(file, "name") else file
|
61 |
chunks = segment_text(file_path)
|
62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
# Process each chunk with a 15-second delay between API calls
|
64 |
results = []
|
65 |
for idx, chunk in enumerate(chunks):
|
|
|
70 |
results.append(result)
|
71 |
logger.info(f"Chunk {idx + 1} processed successfully.")
|
72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
# Wait 15 seconds before the next API call
|
74 |
if idx < len(chunks) - 1: # No need to wait after the last chunk
|
75 |
logger.info("Waiting 15 seconds before the next API call...")
|
|
|
79 |
logger.error(f"Failed to process chunk {idx + 1}: {e}")
|
80 |
raise gr.Error(f"Failed to process chunk {idx + 1}: {str(e)}")
|
81 |
|
82 |
+
return "All chunks processed successfully."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
|
84 |
except Exception as e:
|
85 |
logger.error(f"An error occurred during processing: {e}")
|
|
|
92 |
file_input = gr.File(label="Upload Text File")
|
93 |
prompt_input = gr.Textbox(label="Enter Prompt")
|
94 |
with gr.Row():
|
|
|
95 |
output_message = gr.Textbox(label="Status Message")
|
96 |
submit_button = gr.Button("Submit")
|
97 |
|
98 |
submit_button.click(
|
99 |
process_text,
|
100 |
inputs=[file_input, prompt_input],
|
101 |
+
outputs=[output_message]
|
102 |
)
|
103 |
|
104 |
# Launch the Gradio app with a public link
|