Spaces:
Running
Running
TeacherPuffy
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
from gradio_client import Client
|
3 |
-
import
|
4 |
import logging
|
5 |
import time # Import time module for adding delays
|
6 |
|
@@ -27,38 +27,32 @@ def call_api(prompt):
|
|
27 |
logger.error(f"API call failed: {e}")
|
28 |
raise gr.Error(f"API call failed: {str(e)}")
|
29 |
|
30 |
-
# Function to segment the text
|
31 |
-
def segment_text(
|
32 |
-
try:
|
33 |
-
logger.info(f"Reading file: {file_path}")
|
34 |
-
# Try reading with UTF-8 encoding first
|
35 |
-
with open(file_path, "r", encoding="utf-8") as f:
|
36 |
-
text = f.read()
|
37 |
-
logger.info("File read successfully with UTF-8 encoding.")
|
38 |
-
except UnicodeDecodeError:
|
39 |
-
logger.warning("UTF-8 encoding failed. Trying latin-1 encoding.")
|
40 |
-
# Fallback to latin-1 encoding if UTF-8 fails
|
41 |
-
with open(file_path, "r", encoding="latin-1") as f:
|
42 |
-
text = f.read()
|
43 |
-
logger.info("File read successfully with latin-1 encoding.")
|
44 |
-
except Exception as e:
|
45 |
-
logger.error(f"Failed to read file: {e}")
|
46 |
-
raise gr.Error(f"Failed to read file: {str(e)}")
|
47 |
-
|
48 |
# Split the text into chunks of 1500 words
|
49 |
words = text.split()
|
50 |
chunks = [" ".join(words[i:i + 1500]) for i in range(0, len(words), 1250)]
|
51 |
logger.info(f"Segmented text into {len(chunks)} chunks.")
|
52 |
return chunks
|
53 |
|
54 |
-
# Function to process the text
|
55 |
def process_text(file, prompt):
|
56 |
try:
|
57 |
logger.info("Starting text processing...")
|
58 |
|
59 |
-
#
|
60 |
-
|
61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
# Process each chunk with a 15-second delay between API calls
|
64 |
results = []
|
@@ -70,6 +64,20 @@ def process_text(file, prompt):
|
|
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,7 +87,7 @@ def process_text(file, prompt):
|
|
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
|
83 |
|
84 |
except Exception as e:
|
85 |
logger.error(f"An error occurred during processing: {e}")
|
|
|
1 |
import gradio as gr
|
2 |
from gradio_client import Client
|
3 |
+
from huggingface_hub import HfApi
|
4 |
import logging
|
5 |
import time # Import time module for adding delays
|
6 |
|
|
|
27 |
logger.error(f"API call failed: {e}")
|
28 |
raise gr.Error(f"API call failed: {str(e)}")
|
29 |
|
30 |
+
# Function to segment the text into chunks of 1500 words
|
31 |
+
def segment_text(text):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
# Split the text into chunks of 1500 words
|
33 |
words = text.split()
|
34 |
chunks = [" ".join(words[i:i + 1500]) for i in range(0, len(words), 1250)]
|
35 |
logger.info(f"Segmented text into {len(chunks)} chunks.")
|
36 |
return chunks
|
37 |
|
38 |
+
# Function to process the text and make API calls with rate limiting
|
39 |
def process_text(file, prompt):
|
40 |
try:
|
41 |
logger.info("Starting text processing...")
|
42 |
|
43 |
+
# Read the text directly from the uploaded file
|
44 |
+
text = file.decode('utf-8') # Assuming the file is encoded in UTF-8
|
45 |
+
|
46 |
+
# Segment the text into chunks
|
47 |
+
chunks = segment_text(text)
|
48 |
+
|
49 |
+
# Initialize Hugging Face API
|
50 |
+
hf_api = HfApi(token=os.environ.get("HUGGINGFACE_TOKEN"))
|
51 |
+
if not hf_api.token:
|
52 |
+
raise ValueError("Hugging Face token not found in environment variables.")
|
53 |
+
|
54 |
+
# Repository name on Hugging Face Hub
|
55 |
+
repo_name = "TeacherPuffy/book2"
|
56 |
|
57 |
# Process each chunk with a 15-second delay between API calls
|
58 |
results = []
|
|
|
64 |
results.append(result)
|
65 |
logger.info(f"Chunk {idx + 1} processed successfully.")
|
66 |
|
67 |
+
# Upload the chunk directly to Hugging Face
|
68 |
+
try:
|
69 |
+
logger.info(f"Uploading chunk {idx + 1} to Hugging Face...")
|
70 |
+
hf_api.upload_file(
|
71 |
+
path_or_fileobj=result.encode('utf-8'), # Convert result to bytes
|
72 |
+
path_in_repo=f"output_{idx}.txt", # File name in the repository
|
73 |
+
repo_id=repo_name,
|
74 |
+
repo_type="dataset",
|
75 |
+
)
|
76 |
+
logger.info(f"Chunk {idx + 1} uploaded to Hugging Face successfully.")
|
77 |
+
except Exception as e:
|
78 |
+
logger.error(f"Failed to upload chunk {idx + 1} to Hugging Face: {e}")
|
79 |
+
raise gr.Error(f"Failed to upload chunk {idx + 1} to Hugging Face: {str(e)}")
|
80 |
+
|
81 |
# Wait 15 seconds before the next API call
|
82 |
if idx < len(chunks) - 1: # No need to wait after the last chunk
|
83 |
logger.info("Waiting 15 seconds before the next API call...")
|
|
|
87 |
logger.error(f"Failed to process chunk {idx + 1}: {e}")
|
88 |
raise gr.Error(f"Failed to process chunk {idx + 1}: {str(e)}")
|
89 |
|
90 |
+
return "All chunks processed and uploaded to Hugging Face."
|
91 |
|
92 |
except Exception as e:
|
93 |
logger.error(f"An error occurred during processing: {e}")
|