Spaces:
Paused
Paused
Update app.py
Browse files35 unmodified lines3637383940414243444546474849505152535435 unmodified lineslogger.info(f"Segmented text into {len(chunks)} chunks.")return chunks# Function to process the text and make API calls with rate limitingdef process_text(file, prompt):try:logger.info("Starting text processing...")# Read the file content properlyif hasattr(file, "read"):# If the file is a file-like object, read its contenttext = file.read().decode('utf-8') # Decode bytes to stringelse:# If the file is already a string, use it directlytext = filelogger.info(f"Text length: {len(text)} characters.")# Segment the text into chunks35 unmodified lines3637383940414243444546474849505152535455565758596061626364656667686970717235 unmodified lineslogger.info(f"Segmented text into {len(chunks)} chunks.")return chunks# Function to read file content with fallback encodingdef read_file_content(file):try:# Try reading with UTF-8 encoding firstif hasattr(file, "read"):content = file.read().decode('utf-8')else:content = file.decode('utf-8')logger.info("File read successfully with UTF-8 encoding.")return contentexcept UnicodeDecodeError:# Fallback to latin-1 encoding if UTF-8 failslogger.warning("UTF-8 encoding failed. Trying latin-1 encoding.")if hasattr(file, "read"):file.seek(0) # Reset file pointer to the beginningcontent = file.read().decode('latin-1')else:content = file.decode('latin-1')logger.info("File read successfully with latin-1 encoding.")return contentexcept Exception as e:logger.error(f"Failed to read file: {e}")raise gr.Error(f"Failed to read file: {str(e)}")# Function to process the text and make API calls with rate limitingdef process_text(file, prompt):try:logger.info("Starting text processing...")# Read the file content with fallback encodingtext = read_file_content(file)logger.info(f"Text length: {len(text)} characters.")# Segment the text into chunks
app.py+26-8